SLIC superpixels¶
| Fact | Value |
|---|---|
| Also called | SLIC, Simple linear iterative clustering, Superpixels |
| Status | Implemented and tested |
| Family | Segmentation and regions |
| Engine | Science engine |
| Type | Nonlinear, spatial, local k-means clustering |
| Output | Labels |
| Operator shown as | algorithm (exact) |
| Support radius | global |
| Code | engine/src/pixelnexus_engine/filters/segmentation.py, slic_filter |
| Operator code | engine/src/pixelnexus_engine/filters/segmentation.py, slic_representation |
| Packages | scikit-image |
Overview¶
SLIC splits the image into compact superpixels of about equal size that follow color edges. Achanta and coworkers built it as k-means clustering on color and position, where each pixel only looks at the centers near it. That keeps it fast and makes the superpixels compact. 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¶
- Turn the image into CIELAB and place the centers on a grid.
- Give each pixel to the nearest center within 2 S, by color and position.
- Move each center to the mean of its pixels, and repeat.
- Join cut-off parts to a neighbor, so every superpixel is connected.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(d_c, d_s\): The distances in CIELAB color and in position
- \(S\): The grid step of the starting centers
- \(m\): The compactness
N is the number of pixels and K the number of superpixels, so the centers start on a grid with the step S. Each pixel is compared with the centers within 2 S of it. The distance D joins the color distance d_c in CIELAB and the distance in position d_s, scaled by S and the compactness m. Each pixel joins its nearest center, the centers move to the mean color and position of their pixels, and this repeats. Finally, parts of superpixels that are cut off join a neighbor, so every superpixel is connected. Searching only near each center makes it linear in the number of pixels.
The distance D mixes color and position. Color alone would give scattered regions, and position alone a fixed grid of squares. The compactness m sets the balance. Dividing the position distance by the grid step S makes m work the same at any number of superpixels.
The search window is the key to its speed. Each center only competes for the pixels within 2 S of it, so every pixel is compared with a few centers, not with all of them. The cost is linear in the number of pixels, whatever the number of superpixels.
The clustering alone can leave a superpixel in several pieces. A last pass joins pieces smaller than half the expected size to a neighbor, and the labels are then numbered from 1.
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 superpixels as a label map, drawn over the image and listed in the Features panel. Or the boundaries in yellow over the image, or each superpixel filled with its mean color. |
Superpixels segments |
100 | 2 to 5000 | About how many superpixels to make. scikit-image uses 100. The result can have fewer. |
Compactness compactness |
10 | 0.01 to 100 | How much position counts against color. scikit-image uses 10. |
Iterations iterations |
10 | 1 to 50 | The number of k-means steps. scikit-image uses 10. |
Smoothing sigma |
0 | 0 to 5 px | The sigma of a Gaussian blur before clustering. scikit-image uses 0, no blur. |
Implementation¶
slic_filter in engine/src/pixelnexus_engine/filters/segmentation.py calls skimage.segmentation.slic on the RGB image from 0 to 1. Here scikit-image turns it into CIELAB and enforces connectivity. Its labels start at 1, and relabel_sequential numbers them without gaps. The boundaries output draws skimage.segmentation.mark_boundaries in yellow, its default. The mean colors output fills each region with the mean of each channel, rounded.
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¶
| Superpixels asked | Compactness | Regions | Mean area (px) |
|---|---|---|---|
| 25 | 1 | 6 | 4267 |
| 25 | 10 | 7 | 3657 |
| 25 | 100 | 22 | 1164 |
| 100 | 1 | 48 | 533 |
| 100 | 10 | 60 | 427 |
| 100 | 100 | 95 | 269 |
| 400 | 1 | 37 | 692 |
| 400 | 10 | 262 | 98 |
| 400 | 100 | 400 | 64 |
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 400 superpixels asked and a compactness of 10, the filter makes 262. A compactness of 1 lets color win, so the clusters scatter into many pieces. With 400 asked, the clustering ends with 251 clusters, and the connectivity pass joins their small pieces to neighbors until only 37 remain. A compactness of 100 makes position win, and the superpixels become a near grid of the size asked for. The number asked is a target, and the result depends on the image.
Complexity¶
- Time: \(O(I N)\)
- Memory: \(O(N)\)
N is the number of pixels and I the number of iterations.
Artifacts and limitations¶
Limitations
- The number of superpixels is only a target.
- Thin structures narrower than a superpixel can be lost.
Artifacts
- Grid-like shapes in flat areas, where no edge guides the superpixels.
Tests¶
The labels must equal scikit-image's slic as a partition, numbered from 1 without gaps. The boundaries must equal mark_boundaries, and the mean colors must fill each region with its rounded mean. The label map must encode in the smallest width that fits, and the table must give each region's area, centroid and mean. The engine API test decodes the label map from the JSON result.
References¶
- Achanta, R., Shaji, A., Smith, K., Lucchi, A., Fua, P., and Susstrunk, S. SLIC superpixels compared to state-of-the-art superpixel methods. IEEE Transactions on Pattern Analysis and Machine Intelligence 34(11), 2274 to 2282, 2012. DOI 10.1109/TPAMI.2012.120.
- scikit-image documentation, skimage.segmentation.slic.