Finding and measuring objects
A mask says which pixels are 'part'. But how many parts are there, and how big is each one?
About 45 min · 3 goals · Checked by automated tests
1.Why should I care?
Counting parts on a tray, measuring a seedling, finding where to grip: all of these turn a mask into a list of objects with sizes and positions. It is the step where pixels become things a robot can act on.
- ◇ Group mask pixels into objects with connected-component labelling (flood fill).
- ◇ Measure each object's area, centroid and bounding box.
- ◇ Explain how 4- versus 8-connectivity and noise change the count.
2.Discover
In the Code Lab, write label(mask) that scans a 2D list of 0s and 1s and floods each unvisited 1 with a queue (breadth-first), giving each blob a new label. Return, for each blob, its pixel count and centroid. Test on a mask with two blobs that touch at a corner, using both 4- and 8-connectivity.
Can't use the interactive? Read a description instead
Code Lab task, described: a mask containing two 2 × 2 squares that touch only at one corner gives 2 objects with 4-connectivity (each of area 4) and 1 object of area 8 with 8-connectivity. Each blob's centroid is the mean row and column of its pixels.
3.Checkpoint: guided practice
Check what you just discovered before going on. Hints appear after a wrong answer.
Practice 1
A blob covers 340 pixels and each pixel is 0.5 mm × 0.5 mm. What is its area, in mm²?
4.Apply it
Combine this week's functions into a pipeline: threshold → remove blobs under a minimum area → label → measure. Run it on three images of small objects, hand-count each, and report the counted versus true number of objects.
5.Understand
Connected components
Scan the mask. When you meet an object pixel that has no label, give it a new label and flood outward: put it in a queue, and repeatedly take a pixel out and add its unlabelled object neighbours. When the queue empties, that whole blob is labelled. Continue the scan for the next blob.
Measuring blobs
For each blob: area is its pixel count; centroid is the mean row and mean column of its pixels; bounding box is the min and max row and column. With a known camera scale (mm per pixel) these become real sizes and positions a gripper can use.
Noise makes fake objects
Single noisy pixels above the threshold become tiny one-pixel 'objects', and a speck of shadow can split one object in two. Pipelines remove blobs below a minimum area, smooth or clean the mask first, and check counts against ground truth rather than trusting them.
Explain it another way
Pour paint onto one pixel of a blob and let it spread into every touching object pixel: everything that gets painted is one object.
Connected-component labelling via BFS/DFS flood fill runs in O(W·H). Two-pass union–find algorithms do the same with better memory locality. Moments (area, centroid, second moments) summarise shape.
Centroid (r̄, c̄) = (Σr/A, Σc/A) over the blob's pixels. Converting area to real units multiplies by (mm per pixel)², so a 2× error in scale is a 4× error in area.
Seed-sorting and pill-counting machines count blobs on a conveyor camera and reject anything whose area is outside the expected range, catching broken or doubled items.
Worked example 1
A blob's pixels are at (row, col) (1,1), (1,2), (2,1), (2,2), (3,2). Area and centroid?
Area = 5. Mean row = (1 + 1 + 2 + 2 + 3)/5 = 1.8; mean column = (1 + 2 + 1 + 2 + 2)/5 = 1.6. Centroid (1.8, 1.6).
Common mix-ups
- Number of blobs = number of objects.
✓ Noise adds specks, touching objects merge, and shadows split objects. Counts must be checked against ground truth. - The centroid is always inside the object.
✓ For a C or ring shape the centroid lies in the gap, which matters if a gripper aims for it.
6.Practice on your own
Practice, not a test. Wrong answers get a hint and another try.
Practice 2
A blob has pixels in columns 4, 5, 5, 6, 10. What is its centroid column?
Practice 3
A tray of 12 parts is counted as 19. Seven of the extra 'objects' are 1–3 pixels big. Best fix?
Practice 4
Two touching coins are counted as one object. Which statement is true?
Practice 5 · a fresh version every time
A defect detector is tested on labelled parts: TP = 77, FP = 18, FN = 1, TN = 272. What is its recall, as a percentage?
7.Predict
Two square blobs touch only at one corner. With 4-connectivity (up, down, left, right), how many objects are there?
Guess first. Being wrong here is how the surprise works.
8.Reflect
Concepts: Perception pipelines and how vision fails · Thresholding and simple classification · Lists, dictionaries and tabular data