Nodes/opencv-comfyui/OpenCV rotatedRectangleIntersection_0
ComfyUI Node

OpenCV rotatedRectangleIntersection_0

Do two rotated boxes actually overlap?

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV rotatedRectangleIntersection_0
  • intersectingRegion
  • int
  • nparray
rect1
rect2

rotatedRectangleIntersection_0 asks a question your workflow occasionally needs answered: do these two rotated rectangles overlap, and if so, what's the shape of the overlap? It wraps cv2.rotatedRectangleIntersection, a calib3d utility that's the geometric workhorse behind collision checks, overlap filtering, and "did my crop region collide with that detection" logic.

Why rotated rectangles

Why rotated, though? Because axis-aligned bounding boxes lie. If you're working with detections or regions that are tilted - a face at an angle, an object rotated in frame, a crop box that's been rotated to match a warp - an upright bounding box either over-reports overlap (it includes empty corner space) or misses a real one. A RotatedRect hugs the actual box, so the intersection test on it tells the truth. This is the kind of node that shows up in detection/detailing pipelines where you're deciding whether two regions belong to the same thing or whether a second pass is needed.

How it works

Mechanically it computes the convex polygon of the intersection between the two rectangles. It returns an integer status plus the region itself: 0 means no intersection (INTERSECT_NONE), 1 partial (INTERSECT_PARTIAL), 2 means one fully inside the other (INTERSECT_FULL).

Inputs and outputs

The inputs are where this pack's quirks bite. rect1 and rect2 are STRING inputs - not boxes you drag, but Python literals you type in a specific format. A RotatedRect is ((center_x, center_y), (width, height), angle_degrees), e.g.:

((512.0, 512.0), (200.0, 100.0), 30.0)

The node parses that with ast.literal_eval, so the syntax has to be exact - wrong brackets or a stray comma gives you the pack's signature invalid syntax (<unknown>, line 0) error. Note the angle is in degrees. The optional intersectingRegion input is an out-parameter; leave it alone, the README's standing advice is to skip out-parameters. Outputs are int (the status above) and nparray (the intersection polygon's vertices, ready to be drawn or measured).

Installing

cd ComfyUI/custom_nodes
git clone https://github.com/geroldmeisinger/opencv-comfyui
pip install opencv-contrib-python

Install: ComfyUI Manager → search "opencv-comfyui", or the commands above, then pip install opencv-contrib-python (the pack's declared dependency - no models to fetch).

Verdict

Real talk on scope: this is a niche geometry node. The status integer is the useful part - wire it into a compare or switch to gate downstream work ("if no intersection, skip"). The polygon output is only useful if you have a consumer that can draw or parse points, which this pack mostly doesn't give you, so don't be surprised if the NPARRAY goes nowhere. The _0/_1 variants are the MatLike/UMat overloads and behave identically.

Categoryimage/OpenCV

Inputs (3)

NameTypeDefaultDescription
rect1STRING
rect2STRING
intersectingRegionoptNPARRAY

Outputs (2)

NameTypeDescription
intINT
nparrayNPARRAY