Working with label images

Image analysis

About this lecture

We can label objects and measure them. This unit puts it together into a measurement pipeline and asks what can go wrong with the count.

  • Label image vs binary mask: when you need which.
  • The measurement table and a scatter plot of two properties.
  • Filtering objects by their properties: area opening, roundness, border objects.
  • Counting and its errors: merged, split, debris, and objects cut by the image border; the unbiased counting frame.
  • The whole pipeline on one image: threshold → clean → label → measure → filter → count.

Slides titled The math give the formulas and proofs; the ideas are complete without them.

Label image or mask?

  • 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.
  • Who produces label images: this day’s labelling, the watershed (day 15), graph cuts with several labels (day 27), level sets (day 31), and instance-segmentation networks (day 41).

Three panels of the same small crop: the grey image of two touching nuclei, the thresholded mask in which they are a single blob, and the label image in which they have two different colours.

Touching nuclei in the demo’s synthetic image.

The measurement table

label area eccentricity solidity form factor …
1 5 0.82 0.91 1.03
2 198 0.95 0.86 0.57
3 545 0.21 0.96 1.01
…

One row per object, one column per property.

Scatter plot of solidity against area on a log scale for the 41 labelled objects: 14 grey specks with areas from 1 to 11, 9 orange rods near area 200 with solidity 0.85 to 0.90, 15 blue round nuclei with areas 122 to 545 and solidity 0.93 to 0.96, and 3 red merged pairs with areas 693 to 932 and solidity 0.88 to 0.89. Dashed lines at area 40 and solidity 0.91 separate the round nuclei from the rest.

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.
  • Remove border objects: bounding box touches the image edge (or clear border, morphology day).
  • 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.

Three panels of a crop of the thresholded mask: as thresholded; after the area opening, which removes the two small specks whole and leaves the other objects unchanged; and after an opening with radius 2, which removes the same specks but also nibbles pixels off the corners of the nuclei and the rod. Both leave 27 objects in the whole image, from 41.

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):

  1. Threshold (100): 41 labels.
  2. Open (radius 2) or filter by area (\ge 40): the specks go: 27.
  3. Fill holes, choose 8-connectivity (here neither changes the count).
  4. Label, measure: one row per object.
  5. Filter: keep solidity \ge 0.91: 15 single nuclei (rods and merged pairs fall below).
  6. Border rule: counting frame (margin 20): 19 of the 27.
  7. Count, and report what was excluded and why.

Four panels of the demo's synthetic image: the grey image; the 41 labels after thresholding; the 27 objects with area at least 40; and the counting frame with margin 20, where 19 objects are counted and the objects touching the red exclusion line or lying outside the frame are grey.

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.
  • Counting errors: merges (watershed), splits (closing), debris (area), border (counting frame), connectivity.
  • Counting frame: inclusion edges top/left, exclusion line right/bottom: each object counted once, no size bias.
  • Write the pipeline down: every step changes the number.