ComfyUI Node Runs on cloud

Rect Grab Cut

Cut a subject out of a photo with one rectangle — no model, no prompt, just OpenCV

By bmad4ever·Created 3 years ago·Updated 9 months ago· 70
Rect Grab Cut
  • image
  • IMAGE
x15
y15
x25
y25
iterations25
output_formatRGB

Everybody reaches for a segmentation model to remove backgrounds these days, but sometimes you just want to cut this thing out of this photo and you don't care about a model. Rect Grab Cut is the classic computer-vision answer: you draw a rough rectangle around your subject, and the GrabCut algorithm iteratively figures out which pixels inside that box are the subject and which are background. No model weights, no prompt, no internet - just OpenCV doing a genuinely clever optimization.

It's the rectangle-only variant in this pack's GrabCut family (the "Framed Mask Grab Cut" variants take a full mask instead, which is more powerful but more work). For a beginner who wants a foreground cutout in ten seconds, this is the on-ramp.

How it works

GrabCut treats the rectangle as a strong prior: everything outside the box is assumed background, and everything inside is a mix of probable foreground and background. It then models the color statistics of both groups and iteratively reassigns pixels - each pass, pixels that look like the background's colors get pushed out, pixels that match the subject get pulled in. The iterations input (default 25) controls how many refinement passes happen. More iterations = better convergence on hard images, at the cost of time.

The inputs that matter

  • image - your source photo.
  • x1, y1, x2, y2 - the rectangle, in pixels from the top-left corner. This is the entire interaction, and the whole quality of the result depends on it.
  • iterations - default 25, up to 200.
  • output_format - RGB or GRAY for the resulting mask.

Output is a single IMAGE: white where the algorithm thinks the subject is, black for background. Wire it into a mask→inpaint setup, a matte for compositing, or just preview it.

Where people get burned

  • The box must be tight but not clipping. If your rectangle cuts through the subject's edge, GrabCut treats the clipped part as background and will happily eat your subject's arm. Give the subject a small margin, but keep background out.
  • The box can't be everything. If you box the whole image, there's no outside-background to calibrate against and the result degrades to garbage. The background outside the box is doing half the work.
  • It's statistical, not semantic. A subject whose colors blend into the background will fool it - this is where a segmentation model beats it. Keep it for high-contrast subjects.

A common fix when a cut is mid: bump iterations and re-run, and if the subject touches the box edge, loosen that edge. But if you find yourself fighting it repeatedly, the Framed Mask Grab Cut nodes (which accept a proper mask) are the upgrade path.

Installing it

cd ComfyUI/custom_nodes
git clone https://github.com/bmad4ever/comfyui_bmad_nodes

restart, or search comfyui_bmad_nodes in ComfyUI Manager. Purely OpenCV - no models, no downloads beyond the pack's requirements.

It won't compete with a good segmentation model on hard cases, and it shouldn't have to. For a clean subject on a busy-but-distinct background, it's faster than loading a model and never hallucinates a new background - there is no background, just the mask.

CategoryBmad/CV/GrabCut

Inputs (7)

NameTypeDefaultDescription
imageIMAGE
x1INT50–2000
y1INT50–2000
x2INT50–2000
y2INT50–2000
iterationsINT250–200
output_formatCOMBORGB2 options: RGB, GRAY

Outputs (1)

NameTypeDescription
IMAGEIMAGE