Felzenszwalb-Huttenlocher segmentation¶
| Fact | Value |
|---|---|
| Also called | Felzenszwalb graph-based segmentation, Efficient graph-based segmentation |
| Status | Implemented and tested |
| Family | Segmentation and regions |
| Engine | Science engine |
| Type | Nonlinear, spatial, minimum spanning tree merging |
| Output | Labels |
| Operator shown as | algorithm (exact) |
| Support radius | global |
| Code | engine/src/pixelnexus_engine/filters/segmentation.py, felzenszwalb_filter |
| Operator code | engine/src/pixelnexus_engine/filters/segmentation.py, felzenszwalb_representation |
| Packages | scikit-image |
Overview¶
Felzenszwalb and Huttenlocher's method merges neighboring pixels into regions along a graph. It goes through the links between neighbors from the most similar pair. Two regions merge when the link between them is no larger than the variation inside them, plus a margin that shrinks as the regions grow. Busy areas stay split into small regions, and smooth areas grow large. The regions go to the Features panel with their area, centroid and mean brightness, and are drawn over the image in their label colors. Click a row number in the panel to make that region the selection, or select all regions at once. The selection can then be saved or exported as a mask, as any selection. The filter runs in the PixelNexus engine, with scikit-image.
How it works¶
- Blur the image and link each pixel to its 8 neighbors by color distance.
- Sort the edges from the lightest.
- Merge the two regions of each edge when it passes the test.
- Join regions smaller than the smallest size to a neighbor.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(w(p, q)\): The color distance between neighbors p and q
- \(\operatorname{Int}(C)\): The heaviest edge of the minimum spanning tree of region C
- \(k\): The scale
Each pixel is a node, linked to its 8 neighbors by an edge. Its weight w is the color distance after the blur. The edges are taken from the lightest. An edge between two regions C_1 and C_2 joins them when its weight passes a test for both regions. It must be no larger than the internal difference Int of the region plus k over its size. Int(C) is the heaviest edge of the minimum spanning tree of C, the largest step needed to cross it. The term k / |C| lets small regions merge across larger steps. Large regions need a clear boundary to stay apart. Here scikit-image divides the scale by 255, so k works as on pixel levels from 0 to 255.
The test compares the weight of the linking edge with the internal difference Int of each region, the largest step inside it. A region whose pixels vary a lot accepts a large step to a neighbor, and a uniform region does not. That lets the method keep textured regions together and still separate flat regions with a small step between them.
The term k / |C| matters most for small regions. A single pixel has no internal difference, so without the term nothing would ever merge. The term lets small regions merge across larger steps, and its effect fades as regions grow. So the scale k sets the size of the regions only loosely.
Taking the edges from the lightest builds the minimum spanning tree of each region on the way, which is why Int is the heaviest edge of that tree.
Kernel or operator¶
There is no kernel. The Inspector asks the engine for the stages with the current settings.
Parameters¶
| Parameter | Default | Values | Meaning |
|---|---|---|---|
Output output |
Regions | Regions, Boundaries over the image, Mean color of each region | The regions as a label map, drawn over the image and listed in the Features panel. Or the boundaries in yellow over the image, or each region filled with its mean color. |
Scale scale |
1 | 0.1 to 5000 | How strongly small regions prefer to merge. scikit-image uses 1. |
Smoothing sigma |
0.8 | 0 to 5 px | The sigma of a Gaussian blur before the graph is built. scikit-image uses 0.8. |
Smallest region minSize |
20 | 1 to 10000 px | Regions smaller than this join a neighbor at the end. scikit-image uses 20. |
Implementation¶
felzenszwalb_filter in engine/src/pixelnexus_engine/filters/segmentation.py calls skimage.segmentation.felzenszwalb on the RGB image from 0 to 1. Here scikit-image blurs each channel, links each pixel to its 8 neighbors by the Euclidean color distance, and divides the scale by 255 so it works as on pixel levels. Its labels start at 0, so the filter adds 1. The boundaries output draws skimage.segmentation.mark_boundaries in yellow, and the mean colors output fills each region with its rounded mean.
The regions output is a label map. The engine sends it with a table of the regions, and the studio draws it over the image. See the science engine for how label maps travel.
Worked example¶
| Scale | Smallest region (px) | Regions | Mean area (px) |
|---|---|---|---|
| 1 | 20 | 355 | 72 |
| 1 | 100 | 64 | 400 |
| 10 | 20 | 256 | 100 |
| 10 | 100 | 72 | 356 |
| 100 | 20 | 131 | 195 |
| 100 | 100 | 63 | 406 |
| 1000 | 20 | 89 | 288 |
| 1000 | 100 | 75 | 341 |
The test image of the figure, 160 x 160 pixels, with the other settings at their defaults. The colored squares alone would give 64 regions, and the disks cut some of them.
With scikit-image's defaults, a scale of 1 and a smallest region of 20, the noise breaks the 64 squares into 355 regions. Raising the smallest region to 100 joins the specks and gives 64 regions, close to the squares. A larger scale also merges more, but on this image it never reaches the clean result of the larger smallest region alone. The scale and the smallest region work together, and good values depend on the image and its noise.
Complexity¶
- Time: \(O(N log N)\)
- Memory: \(O(N)\)
N is the number of pixels. Sorting the edges takes the most time.
Artifacts and limitations¶
Limitations
- The scale does not set the number or size of regions directly.
- Gradual ramps can join very different colors in one region.
Artifacts
- Thin regions along soft edges.
Tests¶
The labels must equal scikit-image's felzenszwalb plus 1. On four flat colored quarters with a little noise and a smallest region of 200, it must find exactly the four quarters. The boundaries and mean colors outputs must match mark_boundaries and the rounded means. The representation must have its three stages.
References¶
- Felzenszwalb, P. F., and Huttenlocher, D. P. Efficient graph-based image segmentation. International Journal of Computer Vision 59(2), 167 to 181, 2004. DOI 10.1023/B:VISI.0000022288.19776.77.
- scikit-image documentation, skimage.segmentation.felzenszwalb.