Nodes/SaltAI-Open-Resources/Adaptive Threshold Mask Regions
ComfyUI Node

Adaptive Threshold Mask Regions

Re-threshold a mask locally instead of with one global cutoff

By get-salt-AI·Created 2 years ago·Updated 2 years ago· 84
Adaptive Threshold Mask Regions
  • masks
  • MASKS
block_size11
constant2

Masks rarely come out of a segmentation model as clean black and white. CLIPSeg, SAM, whatever produced yours - you usually get soft, gradient, or noisy pixels, and a single global threshold ("everything above 50% gray is white") often gets it wrong when confidence or lighting varies across the image. Adaptive Threshold Mask Regions fixes that by looking at each pixel's local neighborhood instead of the whole image at once.

How it works

This is OpenCV's adaptiveThreshold operation, wrapped as a mask node - the two parameters here (block_size, constant) are literally that function's arguments. Instead of one global cutoff, it computes a local threshold for each pixel based on the average of the pixels around it, then subtracts a small constant before deciding foreground or background. That local sensitivity is the whole point: it adapts to regions of the mask that are locally brighter or dimmer rather than applying one rule everywhere.

The inputs and outputs that matter

  • masks (required, MASK).
  • block_size (optional, default 11, 3–255, step 2) - the local neighborhood size around each pixel. Has to be odd, which is why the widget steps by 2. Bigger means smoother, more global-feeling results; smaller means more sensitivity to fine local detail.
  • constant (optional, default 2, 0–10) - a small offset subtracted from the local average before thresholding. Raise it if your output mask is coming out too thick/aggressive, lower it if it's too thin/sparse.

Output is MASKS - a re-thresholded mask, typically crisper, ready for inpainting, compositing, or another filter in this same family.

How to install it

ComfyUI Manager: search SaltAI-Open-Resources or SaltAI, install, restart. Manual:

cd ComfyUI/custom_nodes
git clone https://github.com/get-salt-AI/SaltAI

then restart. This is an OpenCV-backed operation, so cv2 needs to be importable in your ComfyUI environment - already the case for most installs, since so many other packs depend on it too.

Common issues & troubleshooting

Output looks noisier or speckled, not cleaner. block_size is probably too small for your image's resolution - scale it up. A tiny neighborhood on a high-res mask reacts to noise as if it were signal.

Large solid regions get chopped into disconnected fragments. That's adaptive thresholding doing exactly what it's designed to do - reacting locally - on an image that would actually be better served by a single global threshold. Not every mask needs "adaptive"; sometimes a plain threshold node is the right tool, and this is the wrong one for that particular mask.

No visible change at all. If your source mask was already close to binary (hard edges, little gradient), there's not much for a local threshold to do differently from a global one - try it on a genuinely soft/gradient mask to see the effect clearly.

CategorySALT/Masking/Filter

Inputs (3)

NameTypeDefaultDescription
masksMASK
block_sizeoptINT113–255
constantoptINT20–10

Outputs (1)

NameTypeDescription
MASKSMASK