Nodes/BrainDead Nodes/BD Mask Correlate
ComfyUI Node

BD Mask Correlate

SAM3 is precise but unruly — this node is the shepherd that keeps segments where they belong

By BizaNator·Created 8 months ago·Updated 3 days ago· 15
BD Mask Correlate
  • candidates
  • reference_image
  • silhouette_mask
  • target_1
  • target_2
  • target_3
  • target_4
  • target_5
  • target_6
  • target_7
  • target_8
  • refined_1
  • refined_2
  • refined_3
  • refined_4
  • refined_5
  • refined_6
  • refined_7
  • refined_8
  • debug_overlay
  • match_info
  • masked_image
  • combined_mask
masked_image_bgtransparent
invert_masked_imagefalse
combined_mask_invertfalse
combined_mask_exclude
labelsleft_brow right_brow left_eye right_eye lips
priorities
min_iou0.05
modeintersect
slot_modes
fallbackoriginal
target_expand0
exclusivefalse
max_target_fill0.95
subtract_slots
overlay_alpha0.55

BD Mask Correlate is the node that reconciles two very different kinds of masks. On one side you have precise candidates - SAM3 segments, pixel-accurate but unpredictable, one per prompt in a batch. On the other side you have coarse guides - MediaPipe face hulls, reliable location but rough shape. This node matches the precise candidates to the coarse targets by IoU, then combines each pair in a mode you choose. If you've ever watched a SAM3 "eyes" segment drift onto the cheek, this is the leash.

The workflow it serves is the pack's face-segmentation pipeline: BD MediaPipe Face Mask gives you tight per-feature hulls (eyes, brows, lips), SAM3 gives you pixel-accurate segments, and BD Mask Correlate is the middle layer that says "that SAM3 segment belongs inside this hull." For each wired target (up to 8), it finds the candidate with the highest overlap and merges them. Result: masks that have SAM3's pixel fidelity and MediaPipe's structural sanity.

The inputs that matter

  • candidates - the SAM3 batch (per_prompt_masks). One frame = one candidate segment.
  • target_1target_8 - your coarse guide masks, e.g. the left_eye, right_brow, lips outputs from BD MediaPipe Face Mask. Leave unwired to skip a slot.
  • mode - the default combine mode for all slots. intersect (default) clips the candidate to the target region - the safest choice for eyes/brows/lips so SAM3 stays inside the MediaPipe hull. replace uses the raw SAM3 segment (right for skin, where SAM3's boundary is the good part). union and weighted_blend exist for the other cases. You can override per-slot in slot_modes (one label: mode per line).
  • min_iou - acceptance threshold (default 0.05, very permissive). Raise to 0.2+ if candidates keep matching the wrong target. When nothing qualifies, fallback decides between returning the original target (original) or an empty mask (blank).
  • exclusive - when ON, each candidate can be claimed by only one target, assigned in priority order. When OFF, one SAM3 segment can match several targets - fine for non-overlapping features, wrong if segments overlap.
  • subtract_slots - post-match subtraction, the skin-pipeline trick. Lines like skin: eyes, brows, lips subtract the refined eye/brow/lip masks from the skin mask, cutting precise feature holes out of a broad skin region.
  • target_expand - dilate targets before matching. If MediaPipe's hull is tight and SAM3's segment sits just outside it, 4–10 px of expansion fixes the match without loosening the final mask.

There's also max_target_fill (default 0.95) which exists for a specific failure mode: when a SAM3 prompt isn't found, the multi-prompt node can return a full-white mask, and a full-white target scores high IoU against everything. This rejects targets covering more than 95% of the frame.

Outputs

You get refined_1refined_8 (one per slot, blank if unwired), a combined_mask (union of all matched slots, optionally clipped by a silhouette_mask), and masked_image - the reference image composited with the matched union as alpha. invert_masked_image flips it to show everything except the matched regions (e.g. the face with eyes and lips cut out), and combined_mask_invert does the same as a MASK. The debug_overlay is color-coded per slot - wire it to a preview and you'll see instantly which candidates landed where, which is usually faster than reading match_info.

Installing

Same as the whole pack - ComfyUI Manager search "BrainDead", or clone + pip install -r requirements.txt + restart. All V3 API.

The mental shortcut: intersect mode + subtract_slots is the recipe this pack actually uses for faces - SAM3 defines the skin boundary, MediaPipe keeps the features in bounds, subtraction carves the features out of the skin. Start there and only reach for the exotic modes when you have a specific failure.

Category🧠BrainDead/Segmentation

Inputs (26)

NameTypeDefaultDescription
candidatesMASKBatch of precise candidate masks (B, H, W) — e.g. SAM3 segment output. Each frame in the batch is a separate candidate segment.
reference_imageoptIMAGEOriginal character image. When wired: • Used as the base for the debug overlay • Produces masked_image (union of all matched slots applied as alpha) Leave unwired to composite the debug overlay on black.
silhouette_maskoptMASKHead or body silhouette (white=subject, black=background). When wired, all output masks are clipped to this boundary after matching. Useful for ensuring matched segments don't bleed outside the head/body shape.
masked_image_bgoptCOMBOtransparentBackground for the masked_image output when reference_image is wired. transparent → RGBA output, background is fully transparent white / black → RGB composite over solid colour checker → grey checkerboard (visually indicates transparency)
invert_masked_imageoptBOOLEANfalseWhen True, masked_image shows everything EXCEPT the matched regions — the inverse composite. If silhouette_mask is wired: image × (silhouette − union_of_matched) — shows the subject with matched areas cut out (e.g. remove eyes/lips from face). If silhouette_mask is not wired: image × (1 − union_of_matched). Useful for: baking a head mask that has feature holes, or showing the non-feature areas of the subject.
combined_mask_invertoptBOOLEANfalseWhen True: combined_mask = silhouette_mask − union (if wired) or 1 − union. Gives you the head-minus-features shape as a mask — same logic as invert_masked_image but as a MASK output for downstream use.
combined_mask_excludeoptSTRINGComma-separated labels or 1-based slot indices to EXCLUDE from the combined_mask output (and from the union used in masked_image). Example: 'skin' or '1' — skin slot is excluded so combined_mask = union of eyes + eyebrows + lips only, not the skin region. Useful when one slot is a large 'base' mask (skin, clothing) and you want combined_mask to represent just the smaller feature masks.
labelsoptSTRINGleft_brow right_brow left_eye right_eye lipsOne label per line, aligned with target_1..target_N slots. Used for the match_info status string and overlay legend.
prioritiesoptSTRINGComma-separated priority values for each slot, aligned with target_1..N. Higher value = matched first in exclusive mode (gets first pick of candidates). Example: '2,2,1,1,3' — slot 5 (lips) gets first pick, slots 1-2 (brows) second, slots 3-4 (eyes) last. Empty (default) = all slots equal priority, processed in slot order.
min_iouoptFLOAT0.050–1Minimum IoU for a candidate to be accepted as a match. 0.05 is permissive (any reasonable overlap counts). Raise to 0.2+ if candidates are bleeding into the wrong target regions.
modeoptCOMBOintersectDefault combine mode for all slots: intersect — candidate clipped to target region (safest; use for eyes/brows/lips so SAM3 segments stay within MediaPipe hull bounds). replace — raw SAM3 segment used directly (use for skin — lets SAM3 define the boundary, then subtract_slots removes features). union — expand target by candidate shape. weighted_blend — smooth blend biased toward confident candidate areas. Override per slot with slot_modes.
slot_modesoptSTRINGPer-slot mode overrides — one line each: label_or_index: mode Overrides the global mode setting for specific slots. All others use mode. Example: skin: replace left_eye: intersect Typical skin pipeline: skin: replace ← SAM3 defines the skin boundary (pixel-accurate) (all feature slots stay on intersect — clipped to MediaPipe hull) subtract_slots: skin: eyes, brows, lips ← then remove features Valid modes: intersect, replace, union, weighted_blend
fallbackoptCOMBOoriginalWhat to output for a target slot when no candidate meets min_iou: original — return the unmodified target mask. blank — return an empty mask (signals 'no confident match').
target_expandoptINT00–60Pixels to dilate each target mask BEFORE computing IoU. Useful when MediaPipe landmarks produce a tight hull that doesn't fully overlap the actual SAM3 segment. 4–10 px is usually enough.
exclusiveoptBOOLEANfalseWhen True each candidate can only be matched to ONE target (assigned in priority order, highest first). When False the same SAM3 segment can match multiple targets (safe for non-overlapping features like left/right brow).
max_target_filloptFLOAT0.950–1If a target mask covers MORE than this fraction of the image, treat it as invalid and apply the fallback instead of attempting to match. Why: BD_SAM3MultiPrompt with invert_negative=True returns a FULL WHITE mask when a prompted item is not found. A full-white target has high IoU with every candidate, causing false matches across the whole image. 0.95 (default) rejects masks covering >95% of pixels. Set to 1.0 to disable. Lower values (e.g. 0.7) also reject partial-failure masks — useful when SAM3 returns an overly broad segment for a missed prompt.
subtract_slotsoptSTRINGPost-matching subtraction rules. Each line: target: source1, source2, ... Tokens can be label names (from the labels field) or 1-based slot numbers. After all slots are matched, the listed source refined masks are subtracted from the target refined mask — useful for cutting precise eye/brow/lip regions out of the skin mask. Example: skin: eyes, brows, lips 1: 3, 4, 5 Subtraction clamps to 0 (no negative values). Applied in line order.
overlay_alphaoptFLOAT0.550–1Opacity of the colour overlay on the debug image. 0 = overlay invisible, 1 = solid colour. Unmatched/fallback slots show at 40% of this value in grey.
target_1optMASKTarget slot 1 — coarse guide mask (e.g. from BD_MediaPipeFaceMask). Leave unwired to skip this slot.
target_2optMASKTarget slot 2 — coarse guide mask (e.g. from BD_MediaPipeFaceMask). Leave unwired to skip this slot.
target_3optMASKTarget slot 3 — coarse guide mask (e.g. from BD_MediaPipeFaceMask). Leave unwired to skip this slot.
target_4optMASKTarget slot 4 — coarse guide mask (e.g. from BD_MediaPipeFaceMask). Leave unwired to skip this slot.
target_5optMASKTarget slot 5 — coarse guide mask (e.g. from BD_MediaPipeFaceMask). Leave unwired to skip this slot.
target_6optMASKTarget slot 6 — coarse guide mask (e.g. from BD_MediaPipeFaceMask). Leave unwired to skip this slot.
target_7optMASKTarget slot 7 — coarse guide mask (e.g. from BD_MediaPipeFaceMask). Leave unwired to skip this slot.
target_8optMASKTarget slot 8 — coarse guide mask (e.g. from BD_MediaPipeFaceMask). Leave unwired to skip this slot.

Outputs (12)

NameTypeDescription
refined_1MASKRefined mask for target slot 1. Blank (zeros) if slot was not wired.
refined_2MASKRefined mask for target slot 2. Blank (zeros) if slot was not wired.
refined_3MASKRefined mask for target slot 3. Blank (zeros) if slot was not wired.
refined_4MASKRefined mask for target slot 4. Blank (zeros) if slot was not wired.
refined_5MASKRefined mask for target slot 5. Blank (zeros) if slot was not wired.
refined_6MASKRefined mask for target slot 6. Blank (zeros) if slot was not wired.
refined_7MASKRefined mask for target slot 7. Blank (zeros) if slot was not wired.
refined_8MASKRefined mask for target slot 8. Blank (zeros) if slot was not wired.
debug_overlayIMAGEColour-coded debug image: each matched slot in a distinct colour, unmatched slots in dimmed grey. Wire to PreviewImage to inspect results.
match_infoSTRINGPer-slot match summary: label, best candidate index, IoU, mode used.
masked_imageIMAGEreference_image composited with the union of all matched slot masks as alpha. With invert_masked_image=True: shows everything EXCEPT the matched regions (clamped to silhouette_mask if wired). Requires reference_image to be wired. Background controlled by masked_image_bg.
combined_maskMASKUnion of all wired+matched refined slot masks as a single MASK. Clipped to silhouette_mask if wired. Use downstream to treat all matched regions as one shape.