Nodes/ComfyUI-WJNodes/mask select mask
ComfyUI Node

mask select mask

Pick the masks that overlap a region, drop the rest

By 807502278·Created 2 years ago·Updated 11 months ago· 20
mask select mask
  • mask
  • select_mask
  • mask
invert_selectfalse
invert_select_maskfalse
merge_maskfalse
Overlap_threshold0.001

Run an automatic segmentation model - SAM2's "segment everything" mode is the obvious example - and you don't get one mask back, you get dozens: every object, region, and blob the model found, all bundled as a batch. Most of the time you only care about the handful that actually overlap something specific - a region you drew, a bounding box you already have, one object you clicked on. This node is the filter: give it a batch of candidate masks and a reference mask, and it keeps only the ones that actually intersect.

It's part of ComfyUI-WJNodes, a small personal pack from an author known only by the GitHub handle 807502278 - the kind of tool that exists because someone needed it for their own SAM-heavy workflow and figured other people probably needed the same thing.

How it works

The core idea, straight from the pack's own description: intersection represents selection. Feed it a batch of candidate masks (mask) and a single reference mask (select_mask) - say, a rough region you drew, or a bounding box converted to a mask. Any mask in the batch that overlaps the reference beyond a set threshold gets kept; anything that doesn't overlap gets dropped. It's a practical way to go from "SAM segmented the whole image into forty pieces" to "give me just the two or three that are actually inside the area I care about."

Inputs and outputs that matter

  • mask - the batch of candidate masks you're filtering. This is typically the output of an auto-segmentation node like Sam2AutoSegmentation_data.
  • select_mask - the reference mask defining the region of interest. Whatever overlaps this is what gets kept.
  • invert_select - flip which side of the selection counts, if you actually want the masks that don't overlap.
  • invert_select_mask - inverts the reference mask itself before comparing, letting you flip what "the region of interest" even means without redrawing it.
  • merge_mask - instead of returning the matching masks as separate items, flatten everything that passed the overlap check into a single combined mask.
  • Overlap_threshold - how much overlap counts as a match, as a fraction from 0 to 1 (default 0.001 - a very low bar, meaning even a tiny sliver of overlap counts by default). Raise this if you only want masks that substantially overlap the reference, not just touch it.

The single output, mask, is the filtered (and optionally merged) result.

Why you'd reach for it

The natural pairing is right upstream of it in this same pack: Sam2AutoSegmentation_data dumps out every object SAM2 found in a frame, and this node is how you cut that down to the one or two objects you actually want to act on, without hand-picking indices out of a batch. It's also handy any time a batch of masks needs to be culled against a region - cleaning up noisy detection output, or isolating results within a specific area of a larger frame before the next processing step.

Installing it

ComfyUI Manager: search "ComfyUI-WJNodes." Manually:

cd ComfyUI/custom_nodes
git clone https://github.com/807502278/ComfyUI-WJNodes.git

Restart ComfyUI. No models or heavy dependencies for this node itself.

Where to be careful

The default Overlap_threshold of 0.001 is deliberately permissive - it'll keep a mask that barely grazes your reference region, not just ones that substantially overlap it. If you're getting more matches than expected, that threshold is the first thing to raise, not a sign something's broken.

The two invert toggles are easy to mix up because they invert different things: invert_select flips which side of the match you keep, invert_select_mask flips the reference mask itself before comparing. Getting them backwards produces a result that looks plausible but is actually the opposite of what you wanted, so if your output seems inverted from what you expected, check which one of the two you actually meant to toggle. There's no public discussion of this node anywhere to cross-check against - it's from a pack with essentially no community footprint - so testing on a small, visually obvious case before trusting it in a larger pipeline is the safest way to confirm you've got the toggles right.

CategoryWJNode/ImageEdit/MaskEdit

Inputs (6)

NameTypeDefaultDescription
maskMASK
select_maskMASK
invert_selectBOOLEANfalse
invert_select_maskBOOLEANfalse
merge_maskBOOLEANfalse
Overlap_thresholdFLOAT0.0010–1

Outputs (1)

NameTypeDescription
maskMASK