cv2.rectangle (2/2)
X, y, width, height — the variant detectors actually give you
- source
- result
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_8for 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
Inputs (9)
| Name | Type | Default | Description |
|---|---|---|---|
| source | COMFY_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_x | INT | 0-2147483648–2147483647 | Rectangle top-left corner X in pixels. |
| rec_y | INT | 0-2147483648–2147483647 | Rectangle top-left corner Y in pixels. |
| rec_w | INT | 00–2147483647 | Rectangle width in pixels (>= 0). |
| rec_h | INT | 00–2147483647 | Rectangle height in pixels (>= 0). |
| color | STRING | (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. |
| thicknessopt | INT | 1-2147483648–2147483647 | Thickness 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). |
| lineTypeopt | COMBO | LINE_AA | Type of the line. See #LineTypes |
| shiftopt | INT | 0-2147483648–2147483647 | Number of fractional bits in the point coordinates. Preset to the OpenCV default (0). |
Outputs (1)
| Name | Type | Description |
|---|---|---|
| result | COMFY_MATCHTYPE_V3 | Echoes the 'source' input's format: an IMAGE link comes back as IMAGE, MASK as MASK, NPARRAY stays NPARRAY. |