GrabCut
Alternate colour-model estimation and graph cuts to separate foreground from background.
The same scene stays here while the working view changes.
The rectangle encloses the intended object. Pixels outside it start as definite background.
The two illustrative distributions summarize different appearances. Native GrabCut fits colour mixtures and combines their likelihoods with neighbourhood smoothness.
The illustrative cut retains the selected object and removes surrounding pixels inside the rectangle. User labels can correct mistakes before another iteration.
Optimize the cut using colour likelihoods and neighbourhood smoothness, then refine.
Illustrative example. The stages explain the method; they are not a live OpenCV execution.
Try it on an image
Experiment at pixel level
Initialize GrabCut with a rectangle. Everything outside is definite background; the inside is learned over several iterations.
The engine loads on your first run. Your images stay in this browser.
Scroll over either image to zoom at the pointer. Use the scrollbars to pan both views over the same relative area. Zoom is relative to the input; pixel coordinates belong to each image. Warps can change scene correspondence.
Pixel inspector RGBA · native values · matched scale · 9 × 9 output pixels
Select a pixel
Select a pixel
When to use it
Extract an object from a loose rectangle or a few foreground/background hints.
How it works
- 01Initialize definite and probable foreground/background labels.
- 02Fit colour mixtures to the current foreground and background.
- 03Optimize the cut using colour likelihoods and neighbourhood smoothness, then refine.
energy = colour fit + boundary smoothness
What to tune
mode chooses rectangle or mask initialization; iterCount controls refinement; mask labels are categories, not alpha values.
Where it breaks down
Similar foreground/background colours and fine translucent structures are difficult. Preserve the model matrices when continuing an existing solve.
TypeScript API
Open an entry for its exact overloads, parameter descriptions, result ownership and pinned upstream source.
All of these calls execute on the CPU. Native objects need explicit disposal. See matrices and ownership and build compatibility.
Related methods
Global thresholding
Divide pixels using one intensity threshold, or choose it automatically with Otsu or the triangle method.
Adaptive thresholding
Compute a different intensity threshold for each local neighbourhood.
Connected components
Give every connected foreground region a distinct integer label.
Contour tracing
Trace the boundary of each binary region as an ordered sequence of points.