Nodes/Region Edit Toolkit/图像分区 · 扫描窗口遮罩合并
ComfyUI Node

图像分区 · 扫描窗口遮罩合并

SAM3 masks from overlapping scan windows, merged by vote

By Liu-Bot24·Created 2 months ago·Updated about a month ago· 1
图像分区 · 扫描窗口遮罩合并
  • original_image
  • window_masks
  • merged_mask
  • report_json
x
y
width
height
detection_threshold0.50
minimum_votes2
allow_single_coveragefalse

Big image, many small targets, and a model that can only chew a crop at a time. That's the problem this node exists for. When you can't run SAM3 over a whole image in one pass, you scan it in overlapping windows, mask each window, and then you're stuck with a pile of local masks that need to become one full-resolution mask. RegionEditWindowMaskMerge is the reassembly step, and it does something smarter than a naive stitch: it votes.

The idea is simple and it's the whole point of using overlapping windows. A real object in the image appears in several windows; a false positive usually appears in one. So the node projects every window mask back onto full-image coordinates and counts, per pixel, how many windows agreed it was a detection. A pixel only makes it into the merged mask if it collects at least minimum_votes (default 2). One-window hallucinations get voted out instead of pasted in. If you've watched SAM3 occasionally "find" something that isn't there, you can see why this is the node you'd reach for instead of a plain union.

How it works

You feed it the full original_image plus the list of window_masks and the parallel x/y/width/height lists from wherever your scan came from - in the Region Edit Toolkit flow, that's RegionEditImageGridWindows, which emits exactly these parallel lists. Each local mask is placed at its coordinates, every pixel gets a coverage count and a vote count (where a pixel counts as "detected" if its value ≥ detection_threshold, default 0.5), and the merge keeps only votes >= minimum_votes.

Two gotchas are baked into the code and worth knowing before they bite:

  • Your scan must tile the whole image. If any pixel has zero coverage, it raises "SAM scan windows do not cover every input-image pixel". Gaps between windows are an error, not a mild imperfection. Use enough overlap.
  • Edge regions need special handling. allow_single_coverage (default off) relaxes the vote requirement to min(minimum_votes, coverage) for pixels only one window covers - otherwise a legit detection near the edge of the scan grid gets zeroed just because no second window overlapped it.

The inputs that matter

  • window_masks - a list of masks (this node takes lists, unlike most). Length must match the coordinate lists exactly.
  • detection_threshold (0.5) - the pixel value that counts as a "detection." Bump it if SAM3's low-confidence blobs are leaking through.
  • minimum_votes (2) - how many windows must agree. Raise it to kill more false positives; lower it if your overlap is thin.
  • allow_single_coverage (off) - the edge-region escape hatch above.

Outputs are just two: merged_mask, the full-resolution result to feed your tile pipeline, and report_json, which tells you window_count, rejected_detected_pixels (how many votes got thrown out - the stat that tells you the voting is actually working), and min/max window coverage. Wire report_json to a text preview once while you're tuning, then forget it.

Installing it

Part of the Region Edit Toolkit pack - search Region Edit Toolkit in ComfyUI Manager, or comfy node install native-region-tile-planner-merge. Via git:

cd ComfyUI\custom_nodes
git clone https://github.com/Liu-Bot24/ComfyUI-Region-Edit-Toolkit.git
cd ComfyUI-Region-Edit-Toolkit
& "<your ComfyUI python>" -m pip install -r requirements.txt

Python 3.10+, deps are numpy/Pillow/scipy plus argostranslate (only for the optional offline translation nodes). No models to download for this node itself; SAM3 lives elsewhere in the workflow.

When it bites you

If you hit the "windows do not cover every pixel" error, your grid has gaps - raise the window size or the overlap in RegionEditImageGridWindows. If the merged mask comes out patchy at the edges, that's the single-coverage problem: turn on allow_single_coverage. If spurious blobs persist, raise minimum_votes before you fight detection_threshold. There's essentially no community lore on this pack yet - it's young and mostly documented in Chinese - so the error strings in the code are the real docs. They're unusually clear ones. For a niche toolkit, this is the node I'd trust to assemble masks it didn't see all of.

CategoryRegion Edit Toolkit/Tile

Inputs (9)

NameTypeDefaultDescription
original_imageIMAGE
window_masksMASK
xINT
yINT
widthINT
heightINT
detection_thresholdFLOAT0.500–1
minimum_votesINT21–16
allow_single_coverageBOOLEANfalse

Outputs (2)

NameTypeDescription
merged_maskMASK
report_jsonSTRING