Nodes/ComfyUI CV/cv2.rectangle (2/2)
ComfyUI Node

cv2.rectangle (2/2)

X, y, width, height — the variant detectors actually give you

By bmad4ever·Created 4 months ago·Updated 15 days ago· 1
cv2.rectangle (2/2)
  • source
  • result
◄rec_x0►
◄rec_y0►
◄rec_w0►
◄rec_h0►
◄color(0, 0, 0, 0)►
◄thickness1►
◄lineTypeLINE_AA►
◄shift0►

Two corners versus one corner and a size

The other cv2.rectangle wrapper wants pt1 and pt2 - two opposite corners. This one takes a Rect: rec_x, rec_y, rec_w, rec_h as four integer widgets, then the same color, thickness, lineType, shift. Under the hood it's cv2.rectangle(img, Rect(x, y, w, h), color, ...), a genuine cv2 overload, not the pack inventing a convenience.

Why it matters: almost nothing in computer vision hands you two corners. Detectors return x, y, w, h. Trackers (Kalman boxes, mean-shift windows, CV Track Window) return x, y, w, h. YOLO exports in xywh. ComfyUI's own BOUNDING_BOX structure is x, y, width, height. So this is the variant you'll use for verification overlays 90% of the time, and the one the pack's own examples reach for: the Kalman tracking workflow stamps a (414, 205, 44, 36) box with LINE_8, and the QR playground draws 90×90 boxes around found codes.

Inputs that matter

  • source - canvas, type-echoing (IMAGE → IMAGE, MASK → MASK, NPARRAY → NPARRAY). Frame-batch friendly: an IMAGE batch gets the same box drawn on every frame.
  • rec_x, rec_y - top-left corner in pixels. Can be negative in theory; in practice anything off-canvas is simply clipped away.
  • rec_w, rec_h - size in pixels, min 0. A zero-size rect draws nothing rather than erroring, which makes a silently-failed detection look like a node problem. Check your numbers.
  • color - the pack's scalar literal, BGR for an IMAGE canvas. Default (0, 0, 0, 0) is black, i.e. invisible on anything dark. The QR example uses (0, 0, 255, 0) - red.
  • thickness - 1 by default; -1 fills. Filled boxes are a nice way to eyeball a mask-shaped result.
  • lineType - LINE_AA (antialiased) by default; LINE_8 for crisp integer edges.
  • shift - sub-pixel fixed-point divisor, default 0. Leave it.

Output: result, the annotated canvas.

Where this fits in a workflow

Think of it as the display end of a detection pipeline, not part of it. Detector → boxes (array) → CV BBoxes to Masks or CV Crop by BBoxes for the actual work; a parallel branch into this node → Preview Image so you can see whether the boxes are right. That split is deliberate in this pack: the curated data nodes never draw, and the drawing nodes never decide anything - so you choose what gets rendered and what stays numeric. It's the separation that makes a graph debuggable rather than a soup of annotated pixels.

It's also useful for the reverse check: when a mask looks too big, draw the box the mask should occupy on top of the image and compare. See CV Masks to BBoxes for getting that box out of the mask.

Install

cd ComfyUI/custom_nodes
git clone https://github.com/bmad4ever/comfyui_cv
pip install "opencv-contrib-python-headless~=5.0.0.93"

Or install "ComfyUI CV" by bmad4ever from ComfyUI Manager and let it pull the dependency. Needs Python ≥ 3.12 and a recent ComfyUI on the V3 node API. Restart ComfyUI, reload the page.

Troubleshooting

The box is offset or the wrong size. Half the time this is a coordinate-space mix-up: x/y/w/h in the original image space, drawn onto a resized frame. The pack's examples resize first (e.g. down to (512, 576) with INTER_AREA) and then draw in the resized space; if your detector ran on the original, scale the numbers with CV Scale BBoxes before drawing.

Colour looks wrong. BGR. (255, 0, 0, 0) is blue.

Rectangle invisible on the preview. Thickness 1 against a busy image, or the black default. Bump thickness to 2–3 for previews.

Still nothing. Check the rect isn't zero-sized, and that you didn't wire the annotated result out of the graph entirely - an unconnected output is free to be optimised away, which is ComfyUI's caching doing its job rather than a bug.

A CV node error mentioning an unknown attribute or missing submodule. Another pack has likely overwritten the contrib OpenCV wheel this pack needs:

python ComfyUI/custom_nodes/comfyui_cv/tools/repair_opencv_contrib.py --check
python ComfyUI/custom_nodes/comfyui_cv/tools/repair_opencv_contrib.py --apply
Categoryimage/CV/low-level/cv2 R

Inputs (9)

NameTypeDefaultDescription
sourceCOMFY_MATCHTYPE_V3 The image output(s) echo this input's format. Accepts a ComfyUI IMAGE/MASK directly (frame 0 of a batch) or an NPARRAY. Arithmetic ops (add, multiply, etc.) process the full IMAGE batch when both inputs have the same batch size.
rec_xINT0-2147483648–2147483647Rectangle top-left corner X in pixels.
rec_yINT0-2147483648–2147483647Rectangle top-left corner Y in pixels.
rec_wINT00–2147483647Rectangle width in pixels (>= 0).
rec_hINT00–2147483647Rectangle height in pixels (>= 0).
colorSTRING(0, 0, 0, 0)Rectangle color or brightness (grayscale image). cv2 Scalar as a literal, e.g. "(0, 255, 0)" (BGR) or "(0, 255, 0, 64)" (BGRA). A bare number broadcasts to every component, so "255" means (255, 255, 255, 255). Components past the target's channel count are ignored by OpenCV.
thicknessoptINT1-2147483648–2147483647Thickness of lines that make up the rectangle. Negative values, like #FILLED, mean that the function has to draw a filled rectangle. Preset to the OpenCV default (1).
lineTypeoptCOMBOLINE_AAType of the line. See #LineTypes
shiftoptINT0-2147483648–2147483647Number of fractional bits in the point coordinates. Preset to the OpenCV default (0).

Outputs (1)

NameTypeDescription
resultCOMFY_MATCHTYPE_V3Echoes the 'source' input's format: an IMAGE link comes back as IMAGE, MASK as MASK, NPARRAY stays NPARRAY.