Nodes/opencv-comfyui/OpenCV minAreaRect_0
ComfyUI Node

OpenCV minAreaRect_0

Find the angle of anything

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV minAreaRect_0
  • points
  • literal

Every shape-measuring pipeline in OpenCV eventually needs this one: minAreaRect computes the smallest-area rotated rectangle that contains a set of points. A plain axis-aligned bounding box is loose - it's the box that clips the corners of a tilted object. The minimum-area rotated rectangle tightens right up against the object and, as a bonus, tells you its orientation. That angle is the whole point: it's how classical CV figures out "this card is rotated 27 degrees" before it deskews, crops, and OCRs the thing.

In a ComfyUI workflow, this is the geometry half of an automated crop pipeline. You've got point coordinates for an object or region - from a detection mask, a contour, or corner points - and you want to know its center, its size, and how far it's tilted so you can rotate-and-crop it straight. minAreaRect_0 hands you all three in one call. It's fast, deterministic, and part of opencv-comfyui's auto-generated ~635-node surface, which means it also carries the pack's usual rough edges.

Inputs and outputs

  • points - one NPARRAY: the set of 2D points, typically an N×2 array of (x, y) coordinates. In OpenCV this is usually a contour (the output of findContours) - but note this pack doesn't include findContours (its return type was filtered out during generation), so in practice your points have to come from elsewhere: a mask's coordinates, corner detections, or a manually constructed array from another node pack.
  • Output literal - a STRING, not an array. It's the string repr of OpenCV's RotatedRect, shaped like ((center_x, center_y), (width, height), angle). For a rotated rectangle that's ((241.5, 173.2), (87.0, 45.0), -27.4) - center, size, and tilt in degrees.

That string output is the thing to internalize: this node gives you numbers as text. You can read them from the queue, wire them into a string-capable node, or feed them onward if you've got a parser in your chain. But literal is not an image and not an nparray you can feed to Nparrays2Image - the README's "not every return type is an image" warning is aimed squarely at outputs like this.

Setup

cd ComfyUI/custom_nodes
git clone https://github.com/geroldmeisinger/opencv-comfyui

or ComfyUI Manager → "opencv-comfyui" → install → restart. Needs opencv-contrib-python, numpy, torch. If another node pack has already pulled in OpenCV, you're probably fine; the conflict signature to watch for is Cannot import name 'guidedFilter' from 'cv2.ximgproc'.

The natural partner for this node's output - actually rotating the image to match - is getRotationMatrix2D plus warpAffine, both of which the pack ships. So the full deskew chain is: get your points, measure the angle here, rotate with the warp nodes, crop. It's a classical-CV assembly line that fits neatly into a ComfyUI graph if you're willing to wire the geometry yourself.

Categoryimage/OpenCV

Inputs (1)

NameTypeDefaultDescription
pointsNPARRAY

Outputs (1)

NameTypeDescription
literalSTRING