ComfyUI Node

Mask Area Comparison

Gate your workflow on whether a mask is actually big enough to matter

By zhangp365·Created 2 years ago·Updated 5 months ago· 135
Mask Area Comparison
  • mask
  • is_greater
  • is_smaller
area_threshold40000

Detection masks aren't binary in the way you'd want them to be for control flow - a face-detector or segmentation node either finds something or it returns an empty-ish mask, and "empty-ish" is exactly where things go wrong if your workflow assumes a mask is always meaningful. MaskAreaComparison turns a mask's size into a plain true/false you can branch on: is this region bigger than a threshold, or not.

What it's for

The obvious use is gating expensive or destructive steps behind a size check. If you're running a face-detailer or an inpaint pass on a masked region, doing that work on a mask that's three pixels wide because detection barely found anything is wasted compute at best and a garbage result at worst. Compare the mask's area to a sane minimum first, and only run the heavy step if it clears the bar. It works the other way too - you can gate a step to run only on small regions (skip re-touching anything already large enough to look fine) using the is_smaller output.

How it works

It counts the mask's active area in pixels - width × height of the "on" region, not the whole canvas - and compares that count against your threshold. No fuzziness, no percentage-of-image normalization, just a raw pixel-area number versus another raw pixel-area number.

The inputs and outputs that matter

  • mask (MASK) - the mask you want to measure.
  • area_threshold (default 40000, 0 to 1,000,000,000) - the cutoff, in pixels. 40000 is roughly a 200×200px region, which gives you a sense of scale for setting your own threshold relative to whatever resolution you're working at.

Two outputs, both BOOLEAN: is_greater (true if the mask's area exceeds the threshold) and is_smaller (true if it doesn't). Wire either one into a switch node - this pack's own ReferenceLatentSwitch uses the same enabled-boolean pattern, or reach for a general-purpose bypass/switch node from whatever else you have installed - to conditionally run or skip a branch of your graph.

How to install it

Via ComfyUI Manager: search "ComfyUI-utils-nodes," install, restart. Manually:

cd ComfyUI/custom_nodes
git clone https://github.com/zhangp365/ComfyUI-utils-nodes

Restart ComfyUI. Pure math on a mask tensor - no models, no extra dependencies, quick to install.

Common issues & troubleshooting

The one thing to get right is scale: area_threshold is a raw pixel count, and pixel counts scale with the square of your resolution. A threshold tuned for a 512×512 workflow will read as tiny once you move to 2048×2048 renders - a region that would have tripped is_greater at the lower resolution might sit well under threshold at the higher one just because the whole canvas got bigger. If you're switching resolutions across projects, revisit the threshold rather than assuming one number works everywhere.

Beyond that, remember this measures area, not shape or connectivity - a thin scattered mask with the same total pixel count as a solid blob reads identically to this node. If what you actually care about is "is there one solid detected region" rather than "how many pixels total," this node won't distinguish that for you; you'd want to clean up or filter the mask (removing small disconnected specks) before it hits this comparison, or the threshold check can pass on a mask that's technically big enough but practically useless.

Categorymask/utils

Inputs (2)

NameTypeDefaultDescription
maskMASK
area_thresholdINT400000–1000000000

Outputs (2)

NameTypeDescription
is_greaterBOOLEAN
is_smallerBOOLEAN