DIP+CV · Programming Exercise

EP08_06 — 🟡 Bounding Boxes , Centroids, and Instance Properties with mm.measure

8.14.6 EP08_06 🟡 Bounding Boxes, Centroids, and Instance Properties with mm.measure

In the previous exercise (EP08_05), you can observe how segmentation by connected components labels contiguous binary regions to separate instances. However, for detection, tracking, and quantitative object analysis tasks, the simple label map is not sufficient. It becomes necessary to extract spatial and geometric metrics that characterize each instance individually.

This EP focuses on calculating and automatically extracting the fundamental computer vision properties for each connected component found in the binary mask, using the native mm.measure(img) method from the morph library:

  1. Bounding Box: The smallest axis-aligned rectangle that completely encloses the instance, defined by its top-left corner \((x, y)\), width \(w\), and height \(h\).
  2. Geometric Centroid \((\bar{x}, \bar{y})\): The center of mass of the instance on the discrete grid, equivalent to the first-order spatial moments \(M_{10}/M_{00}\) and \(M_{01}/M_{00}\).
  3. Geometric Contour Area (\(A\)): The area enclosed by the instance contour, calculated via mm.contourArea(c).

8.14.6.1 📋 Implementation Guidelines

  1. Input: Read the dimensions \(H \times W\) of the binary mask, the \(H \times W\) values (\(0\) or \(1\)), and the connectivity parameter \(c \in \{4, 8\}\).
  2. Automatic Extraction with mm.measure: Pass the binarized image to the mm.measure(img_bin) function, which extracts OpenCV contours and returns a list of dictionaries containing the geometric properties of each instance.
  3. Returned Properties: For each dictionary \(m\) in the list returned by medidas = mm.measure(img_bin):
    • Area (area): Numerical value of the geometric contour area mm.contourArea(c).
    • Bounding Box (bbox): Tuple \((x, y, w, h)\) representing the top-left corner, width, and height.
    • Centroid (center): Tuple \((c_x, c_y)\) with the coordinates of the center of mass \(M_{10}/M_{00}\) and \(M_{01}/M_{00}\). Format with two decimal places.
  4. Output: For each instance \(1, \dots, K\) found (ordered by discovery/position in the image), print a line containing its properties. Finally, print the total number of instances.
    • For sorting, use medidas.sort(key=lambda m: (m['bbox'][1], m['bbox'][0])).

8.14.6.2 🧠 Theoretical Foundation

Property in mm.measure Mathematical Calculation / Discrete Logic Practical Application in Vision
bbox (OpenCV) \([x, y, w, h] = [\min(c), \min(r), \Delta c + 1, \Delta r + 1]\) Classic OpenCV format. Note: networks such as YOLO convert this rectangle to \((c_x, c_y, w, h)\) normalized.
center \(\bar{x} = \frac{M_{10}}{M_{00}}, \quad \bar{y} = \frac{M_{01}}{M_{00}}\) Exact center of mass of the mask (used in tracking and trajectory analysis).
area \(A = \text{contourArea}(C)\) (Polygon Formula) Continuous metric of the object’s surface.

8.14.6.3 📦 Input and Output Specification (VPL)

Input:

  • Line 1: Integers \(H\) and \(W\).
  • Next \(H\) lines: \(W\) integers (\(0\) or \(1\)) each.
  • Last line: Integer \(c\) (\(4\) or \(8\)).

Output:

  • One line per instance in discovery order: Instance l: Area=A, BBox=(x,y,w,h), Centroid=(cx,cy)
  • Last line: Total instances: K.

8.14.6.4 📌 Examples

Input Output Observation
6 6
0 0 0 0 0 0
0 1 1 0 0 0
0 1 1 0 0 0
0 0 0 0 0 0
0 0 0 0 1 1
0 0 0 0 1 1
8
Instance 1: Area=1.0, BBox=(1,1,2,2), Centroid=(1.50,1.50)
Instance 2: Area=1.0, BBox=(4,4,2,2), Centroid=(4.50,4.50)
Total instances: 2
Aligned \(2\times2\) blocks. The geometric contour area calculation results in \(1.0\). The centroid of the block in columns 1–2 and rows 1–2 is exactly \((1.50,\,1.50)\).
4 6
0 0 0 0 0 0
0 1 1 1 1 0
0 0 0 1 0 0
0 0 0 0 0 0
4
Instance 1: Area=2.0, BBox=(1,1,4,2), Centroid=(2.40,1.20)
Total instances: 1
Asymmetric inverted “T” shaped object. The geometric contour area is \(2.0\). The centroid reflects the pixel distribution of the object.
🧮 Simulator EP08_06: Native Morphological Metrics (mm.measure) OpenCV Contour & Moments
ACTION
Precision Parameter (approxPolyDP): precision = 0.01
INSTANCE LABEL MAP
METRICS EXTRACTED BY MM.MEASURE
id area perimeter center (cx, cy) bbox (x,y,w,h) circularity solidity vertices
Figure 8.20: Simulador EP08_06: Extraction of Bounding Boxes, Centroids and Properties with mm.measure
%%writefile EP08_06.py
# Python code
Overwriting EP08_06.py
TestSuite("EP08_06.py").run()
✔️ EP08_06.cases already exists in casos/
📋 3 case(s) loaded from casos/EP08_06.cases

🔍 Testing Python: EP08_06.py
⚠️ EP08_06.py: Empty file (fewer than 3 lines). Tests skipped.