Nodes/ComfyUI-Utility-Suite/Combine Masks (SEGS & SEGS)
ComfyUI Node

Combine Masks (SEGS & SEGS)

Subtract One Detector's Masks From Another's, Inside SEGS

By tom-m-2020·Created about a month ago·Updated 7 days ago· 1
Combine Masks (SEGS & SEGS)
  • segs_destination
  • segs_source
  • segs
◄x0►
◄y0►
◄operation▾►

Detailing a picture usually means running more than one detector. Faces from one YOLO, hands from another, a person-segmentation model over the whole frame, and then some SAM instance to tidy the edges. Each of those hands you a SEGS, and each SEGS's cropped masks are a claim about which pixels belong to the region. The thing nobody tells you is that those claims overlap: the person mask contains the face, the face mask contains the eyes, and if you feed all of them into detail passes you process the same pixels two or three times. That gives you double-denoised patches, doubled render time, and the particular ugliness of a face that was improved twice.

This node is the SEGS-versus-SEGS version of the mask compositing idea: both the destination and the source come in as SEGS, and the operation happens on their cropped masks.

How it works

segs_destination supplies the structure. For each destination entry the node takes that entry's cropped mask as the canvas and the corresponding source entry's cropped mask as the stamp, places the stamp at x, y, applies the chosen operation, clamps to 0-1, and writes the result back into the destination entry. The destination's crop regions, bboxes, labels and confidences survive; only cropped_mask changes.

The same two behaviours as the MASK variant, and both are worth internalising:

  • x, y are crop-local. Zero means the top-left corner of the destination segment's own crop, not the image origin. In the move-the-stamp sense you are shifting one crop's mask relative to another crop's mask.
  • Sources pair by index, and the last one repeats. One source SEGS with three entries and a destination with five gives entries 0, 1, 2 their own masks and entries 3 and 4 a repeat of entry 2. The source is not resized or refitted - mismatched sizes are clipped at the destination's edge.

operation is multiply, add, subtract, and, or, xor. multiply, add and subtract work on continuous values, and/or/xor round to boolean first. Both inputs must be well-formed SEGS - a two-tuple whose entries have crop regions inside the declared canvas - or you get a type error naming the entry.

The recipe it exists for

Detect a person, detect a face, then subtract the face SEGS from the person SEGS and run your two detail passes on disjoint masks. Same trick for hands inside a person mask, or for peeling a background mask out of a foreground subject before an inpainting pass. The alternative in vanilla ComfyUI is exporting both sets to masks, compositing them on the canvas, converting back to SEGS - which works, and is exactly the kind of graph surgery that makes people give up on mask logic.

Union is just as useful going the other way: add or or two detectors' regions into the destination so a single DetailerForEach covers both, which keeps the number of sampling passes down when the two regions are adjacent and the same prompt applies to both.

Install

The node ships in ComfyUI-Utility-Suite. ComfyUI Manager → search ComfyUI-Utility-Suite → install → restart. Or:

cd ComfyUI/custom_nodes
git clone https://github.com/tom-m-2020/ComfyUI-Utility-Suite

Restart ComfyUI after cloning. It understands the SEGS structure but does not ship Impact Pack, so install that separately if you do not already have it - the SEGS has to come from somewhere. No model downloads, and the pack's only listed dependency is opencv-python-headless. Written against the newer V3 node API, so a recent ComfyUI is required.

Traps

Nothing aligns the two crops for you. Place the stamp at (0,0) and you are comparing two different parts of the image; you only get sensible results when the destination and source crops genuinely refer to the same region, or when the offset makes sense to you. This is not a registration tool.

add is saturating, and/or/xor are binarising. If you union feathered masks with add, the overlaps pile towards 1.0 and the transition zone gets clipped flat; if you use or you lose the feather entirely. Use add when you trust the ramps and grid-aligned hard masks otherwise.

The destination is authoritative. Because placement and clipping are relative to the destination crop, a source region bigger or smaller than the destination is not an error - it just gets cut. If your result looks like the mask simply stops at an invisible line, that line is your destination's crop rectangle.

CategoryUtility Suite/SEGS

Inputs (5)

NameTypeDefaultDescription
segs_destinationSEGS—
segs_sourceSEGS—
xINT00–16384—
yINT00–16384—
operationCOMBO6 options: multiply, add, subtract, and, or, xor

Outputs (1)

NameTypeDescription
segsSEGS—