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

Framed Mask Grab Cut 2

GrabCut With Separate 'Maybe' and 'Sure' Foregrounds

By bmad4ever·Created 3 years ago·Updated 9 months ago· 70
Framed Mask Grab Cut 2
  • image
  • thresh_maybe
  • thresh_sure
  • IMAGE
iterations25
margin2
frame_optionFULL_FRAME
binary_threshold128
maybe_black_is_sure_backgroundfalse
output_formatRGB

Framed Mask Grab Cut 2 is the two-input version of the pack's GrabCut node. Where the original feeds one threshold image and two threshold values, this one takes two separate images - one that says "probably foreground," one that says "definitely foreground" - and lets OpenCV's GrabCut refine from there. It's the same framed-background trick, with more explicit control over the seeds.

Why you'd reach for it

GrabCut's quality lives or dies on its seed flags. Version 1 gets there by thresholding a single image two ways; version 2 lets you supply the two seed sources yourself, which is exactly the situation you're in when one mask is a confident detection and another is a fuzzy guess. Feed the fuzzy one to thresh_maybe and the confident one to thresh_sure, and GrabCut gets the layered hint it thrives on.

The real niche is when you have a two-tier signal available - say, a strict segmentation output (sure) and a loose bounding-region mask (maybe). That's a common pairing in real CV pipelines, and this node is built for it.

How it works

Both thresh_maybe and thresh_sure are thresholded against the same binary_threshold (default 128) - it's the safeguard for inputs that aren't clean 0/255 images. Pixels >= binary_threshold in thresh_maybe become probable foreground (GC_PR_FGD); pixels >= binary_threshold in thresh_sure become sure foreground (GC_FGD) on top of that. The frame - margin pixels on the border - is painted sure background (GC_BGD), with frame_option to skip sides (IGNORE_TOP/BOTTOM/LEFT/RIGHT/HORIZONTAL/VERTICAL).

Then there's the option the first version doesn't have: maybe_black_is_sure_background. Flip it on and pixels that are black in thresh_maybe become sure background rather than just probable - a strong claim that anything your loose mask leaves out is genuinely background. Default is off, because that claim isn't always true.

GrabCut runs iterations passes, and the output is a mask (background/probable-background → black, everything else → white) in RGB or GRAY per output_format.

The inputs that matter

  • thresh_maybe / thresh_sure - your two seed images, both thresholded at binary_threshold.
  • maybe_black_is_sure_background - the big new lever. On when your maybe-mask is a reliable enclosure; off when it's noisy.
  • iterations - refinement passes (default 25).
  • margin / frame_option - size and sides of the guaranteed-background frame.

Output is a single IMAGE mask.

Install

OpenCV-backed, so requirements matter:

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"; without OpenCV the CV nodes won't load.

Common issues

  • Both inputs look the same? Then you're not actually giving GrabCut a two-tier hint - use v1, which is simpler for single-source cases.
  • Background bleeding in. Turn on maybe_black_is_sure_background if your maybe-mask reliably excludes the background.
  • Subject touching the frame. Exclude that side with frame_option, since the frame is declared background.
  • Zero iterations? iterations can be 0, which means "seed mask only, no refinement" - occasionally useful for debugging, rarely what you want.

Version 2 is the more capable sibling, but only if you actually have two distinct seed sources. If you have one threshold image, the original Framed Mask Grab Cut is the right tool.

CategoryBmad/CV/GrabCut

Inputs (9)

NameTypeDefaultDescription
imageIMAGE
thresh_maybeIMAGE
thresh_sureIMAGE
iterationsINT250–200
marginINT21–100
frame_optionCOMBOFULL_FRAME7 options: FULL_FRAME, IGNORE_BOTTOM, IGNORE_TOP, IGNORE_RIGHT, IGNORE_LEFT, IGNORE_HORIZONTAL, +1
binary_thresholdINT1281–255
maybe_black_is_sure_backgroundBOOLEANfalse
output_formatCOMBORGB2 options: RGB, GRAY

Outputs (1)

NameTypeDescription
IMAGEIMAGE