Nodes/NH-Nodes/NH Create Box Mask
ComfyUI Node

NH Create Box Mask

Turn any mask into a clean rectangle

By jetthuangai·Created 5 months ago·Updated 21 days ago· 6
NH Create Box Mask
  • mask
  • box_mask

Some jobs want the region, not the shape. NH Create Box Mask (class NH_MaskToBBox) takes any mask - a person, a garment, a blob - and replaces it with a solid rectangle that exactly bounds it. That box mask is the lazy-but-reliable version of a region: simpler to composite, easier to reason about, and it's what a lot of downstream tooling actually expects.

How it works

For each mask in the input batch, it finds the lit pixels (threshold 0.5) and computes their min/max rows and columns - the bounding box. Then it writes a solid rectangle over that box in a new mask: every pixel inside the bbox is 1.0, everything outside is 0.0. The output box_mask is the same resolution and batch size as the input, so it plugs straight back into anything that consumed the original.

That's it. No expansion, no smoothing, no options - one mask in, one boxed mask out. The simplicity is the feature.

Where you'd actually use it

Whenever a downstream step works better with a rectangular region than with an organic shape. Inpainting is the big one: a rectangular mask gives the sampler a larger, cleaner region to rework, and some users find it more predictable than a tight silhouette mask (at the cost of re-rendering background too). It's also the bridge to bounding-box logic - pair it with Mask Properties (NH) if you want the same bbox as numbers, or with a crop node that takes a region mask. In the NH pack it sits right next to the aspect-ratio and morphology tools, all aimed at making masks behave.

Installing

ComfyUI Manager → search NH-Nodes → install → restart. Or:

cd ComfyUI/custom_nodes
git clone https://github.com/jetthuangai/NH-Nodes.git
cd NH-Nodes
pip install -r requirements.txt

Restart and refresh the browser page. numpy/torch only.

Gotchas

A mask with no lit pixels (all black) produces an all-black box mask - silently. That's technically correct, but if your detector produced nothing, you'll get an empty box with no warning, so check upstream. And remember the box is tight: there's no padding parameter, so the rectangle hugs the mask edges exactly. If you want a box with breathing room, chain Mask Morphology (NH)'s expand first and then convert. The trade-off is inherent: you gain a clean region and lose the subject-shaped precision, so don't reach for this when the silhouette matters.

CategoryNH-Nodes/Mask

Inputs (1)

NameTypeDefaultDescription
maskMASK

Outputs (1)

NameTypeDescription
box_maskMASK