Nodes/ComfyUI_KimNodes/🍒Mask_Noise_Cleaner🧹蒙版噪点清理器
ComfyUI Node

🍒Mask_Noise_Cleaner🧹蒙版噪点清理器

Kill the specks, keep the actual object in your mask

By wjl0313·Created 2 years ago·Updated 12 months ago· 54
🍒Mask_Noise_Cleaner🧹蒙版噪点清理器
  • mask
  • 清理后的遮罩
  • 遮罩图像
仅保留最大连通区域true
保留面积阈值100

Mask_Noise_Cleaner is the "my mask has a thousand tiny islands and one big blob" fix. Segmentation masks - from SAM, from background-removal models, from manual painting - almost always come out with stray white specks: dust in the corners, disconnected flecks where the model got confused. If you feed that mess straight into an inpaint or a compositor, those specks become visible artifacts. This node looks at the mask's connected regions, keeps the big one, and throws away the noise. The README's demo shows exactly that: a speckle-riddled mask cleaned down to one clean subject.

How it works

It converts the mask to 8-bit, runs cv2.findContours to pull out every connected white region, and sorts them by area. Then it decides what survives:

  • 仅保留最大连通区域 (keep only largest connected region) on - the default - keeps just the single biggest blob and fills it in, discarding everything else.
  • Off - it keeps every region whose area is at least 保留面积阈值 (retention area threshold, default 100 pixels). That's the mode for masks with multiple legitimate objects where you just want the micro-flecks gone.

Both are applied with fillPoly, so the output is a clean, solid mask - holes inside the main blob get filled too, which is usually what you want. It returns the cleaned MASK plus a 3-channel IMAGE version of it, so you can preview exactly what survived without a separate visualizer node.

The inputs that matter

  • mask - the noisy input mask.
  • 仅保留最大连通区域 - keep the biggest blob only (on) vs. keep everything above the area threshold (off).
  • 保留面积阈值 - pixel-area cutoff for the "keep several regions" mode. For a 1024×1024 image, 100 pixels is a tiny speck; raise it toward 1,000–5,000 to be more aggressive.

Outputs: 清理后的遮罩 (MASK) and 遮罩图像 (IMAGE).

Installation

Ships in ComfyUI_KimNodes:

cd ComfyUI/custom_nodes
git clone https://github.com/wjl0313/ComfyUI_KimNodes

Restart ComfyUI. Needs OpenCV, which any working ComfyUI install already has.

Common issues

If the mask is entirely empty, it returns an all-black mask (which is the correct "nothing here" answer, just be aware of it). If it's entirely white - one giant region - the "largest only" mode is a no-op and you keep the whole thing. Watch the area-threshold unit: it's in raw pixels, so the same value behaves very differently on a 512px image versus a 2048px one. And note that on list input it processes the first item - for cleaning a batch of masks frame-by-frame you'd want per-item handling. This node has become the default stop between "segmentation gave me a mask" and "inpaint actually used it" in a lot of workflows for good reason: it's one toggle and it fixes the single most annoying mask artifact.

Category🍒 Kim-Nodes/🔲Mask_Tools | 蒙板工具

Inputs (3)

NameTypeDefaultDescription
maskMASK
仅保留最大连通区域BOOLEANtrue
保留面积阈值INT1000–100000

Outputs (2)

NameTypeDescription
清理后的遮罩MASK
遮罩图像IMAGE