Split-and-merge segmentation¶
| Fact | Value |
|---|---|
| Also called | Quadtree segmentation, Split and merge |
| Status | Implemented and tested |
| Family | Segmentation and regions |
| Engine | Science engine |
| Type | Nonlinear, spatial, quadtree split and merge |
| Output | Labels |
| Operator shown as | algorithm (exact) |
| Support radius | global |
| Code | engine/src/pixelnexus_engine/filters/split_merge.py, split_merge |
| Operator code | engine/src/pixelnexus_engine/filters/split_merge.py, split_merge_representation |
| Packages | NumPy |
Overview¶
Split-and-merge works on a quadtree, after Horowitz and Pavlidis. It cuts the image into quarters, again and again, until every block is uniform. Then it glues neighboring blocks back together while the result stays uniform. A region counts as uniform when its brightest and darkest pixels differ by at most a threshold. The blocks of the split can also be shown. 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 filter runs in the PixelNexus engine, in our own code.
How it works¶
- Take the luma.
- Split each block that is not uniform into four, down to the smallest block.
- Merge neighboring regions whose union is still uniform, closest means first.
- Number the regions.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(H(R)\): Whether region R is uniform
- \(Y\): The luma in pixel levels
- \(T\): The threshold
The predicate H says whether a region R is uniform: its luma Y spans at most the threshold T. This predicate is our choice. Horowitz and Pavlidis allow any test of uniformity. The split starts with the whole image and cuts every block that fails into four quarters, down to the smallest block. The quadtree then over-segments, since a uniform area cut by the grid stays in many blocks. The merge joins two neighboring regions when their union still passes the test. Here the pairs with the closest mean brightness are tried first, which is also our choice, and a merged region is tested again with its new neighbors.
The split alone over-segments. A uniform area that the grid of quarters cuts stays in several blocks, since the split never looks across block borders. The merge fixes that by testing neighbors across those borders.
The order of the merge matters, because a merge changes the range of the merged region. This filter tries the neighbors with the closest mean brightness first, and tests each new pair against the regions as they are after earlier merges. Horowitz and Pavlidis leave both the test and the order open. Both choices here are ours.
The range test is strict. One bright pixel fails a whole block, so noise needs a threshold well above its spread.
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, Blocks after the split, 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 blocks of the quadtree before merging, as a label map. Or the boundaries in yellow over the image, or each region filled with its mean color. |
Threshold threshold |
30 | 0 to 255 levels | A region is uniform when its brightest and darkest pixels differ by at most this. |
Smallest block smallest |
4 | 1 to 64 px | A block with both sides at most this does not split further. |
Implementation¶
split_merge in engine/src/pixelnexus_engine/filters/split_merge.py takes the luma in pixel levels. The split keeps a stack of blocks, starting with the whole image. A block that is not uniform splits into four, unless both of its sides are at most the smallest block. A side of odd length splits into a first half of length // 2 and a second half of the rest, so blocks need not be square. A side of 1 cannot split, so such a block splits into two halves only.
A block that stops at the smallest size can still fail the test. It then merges with no neighbor, so it stays a region of its own. The merge keeps the range and the mean of every region in a union-find structure and takes the pairs from a priority queue. Last, the regions are numbered in the raster order of their first pixel.
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¶
| Threshold | Smallest block (px) | Regions | Mean area (px) |
|---|---|---|---|
| 20 | 1 | 464 | 55 |
| 20 | 4 | 562 | 46 |
| 30 | 1 | 77 | 332 |
| 30 | 4 | 171 | 150 |
| 60 | 1 | 49 | 522 |
| 60 | 4 | 119 | 215 |
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.
The noise of sigma 6 spreads each square over about 30 levels of luma. With a threshold of 20, almost no block passes, and the filter makes hundreds of regions. With 30 and blocks down to 1 pixel, it makes 77, close to the 64 squares. With the default smallest block of 4, blocks that straddle an edge stop splitting and stay as regions of their own, which gives 171. A threshold of 60 merges neighboring squares of similar brightness.
Complexity¶
- Time: \(O(N log N)\)
- Memory: \(O(N)\)
N is the number of pixels. The split visits each pixel once per level of the quadtree.
Artifacts and limitations¶
Limitations
- The range test is very sensitive to noise. One outlier pixel fails a whole block.
- Region borders follow the block grid where the merge cannot smooth them.
- Small blocks that straddle an edge are not uniform but cannot split, so they stay regions of their own.
- Gradual shading breaks into bands.
Artifacts
- Blocky, staircase borders along curves and diagonals.
Tests¶
A uniform image must be one block. A single bright pixel must split its quarter down to that pixel. Odd sides must split down to single pixels. With a smallest block of 4, a random image must split into 16 blocks of 4 x 4. The merge must join the grid back into two uniform areas, and every merged region must stay within the threshold when blocks go down to one pixel. With the default smallest block, blocks across an edge must stay separate regions.
References¶
- Horowitz, S. L., and Pavlidis, T. Picture segmentation by a tree traversal algorithm. Journal of the ACM 23(2), 368 to 388, 1976. DOI 10.1145/321941.321956.