Resources from the book

Mini-Box Combiner

Section 21.6 · Combine nearby Mini-Boxes into same-label detection families.

Section 21.6.1

Purpose

Combine nearby Mini-Boxes into a single enclosing bounding box. This utility processes detections from one neural network and one inference frame.

Section 21.6.2

Why It Exists

Mini-Boxes can identify small regions of highly variable objects or artifacts, such as scratches, dents, or anomalies. Several boxes may describe different parts of one artifact.

If you need to report how many scratches are present, counting the original boxes can overcount the artifacts. Combining nearby boxes provides a way to group them before counting.

Grouping depends on the application.

Nearby boxes do not necessarily represent the same physical artifact. Choose and validate the merge distance against representative images; the book explicitly describes this as an imperfect approach.

Section 21.6.3

Input

Supply an array<BoundingBox@>, named bbox_arr in the example, from your inference process. Select the labels to combine and a pixel-distance threshold:

array<int> target_label_id_arr = {0,1};
int pixel_distance_to_combine = 100;

The example processes labels 0 and 1, but keeps their families separate. Boxes with different label IDs never merge.

Each input provides upper-left pixel coordinates, width, height, label ID and name, confidence, and display colors. Use valid objects with positive dimensions in the same image coordinate system.

Check the confidence field name.

This utility’s input code reads bbox.probability. If your local BoundingBox class uses confidence, replace the two bbox.probability references in the code. The output class always uses confidence.

Only selected labels are retained in the output. Preserve any other detections separately if your application still needs them.

Review 21.1 · Inference Data & BoundingBox →

Section 21.6.4

Output

Results are stored in merged_bbox_dict. Each key is a label ID converted to a string; its value is an array<CustomBBox> containing the final families for that label.

dictionary merged_bbox_dict;
// "0" -> array<CustomBBox> for label 0
// "1" -> array<CustomBBox> for label 1

A CustomBBox contains geometry, an integer center point, label information, display colors, mean confidence, and the confidence sum/count used to calculate that mean.

FieldsMeaning
x, y, width, heightThe rectangle enclosing the family.
center_x, center_yInteger center of that rectangle.
label_id, label_name and colorsRetained from the existing family.
sum_confidence, freq_confidenceCumulative input confidence and contributing-box count.
confidenceThe arithmetic mean after finalization. Unmerged boxes retain their original confidence.

A small example

Two label-0 boxes at (10, 10, 20, 20) and (35, 10, 20, 20) have an edge gap of 5 pixels. At a threshold of 5, they merge into (10, 10, 45, 20). Input confidences of 0.8 and 0.6 produce a mean of 0.7.

A label-1 box remains separate even if it overlaps that label-0 family. The final box count describes the resulting families, not a guaranteed count of real-world artifacts.

Distance and merge behavior

The distance is the minimum edge-to-edge Euclidean gap, not the distance between centers. Overlap, edge contact, and corner contact all have distance zero.

Each incoming box joins the first existing family of the same label within the threshold. Otherwise it starts a new family. The algorithm does not repeatedly merge existing families together, and its results can depend on input order.

The guide behind the codeFrom the page
to the plant floor.
Practical Guide to AI Computer Vision Integration in Industrial ManufacturingExplore the Book