GrabCut Cuts Objects Out From One Box
Draw one loose box around the object and GrabCut learns what foreground and background look like, then refines the border itself by repeating graph cut with color models.
Why Does This Exist?
Graph cut finds the optimal binary labeling but demands per-pixel evidence: somebody must say which pixels are foreground and which are background. Painting exact seeds is slow, expert work. GrabCut (Rother, Kolmogorov, and Blake, 2004) asks for one loose rectangle instead and manufactures the rest itself, which made it the default photo cutout tool in editors for a decade.
The prerequisite is the graph-cut machinery: t-links, n-links, and min-cut optimality. This page covers the color models, the tri-map, and the iteration loop. Fully automatic multi-class labeling belongs to semantic segmentation instead, and prompt-driven cutouts belong to Segment Anything.
Think of It Like This
A bouncer with an improving description
A bouncer gets one rough instruction, "everyone inside the rope line might belong, everyone outside definitely does not," plus a description of the guest of honor that sharpens every round. Each round the bouncer re-sorts the crowd, studies the two groups afresh, and redraws the line. After a few rounds the line sits exactly on the guest list.
Where it stops: when the guest wears the same uniform as the crowd, no description separates them. Camouflage in vision terms defeats color evidence entirely, which is why GrabCut accepts corrective strokes as new sure labels.
How It Actually Works
The tri-map from one box
Everything outside the user box is marked sure background. Everything inside starts as unknown, tentatively foreground. That three-way labeling (sure background, probable foreground, probable background after the first cut) is the tri-map, and it is the only human input the algorithm ever sees unless corrections arrive later.
Color models both sides learn
Foreground and background colors are each modeled as a Gaussian mixture with five components in RGB space. Unknown pixels get assigned to their most likely component, the mixtures refit to their assigned pixels, and a graph cut re-labels every pixel using the fresh mixtures as its data term. Assign, learn, cut, repeat, usually about five rounds, until labels stop changing. The smoothness term keeps borders clean while the learned colors pull interiors, and a final border-matting pass estimates partial alpha along the edge for hair-soft transitions.
Worked example
Take a photo with a user box. Total pixels: . Unknown inside the box: . Sure background outside: . A typical run settles with roughly of the unknown labeled foreground, leaving reclaimed as background, the mixtures having learned skin and shirt tones on one side and grass tones on the other. The exact split depends on the image; the budget arithmetic does not.
One assign step by hand: suppose the foreground mixture currently centers on brightness and the background on . Unknown pixel sits from foreground but from background, so it joins foreground. Unknown pixel sits from background against from foreground, so it joins background. Refitting then moves each mixture toward its new members, and the next cut uses those sharper models.
Code
Pure Python, no imports: the tri-map budget from the worked example plus one nearest-model assignment round on toy brightness values.
total = 100 * 100unknown = 60 * 60sure_bg = total - unknownprint(total, unknown, sure_bg)# -> 10000 3600 6400
fg_model, bg_model = 190, 30unknowns = [175, 60, 200, 25]labels = ["FG" if abs(p - fg_model) < abs(p - bg_model) else "BG" for p in unknowns]print(labels)# -> ['FG', 'BG', 'FG', 'BG']
settled_fg, settled_bg = 2100, 3600 - 2100print(settled_fg, settled_bg)# -> 2100 1500The box manufactures sure-background seeds from nothing, the toy round sorts and to foreground with and to background, and the fixture settles at foreground against reclaimed background.
Watch Out For
Camouflage bleeding across the border
The cut leaks into background regions whose colors match the subject, like a green shirt against foliage, and more iterations make it worse as the mixtures absorb the leak. Color is the only evidence GrabCut learns. Fix it with corrective strokes (sure-foreground inside the subject, sure-background in the leak) and rerun in mask-correction mode.
Losing hair, wires, and thin straps
Fine structures vanish because the smoothness term charges per boundary pixel and thin shapes are nearly all boundary. The energy genuinely prices them out. Fix it by over-protecting thin parts with foreground strokes, or accept the limit and finish sub-pixel strands with border matting or manual alpha work.
The Quick Version
- GrabCut needs one bounding box: outside is sure background ( pixels in the fixture), inside starts unknown ().
- Five-component Gaussian mixtures learn both sides' colors, then graph cut re-labels with those models as evidence.
- Assign, refit, and cut iterate about five times until the border stops moving, settling near foreground here.
- Border matting finishes the edge with partial alpha for soft transitions like hair.
- Camouflaged colors and hair-thin structures are the two classic failures; corrective strokes fix the first.