ComfyUI Node

Mask Refine

Cleans up a mask in one fixed-order pass: expand (grow/shrink by whole pixels), fill enclosed holes (all of them, or only those up to max_hole_size), smooth (melts staircase jaggies without feathering), feather with a gaussian blur, edge-refine against the optional guide_image (guided filter or alpha matting), then remap levels with black_point / white_point to clear gray haze. Returns the refined mask and its inverse.

By live1617·Created 19 days ago·Updated a day ago· 3
Mask Refine
  • mask
  • guide_image
  • mask
  • mask_inverted
◄expand0►
◄blur0.0►
◄fill_holesfalse►
◄smooth0►
◄black_point0.00►
◄white_point1.00►
◄edge_refineoff►
◄previewtrue►
◄max_hole_size0.0►
CategoryQG-tools/Mask

Inputs (11)

NameTypeDefaultDescription
maskMASKBHW mask; white is the selected area.
expandINT0-1024–1024Pixels to grow (+) or shrink (-) the mask.
blurFLOAT0.00–100Gaussian feather strength in pixels; 0 keeps hard edges.
fill_holesBOOLEANfalseFill fully enclosed gaps inside the mask before feathering. Gaps that touch the frame edge never count as holes.
smoothINT00–256Melts staircase jaggies by this many pixels while keeping a hard edge; 0 is off. Runs before the feather blur.
black_pointFLOAT0.000–1Mask values at or below this become fully black; clears faint gray haze outside the subject. Applied last.
white_pointFLOAT1.000–1Mask values at or above this become fully white; solidifies the mask core. Applied last.
edge_refineCOMBOoffSnap the mask edge to the guide_image. 'guided filter' is fast edge-aware filtering (needs opencv-contrib-python); 'matting' solves closed-form alpha matting around the edge (needs pymatting). Both require the guide_image input.
previewoptBOOLEANtrueShow the refined mask on the node face. Off skips writing the preview file to ComfyUI's temp folder.
guide_imageoptIMAGERGB frames the mask belongs to; required when edge_refine is 'guided filter' or 'matting'.
max_hole_sizeoptFLOAT0.00–100The biggest hole fill_holes fills, in percent of the picture. 2 fills the gaps a quick brush leaves but keeps a person or pet you left unpainted; 0 fills every hole.

Outputs (2)

NameTypeDescription
maskMASKThe refined BHW mask.
mask_invertedMASKThe refined mask inverted, for operations that target the background.