Nodes/Transparency Background Remover/Auto GrabCut Background Remover
ComfyUI Node

Auto GrabCut Background Remover

YOLO finds the subject, GrabCut carves it out

By Limbicnation·Created about a year ago·Updated 4 months ago· 22
Auto GrabCut Background Remover
  • image
  • initial_mask
  • image
  • mask
  • bbox_coords
  • confidence
  • metrics
  • bbox_tensor
object_class
confidence_threshold0.50
grabcut_iterations5
margin_pixels20
edge_refinement0.7
edge_blur_amount0.0
bbox_safety_margin30
min_bbox_size64
fallback_margin_percent0.20
binary_threshold200
output_sizeORIGINAL
scaling_methodNEAREST
auto_adjustfalse
output_formatRGBA
invert_maskfalse
custom_width512
custom_height512
edge_detection_modeAUTO

This is the one node in the pack that actually runs a neural network, and it's the one that feels like magic on real photos. Instead of guessing what "background" means by color, Auto GrabCut first runs YOLOv8 object detection to find the subject (the yolov8n weights ship right in the repo), then hands the detected bounding box to OpenCV's GrabCut. GrabCut segments the object by iteratively modeling foreground and background color distributions, so the boundary snaps to actual object edges rather than a color flood-fill. A second pass refines those edges and optionally feathers them.

That two-stage design is the whole point. The flagship's color clustering has to guess; here YOLO already told the node where the person is, and GrabCut only has to separate person from surroundings inside that box. On a clean subject it beats the color approach, and it's the pack's answer for photos rather than sprites.

The input that matters most

object_class - auto, person, product, vehicle, animal, furniture, electronics. Honest caveat from the source: those presets map to single YOLO classes. "product" literally means "bottle," "animal" means "dog," "vehicle" means "car," "electronics" means "laptop." It's a pragmatic shortcut, not a taxonomy, so a photo of a couch under "furniture" (→ chair) can miss. auto just takes the highest-confidence detection of any class, which is the sensible default.

The rest of the knobs are about the detection and the cut: confidence_threshold (0.3-0.9) for how sure YOLO must be, grabcut_iterations (1-10), margin_pixels around the box, edge_refinement (0-1), and edge_blur_amount (0-10) for Gaussian-feathered mask edges - 0 skips blur entirely for sharp cuts. bbox_safety_margin and min_bbox_size police over-cropping; fallback_margin_percent is what happens when YOLO finds nothing (a margin-based box instead of a crash). output_size goes ORIGINAL, 512, 1024, 2048, or custom with custom_width/custom_height. output_format is RGBA or a plain MASK, invert_mask flips it, and there's an optional initial_mask to skip detection when you already have a rough mask.

Outputs - two useful, four for the curious

image and mask are the normal ones. The other four - bbox_coords (STRING), confidence (FLOAT), metrics (STRING), and bbox_tensor (a custom BBOX_TENSOR type) - are inspection streams. confidence is genuinely handy: it tells you how sure YOLO was, so a weak cut on a low-confidence frame is explainable. The custom tensor type won't plug into stock nodes, so you'll mostly ignore those four.

The catch

This node needs ultralytics, and if it's missing, the entire GrabCut section of the pack is disabled at load with a console warning. It's in requirements.txt, so a normal install gets it:

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

YOLO init is lazy and cached, so the first run pays a model-load tax and later ones don't. And since the neural path is really about subject location, not matting - same limits as every segmentation approach: hair and semi-transparent material still come out imperfectly. That's when you'd move up to a dedicated matting model.

Categoryimage/processing

Inputs (20)

NameTypeDefaultDescription
imageIMAGE
object_classCOMBO7 options: auto, person, product, vehicle, animal, furniture, +1
confidence_thresholdFLOAT0.500.3–0.9Minimum confidence for object detection
grabcut_iterationsINT51–10Number of GrabCut algorithm iterations
margin_pixelsINT200–50Pixel margin around detected object
edge_refinementFLOAT0.70–1Edge refinement strength (0=none, 1=maximum)
edge_blur_amountFLOAT0.00–10Amount of Gaussian blur to apply to mask edges (0=none, 10=maximum)
bbox_safety_marginINT300–100Extra pixels beyond detected bounding box for safety
min_bbox_sizeINT6432–256Minimum bounding box dimensions to prevent over-cropping
fallback_margin_percentFLOAT0.200.1–0.5Margin percentage for fallback bbox when no object detected
binary_thresholdINT200128–250Threshold for binary mask conversion
output_sizeCOMBOORIGINALTarget output size for the processed image and mask
scaling_methodCOMBONEARESTInterpolation method for scaling: NEAREST (pixel-perfect), BILINEAR (smooth), BICUBIC (high-quality), LANCZOS (best quality)
auto_adjustBOOLEANfalseAutomatically adjust parameters based on image content analysis
initial_maskoptMASK
output_formatoptCOMBORGBAOutput format: RGBA with alpha channel or binary MASK (0=background, 255=foreground)
invert_maskoptBOOLEANfalseInvert the output alpha/mask (swap foreground and background)
custom_widthoptINT51264–4096Custom width (used when output_size is 'custom')
custom_heightoptINT51264–4096Custom height (used when output_size is 'custom')
edge_detection_modeoptCOMBOAUTOEdge detection optimization: AUTO (detect content type), PIXEL_ART (sharp edges), PHOTOGRAPHIC (smooth edges)

Outputs (6)

NameTypeDescription
imageIMAGE
maskMASK
bbox_coordsSTRING
confidenceFLOAT
metricsSTRING
bbox_tensorBBOX_TENSOR