Nodes/ComfyUI-BrainDead/BD Mask Resolver
ComfyUI Node

BD Mask Resolver

Priority-based mask separation. Given a silhouette plus overlapping category masks (skin, clothes, accessories) and the original image, produces clean mutually-exclusive masks via priority + HSV color voting + neighbor-aware gap-fill. Python port of the Mask Resolver GLSL shader.

By BizaNator·Created 7 months ago·Updated 16 days ago· 14
BD Mask Resolver
  • silhouette
  • skin_mask
  • clothes_mask
  • original_image
  • accessories_mask
  • clean_skin
  • clean_clothes
  • clean_accessories
  • residual
  • debug_overlay
  • status
silhouette_threshold0.40
skin_threshold0.50
clothes_threshold0.50
accessories_threshold0.50
skin_priority1.0
clothes_priority2.0
accessories_priority3.0
edge_softness0.30
claim_threshold0.15
gap_fill_strength0.80
gap_neighbor_radius8
color_weight0.60
skin_color_modeadaptive_lab
adaptive_tolerance25
adaptive_sample_threshold0.70
adaptive_min_samples50
overlap_modepriority
output_debug_vizfalse
Category🧠BrainDead/Segmentation

Inputs (23)

NameTypeDefaultDescription
silhouetteMASKFull character outline mask (white = character body, black = background). Defines the universe of pixels to assign to a category. Anything outside silhouette becomes background in all outputs.
skin_maskMASKMask of pixels you believe are skin (e.g. from BD_SAM3MultiPrompt with positive skin prompts). Will be clamped to silhouette.
clothes_maskMASKMask of pixels you believe are clothing. Will be clamped to silhouette.
original_imageIMAGEOriginal character image. Sampled per-pixel for HSV-based skin-tone detection (see color_weight). Resized to silhouette resolution if mismatched.
accessories_maskoptMASKOptional mask for hard accessories (gloves, boots, belts, weapons). Defaults to all-zero if not provided. Highest default priority — wins overlaps with skin/clothes.
silhouette_thresholdoptFLOAT0.400–1Noise floor on the silhouette mask. Pixels in silhouette ABOVE this value count as character body, below count as background. Higher = stricter silhouette interpretation. NOT include/exclude — it's a noise filter.
skin_thresholdoptFLOAT0.500–1Noise floor on the skin_mask input. Pixels above count as 'skin candidate', below are zeroed. Higher = only confidently-detected skin counts. Lower = pick up faint partial detections (and noise).
clothes_thresholdoptFLOAT0.500–1Noise floor on the clothes_mask input. Same semantics as skin_threshold.
accessories_thresholdoptFLOAT0.500–1Noise floor on the accessories_mask input. Same semantics as skin_threshold.
skin_priorityoptFLOAT1.00.1–10Priority weight for skin when multiple masks claim a pixel. Default ranking: accessories(3) > clothes(2) > skin(1) — meaning a pixel claimed by both clothes and skin goes to clothes. Raise skin_priority above 2.0 to flip it.
clothes_priorityoptFLOAT2.00.1–10Priority weight for clothes. See skin_priority for the ranking convention.
accessories_priorityoptFLOAT3.00.1–10Priority weight for accessories. See skin_priority for the ranking convention.
edge_softnessoptFLOAT0.300–1Smoothstep width around each input threshold. 0 = hard binary thresholds (sharp edges), higher = anti-aliased gradient at mask boundaries.
claim_thresholdoptFLOAT0.150–2A pixel is 'claimed' by the winning category only if the winning score exceeds this. Pixels with weak/uncertain scores below claim_threshold become residual and are eligible for gap_fill. Raise this (e.g. 0.5+) to make residual generous (more gaps to fill); lower it to make claim aggressive (almost no residual). Default 0.15 = mild claim.
gap_fill_strengthoptFLOAT0.800–1How aggressively to assign residual pixels via neighbor voting. 0 = leave residual untouched, 1 = fully assign residual to the strongest neighbor category. Only has an effect when residual is non-empty (controlled by claim_threshold).
gap_neighbor_radiusoptINT81–64Radius (in pixels) of the neighborhood used to vote on residual pixels. Higher = looks further to find dominant category. Default 8 covers small gaps; raise to 16-32 for character art at high res.
color_weightoptFLOAT0.600–1How much skin-tone detection influences scoring. 0 = ignore image color, only use input masks + priorities. 1 = strong color influence (skin pixels boosted toward skin, non-skin pixels boosted toward clothes).
skin_color_modeoptCOMBOadaptive_labHow to detect skin-toned pixels in the original image: adaptive_lab — sample reference skin color from confident skin_mask pixels, compute LAB ΔE distance per pixel. Self-tunes per character (works on pale white through dark African through reddish Navajo skin). fixed_hsv — hard-coded HSV ranges from the original GLSL shader. Fails on very pale/dark skin. Fallback for when adaptive can't sample enough pixels. both — multiply the two likelihoods. Strictest mode.
adaptive_toleranceoptFLOAT252–100LAB ΔE distance at which adaptive likelihood = 0.5. Smaller = stricter (only pixels very close to sampled skin tone count). Larger = more permissive. ΔE 2.3 = just-noticeable difference, 25 = clearly different but related (typical skin variation), 50+ = very loose.
adaptive_sample_thresholdoptFLOAT0.700–1Only skin_mask pixels above this value are used as reference samples for adaptive mode. Higher = use only the most confident skin pixels (better reference, fewer samples). Lower = use more pixels including soft edges (more samples, may include non-skin).
adaptive_min_samplesoptINT5010–10000Minimum number of confident skin pixels needed for adaptive mode to activate. If fewer, adaptive returns zeros and falls back to fixed_hsv (in 'both' mode) or off.
overlap_modeoptCOMBOprioritypriority = winner takes all (each pixel goes to exactly ONE category). soft_blend = distribute proportionally by score (each pixel split fractionally; outputs may sum to >100% in overlap zones).
output_debug_vizoptBOOLEANfalseIf True, debug_overlay shows color-coded overlap map: green=skin, blue=clothes, red=access, yellow=skin+clothes, magenta=skin+access, cyan=clothes+access, white=triple, gray=residual gap. Tinted by skin-tone confidence. If False, debug_overlay just shows the residual mask as RGB grayscale.

Outputs (6)

NameTypeDescription
clean_skinMASK
clean_clothesMASK
clean_accessoriesMASK
residualMASK
debug_overlayIMAGE
statusSTRING