Nodes/Comfyui_PDuse/PD:MASK SELECTION
ComfyUI Node

PD:MASK SELECTION

Two masks in, the smaller one wins — an A/B picker for regions

By 7BEII·Created 2 years ago·Updated 15 days ago· 53
PD:MASK SELECTION
  • mask1
  • image1
  • mask2
  • image2
  • Selected Mask
  • Selected Image

Sometimes you have two candidate masks for the same job - a coarse auto-mask and a fine one, or a loose detector box and a tight segmenter - and you want the safer one. PD_MASK_SELECTION ("PD:MASK SELECTION") picks the smaller: it takes two mask+image pairs, measures the non-zero area of each mask, and returns the pair whose mask has less area. Smaller area means the more conservative, more constrained region - usually the one that won't over-reach and paint outside the subject.

It's from the 7BEII/Comfyui_PDuse pack, and it's the A/B-selector flavor of mask routing (the pack's PD_Mask-Selector is the different job of picking one region out of a single mask by index; this one compares two whole masks).

How it works

Dead simple under the hood: convert each mask to pixels, count the non-zero cells. Then a small decision tree:

  • Both masks empty → return the first pair unchanged.
  • One empty, one not → return the non-empty pair (you get something instead of nothing).
  • Both have content → return the pair with the smaller area.

The image travels with the mask - you feed mask1/image1 and mask2/image2, and the selected pair comes out as Selected Mask and Selected Image, guaranteed to match. That's the point: the image that goes downstream is the one belonging to the mask you chose, not a mismatched grab.

The inputs and outputs

  • mask1, image1, mask2, image2 - all required. Two complete pairs.
  • Selected Mask (MASK) and Selected Image (IMAGE) - whichever pair won.

No knobs, no thresholds, no optional inputs. It's the simplest node in the pack's mask family, and that's a feature: there's nothing to misconfigure.

Installing

ComfyUI Manager, search Comfyui_PDuse; or:

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

Restart. No models, pure tensor math.

Gotchas

The selection rule is "smaller wins," which is right for avoiding over-reach but wrong if your smaller mask is the bad one (a tiny speck compared to a real region - though in that case the area difference isn't what you want to optimize anyway). It measures raw non-zero pixels, not relative coverage, so comparing masks of different input sizes can surprise you - if image1 is 512 and image2 is 1024, a proportionally identical mask has four times the pixels in the big one and loses. Feed it same-resolution pairs and the behavior is exactly what the name promises. If you need a selectable A/B switch instead of an automatic one, this isn't it - this node decides for you, by area, every time.

CategoryPDuse/Mask

Inputs (4)

NameTypeDefaultDescription
mask1MASK
image1IMAGE
mask2MASK
image2IMAGE

Outputs (2)

NameTypeDescription
Selected MaskMASK
Selected ImageIMAGE