TactivergeLite

Lite / Intelligent Robotic Systems / Week 25

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 · Full interactive version

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.

Predict

Two square blobs touch only at one corner. With 4-connectivity (up, down, left, right), how many objects are there?

  1. 1
  2. 2
  3. It depends on the threshold
Guess first, then reveal

4-connectivity ignores diagonal neighbours, so a single shared corner does not join them: 2 objects. With 8-connectivity they would merge into 1. The choice is a design decision.

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.

What the interactive shows (text version)

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.

You can also open the interactive on the full site when you have a faster connection.

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

Intuitive

Pour paint onto one pixel of a blob and let it spread into every touching object pixel: everything that gets painted is one object.

Technical

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.

Mathematical

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.

Applied

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

Practice

Practice, not a test. Answers are checked on our server; a wrong answer gets a hint and another try. Nothing is saved.

mm²

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?

%

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.

Reflect

Where would a robot's gripper go wrong if it trusted centroids without checking shape?

Write your answer on paper or in your own notes. Lite never stores what you write.

← Edge detection with SobelConfusion matrices, precision and recall →