PD_Mask Selector
Pick the Nth object out of a mask — the third sticker in the sheet
- image
- mask
- image
- mask
PD_Maskfenkai splits a whole sheet into every object; PD_Mask-Selector ("PD_Mask Selector") goes the other way - it finds the objects in a mask and hands you back one specific one. "The third sticker," "the largest face," "the leftmost logo": you pick an index, a sort order, and how much padding you want, and it returns that single object as a cropped image and mask. Where the splitter is for bulk extraction, this is for when you need exactly one region out of a busy mask and you know which one.
It's from the 7BEII/Comfyui_PDuse pack. Note the class has a space in it - PD_Mask Selector - which is a small taste of this pack's naming chaos.
How it works
It runs OpenCV's connected-components on the mask (8-connectivity, keeping only blobs above min_area), then orders the blobs. The sort_by dropdown gives you two orderings:
- left_to_right - by each blob's center x-coordinate, so "mask1" = the leftmost object, "mask2" = the second-leftmost, and so on.
- area_desc - by blob size, biggest first. "mask1" = the largest region.
Then select (mask1 / mask2 / mask3 / mask4) picks which sorted blob you get - index 0 through 3. If the sheet has fewer objects than you asked for, you get a blank output rather than a crash (all-black in full-canvas mode, a 1×1 blank in crop mode).
Two output modes for how the winner comes back:
- crop - the object cut out with
paddingpixels of margin, padded to a consistent batch size. - full_canvas - the whole original frame, with only that object's mask nonzero and the image masked to that region. This is the mode to use when position on the original canvas matters (compositing back, or feeding a positional node).
The inputs that matter
image+mask- the pair you're selecting from.select- which object (mask1…mask4).sort_by- the ordering that defines "first."min_area- ignore blobs smaller than this so dust doesn't eat your indices (default 100).padding- margin around the crop (default 10).output_mode- crop vs full_canvas, above.
Outputs: image and mask, the selected single object.
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. Requires opencv-python, in the pack requirements.
Gotchas
The ordering is what will bite you. "mask1" is only "first" in the sense of the sort you chose - change sort_by and mask1 becomes a different object, so if your workflow's "which one" logic changes, the index silently changes meaning. min_area matters more than it looks: if dust and specks sit in the mask, they count as blobs and steal your indices, so set it high enough to kill them (the pack's PD_MaskRemoveSmallObjects is the dedicated sweep if you need a pre-pass). And unlike the splitter, this node uses the normal white-on-black mask convention - no inversion surprise. It also returns a batch of one, so downstream list nodes are happy. For "grab region #2 out of this sheet and do something specific to it," this is the tool.
Inputs (7)
| Name | Type | Default | Description |
|---|---|---|---|
| image | IMAGE | — | |
| mask | MASK | — | |
| select | COMBO | 4 options: mask1, mask2, mask3, mask4 | |
| sort_by | COMBO | 2 options: left_to_right, area_desc | |
| min_area | INT | 1000–100000 | — |
| padding | INT | 100–200 | — |
| output_mode | COMBO | 2 options: crop, full_canvas |
Outputs (2)
| Name | Type | Description |
|---|---|---|
| image | IMAGE | — |
| mask | MASK | — |