Nodes/opencv-comfyui/OpenCV approxPolyN_0
ComfyUI Node

OpenCV approxPolyN_0

Force a contour into exactly N sides

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV approxPolyN_0
  • curve
  • approxCurve
  • nparray
nsides
epsilon_percentage
ensure_convex

approxPolyDP simplifies a contour and hopes you get a nice number of corners. approxPolyN doesn't hope - you tell it "give me a pentagon" and it gives you a pentagon. This node is the OpenCV wrapper for cv2.approxPolyN, a relatively new function (added in OpenCV 4.10) that fits an exactly-N-sided polygon to a noisy curve. It's the right tool when you're detecting a shape with a known geometry: a hex bolt, a stop sign, a license plate, an eight-sided crop region.

Fair warning up front: this requires an OpenCV version new enough to have approxPolyN. If you installed opencv-python-contrib a while ago and get AttributeError: module 'cv2' has no attribute 'approxPolyN', update OpenCV first - the function doesn't exist in 4.9 and earlier.

How it works

Unlike Douglas-Peucker's epsilon-until-it-stops approach, approxPolyN holds the side count fixed and iteratively adjusts vertices until the fitted polygon fits the curve as closely as possible, with optional convexity enforcement. The inputs:

  • curve - the contour as an NPARRAY of points. Same shape expectations as approxPolyDP: N×1×2 or N×2, float works best.
  • nsides - exactly how many sides you want. The whole point of the node. Ask for 4 and get a quadrilateral.
  • epsilon_percentage - a tolerance for how faithfully the N-gon may deviate from the original curve. OpenCV's default is 5.0 (percent). Crank it up to allow a looser fit; drop it if the polygon misses the contour's real corners.
  • ensure_convex - True forces the result to be convex, which matters when you're fitting things like hardware or frames that must not have concave kinks. Default is False.
  • approxCurve (optional) - OpenCV's out-parameter; leave unwired.
  • Output: one nparray of the fitted polygon's points.

The generated node actually reorders these into OpenCV's call signature (curve, nsides, approxCurve, epsilon_percentage, ensure_convex), but the widgets you see are what matter - don't worry about the internals.

When you'd reach for it

The "detect the stop sign" tutorial shape. Or more realistically for ComfyUI: fitting a fixed-vertex polygon to a hand-drawn mask so you can do a clean, stable crop region that doesn't jitter frame to frame, or detecting a quadrilateral document region to warp and deskew. It's a niche geometry primitive, but when you need an exact side count there is no knob on approxPolyDP that gets you there - you'd be tuning epsilon and hoping.

The nparray catch

Same as its sibling: the output is a point array, not an image. Wiring it straight into Nparrays2Image triggers the pack's classic 'NoneType' object has no attribute 'shape' error. Points feed other geometry nodes, or get rasterized by a drawing node before preview.

Install

Part of opencv-comfyui. ComfyUI Manager → search "opencv-comfyui", or:

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

The -U is deliberate - you want OpenCV 4.10+ for this node to exist. Restart ComfyUI and you're set; there are no models to download, and the only known pack-level install trap is a conflicting OpenCV (Cannot import name 'guidedFilter' from 'cv2.ximgproc'), which a clean reinstall fixes.

Categoryimage/OpenCV

Inputs (5)

NameTypeDefaultDescription
curveNPARRAY
nsidesINT
epsilon_percentageFLOAT
ensure_convexBOOLEAN
approxCurveoptNPARRAY

Outputs (1)

NameTypeDescription
nparrayNPARRAY