Nodes/ComfyUI Layer Style/LayerMask: Mask Box Detect
ComfyUI Node Runs on cloud

LayerMask: Mask Box Detect

Turn a mask into a bounding box and crop coords

By chflame163·Created 3 years ago·Updated 5 days ago· 3,113
LayerMask: Mask Box Detect
  • mask
  • box_preview
  • x_percent
  • y_percent
  • width
  • height
  • x
  • y
  • crop_box
◄detect▾►
â—„x_adjust0â–º
â—„y_adjust0â–º
â—„scale_adjust1.00â–º

You've got a mask - say, the subject cut out of a photo - and now you want the box around it: where it sits, how big it is, so you can auto-crop to it or place something relative to it. That's this node. Give it a mask, it finds the rectangle, and it hands you back the coordinates as numbers plus a ready-to-use crop box. It's the glue node that turns "here's a blob" into "crop here, at these pixels."

This is the kind of node that makes automated pipelines work. Detect a subject with any masking node, box-detect it here, and now downstream nodes can crop tight to the subject, position an overlay, or feed the box into a crop-and-stitch inpaint. No manual coordinate typing.

How it works

It scans the mask's white region and computes a rectangle from it, in one of three modes. min_bounding_rect is the tightest box that contains the whole mask - the usual choice. max_inscribed_rect is the largest rectangle that fits inside the mask (useful when you want an area guaranteed to be within the shape). mask_area works off the filled area. Then you can nudge and scale that box before it outputs.

The inputs and outputs that matter

  • mask (MASK) - the region to box.
  • detect - the mode: min_bounding_rect (tight outer box, the default reach), max_inscribed_rect (biggest inner box), or mask_area.
  • x_adjust / y_adjust (INT) - shift the box by pixels.
  • scale_adjust (FLOAT, 0.01–100, default 1) - grow or shrink the box; 1.1 pads it out 10%, handy so a tight crop isn't clipping the subject's edge.

Outputs, and there are a lot of them because it's a data node:

  • box_preview (IMAGE) - a visual of the detected box so you can sanity-check.
  • x_percent / y_percent (FLOAT) - position as a fraction of the frame.
  • width / height / x / y (INT) - the box in pixels.
  • crop_box (BOX) - the packaged box you feed straight into a crop node.

You rarely use all of them; grab the ones your downstream node asks for - usually crop_box, or the individual ints.

How to install it

ComfyUI Manager → search ComfyUI Layer Style → install → restart. Or:

cd ComfyUI/custom_nodes
git clone https://github.com/chflame163/ComfyUI_LayerStyle

Restart to install requirements. Pure geometry, no models.

Common issues & troubleshooting

Missing node usually means the pack failed to load - look for IMPORT FAILED on ComfyUI_LayerStyle at startup. The two usual culprits across this pack: a stale install from before it split into ComfyUI_LayerStyle and ComfyUI_LayerStyle_Advanced (delete the old folder, reinstall), or a broken dependency in your environment crashing the import. Reinstall clean via Manager.

Practical notes: if your box is way too big or grabs the whole frame, your mask has stray white pixels or noise outside the subject - clean the mask first (a threshold or a MaskEdgeShrink pass) so the bounding rect isn't chasing specks. If a tight crop is clipping edges, bump scale_adjust slightly above 1 to pad. And feed the crop_box output to a crop node that expects a BOX; the individual x/y/width/height ints are there for nodes that want raw numbers instead.

Category😺dzNodes/LayerMask

Inputs (5)

NameTypeDefaultDescription
maskMASK—
detectCOMBO3 options: min_bounding_rect, max_inscribed_rect, mask_area
x_adjustINT0-9999–9999—
y_adjustINT0-9999–9999—
scale_adjustFLOAT1.000.01–100—

Outputs (8)

NameTypeDescription
box_previewIMAGE—
x_percentFLOAT—
y_percentFLOAT—
widthINT—
heightINT—
xINT—
yINT—
crop_boxBOX—