Nodes/opencv-comfyui/OpenCV approxPolyDP_0
ComfyUI Node

OpenCV approxPolyDP_0

Turn jagged contour edges into clean polygons with Douglas-Peucker

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV approxPolyDP_0
  • curve
  • approxCurve
  • nparray
epsilon
closed

Your findContours output is always ugly. Contours come back with hundreds of points tracing every tiny wobble of an edge, and if you're trying to detect rectangles, road lanes, or document borders, that noise is in the way. approxPolyDP_0 is the node that collapses a noisy contour into a clean polygon with a handful of corners - it's the Douglas-Peucker algorithm, the same thing every barcode scanner and sheet-of-paper detector uses under the hood.

This is one node in opencv-comfyui, an auto-generated pack that wraps hundreds of top-level OpenCV functions. It's raw, the author says "expect dragons," but the geometry functions like this one are genuinely handy once you get the nparray plumbing sorted.

How it works

Douglas-Peucker asks: which points on this curve actually matter? It walks the contour and keeps only the points that deviate from a straight line by more than a tolerance - everything else is thrown away. That tolerance is your epsilon input: the maximum allowed distance between the original curve and the simplified one. Small epsilon = faithful, still noisy. Large epsilon = aggressive, blocky.

The inputs:

  • curve - the contour as an NPARRAY of points. OpenCV normally gives you contours as N×1×2 arrays; reshape/convert them to float so the algorithm behaves. You'll usually pull this from a findContours node upstream.
  • epsilon - the simplification tolerance. The classic recipe: epsilon = 0.01 * arcLength(curve, True). That scales the tolerance to the contour's size so small shapes aren't over-simplified. Wire an arcLength_0 node in and multiply.
  • closed - whether the curve is a closed loop (True for contours from a mask, False for open polylines).
  • approxCurve (optional) - OpenCV's out-parameter. Leave it unwired; the node returns the result.
  • Output: a single nparray holding the simplified polygon's points.

A True on closed with a rectangle contour gives you exactly four points back. That's the whole reason to reach for this: a polygon you can count corners on, feed into drawContours, or pass on as a region.

The nparray dance

This pack works on OpenCV arrays, not Comfy IMAGE. Convert in with Image2Nparray, and beware: the output here is a list of points, not an image. Route it into Nparrays2Image and you'll hit the README's classic 'NoneType' object has no attribute 'shape' because a point array isn't a picture. Points go to other point consumers, or you rasterize them back with a drawing node - you don't preview them directly.

Installing

ComfyUI Manager → search opencv-comfyui → install, or:

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

Restart ComfyUI. No model downloads. The only setup gotcha worth knowing: conflicting OpenCV installs produce Cannot import name 'guidedFilter' from 'cv2.ximgproc' - uninstall the stray one.

The one knob that matters

Epsilon. Too small and you've achieved nothing; too large and your rectangle becomes a triangle. The 0.01 * arcLength rule of thumb is a solid starting point, and it's precisely why arcLength and approxPolyDP live in the same pack - they're designed to be wired together.

Categoryimage/OpenCV

Inputs (4)

NameTypeDefaultDescription
curveNPARRAY
epsilonFLOAT
closedBOOLEAN
approxCurveoptNPARRAY

Outputs (1)

NameTypeDescription
nparrayNPARRAY