Mean shift¶
| Fact | Value |
|---|---|
| Also called | Mean-shift segmentation, Mean-shift filtering |
| Status | Implemented and tested |
| Family | Segmentation and regions |
| Engine | Science engine |
| Type | Nonlinear, color, mode seeking in position and color |
| Output | Labels |
| Operator shown as | algorithm (exact) |
| Support radius | global |
| Code | engine/src/pixelnexus_engine/filters/segmentation.py, mean_shift |
| Operator code | engine/src/pixelnexus_engine/filters/segmentation.py, mean_shift_representation |
| Packages | OpenCV, SciPy |
Overview¶
Mean shift treats each pixel as a point in position and color, and moves it to the nearest peak of the density of its neighbors. Pixels in one object climb to the same peak and take nearly the same color, so areas flatten and edges stay sharp. Comaniciu and Meer made it a standard tool for filtering and segmentation. The filtered image can be shown as it is. 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, with OpenCV.
How it works¶
- Treat each pixel as a point in position and color.
- Move each point to the mean of its window, until it stops.
- Give each pixel the color where its point stopped.
- Group neighbors of similar filtered color into regions.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(x_s, x_c\): The position and the color of a point
- \(h_s, h_c\): The spatial and color radii
- \(W(x)\): The window around x
Each pixel is a point x in a joint space of position x_s and color x_c. The window W(x) holds the pixels within the spatial radius h_s and the color radius h_c. Mean shift moves x to the mean of its window and repeats. The mean always points uphill on the density of the points, so x climbs to a peak, a mode, as Comaniciu and Meer showed. OpenCV stops after 5 steps or when the shift falls below 1, and gives the pixel the color where it stopped. Pixels in one object climb to the same mode, so they get nearly the same color. Edges stay sharp, because a pixel across an edge is outside the color radius. The regions are then the connected patches of similar color.
The mean of the points in a window always points toward where the points are denser. Moving to it again and again climbs the density to a peak, a mode. Comaniciu and Meer showed that this climb converges, and that with a flat window the step goes to the mean of the window.
The window is a box in position and a ball in color. A pixel across an edge differs in color by more than the color radius, so it does not pull the mean across the edge. That is why mean shift smooths areas and keeps edges, like a bilateral filter that keeps going until it settles.
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, Filtered image, 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 mean-shift filtered image, which is flat inside each region. Or the boundaries in yellow over the image, or each region filled with its mean color. |
Spatial radius spatialRadius |
10 | 1 to 50 px | How far in position the window reaches. |
Color radius colorRadius |
20 | 1 to 100 levels | How far in color the window reaches. |
Pyramid levels levels |
1 | 0 to 3 | OpenCV runs on a pyramid with this many smaller levels, and refines from the smallest. OpenCV uses 1. |
Color tolerance colorTolerance |
2 | 0 to 20 levels | Neighbors whose filtered colors differ by at most this in each channel join one region. 0 joins equal colors only. |
Implementation¶
mean_shift in engine/src/pixelnexus_engine/filters/segmentation.py calls cv2.pyrMeanShiftFiltering on the 8-bit RGB image with the spatial and color radii. OpenCV stops each pixel after 5 steps or when it moves less than 1, and gives it the color where it stopped. With pyramid levels above 0, OpenCV first works on smaller copies of the image and refines from the smallest.
The filtered colors are not exactly equal inside a region, so the regions are the 4-connected parts in which neighbors differ by at most the color tolerance in each channel. This grouping is our choice. A tolerance of 0 joins equal colors only.
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¶
| Spatial radius (px) | Color radius | Color tolerance | Regions | Mean area (px) |
|---|---|---|---|---|
| 5 | 20 | 0 | 3507 | 7 |
| 5 | 20 | 2 | 216 | 119 |
| 5 | 20 | 6 | 168 | 152 |
| 10 | 20 | 0 | 564 | 45 |
| 10 | 20 | 2 | 185 | 138 |
| 10 | 20 | 6 | 164 | 156 |
| 10 | 40 | 0 | 986 | 26 |
| 10 | 40 | 2 | 236 | 108 |
| 10 | 40 | 6 | 167 | 153 |
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 no tolerance, even the default radii give 564 regions, since the filtered colors still differ by a level here and there. A tolerance of 2 brings that down to 185, and 6 to 164. That is still more than the 64 squares, because the noise leaves small patches of their own color inside some squares.
Complexity¶
- Time: \(O(I N h_s^2)\)
- Memory: \(O(N)\)
N is the number of pixels, I the number of steps, at most 5, and h_s the spatial radius.
Artifacts and limitations¶
Limitations
- Noise and gradual shading leave many small regions.
- Slow for large radii.
Artifacts
- Thin bands of their own color along soft edges.
Tests¶
The filtered colors must equal OpenCV's pyrMeanShiftFiltering, and the filtered output must show them. Grouping must join only equal colors with a tolerance of 0, and colors within the tolerance otherwise. The representation must have its three stages.
References¶
- Comaniciu, D., and Meer, P. Mean shift, a robust approach toward feature space analysis. IEEE Transactions on Pattern Analysis and Machine Intelligence 24(5), 603 to 619, 2002. DOI 10.1109/34.1000236.
- OpenCV documentation, cv2.pyrMeanShiftFiltering.