Skip to content

GrabCut

Algorithm atlasSegmentation

Alternate colour-model estimation and graph cuts to separate foreground from background.

Try it on your image ↓

VISUAL WALKTHROUGHSegmentation
REFERENCE INPUTObject inside a selection rectangle

The same scene stays here while the working view changes.

RESULTCut away probable background

The illustrative cut retains the selected object and removes surrounding pixels inside the rectangle. User labels can correct mistakes before another iteration.

STEP 03 / 03

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

YOUR IMAGE · REAL OPENCV

Experiment at pixel level

Open full lab ↗

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.

Input448 × 320
OutputWaiting for a result

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
Hover to inspect. Click to pin a pixel.
Input
Select a pixel

Output
Select a pixel

Sample models and licenses

When to use it

Extract an object from a loose rectangle or a few foreground/background hints.

How it works

  1. 01Initialize definite and probable foreground/background labels.
  2. 02Fit colour mixtures to the current foreground and background.
  3. 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.