Nodes/Comfyui_PDuse/PD:Select_Mask
ComfyUI Node

PD:Select_Mask

Select_Mask: keep only the biggest (or smallest) blob in your mask

By 7BEII·Created 2 years ago·Updated 15 days ago· 53
PD:Select_Mask
  • image
  • filtered_image
  • filtered_mask
  • filtered_mask_image
modemax

PD:Select_Mask (mask_edge_selector) looks at a mask, finds every separate "blob" in it, and keeps only one - the largest, or the smallest, your choice. It's a filter, not a mask generator. If your mask came out speckled with stray noise or a subject is broken into disconnected pieces, this node collapses the mess into a single clean region.

How it works

The implementation is textbook connected-component analysis. It takes the dark/transparent parts of the input (alpha == 0 for RGBA, or brightness == 0 for RGB) as the "foreground" regions, labels each connected component, and measures their areas. In max mode it keeps the biggest blob; in min mode the smallest. Everything else is zeroed out. The three outputs are the payoff:

  • filtered_image - the input pixels but with only the chosen component's region intact.
  • filtered_mask - just that component as a mask.
  • filtered_mask_image - the same mask visualized as an image, which saves you a Convert Mask to Image step when you want to actually look at it.

When you'd reach for it

Mostly cleanup. Detectors and auto-maskers are sloppy - a background-removal result or an edge mask often has little islands of noise that pollute downstream steps. max mode is the "keep the main subject, drop the confetti" button. min mode is rarer but genuinely useful in the reverse case: keep the small specks and drop the big region, which is the move when the big area is the background you're trying to exclude.

Inputs

Just two: image and mode (max or min, default max). That's the whole surface. No threshold knob, no min-area - if a component is connected to another by even one pixel, it counts as one blob, so a barely-touching artifact can merge two regions into one. Worth knowing before you blame the node.

Installing

Part of the 7BEII/Comfyui_PDuse pack. ComfyUI Manager → search "Comfyui_PDuse" → install, or:

cd ComfyUI/custom_nodes
git clone https://github.com/7BEII/Comfyui_PDuse
cd Comfyui_PDuse
pip install -r requirements.txt

Restart after. No models; dependencies are Pillow, numpy, scipy (for the labeling), opencv-python and friends.

Gotchas

The mask semantics are worth double-checking: this node treats dark = foreground, which is the inverse of ComfyUI's usual "white = keep" mask convention. Feed it a standard ComfyUI mask where white is the subject and max mode will happily keep the black background instead. If your output looks inverted, flip your mask before it enters. And remember it selects by total pixel area, not by bounding-box size or "importance" - a long thin line has more area than a small square and will win in max mode.

CategoryPDuse/Mask

Inputs (2)

NameTypeDefaultDescription
imageIMAGE
modeCOMBOmax2 options: max, min

Outputs (3)

NameTypeDescription
filtered_imageIMAGE
filtered_maskMASK
filtered_mask_imageIMAGE