cv2.clipLine
Trim a segment to a box, and answer \"is this visible at all?\"
- pt1
- pt2
- retval
- pt1
- pt2
Why you'd reach for it
Two jobs, both plumbing. First: you have a line segment that runs off the edge of a crop, and you want the part that is actually on screen. Second, and this is the one people miss: you want a yes/no answer to "does this line touch my region at all?" - and a boolean is a much cleaner thing to branch on than a drawn image you then have to measure.
It's a raw OpenCV wrapper from ComfyUI CV (bmad4ever/comfyui_cv), so it lives in image/CV/low-level/cv2 C and behaves the way cv2.clipLine does in Python - flat integers for the rectangle, composite points for the segment, three outputs. This is the Cohen–Sutherland clipping step, which OpenCV keeps around mainly as a utility for line-detection and geometry code.
How it works
Give it a rectangle and a segment, and it clips the segment against the rectangle - the standard "walk the line, find where it crosses an edge, truncate" algorithm. What comes back is the surviving piece: a boolean saying "any of it survived", and the two (possibly moved) endpoints of the clipped segment.
The interesting design choice in this pack is that cv2.clipLine's Rect is flattened into four separate INTs (imgRect_x, imgRect_y, imgRect_w, imgRect_h) rather than a single composite, while the two points stay composite CV_TUPLE values. That's inconsistent with how the pack usually handles Rect shapes, and it matters - you can't wire a bounding box in directly, you have to split it first.
Inputs and outputs that matter
Required, all of them:
- imgRect_x, imgRect_y - top-left corner in pixels.
- imgRect_w, imgRect_h - width and height (≥ 0).
- pt1, pt2 - the endpoints, each a
CV_TUPLE(x, y). Type them in, or wire them from CV Tuple / CV Split Point's reverse. You cannot half-connect one.
Outputs:
- retval -
BOOLEAN.Trueif any part of the segment is inside the rectangle. - pt1, pt2 -
CV_TUPLE. The clipped endpoints. Yes, the outputs carry the same names as the inputs; the node is a wrapper, not a curated UI.
Where this earns its keep in a graph: retval goes into any boolean switch to mute a branch or flag a frame, and the clipped pt1/pt2 go into cv2.line, cv2.arrowedLine, or the curated CV Draw Segments node to render the visible part. Clipping first means your overlay stops at the crop boundary instead of drawing a line across the whole tile - the difference between a readable annotation and a mess when you're tiling a big image into crops.
Installing the pack
ComfyUI Manager → search ComfyUI CV, or:
cd ComfyUI/custom_nodes
git clone https://github.com/bmad4ever/comfyui_cv
Restart afterwards. The pack needs Python ≥ 3.12 and a ComfyUI with the V3 node API; the only real dependency is opencv-contrib-python-headless. Roughly 470 raw cv2.* nodes appear in the node list once it loads, so don't be startled by the menu.
Where people get burned
- Everything defaults to zero.
x=0, y=0, w=0, h=0is a zero-area rectangle, and clipping against a zero-area rectangle returnsFalseevery time. Ifretvalis stubbornly false and your points look sane, check the rectangle before anything else. - A rectangle in the wrong coordinate space.
imgRect_x/imgRect_y/imgRect_w/imgRect_hare in the same pixel space as your points. Points measured on the full 2048px frame, rectangle in 512px crop coordinates, and you'll get a plausible-but-wrong clip with no error. - Expecting it to draw. It never draws. It returns numbers. If you wanted pixels, you're looking for
cv2.line. - Pack caveats worth internalising once. The README states plainly that the pack is LLM-assisted, may have test-overfitted corners, is "not recommended for production" without review, and that updates are not planned. There is also no community corpus behind it - searching Reddit for the pack returns nothing - so you are the support channel. The one failure class that bites broadly is a broken
cv2install: OpenCV-based node packs famously die together withDLL load failed while importing cv2or a missing contrib build, and a pack whose entire dependency is OpenCV feels that hardest. The repo'stools/repair_opencv_contrib.py --checkis the first thing to run if contrib nodes have gone missing.
Inputs (6)
| Name | Type | Default | Description |
|---|---|---|---|
| imgRect_x | INT | 0-2147483648–2147483647 | Rectangle top-left corner X in pixels. |
| imgRect_y | INT | 0-2147483648–2147483647 | Rectangle top-left corner Y in pixels. |
| imgRect_w | INT | 00–2147483647 | Rectangle width in pixels (>= 0). |
| imgRect_h | INT | 00–2147483647 | Rectangle height in pixels (>= 0). |
| pt1 | CV_TUPLE | 0,0 | First line point. One value with 2 components (x, y) - it travels as a whole, so it cannot arrive half-connected. Wire it from 'CV Tuple' or type the components in place. |
| pt2 | CV_TUPLE | 0,0 | Second line point. One value with 2 components (x, y) - it travels as a whole, so it cannot arrive half-connected. Wire it from 'CV Tuple' or type the components in place. |
Outputs (3)
| Name | Type | Description |
|---|---|---|
| retval | BOOLEAN | — |
| pt1 | CV_TUPLE | - - - Pixel coordinates (x, y) as ONE composite value - wire it straight into any cv2 point input (center, seedPoint, pt1/pt2) or into 'CV Split Tuple' for the separate numbers. |
| pt2 | CV_TUPLE | - - - Pixel coordinates (x, y) as ONE composite value - wire it straight into any cv2 point input (center, seedPoint, pt1/pt2) or into 'CV Split Tuple' for the separate numbers. |