Nodes/Mask Analyzer/Mask Analyze
ComfyUI Node

Mask Analyze

Tell your workflow what kind of mask it's holding

By imk-design·Created 5 months ago·Updated 5 months ago· 0
Mask Analyze
  • mask
  • component_count
  • small_component_count
  • small_component_ratio
  • aspect_ratio
  • complexity_score
  • strategy
  • use_overlay_mode
threshold0.50
min_component_area40
small_component_area300
direct_max_components4
overlay_min_components9
wide_aspect_overlay3.2
small_ratio_overlay0.45

This node is a "what kind of mask am I looking at?" helper. Feed it any MASK tensor - the kind that falls out of SAM or a background-removal pass, or one you drew by hand for an inpaint - and it binarizes it, counts its connected components, measures how wide its bounding box is, and hands back a recommended strategy plus a pile of numbers about what it found.

Here's the use case that makes it worth a look. Mask-heavy workflows have a nasty habit of needing different downstream handling depending on the shape of the mask. A clean headshot cutout is one solid blob - you want to run it straight through, maybe a light cleanup. A hair-sliced cutout against a busy background is fifty fragments - you want a heavier simplification pass or a different composite path. A long thin mask is basically an overlay strip. Mask Analyze tries to tell these apart so your graph can pick a branch automatically instead of you babysitting it. It's a small, opinionated heuristic, not a model - and that's fine, because it's honest about it.

How it works

The mechanism is simple and fully local. It binarizes the mask at threshold (default 0.5), then:

  • Computes the aspect ratio of the bounding box around the non-zero pixels (width / height).
  • Finds connected components with 8-connectivity. If opencv-python is importable it uses connectedComponentsWithStats; otherwise it falls back to a pure NumPy flood-fill. That fallback is why the README can say "no extra dependencies" - numpy and torch are already in any ComfyUI install.
  • Drops components smaller than min_component_area, counts how many of the rest are "small" (≤ small_component_area), and rolls everything into a complexity_score from 0–100.

Then it picks a strategy. overlay wins if there are at least overlay_min_components, or the aspect ratio beats wide_aspect_overlay, or the small-component ratio beats small_ratio_overlay. Otherwise you get direct when the component count is at or under direct_max_components and the small ratio is under 0.25. Everything in between - including an empty mask, which returns direct with all-zero numbers - lands on simplified. Notice the deliberate gap between direct_max_components (4) and overlay_min_components (9): that band is the middle ground, and it's a feature, not a bug.

The inputs that matter

Most defaults are sensible for typical 1024-class masks. You'll actually touch these:

  • mask - the MASK tensor. Only the first frame of a batch is analyzed, so feed it one mask at a time.
  • threshold - binarization cutoff. Soft, anti-aliased masks benefit from tuning this down a touch.
  • min_component_area and small_component_area - both in pixels, so they're resolution-dependent. A mask analyzed at 512×512 will report different component counts than the same logical mask at 1024×1024. If your pipeline changes resolution, expect to re-tune.
  • The four strategy knobs (direct_max_components, overlay_min_components, wide_aspect_overlay, small_ratio_overlay) - these are the author's guess at your intent. Treat them as tunable, not sacred.

Outputs

Seven of them, but the ones that matter: strategy (direct / simplified / overlay), use_overlay_mode (a BOOLEAN, true exactly when strategy is overlay), and complexity_score if you want your own cutoff. component_count, small_component_count, small_component_ratio, and aspect_ratio are the raw measurements - handy for debugging why a mask got classified the way it did. Wire strategy into the sibling Mask Strategy Switch node, or straight into a text display while you're tuning.

Install

The pack is on the ComfyUI Manager registry searchable as "Mask Analyzer" (or "MaskAnalyze"). Manual route:

cd /path/to/ComfyUI/custom_nodes
git clone https://github.com/imk-design/ComfyUI-Mask-Analyzer.git

Restart ComfyUI. No model downloads, no requirements step - numpy/torch are already there and OpenCV is optional.

Where people get burned

Three things to know. First, the aspect ratio is computed from the bounding box, not the mask itself - a diagonal or scattered mask fills its box, so it can trigger overlay on shapes that aren't visually wide. Second, resolution dependence bites harder than people expect: the same scene at two resolutions can flip strategy. Third, complexity_score looks like a magic number but it's just a weighted sum of components, small-ratio, and width - useful as a threshold you pick yourself, useless as an absolute "quality" score. None of it is a failure mode, just calibration. It's a tiny niche pack (basically zero community discussion as of writing), so don't expect a library of shared tuned workflows yet - but for automating mask routing it genuinely fills a gap nothing else in the standard mask toolset covers.

CategoryMask Tools

Inputs (8)

NameTypeDefaultDescription
maskMASK
thresholdFLOAT0.500–1
min_component_areaINT401–100000
small_component_areaINT3001–100000
direct_max_componentsINT41–1000
overlay_min_componentsINT91–1000
wide_aspect_overlayFLOAT3.21–20
small_ratio_overlayFLOAT0.450–1

Outputs (7)

NameTypeDescription
component_countINT
small_component_countINT
small_component_ratioFLOAT
aspect_ratioFLOAT
complexity_scoreFLOAT
strategySTRING
use_overlay_modeBOOLEAN