Connected-component filtering¶
| Fact | Value |
|---|---|
| Also called | Area filter, Remove small objects |
| Status | Implemented and tested |
| Family | Morphological filters |
| Engine | Browser |
| Type | Nonlinear, spatial, component labeling and area test |
| Output | Mask |
| Operator shown as | algorithm (exact) |
| Support radius | global |
| Code | web/src/core/filters/morphology/binary.ts, cclFilter |
| Operator code | web/src/core/filters/morphology/binary.ts, labelComponents |
| Channels | Brightness only, RGB |
Overview¶
Connected-component filtering keeps the white objects whose area lies in a chosen range and removes the others. It cleans up specks after a threshold without changing the shape of the objects that stay.
How it works¶
- Find the separate objects, with 4 or 8 neighbors.
- Count the pixels of each object.
- Keep the objects whose area lies in the range.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(C_i\): The objects
- \(a_{\\min}, a_{\\max}\): The smallest and largest area
C_i are the objects, the connected parts of the foreground, and |C_i| their areas in pixels. Objects with an area in the range are kept, all others become background.
Labeling gives each connected object its own number. The area of an object is its number of pixels. Keeping objects by area is the simplest attribute filter: it removes whole objects and never changes the ones it keeps.
Kernel or operator¶
There is no kernel. The Inspector lists the labeling and the area range.
Algorithm¶
- Make the image binary.
- Label its objects with 4 or 8 neighbors.
- Count the pixels of each object.
- Keep the objects in the area range.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Neighbors connectivity |
8 neighbors | 8 neighbors, 4 neighbors | Which foreground pixels count as one object. |
Smallest area minArea |
10 | 0 to 1e+06 px | Objects with fewer pixels are removed. |
Largest area maxArea |
100000 | 1 to 1e+06 px | Objects with more pixels are removed. |
Channels channelScope |
Brightness only | RGB, Brightness only | Which part of the color image the filter changes. |
Implementation¶
labelComponents in web/src/core/filters/morphology/binary.ts labels the objects by flood fill, and cclFilter keeps those in the area range. It is a global filter, so it always runs on the whole image in one tile, and a selection is cut out of the result. The filter runs in the browser engine through the shared filter framework in web/src/core/filters/run.ts, which handles the channel scope and alpha. Values at or above 128 count as white, and the result is 255 or 0.
Worked example¶
| Row | Input | Result |
|---|---|---|
| 0 | 1 0 0 0 0 0 0 0 0 0 |
0 0 0 0 0 0 0 0 0 0 |
| 1 | 0 0 0 0 0 0 0 0 0 0 |
0 0 0 0 0 0 0 0 0 0 |
| 2 | 0 1 1 1 1 1 1 0 0 0 |
0 1 1 1 1 1 1 0 0 0 |
| 3 | 0 1 1 1 1 1 1 0 0 0 |
0 1 1 1 1 1 1 0 0 0 |
| 4 | 0 1 1 1 1 1 1 0 0 0 |
0 1 1 1 1 1 1 0 0 0 |
| 5 | 0 0 0 0 0 0 0 0 0 0 |
0 0 0 0 0 0 0 0 0 0 |
| 6 | 0 0 1 0 0 0 0 0 0 0 |
0 0 1 0 0 0 0 0 0 0 |
| 7 | 0 0 1 0 0 0 0 0 0 0 |
0 0 1 0 0 0 0 0 0 0 |
| 8 | 0 0 1 1 1 0 0 0 0 0 |
0 0 1 1 1 0 0 0 0 0 |
A small binary image, 1 for white and 0 for black. The isolated pixel is an object of area 1, below the smallest area of 3, so it is removed. The bar of 18 pixels stays.
Complexity¶
- Time: \(O(N)\)
- Memory: \(O(N)\)
N is the number of pixels per channel.
Artifacts and limitations¶
Limitations
- Area alone ignores shape.
Artifacts
- Thin long objects count as large even when they are thin.
Tests¶
The reference uses scipy.ndimage.label. The cases cover two area ranges with 4 and 8 neighbors.
References¶
- Gonzalez, R. C. and Woods, R. E. Digital Image Processing, 4th edition. Pearson, 2018. Chapter 9, morphological image processing.
- SciPy documentation, scipy.ndimage.label. https://docs.scipy.org/doc/scipy/reference/generated/scipy.ndimage.label.html