GrabCut¶
| Fact | Value |
|---|---|
| Also called | GrabCut-inspired interactive segmentation, Iterated graph cuts |
| Status | Implemented and tested |
| Family | Segmentation and regions |
| Engine | Science engine |
| Type | Nonlinear, iterative, iterated graph cuts |
| Output | Labels |
| Operator shown as | algorithm (exact) |
| Support radius | global |
| Code | engine/src/pixelnexus_engine/filters/interactive.py, grabcut |
| Operator code | engine/src/pixelnexus_engine/filters/interactive.py, grabcut_representation |
| Packages | OpenCV |
Overview¶
GrabCut cuts out the object inside a box, after Rother, Kolmogorov and Blake. Everything outside the rectangle around your selection is background. GrabCut learns the colors of the object and the background, cuts the image with a graph cut, and repeats with the colors of the new result. The object goes to the Features panel and is drawn over the image. Click its row number to make it the selection. The filter runs in the PixelNexus engine, with OpenCV. The Object Select tool uses the same code.
How it works¶
- Mark everything outside the box as certain background, and the inside as probable object.
- Fit a mixture of 5 colors to the object pixels and one to the background pixels.
- Cut the graph to find the cheapest labels.
- Fit the mixtures again to the new labels, and repeat.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(\alpha\): The label of each pixel, object or background
- \(z\): The color of each pixel
- \(k\): Which mixture color each pixel belongs to
- \(\theta\): The two Gaussian mixture models
- \(U, V\): The data term and the smoothness term
z is the color of each pixel and alpha its label, object or background. Each side has a Gaussian mixture model of 5 colors, with the parameters theta. k says which of the 5 colors a pixel belongs to. The data term U is minus the log likelihood of each color under the model of its label. The smoothness term V costs gamma whenever neighbors get different labels. It costs less across a strong color change and between diagonal neighbors. Rother, Kolmogorov and Blake set gamma to 50 and beta from the mean color difference of neighbors. Each round assigns pixels to mixture colors, fits the mixtures, and finds the best labels with a graph cut. Pixels outside the box or marked certain never change.
GrabCut extends the graph cuts of Boykov and Jolly in two ways. The region terms come from mixtures of colors instead of histograms of brightness. And no object seeds are needed: the box gives the background, and the rounds of fitting and cutting find the object. Each round can only lower the energy, so the result settles after a few rounds.
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 |
Object | Object, Boundaries over the image | The object as a label map, drawn over the image and listed in the Features panel, or its boundary in yellow over the image. |
Start start |
Box around the selection | Box around the selection, Selection as a map | With the box, the rectangle around the selection holds the object and everything outside it is background. As a map, pixels selected at 192 or more are certain object and at 128 to 191 probable object. Pixels at 64 to 127 are probable background, and at 63 or less certain background. |
Iterations iterations |
5 | 1 to 20 | How many rounds of fitting the color models and cutting. Our choice of 5. |
Implementation¶
grabcut in engine/src/pixelnexus_engine/filters/interactive.py calls cv2.grabCut on the 8-bit image in BGR order. With the box start, the box is the rectangle around the selected pixels, those above 127. The filter stops with a message when the selection is empty, or when the box covers the whole image, since then there is no background to learn from. With the map start, the selection levels become OpenCV's four classes: certain object, probable object, probable background and certain background. The object is every pixel that ends as certain or probable object.
The selection is only an input here. Its catalog entry sets selectionRole: markers, so the studio runs the filter on the whole image. The object is a label map with one label.
Worked example¶
| Image | Noise sigma | Iterations | Area (px) | Overlap |
|---|---|---|---|---|
| Color | 10 | 1 | 2453 | 1.000 |
| Color | 10 | 5 | 2453 | 1.000 |
| Color | 10 | 10 | 2453 | 1.000 |
| Color | 30 | 1 | 2452 | 1.000 |
| Color | 30 | 5 | 2452 | 1.000 |
| Color | 30 | 10 | 2452 | 1.000 |
| Color | 60 | 1 | 2453 | 0.997 |
| Color | 60 | 5 | 2453 | 0.997 |
| Color | 60 | 10 | 2453 | 0.997 |
| Gray | 10 | 1 | 2452 | 0.966 |
| Gray | 10 | 5 | 2460 | 0.968 |
| Gray | 10 | 10 | 2460 | 0.968 |
| Gray | 30 | 1 | 0 | 0.000 |
| Gray | 30 | 5 | 0 | 0.000 |
| Gray | 30 | 10 | 0 | 0.000 |
| Gray | 60 | 1 | 0 | 0.000 |
| Gray | 60 | 5 | 0 | 0.000 |
| Gray | 60 | 10 | 0 | 0.000 |
The box of the figure. Color is the image of the figure, with the noise added to each color channel. Gray is a disk of 190 and a square of 160 on a ground of 50, slightly blurred, with gray noise. The disk has 2453 pixels. The overlap is the area of the object and the disk together divided by the area of either.
On the color image, GrabCut finds the disk almost exactly even with noise of sigma 60, and one round is already enough. The red of the disk and the blue of the ground stay far apart in color.
On the gray image, it finds the disk at noise sigma 10, but returns nothing at sigma 30 and 60. We did not trace where OpenCV loses the object. The lesson is that GrabCut leans on color, and a gray image gives it much less to work with. Graph cuts or the random walker may do better there.
Complexity¶
- Time: \(O(K C)\)
- Memory: \(O(N)\)
N is the number of pixels, K the number of rounds and C the cost of one graph cut.
Artifacts and limitations¶
Limitations
- Colors shared by object and background confuse the mixtures.
- On gray images with strong noise it can find nothing. In our tests it returned an empty object at noise sigma 30 on a gray image, while a color image worked at sigma 60.
Artifacts
- Thin parts such as hair cut off or kept with background.
Tests¶
The box must be the rectangle around the selection, and an empty or full selection must stop with a message. The map levels must give OpenCV's four classes. With a box, the result must equal OpenCV's own call, and overlap a red disk by more than 0.95. With a map, pixels marked as certain object must stay object. The representation must have its four stages for both starts.
References¶
- Rother, C., Kolmogorov, V., and Blake, A. GrabCut, interactive foreground extraction using iterated graph cuts. ACM Transactions on Graphics 23(3), 309 to 314, 2004. DOI 10.1145/1015706.1015720.
- OpenCV documentation, cv::grabCut.