Nodes/ComfyUI-DAELab-Custom-Nodes-Library/Badge Render-Space Mask Align V1 (DAELab)
ComfyUI Node

Badge Render-Space Mask Align V1 (DAELab)

Your flat badge mask no longer fits the render? This node re-registers it

By TeutonicMepheles·Created 4 months ago·Updated 3 days ago· 0
Badge Render-Space Mask Align V1 (DAELab)
  • source_image
  • target_image
  • source_foreground_mask
  • source_region_mask
  • target_foreground_mask
  • aligned_region_mask
  • aligned_source_image
  • alignment_overlay
  • confidence_preview
  • report
  • color_region_map
  • palette_report
  • source_color_region_map
◄extraction_mode▾►
◄background_threshold24►
◄max_rotation8.0►
◄max_scale_delta0.20►
◄snap_radius6►
◄region_inset_px0►
◄minimum_iou0.70►
◄color_region_count12►
◄color_smoothing_px2►
◄snap_region_boundariestrue►
◄split_disconnected_regionsfalse►
◄minimum_color_region_pixels256►
◄region_boundary_tolerance_px3►
◄maximum_color_regions32►
◄id_map_basis▾►

The badge workflow this pack is built around has an awkward middle step. You start with flat artwork - clean color blocks, crisp edges, a mask for the badge foreground, another mask for the region you want to touch. Then GPT Image 2 renders it, and the render is nearly your design: same silhouette, slightly rotated, slightly scaled, colors drifted a few points, edges now soft and lit. Your masks are suddenly wrong by twenty pixels and nothing downstream trusts them.

Badge Render-Space Mask Align V1 is the node that fixes that. It takes your flat-artwork masks and registers them into the render's coordinate space, then hands you an ID map in the render's own colors so you can select regions on the photo, not on the design file.

What it actually does

target_image is the coordinate authority - the generated badge. source_image plus its two masks are the things being moved. The registration is a deterministic similarity search: translation, uniform scale and rotation, bounded by max_rotation (default 8°) and max_scale_delta (default ±20%). No flow field, no segmentation model, no API call. The node's own README is refreshingly honest that this version exists to test whether a global similarity transform is enough - if alignment_overlay still shows warping, the answer is no and you should supply a real segmentation mask.

Foreground extraction is the other half. In auto_border mode it samples the border color of the render and keeps pixels further than background_threshold (default 24, RGB distance) from it, then refines the result so it can't drift more than snap_radius pixels off the aligned source silhouette. source_prior_only skips that and just propagates your existing foreground - useful for checking whether the transform is right before you trust the extraction. If the auto extraction grabs under 1% or over 90% of the frame, it silently falls back to the source prior, which is normally what you want.

Then comes the part beginners miss. The node clusters the image globally in Lab space into color_region_count categories (default 12), smooths noise with color_smoothing_px, and optionally snaps region borders to the render's own edges (snap_region_boundaries). That's what makes color_region_map usable as an ID map - flat, high-contrast, one exact color per semantic region, black background. Feed it to a Color To Mask style node and you can pick "the enamel ring" precisely instead of guessing hex codes.

id_map_basis decides whose colors win: target_appearance clusters what the render actually looks like (two regions that came out the same color share one ID), source_palette propagates your flat-artwork categories instead and uses split_disconnected_regions / minimum_color_region_pixels to keep separated blocks apart. Default target_appearance is right for "edit what I see"; switch to source palette when you care that region 3 is still region 3.

The dials that matter

Most of the twenty-odd inputs are QA knobs. Set these four:

  • extraction_mode - auto_border for a plain or light backdrop, source_prior_only for anything photographic.
  • background_threshold - push it up (40–60) if the render's backdrop has a gradient or shadow.
  • minimum_iou (0.70) - the pass/fail line for the QA report. Below it, report will tell you the alignment isn't valid.
  • region_inset_px - shrink the propagated region a couple of pixels so a paid local edit can't spill over the boundary.

Outputs and where they go

target_foreground_mask and aligned_region_mask are the masks the rest of the pipeline expects. color_region_map is the ID map; source_color_region_map is the same map in source coordinates so you can compare them by eye. alignment_overlay draws the region in red, the old silhouette edge in cyan and the new one in green - green-on-cyan is what you want. confidence_preview, report and palette_report (exact hex per region, plus the matching source color) are diagnostics. aligned_source_image is just your design warped with the same matrix.

If report says the alignment is invalid, don't queue a paid GPT edit against those masks. Fix the threshold or the search bounds first. That advice is straight out of the pack's own documentation and it's the difference between a wasted credit and a usable result.

Install

cd ComfyUI/custom_nodes
git clone https://github.com/TeutonicMepheles/ComfyUI-DAELab-Custom-Nodes-Library.git

Restart ComfyUI, then hard-refresh the browser - the pack ships a web/ directory of front-end panels and thumbnails that won't appear until it reloads. Clone the whole repo; copying just nodes/badge_render_space_mask/ gets you a node that never registers.

Gotchas

source_foreground_mask must contain something. An all-black mask raises outright rather than quietly producing an empty result.

The module imports OpenCV at the top, and OpenCV is not in the pack's requirements.txt (that file only declares python-docx, openpyxl, pdfplumber for document import). On a stock ComfyUI install it's already there; on a trimmed environment you'll get No module named 'cv2' and the fix is pip install opencv-python-headless inside ComfyUI's venv.

And keep expectations calibrated: this transfers geometry and colour categories. It cannot recover structure that the generation already deleted or redesigned. If a render dropped a ring detail, no amount of mask alignment brings it back - re-render, or paint the region by hand with the pack's selection-mask node.

CategoryDAELab/Badge/Mask

Inputs (19)

NameTypeDefaultDescription
source_imageIMAGE—
target_imageIMAGE—
source_foreground_maskMASK—
source_region_maskMASK—
extraction_modeCOMBO2 options: auto_border, source_prior_only
background_thresholdFLOAT241–128—
max_rotationFLOAT8.00–30—
max_scale_deltaFLOAT0.200–0.5—
snap_radiusINT60–32—
region_inset_pxINT00–16—
minimum_iouFLOAT0.700–1—
color_region_countINT122–32—
color_smoothing_pxINT20–8—
snap_region_boundariesBOOLEANtrue—
split_disconnected_regionsBOOLEANfalse—
minimum_color_region_pixelsINT2561–65536—
region_boundary_tolerance_pxINT30–32—
maximum_color_regionsINT322–32—
id_map_basisCOMBO2 options: target_appearance, source_palette

Outputs (9)

NameTypeDescription
target_foreground_maskMASK—
aligned_region_maskMASK—
aligned_source_imageIMAGE—
alignment_overlayIMAGE—
confidence_previewIMAGE—
reportSTRING—
color_region_mapIMAGE—
palette_reportSTRING—
source_color_region_mapIMAGE—