Nodes/opencv-comfyui/OpenCV fitEllipseAMS_0
ComfyUI Node

OpenCV fitEllipseAMS_0

The careful ellipse fitter — fitEllipseAMS when outliers lurk

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

Plain fitEllipse fits an ellipse to points with a classic least-squares cost, which means a few stray points can drag the whole ellipse off. cv2.fitEllipseAMS is the "Approximate Mean Square" variant, and it's the more careful cousin: it minimizes a different error metric that's far less sensitive to outliers, so a couple of bad points don't wreck the fit. OpenCV fitEllipseAMS_0 wraps it for ComfyUI.

When does that matter? When your point set is noisy - a findNonZero point list from a real mask almost always carries a few stray pixels, and those are exactly the points that pull a least-squares ellipse around. If you're measuring a slightly-irregular blob and the ellipse keeps twitching between frames, AMS is the stabilizer.

Input and output

Skeleton-simple:

  • points - NPARRAY of 2D points (Nx1x2 or Nx2, float). At least 5.

Output: a literal (STRING) - the fitted ellipse as a RotatedRect serialized as ((center_x, center_y), (width, height), angle). It's a value, not an image; previewing it via Nparrays2Image earns you the pack's 'NoneType' object has no attribute 'shape' error, because that's not what it is.

The honest tradeoff

AMS is more robust, but it's not free. It's a bit slower, and its fitted ellipse can differ slightly from the least-squares one even on clean data - the two cost functions genuinely disagree sometimes about what "best" means. The practical guidance: for clean, dense contours, fitEllipse is fine and you won't see a difference. For noisy or outlier-riddled points - or when you want a second opinion on a fit - use AMS. There's no penalty to trying both and comparing the literal outputs.

Everything else matches the fitEllipse_0 article: floats, five points minimum, feed the center/axes/angle back into this pack's ellipse node to draw the result.

Install

Part of geroldmeisinger/opencv-comfyui. ComfyUI Manager → search opencv-comfyui → install → restart, or:

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

requirements.txt installs opencv-contrib-python, numpy, torch. The pack imports cv2 at startup - a missing or conflicting OpenCV install (the guidedFilter error is the tell) means none of its nodes register.

Verdict

fitEllipseAMS_0 is the one you reach for when "roughly elliptical" has some rough in it. There's a fitEllipseAMS_1 twin that's byte-identical, and a fitEllipseDirect sibling in the pack too - AMS for robustness, Direct for speed. Start with the default fitEllipse, and graduate to AMS the first time a stray pixel ruins your measurement.

Categoryimage/OpenCV

Inputs (1)

NameTypeDefaultDescription
pointsNPARRAY

Outputs (1)

NameTypeDescription
literalSTRING