Nodes/comfyui_bmad_nodes/BoundingRect (contours)
ComfyUI Node Runs on cloud

BoundingRect (contours)

From 'there's a shape here' to four crop coordinates

By bmad4ever·Created 3 years ago·Updated 9 months ago· 70
BoundingRect (contours)
  • contour
  • INT
  • INT
  • INT
  • INT
return_modetop-left XY + bottom-right XY

Contour detection gives you shapes - lists of points tracing the outlines of things in an image. But most of what you want to do with a detected object - crop it, mask it, feed it to inpainting - starts with a boring box around it. BoundingRect (contours) is the bridge: it takes a detected contour and returns its axis-aligned bounding rectangle as four integers, in the coordinate format of your choice.

It's the "get a crop region from a detection" step in a masking pipeline. The typical chain in this pack: Contours (finds the contours) → Filter Contour (picks the one you want) → BoundingRect (contours) → the four numbers go into a crop, an inpaint region, or AdjustRect to snap them to latent-friendly sizes.

Inputs and output

  • contour - a CV_CONTOUR, the pack's contour type, straight from its Contours node (or a node in another pack that emits the same type).
  • return_mode - how the four output numbers are expressed: top-left XY + WH, top-left XY + bottom-right XY (default), center XY (floored) + WH, or center XY + half WH (all floored). Pick whatever your downstream crop or AdjustRect expects.

Output is four INTs per the chosen format. That's the whole node - cv.boundingRect(contour) under the hood, with the format conversion applied after.

Install

Part of bmad4ever/comfyui_bmad_nodes, under Bmad/CV/Contour:

cd ComfyUI/custom_nodes
git clone https://github.com/bmad4ever/comfyui_bmad_nodes
cd comfyui_bmad_nodes
pip install -r requirements.txt

Restart, or ComfyUI Manager → search comfyui_bmad_nodes. Pure OpenCV (opencv-python~=4.8.1.78 pinned in requirements.txt); no models.

Gotchas

The biggest trap is the input type. This node only accepts CV_CONTOUR - you can't feed it a mask and expect it to find shapes for you. If your contour source isn't the pack's Contours node, confirm it emits the same custom type or this socket won't connect. Second, a bounding rect is axis-aligned: if your detected object is a diagonal person or a rotated sign, the box will include big empty triangles of background. For tight rotated boxes you'd want a minimum-area rectangle instead - the pack exposes related geometry through its Filter Contour helpers, but this node is strictly the straight box. And if contour comes in as None, the node quietly returns four zeros and prints a warning - check for that before feeding crops.

CategoryBmad/CV/Contour

Inputs (2)

NameTypeDefaultDescription
contourCV_CONTOUR
return_modeCOMBOtop-left XY + bottom-right XY4 options: top-left XY + WH, top-left XY + bottom-right XY, center XY (floored) + WH, center XY + half WH (all floored)

Outputs (4)

NameTypeDescription
INTINT
INTINT
INTINT
INTINT