Nodes/ComfyUI CV/cv2.getClosestEllipsePoints
ComfyUI Node

cv2.getClosestEllipsePoints

The ellipse function OpenCV 5 added and nobody has heard of

By bmad4ever·Created 4 months ago·Updated 15 days ago· 1
cv2.getClosestEllipsePoints
  • ellipse_params
  • points
  • nparray

Everything in image processing that fits a shape hands you the shape. Almost nothing hands you the residual - how far off the data was. cv2.getClosestEllipsePoints is that missing half for ellipses: give it an ellipse and a set of points, and it returns the point on the ellipse closest to each one. Subtract, take a norm, and you have per-point distances.

If you've ever run fitEllipse and wondered whether the ring you fitted actually follows your contour or just found a plausible-looking average, this is the node that answers the question.

The inputs

ellipse_params accepts a rotated rectangle - ((center_x, center_y), (width, height), angle in degrees) - or the same thing typed as a literal string. The pack wires it straight from cv2.fitEllipse, cv2.minAreaRect or cv2.CamShift, all of which emit exactly that structure, so the fit → residual round trip is two wires. The flat spelling (cx, cy, w, h, angle) is also accepted if you'd rather type numbers than fight the tuple.

points is an NPARRAY and only an NPARRAY - this is a geometry node, not an image node. Feed it a point set from a contour (CV Contour To Points), a detector, or CV Points Literal.

One output, nparray: one closest-point row per input point, so the two arrays stay aligned and you can subtract them directly to get the residual vector, then take cv2.norm for the magnitudes.

width and height in a rotated rect are full axis lengths, not radii. Half them, or your "ellipse" is twice the size of the one you fitted, and every distance you compute is wrong in a way that looks almost plausible.

The catch: this is OpenCV 5 API

getClosestEllipsePoints does not exist in OpenCV 4.x. I checked against a 4.8 build and the attribute isn't there. So this node is only reachable if the cv2 your ComfyUI imports is a 5.0-era wheel - which is exactly what this pack pins (opencv-contrib-python-headless~=5.0.0.93). That's a real feature of how the pack works: its node registry is generated from the functions the installed build exposes, so a function your OpenCV doesn't have simply never appears in the menu. A missing node is a fact about your wheel, not a broken install.

Because it's new, you'll find essentially no tutorials, no Stack Overflow answers and no community workflows using it. The OpenCV docs are the only reference, which is fine here - there isn't much to it.

What you'd actually use it for

  • Fit quality. Fit an ellipse to a ring, gasket, ball, wheel or coin outline and get a maximum residual. Large and clustered residuals mean the thing isn't an ellipse and a downstream measurement is about to be garbage.
  • Snapping. Pull noisy detected points onto the ideal ellipse before measuring something along it - angle, position, spacing.
  • Gauge checking. Distance from a sampled contour to the nominal ellipse is the same calculation as a tolerance band, just without the metrology vocabulary.

It's a small utility with one honest limitation: there's no batched "give me the distances" convenience, so you build the subtraction yourself. That's two more nodes, and the whole chain is worthless if the points came from a bad fit - this measures the fit, it doesn't save it.

Install

Manager → search ComfyUI CV, or:

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

Restart ComfyUI afterwards - the node menu is built at startup. Needs Python ≥ 3.12 and a recent ComfyUI on the V3 node API. No models, no downloads. And if the node isn't there at all, print your version:

python -c "import cv2; print(cv2.__version__, hasattr(cv2, 'getClosestEllipsePoints'))"

Run that in the same environment ComfyUI uses. False is your answer.

Categoryimage/CV/low-level/cv2 G

Inputs (2)

NameTypeDefaultDescription
ellipse_paramsCV_ROTATED_RECT,STRING - - - A rotated rectangle: ((center_x, center_y), (width, height), angle in degrees). Wire it from cv2.fitEllipse / cv2.minAreaRect / cv2.CamShift, or type the literal (the flat (cx, cy, w, h, angle) spelling is accepted too).
pointsNPARRAY - - - A data array (points / matrix), NOT an image - only an NPARRAY link is accepted here.

Outputs (1)

NameTypeDescription
nparrayNPARRAY—