Nodes/ComfyUI-BrainDead/BD Remove Background
ComfyUI Node

BD Remove Background

Self-contained background removal. SAM3 text-grounded segmentation → hole fill → optional pymatting edge refinement → RGBA output. Positive prompts select the subject; negative prompts are subtracted. Matting mode 'closed_form' uses pymatting for soft, hair-accurate edges.

By BizaNator·Created 7 months ago·Updated 16 days ago· 14
BD Remove Background
  • image
  • external_mask
  • rgba
  • mask
  • rgb_white_bg
  • rgb_black_bg
  • crop_box
  • sticker
promptssubject person foreground
matting_modeclosed_form
negative_promptsbackground
mask_modeconstrain
invert_external_masktrue
matting_erode8
matting_dilate8
fill_holes_radius4
edge_blur0.0
decontaminatetrue
edge_shrink0
edge_refinenone
vitmatte_modelsmall
sharpen0.00
bg_clean0.00
key_gapsfalse
key_tolerance0.10
key_max_area0.04
key_enclosedfalse
sticker_outline0
sticker_color#ffffff
crop_to_contenttrue
crop_padding16
output_size0
output_size_modenone
mask_threshold0.50
unload_modelfalse
Category🧠BrainDead/Segmentation

Inputs (29)

NameTypeDefaultDescription
imageIMAGE
promptsSTRINGsubject person foregroundOne SAM3 text prompt per line. Their masks are union-combined. Typical: 'person', 'character', 'product', 'foreground object'.
matting_modeCOMBOclosed_formEdge refinement after SAM3: none — use SAM3 mask directly (fast, hard edges) closed_form — pymatting closed-form alpha matting around the mask boundary for soft, hair-accurate transparency.
negative_promptsoptSTRINGbackgroundOne prompt per line. SAM3 runs each → unioned → subtracted from positive mask. Useful to punch out backgrounds that bleed into the positive mask. Defaults to 'background' since that reliably helps SAM3 drop the backdrop.
external_maskoptMASKOptional mask from any source (ATR parser, SAM3, MediaPipe, manual, etc.). Behaviour is controlled by mask_mode.
mask_modeoptCOMBOconstrainHow to use external_mask: constrain — intersect SAM3 result with external_mask (prevents bleed outside rough silhouette) subtract — subtract external_mask from SAM3 result (punch out an already-masked region) use_directly — skip SAM3 entirely, use external_mask as the alpha (prompts ignored). Useful for hole-fill + matting on a pre-made mask.
invert_external_maskoptBOOLEANtrueInvert external_mask before use. Default ON because ComfyUI's LoadImage MASK output is the INVERSE of the subject (subject=0, background=1) — so without inverting, constrain/subtract/use_directly act on the wrong region. Turn OFF if your mask already has subject=1 (e.g. straight from SAM3).
matting_erodeoptINT80–64Pixels to erode inward to define the definite-foreground trimap band. Larger = wider unknown band = more matting area.
matting_dilateoptINT80–64Pixels to dilate outward to define the definite-background trimap band.
fill_holes_radiusoptINT40–64Morphological closing radius (pixels) applied before matting to seal interior gaps from SAM3. 0 = skip.
edge_bluroptFLOAT0.00–16Gaussian blur radius applied to the final alpha for soft feathering. 0 = no blur. Use after 'none' matting for quick soft edges.
decontaminateoptBOOLEANtrueEstimate the true foreground colour (pymatting) to remove background colour spill at edges — fixes the faint white outline you see on the black-bg composite. Slight cost; runs on the cropped region.
edge_shrinkoptINT00–32Erode the alpha inward by N px to cut a thin fringe halo. Use 1–2 if a hard edge ring remains after decontaminate.
edge_refineoptCOMBOnoneEdge-aware SOFT matte refine, run on the BOUNDARY ROI only (confident interior/exterior are locked, so it refines the edge — it won't fade the whole image): guided — cv2 guided filter (fast, RGB-guided) vitmatte — VitMatte deep matting (cleanest on hair/soft edges; GPU; auto-downloads hustvl/vitmatte on first use).
vitmatte_modeloptCOMBOsmallVitMatte variant for edge_refine='vitmatte'. Auto-downloaded from HF.
sharpenoptFLOAT0.000–1Crisp the matte edge (smoothstep). 0 = leave soft, 1 = tight/clean cutout. Use to reduce a noisy/blurry boundary.
bg_cleanoptFLOAT0.000–0.5Zero any alpha below this value to kill faint background ghosting / noise (e.g. 0.05). 0 = off.
key_gapsoptBOOLEANfalsePunch out background-coloured ISLANDS left inside the mask — the bits of original background showing through gaps (between legs, fingers, handles). Samples the bg colour from the removed area and removes only small matching islands, so large same-coloured SUBJECT areas (white clothing) are kept.
key_toleranceoptFLOAT0.100.01–0.4key_gaps colour-match tolerance (LAB). Higher catches more (risks subject); lower is safer. ~0.08–0.15 for a clean white/solid background.
key_max_areaoptFLOAT0.040–0.5key_gaps only removes bg-coloured islands smaller than this fraction of the frame (protects large subject areas). ~0.03–0.06 for small gaps.
key_enclosedoptBOOLEANfalsekey_gaps: also remove ENCLOSED bg-coloured islands (e.g. between fingers in a fist) that aren't connected to the outer background. OFF by default so interior details NOT touching the background (white nametag, buttons) are kept. Open gaps (between legs, around an arm) are always removed.
sticker_outlineoptINT00–128Die-cut STICKER mode: add a coloured trim border of N px around the subject (0 = off). The `sticker` output is RGBA with the subject + this outline, transparent outside.
sticker_coloroptSTRING#ffffffTrim colour for the sticker outline (hex, e.g. #ffffff white, #f00078 pink).
crop_to_contentoptBOOLEANtrueCrop the output to the bounding box of the mask (plus padding). Removes excess transparent border.
crop_paddingoptINT160–512Extra pixels added around the crop bounding box.
output_sizeoptINT00–81920 = native resolution. >0 = target size applied via output_size_mode.
output_size_modeoptCOMBOnoneHow to apply output_size (the subject is cropped to the mask first when crop_to_content is on): none — ignore output_size pad_square — scale the crop so its LONGEST side = output_size (keeps aspect), then pad the shorter side → output_size × output_size. e.g. 1024 → fills 1024×1024 with bars top/bottom or left/right. resize_square — resize the crop straight to output_size × output_size (may distort aspect).
mask_thresholdoptFLOAT0.500–1SAM3 confidence threshold. Lower = keep more area. Higher = tighter mask.
unload_modeloptBOOLEANfalseUnload SAM3 from VRAM after this node finishes.

Outputs (6)

NameTypeDescription
rgbaIMAGERGBA image with alpha = subject mask.
maskMASKSubject alpha mask (H,W) [0,1].
rgb_white_bgIMAGESubject composited over pure white — good for 3D pipelines.
rgb_black_bgIMAGESubject composited over pure black.
crop_boxSTRING'x0,y0,x1,y1' bounding box of the cropped region in the original image.
stickerIMAGERGBA die-cut sticker: subject + coloured trim outline (sticker_outline/sticker_color), transparent outside. When sticker_outline=0 this is the plain RGBA subject.