Graph cuts¶
| Fact | Value |
|---|---|
| Also called | Interactive graph cuts, Boykov-Jolly, Min cut segmentation |
| Status | Implemented and tested |
| Family | Segmentation and regions |
| Engine | Science engine |
| Type | Nonlinear, iterative, minimum graph cut |
| Output | Labels |
| Operator shown as | algorithm (exact) |
| Support radius | global |
| Code | engine/src/pixelnexus_engine/filters/interactive.py, graph_cut |
| Operator code | engine/src/pixelnexus_engine/filters/interactive.py, graph_cut_representation |
| Packages | SciPy |
Overview¶
Graph cuts split the image into object and background from a few seeds, after Boykov and Jolly. Every pixel becomes a node in a graph. Each node is joined to its neighbors, and to two terminals, one for the object and one for the background. The cheapest cut that separates the terminals, and keeps every seed on its own side, gives the object. 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 SciPy.
How it works¶
- Read the object and background seeds from the selection.
- Count the brightness of each set of seeds in a histogram.
- Join every pixel to its 8 neighbors and to the two terminals.
- Find the maximum flow from the object terminal to the background terminal.
- The object is every pixel the object terminal still reaches.
Mathematical foundation¶
The formula shows the default parameter values.
where
- \(A_p\): The label of pixel p, object or background
- \(R_p\): The region cost of the label at p
- \(B_{pq}\): The boundary cost between neighbors p and q
- \(\lambda\): The region weight
- \(\sigma\): The edge contrast
A labels every pixel p as object or background. The region term R is the cost of the label at p. It is minus the log of how likely the brightness I at p is among the seeds of that label. The boundary term B is the cost of cutting between neighbors p and q. It is high when they look alike. Boykov and Jolly built a graph with one node per pixel and two terminals. The source stands for the object, the sink for the background. Neighbor links carry B. Each unknown pixel links to the source with lambda R(background) and to the sink with lambda R(object). A seed links to its own terminal with a weight K. K is larger than any cut through its neighbors, so a seed never changes side. A minimum cut of this graph is a minimum of E, and a maximum flow finds it.
The energy has two parts that pull against each other. The region term asks each pixel to look like the seeds of its side. The boundary term asks the cut to be short and to run where neighbors differ. Boykov and Jolly showed that the minimum of this energy, with the seeds fixed, is a minimum cut of their graph. A maximum flow finds that cut exactly, so the result is the global best for the energy, not a local one.
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. |
Seeds seeds |
Selection and image edge | Selection and image edge, Selection as a map | With the selection and image edge, the selected pixels are object seeds and the unselected pixels on the image edge are background seeds. As a map, pixels selected at 192 or more are object seeds, those at 63 or less are background seeds, and partly selected pixels are unknown. |
Edge contrast sigma |
10 | 1 to 100 levels | The sigma in the boundary weight. Neighbors that differ by much more than sigma are cheap to cut apart. Boykov and Jolly set it from the camera noise. 10 is our choice. |
Region weight lambda |
1 | 0 to 10 | The weight lambda of the region terms against the boundary terms. |
Implementation¶
graph_cut in engine/src/pixelnexus_engine/filters/interactive.py builds the graph itself and cuts it with scipy.sparse.csgraph.maximum_flow, which uses Dinic's method. The seeds come from the selection, as the Seeds parameter says. The region terms come from histograms of the luma of each set of seeds, with 32 bins of 8 levels. A level that no seed has gets the probability 0.0001, the same for both sides (our choice, so its cost is finite and favors neither side). SciPy needs whole-number capacities, so all weights are multiplied by 1000 and rounded. The object is every pixel the object terminal still reaches when the flow is at its maximum.
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¶
| Edge contrast sigma | Region weight lambda | Seeds | Area (px) | Overlap | Share of the square |
|---|---|---|---|---|---|
| 5 | 0 | Spot | 2457 | 0.960 | 0.00 |
| 5 | 0 | Map | 2457 | 0.960 | 0.00 |
| 5 | 1 | Spot | 4318 | 0.549 | 0.96 |
| 5 | 1 | Map | 2500 | 0.771 | 0.18 |
| 10 | 0 | Spot | 2445 | 0.967 | 0.00 |
| 10 | 0 | Map | 2445 | 0.967 | 0.00 |
| 10 | 1 | Spot | 4314 | 0.552 | 0.96 |
| 10 | 1 | Map | 2374 | 0.824 | 0.10 |
| 20 | 0 | Spot | 88 | 0.036 | 0.00 |
| 20 | 0 | Map | 88 | 0.036 | 0.00 |
| 20 | 1 | Spot | 4330 | 0.552 | 0.96 |
| 20 | 1 | Map | 2246 | 0.874 | 0.03 |
The image of the figure. Spot is the small square selected inside the disk, with the image edge as background. Map also marks a small square inside the square as background. The disk has 2453 pixels. The overlap is the area of the object and the disk together divided by the area of either. The share of the square is the part of the square inside the object.
With only edges, a region weight of 0, the cut follows the disk edge, with an overlap of 0.967 at the default contrast. At a contrast of 20, edges are too cheap to matter, and the cut shrinks to 88 pixels around the seed.
With the default region weight, the square joins the object. Its gray of 160 is far closer to the bright seed in the disk than to the dark image edge, so the region terms pull it in. Marking a spot in the square as background helps, but the square's histogram overlaps the disk's, so some of its pixels stay object. The figure shows them as specks. Graph cuts on brightness alone cannot do better here. GrabCut, which uses color, can.
Complexity¶
- Time: \(O(N^2 E)\)
- Memory: \(O(N)\)
N is the number of pixels and E the number of links, for Dinic's method in the worst case. Image graphs are much faster in practice.
Artifacts and limitations¶
Limitations
- Only brightness, not color. Objects of the same brightness as the background need many seeds.
- Parts that look like the object seeds join the object unless a background seed marks them.
Artifacts
- With a low region weight, the cut shrinks around the seeds.
Tests¶
The seeds must follow the border and map rules, and missing seeds must stop with a message. The region costs must come from the histogram with its floor. On a small noisy image, the energy of the result, written out on its own in the test, must be no higher than that of 23 other labelings. From a spot in a noisy disk, the object must overlap the disk by more than 0.95. The representation must have its four stages.
References¶
- Boykov, Y. Y., and Jolly, M.-P. Interactive graph cuts for optimal boundary and region segmentation of objects in N-D images. Proceedings of the Eighth IEEE International Conference on Computer Vision (ICCV), volume 1, 105 to 112, 2001. DOI 10.1109/ICCV.2001.937505.
- SciPy documentation, scipy.sparse.csgraph.maximum_flow.