Nodes/opencv-comfyui/OpenCV contourArea_0
ComfyUI Node

OpenCV contourArea_0

ContourArea without leaving the graph

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV contourArea_0
  • contour
  • float
oriented

Once you start automating image fixing instead of hand-painting masks, the first question you ask about every detected region is "how big is it, actually?" That's the whole job of contourArea_0. Feed it a contour - a closed polygon of points, usually from findContours_0 - and it hands you the area as a plain FLOAT you can wire into a comparison, a counter, or a threshold that decides whether a region is worth detailing at all.

This is a raw cv2.contourArea call wrapped in a node. It's part of geroldmeisinger/opencv-comfyui, an auto-generated pack that turns hundreds of top-level OpenCV functions into ComfyUI nodes. The author says it plainly in the README: these are generated from type definitions, so they're "ugly and complex to use. Expect dragons!" What you get is a thin, honest wrapper around the real thing - no magic, no retraining, just OpenCV math on your nparrays.

How it works

The node takes a contour (NPARRAY) and an oriented boolean, calls cv2.contourArea, and returns one float output. That's the entire mechanism - check the node source and it's literally apply_function(cv2.contourArea, [contour, oriented], [0], []).

The only setting you'll touch is oriented. Leave it off (False) and you get the plain geometric area, always positive. Flip it True and the sign tells you winding order: positive area for counterclockwise contours, negative for clockwise. That's a niche trick mostly used to sanity-check how findContours traced a shape; beginners can leave it False and never miss it.

A word on the input, because this trips people up: a contour is not an image. It's a (N, 1, 2) array of x/y points, usually the nparray_0 output of a findContours_0 node (also in this pack). If you wire a plain image in here you'll get an assertion error, not a number.

The two inputs that matter

  • contour - your point array from findContours. This is the one that matters.
  • oriented - signed area or not. Leave False.

That's it. The FLOAT output feeds anything: a PrimitiveInt-style compare, a math node, or a Threshold gate that says "if this region is under 500 px², skip it."

Why you'd want that: the detailing loop in ComfyUI (the SEGS/detector dance covered in modidex's detection docs) produces all kinds of junk regions, and area is the cheapest way to filter noise out of a detection pass before you waste sampling time on it.

How to install

Via ComfyUI Manager, search for opencv-comfyui and install, or clone it:

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

Then make sure OpenCV is present - the pack's own requirements.txt wants opencv-contrib-python, numpy, torch:

pip install opencv-python-contrib

Restart ComfyUI. No model files to download; this is all CPU math.

Troubleshooting

The big one: OpenCV works on numpy nparrays, not on Comfy's IMAGE tensors. You must route an image through Image2Nparray first (batch size 1 only - use ImageFromBatch with length=1 if it complains), do your contour math, and only convert back to IMAGE via Nparrays2Image when you're done. If contourArea errors with an assertion about array shape, you didn't feed it a contour. And if you hit the Cannot import name 'guidedFilter' from 'cv2.ximgproc' wall on install, that's the classic conflicting-OpenCV-packages problem - the README links the fix, and it's almost always one environment fighting another.

One honest caveat: contourArea_0 and contourArea_1 are two numbered overloads of the same OpenCV function (in the type definitions one takes MatLike, the other UMat), and the generated nodes came out byte-identical. Pick either; they behave the same.

Categoryimage/OpenCV

Inputs (2)

NameTypeDefaultDescription
contourNPARRAY
orientedBOOLEAN

Outputs (1)

NameTypeDescription
floatFLOAT