Framed Mask Grab Cut
OpenCV's GrabCut With the Background Pointed Out For You
- image
- thresh
- IMAGE
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_FGDbecome probable foreground (GC_PR_FGD), ifthreshold_FGD > threshold_PR_FGD. - Pixels with
thresh >= threshold_FGDbecome sure foreground (GC_FGD), ifthreshold_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_FGDto exactly 0; it gets ignored. - Foreground only - set
threshold_FGDlower thanthreshold_PR_FGD. - Both - keep
threshold_FGDhigher thanthreshold_PR_FGD, and make sure yourthreshimage 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 viathresh. - Subject touches the frame. The frame is declared background - if your subject runs to the border, it'll be cut. Use
frame_optionto ignore that side. - All-black or all-white output. Thresholds are mis-set (see the three recipes above) or the
threshimage 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.
Inputs (8)
| Name | Type | Default | Description |
|---|---|---|---|
| image | IMAGE | — | |
| thresh | IMAGE | — | |
| iterations | INT | 250–200 | — |
| margin | INT | 21–100 | — |
| frame_option | COMBO | FULL_FRAME | 7 options: FULL_FRAME, IGNORE_BOTTOM, IGNORE_TOP, IGNORE_RIGHT, IGNORE_LEFT, IGNORE_HORIZONTAL, +1 |
| threshold_FGD | INT | 2500–255 | — |
| threshold_PR_FGD | INT | 1281–255 | — |
| output_format | COMBO | RGB | 2 options: RGB, GRAY |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| IMAGE | IMAGE | — |