Nodes/comfyui_bmad_nodes/Framed Mask Grab Cut
ComfyUI Node Runs on cloud

Framed Mask Grab Cut

OpenCV's GrabCut With the Background Pointed Out For You

By bmad4ever·Created 3 years ago·Updated 9 months ago· 70
Framed Mask Grab Cut
  • image
  • thresh
  • IMAGE
iterations25
margin2
frame_optionFULL_FRAME
threshold_FGD250
threshold_PR_FGD128
output_formatRGB

Framed Mask Grab Cut wraps OpenCV's GrabCut algorithm in a node and, crucially, supplies the one thing GrabCut refuses to guess: where the background actually is. You give it an image and a rough threshold mask, it treats the border of the frame as guaranteed background, and it returns a refined foreground/background mask.

Why you'd reach for it

GrabCut is a classic interactive segmentation algorithm: it builds a statistical model of foreground vs background and refines it over iterations, but it needs a hint to start. The classic UI asks you to draw a rectangle around the subject. This node's twist is the "framed" part - the border margins of the image are declared sure background automatically, so if your subject sits well inside the frame, the algorithm has all the seed information it needs. This is the workhorse for cutting a subject out of a photo when you want algorithmic precision instead of a hand-painted mask.

It belongs to the manual, art-directed end of the mask spectrum - the same toolbox as the pack's FadeMaskEdges and contour nodes. If automatic background removal (BiRefNet, RMBG, SAM) already nails your image, you don't need this; when it doesn't, GrabCut's model-based refinement is a genuinely different tool.

How it works

The thresh input should be a grayscale image - often a thresholded mask from elsewhere in your graph, but not necessarily (the thresholds below treat it as intensity). It's used to paint "probable foreground" and "sure foreground" flags:

  • Pixels with thresh >= threshold_PR_FGD become probable foreground (GC_PR_FGD), if threshold_FGD > threshold_PR_FGD.
  • Pixels with thresh >= threshold_FGD become sure foreground (GC_FGD), if threshold_FGD > 0.

Meanwhile the frame - margin pixels deep on the borders - is painted sure background (GC_BGD), with frame_option letting you skip any side (IGNORE_TOP/BOTTOM/LEFT/RIGHT/HORIZONTAL/VERTICAL). Then OpenCV runs GrabCut in GC_INIT_WITH_MASK mode for iterations passes and out comes a mask where background and probable-background pixels go black, everything else white. output_format chooses RGB or GRAY.

Getting the thresholds right

The README gives you the three recipes and they're worth quoting:

  • Probable foreground only - set threshold_FGD to exactly 0; it gets ignored.
  • Foreground only - set threshold_FGD lower than threshold_PR_FGD.
  • Both - keep threshold_FGD higher than threshold_PR_FGD, and make sure your thresh image actually contains values in the intended range.

The threshold inputs double as a safeguard: if your "binary" mask secretly contains values other than 0 and 255, they won't silently mis-seed the algorithm.

Install

This node needs OpenCV, so install the pack with requirements:

cd ComfyUI/custom_nodes
git clone https://github.com/bmad4ever/comfyui_bmad_nodes
pip install -r requirements.txt

then restart ComfyUI. Manager users search "comfyui_bmad_nodes". If CV nodes show "not loaded" in the startup log, the requirements are missing.

Common issues

  • Output isn't a clean mask. Raise iterations (default 25, up to 200) or give GrabCut better seeds via thresh.
  • Subject touches the frame. The frame is declared background - if your subject runs to the border, it'll be cut. Use frame_option to ignore that side.
  • All-black or all-white output. Thresholds are mis-set (see the three recipes above) or the thresh image has no useful intensity range.

It's the most "real computer vision" node in the pack, and once the threshold logic clicks it's remarkably reliable for cutouts.

CategoryBmad/CV/GrabCut

Inputs (8)

NameTypeDefaultDescription
imageIMAGE
threshIMAGE
iterationsINT250–200
marginINT21–100
frame_optionCOMBOFULL_FRAME7 options: FULL_FRAME, IGNORE_BOTTOM, IGNORE_TOP, IGNORE_RIGHT, IGNORE_LEFT, IGNORE_HORIZONTAL, +1
threshold_FGDINT2500–255
threshold_PR_FGDINT1281–255
output_formatCOMBORGB2 options: RGB, GRAY

Outputs (1)

NameTypeDescription
IMAGEIMAGE