A binary mask says object or background. Two touching nuclei are one blob: the boundary between them does not exist in a mask.
A label image stores an integer per pixel: 0 = background, k = object k. Touching objects can have different labels; the mask of object k is \{L = k\}.
Data type: 8 bits allow only 255 objects; use 16 or 32 bits. After deleting objects, relabel sequentially (1, 2, 3, … without gaps).
Display with a label palette where neighbouring numbers get very different colours, never with a grey ramp.
The 41 objects of the demo’s synthetic image (threshold 100), by the kind of object they came from.
A scatter plot of two columns shows populations: in the synthetic image of the demo, round nuclei (area 122–545, eccentricity up to 0.91, solidity 0.93–0.96), rods (area 158–220, eccentricity 0.95–0.98), specks (area 1–11) and merged pairs (area 693–932, solidity 0.88–0.89). No single threshold on one property separates all of them; two properties together often do.
Filtering objects
Remove small objects: keep components with area \ge a. This is the area opening, a connected filter: objects are kept or removed whole, the shapes of the survivors are untouched. A morphological opening removes small objects too, but also rounds the corners and cuts thin parts of all the others (opening by reconstruction, morphology day, does the same as the area opening for “contains a disc of radius r”).
Keep round objects: eccentricity, form factor or solidity in a range.
Hysteresis: label the low-threshold mask and keep the labels that contain at least one high-threshold pixel (the idea is from the thresholding day; reconstruction does the same).
Filters on the table, then relabel the label image.
Orange: pixels removed (64 × 64 crop; counts for the whole image).
The math: connected filters
A connected filter acts on whole components: \psi(X) = \bigcup\{C : C \text{ a component of } X,\ \text{criterion}(C)\}.
Anti-extensive: \psi(X) \subseteq X. Idempotent: \psi(\psi(X)) = \psi(X) (the kept components are still components and still pass).
Increasing (X \subseteq Y \Rightarrow \psi(X) \subseteq \psi(Y)) when the criterion is increasing, such as “area \ge a”: a component of X lies in a component of Y that is at least as large. Then \psi is an algebraic opening: the area opening.
Not increasing: “eccentricity < 0.8” (a larger set can be more elongated). Then the filter is called a thinning in morphology’s sense.
Area opening of a grey image: apply it to every threshold set (the max-tree), the grey counterpart; the watershed day cuts its merge tree by area, and merge trees (day 29) build the tree itself.
Counting, and how it goes wrong
The count = number of labels after filtering. Each error has a typical fix:
error
effect
fix
touching objects merge
undercount
split them: watershed (day 15); solidity flags them
an object falls apart (gaps, dim parts)
overcount
closing or a lower threshold before labelling
noise specks, debris
overcount
area filter, opening
objects cut by the border
wrong sizes, count bias
counting frame
4 vs 8 connectivity
diagonal contacts change the count
fix the convention (8/4)
A count can be right while the objects are wrong (one merge + one split): only matching objects to a ground truth catches that (evaluation day, day 38).
Border objects and the counting frame
Removing all objects that touch the border biases the count and the sizes: large objects touch the border more often, so they are under-represented.
Keeping all of them counts objects that are mostly outside, and an object cut by two neighbouring fields is counted twice.
Unbiased counting frame (Gundersen, 1977): an inner frame with inclusion edges (top, left) and an exclusion line (right and bottom, extended to the image border). Count an object if it hits the frame and does not touch the exclusion line. Tiling the plane with such frames counts every object exactly once.
The counting frame as pseudocode
for every pixel p with label k > 0:
if p lies in the frame [m, W − m) × [m, H − m):
hits[k] = true
if p lies on the exclusion line: # column W − m, rows 0 … H − m,
excluded[k] = true # and row H − m, columns 0 … W − m
count = number of labels k with hits[k] and not excluded[k]
One scan of the label image, two yes/no values per label: no shapes, no distances.
The objects that are counted may reach into the margin m: it must be wider than the largest object, or their sizes are cut.
The math: why the frame is unbiased
Associated points. Give each object one unique point, e.g. its first pixel in raster order, and count the objects whose point lies in the frame [x_0, x_1) \times [y_0, y_1). Frames that tile the plane partition the points, so every object is counted exactly once, and for objects scattered at random the expected count is (number density) × (frame area), whatever their size and shape.
Gundersen’s frame does the same without finding the point. In a tiling, a connected object hits a block of neighbouring frames; the exclusion lines (right edge extended upwards, bottom edge extended leftwards, the mirror image of Gundersen’s left/bottom convention) are placed so that exactly one frame of that block counts it. (Stated, not proved here.)
Discarding border objects instead keeps an object only if it fits entirely in the image, with probability \approx (W - w)(H - h)/(WH) for an object with a w \times h box: smaller for larger objects. The count is biased towards small objects, and so is the mean size.
The guard zone outside the frame must be at least as wide as the largest object, so that every counted object is measured completely.
The pipeline
On the synthetic image of the demo (256 × 256: 15 nuclei, 3 touching pairs, 9 rods, 22 specks of which 14 are bright enough):
Threshold (100): 41 labels.
Open (radius 2) or filter by area (\ge 40): the specks go: 27.
Fill holes, choose 8-connectivity (here neither changes the count).
Label, measure: one row per object.
Filter: keep solidity \ge 0.91: 15 single nuclei (rods and merged pairs fall below).
Border rule: counting frame (margin 20): 19 of the 27.
Count, and report what was excluded and why.
Frame: inclusion edges green, exclusion line red; grey = not counted.
Each step changes the count; write the steps down as part of the result.
Try it: filter and count
Summary
A label image keeps touching objects apart; relabel after deleting, use 16/32 bits and a label palette.
The measurement table (one row per object) and a scatter plot of two properties reveal populations.
Area opening = connected filter: removes small objects whole, keeps the others’ shapes.