Nodes/ComfyUI Essentials/πŸ”§ Mask From Segmentation
ComfyUI Node Runs on cloud

πŸ”§ Mask From Segmentation

Turning a flat-color segmentation map into usable masks

By cubiqΒ·Created 3 years agoΒ·Updated about a year agoΒ· 1,152
πŸ”§ Mask From Segmentation
  • image
  • MASK
β—„segments6β–Ί
β—„remove_isolated_pixels0β–Ί
β—„remove_small_masks0.00β–Ί
β—„fill_holesfalseβ–Ί

If you've got an image where distinct regions are already flat, distinct colors - the output of a semantic segmentation model, one of ControlNet's segmentation preprocessor maps, or a mask sheet you hand-painted in flat color fills - Mask From Segmentation pulls those regions apart into clean, usable masks instead of you eyeballing hex values and building masks by hand.

This is not a segmentation model itself. It doesn't look at a photo and figure out where the person or the car is. It assumes the segmenting already happened and someone (or something) handed it an image where "region" already means "block of one flat color."

How it works

The node clusters the image's colors into segments buckets (1–16, default 6) and turns each cluster into its own region. From there, two cleanup passes and one fill option shape the result: remove_isolated_pixels (0–32) strips small stray pixels that don't belong to a real region - the kind of noise antialiasing or compression leaves along a segmentation map's edges. remove_small_masks (0–1, a fraction of total image area) drops whole masks that are too tiny to be a genuine segment rather than noise. fill_holes (default false) patches interior gaps so each resulting mask is one solid blob instead of swiss cheese.

Inputs and outputs

Required: image, segments, remove_isolated_pixels, remove_small_masks, fill_holes. Output: MASK.

Installing it

Via ComfyUI Manager, search "ComfyUI Essentials". Manually:

cd ComfyUI/custom_nodes
git clone https://github.com/cubiq/ComfyUI_essentials

Restart. The pack has been in maintenance-only mode since April 2025.

Common issues & troubleshooting

Results look nothing like your source image's regions. Check what you're actually feeding it first - this only works cleanly on input that's already flat and segmented. Point it at a normal photo and segments will just cluster by dominant color, which produces something that looks plausible but isn't a real segmentation.

A region you wanted got merged into another one, or disappeared. segments is probably set lower than the actual number of distinct colors in your source map. Bump it up to match.

Small real regions vanish. remove_small_masks is likely too aggressive for your image. Start it at 0 and only raise it once you've confirmed you're fighting genuine noise, not real content.

Masks have unwanted holes in them. Turn on fill_holes - it's off by default, which surprises people expecting solid regions out of the box.

Categoryessentials/mask

Inputs (5)

NameTypeDefaultDescription
imageIMAGEβ€”
segmentsINT61–16β€”
remove_isolated_pixelsINT00–32β€”
remove_small_masksFLOAT0.000–1β€”
fill_holesBOOLEANfalseβ€”

Outputs (1)

NameTypeDescription
MASKMASKβ€”