extractDetectionBoxes function
Extracts text boxes from a DB-style detection probability map.
probMap is a row-major width x height map of values in [0, 1]
(the detector's per-pixel text probability). The map is binarized at
threshold; each 4-connected blob becomes one axis-aligned box, dropped
if its mean probability is below boxScoreThreshold or it is smaller than
minSize on either side. Boxes are then unclipped and finally scaled by
scaleX/scaleY back into the original image's pixels.
Unclip is DB's own (and PaddleOCR's DBPostProcess.unclip): DB trains
on text polygons shrunk inwards by area * (1 - r^2) / perimeter, so a
detection is grown back outwards by a distance area * unclipRatio / perimeter on every side - not scaled about its centre. For a line of
text that is wide and short, the distance is set by the height, so the
box gains vertical margin for ascenders/descenders while its ends grow by
the same few pixels instead of reaching into the neighbouring label.
This uses axis-aligned bounding boxes rather than rotated min-area rects:
the OCR layer this feeds (PdfEditor.injectTextLayer) places runs along
the page's on-screen reading direction (horizontal in this raster), and
axis-aligned boxes keep the post-process pure-Dart and robust.
Implementation
List<DetectedBox> extractDetectionBoxes(
Float32List probMap,
int width,
int height, {
double threshold = 0.3,
double boxScoreThreshold = 0.6,
double unclipRatio = 1.5,
int minSize = 3,
double scaleX = 1.0,
double scaleY = 1.0,
}) {
final visited = Uint8List(width * height);
final boxes = <DetectedBox>[];
final stack = <int>[];
for (var start = 0; start < probMap.length; start++) {
if (visited[start] != 0 || probMap[start] < threshold) continue;
// Flood-fill this connected component.
var minX = width, minY = height, maxX = -1, maxY = -1;
var sum = 0.0;
var count = 0;
stack.add(start);
visited[start] = 1;
while (stack.isNotEmpty) {
final idx = stack.removeLast();
final x = idx % width;
final y = idx ~/ width;
sum += probMap[idx];
count++;
if (x < minX) minX = x;
if (y < minY) minY = y;
if (x > maxX) maxX = x;
if (y > maxY) maxY = y;
// 4-neighbours.
if (x > 0) _push(stack, visited, probMap, idx - 1, threshold);
if (x < width - 1) _push(stack, visited, probMap, idx + 1, threshold);
if (y > 0) _push(stack, visited, probMap, idx - width, threshold);
if (y < height - 1) {
_push(stack, visited, probMap, idx + width, threshold);
}
}
final boxW = maxX - minX + 1;
final boxH = maxY - minY + 1;
if (boxW < minSize || boxH < minSize) continue;
final score = sum / count;
if (score < boxScoreThreshold) continue;
// Unclip: offset every side outwards by area * ratio / perimeter.
final d = boxW * boxH * unclipRatio / (2 * (boxW + boxH));
final rect = Rect.fromLTRB(
((minX - d) * scaleX).clamp(0.0, width * scaleX),
((minY - d) * scaleY).clamp(0.0, height * scaleY),
((maxX + 1 + d) * scaleX).clamp(0.0, width * scaleX),
((maxY + 1 + d) * scaleY).clamp(0.0, height * scaleY),
);
boxes.add(DetectedBox(rect: rect, score: score));
}
// Top-to-bottom, then left-to-right - a reasonable reading order for the
// (independent) runs an OCR text layer holds.
boxes.sort((a, b) {
final dy = a.rect.top.compareTo(b.rect.top);
return dy != 0 ? dy : a.rect.left.compareTo(b.rect.left);
});
return boxes;
}