Nodes/ComfyUI CV/cv2.contourArea
ComfyUI Node

cv2.contourArea

How big is this shape, really

By bmad4ever·Created 3 months ago·Updated 14 days ago· 1
cv2.contourArea
  • contour
  • float
◄orientedfalse►

Why you'd want a number

Because "is this blob worth keeping?" is an area question. A mask cleaned of its speckle, a segment filtered down to the plausible objects, a detected region ranked against its neighbours - all of that is thresholding on size, and cv2.contourArea is the size measurement. It's the arithmetic behind the classic "drop contours smaller than 200 pixels" line in every OpenCV tutorial.

It's a raw wrapper from ComfyUI CV (bmad4ever/comfyui_cv), category image/CV/low-level/cv2 C. It's also a good example of where the raw node and the curated one differ in intent: this one gives you the bare number, and the pack's CV Select Contour / CV Filter Contours / CV Region Properties nodes give you the filtered result.

How it works

It's the shoelace formula on a polygon: sum the cross products of consecutive vertices and halve. Two consequences worth internalising before you compare numbers:

  1. It is not a pixel count. For a smooth or antialiased contour, the polygon area and the number of foreground pixels differ - usually by a percent or two, on thin shapes by much more. If you need exact pixels, count mask pixels instead.
  2. The sign carries orientation when oriented is on. Positive and negative correspond to winding direction, and in image coordinates (y pointing down) that flips the intuition you might have from maths class. The pack's own subgraph docs make the operational point: "Point order matters to contourArea's sign; the blueprint takes the areas as returned, so pass consistently wound contours." Mix a clockwise contour with a counter-clockwise one and your comparison is nonsense.

Inputs and outputs that matter

  • contour - required, and NPARRAY only. A point set, not an image: the author's tooltip says plainly that an IMAGE or MASK link is not accepted here. The expected layout is the classic (N,1,2) or (N,2) vertex array - typically from CV Find Contours → CV Contour To Points, or from any point-set node in this pack.
  • oriented - optional BOOLEAN, default False, and an advanced input so it's collapsed in the UI. Leave it False for a plain unsigned area; turn it on only when you want the winding sign.
  • float - the area. A plain FLOAT output, so it wires into comparisons, switches, and PrimitiveFloat maths without ceremony.

Feeding the result: a comparison against a threshold plus a switch is the obvious use, but the curated nodes do it in one step - CV Filter Contours keeps contours by a measured property range, CV Select Contour picks one by rank (largest by area, for instance). Use this node when you want the number in the graph rather than a filter decision.

Installing the pack

Manager → search ComfyUI CV, or:

cd ComfyUI/custom_nodes
git clone https://github.com/bmad4ever/comfyui_cv

Restart ComfyUI. Python ≥ 3.12 and a V3 node API build are required; dependencies are opencv-contrib-python-headless~=5.0.0.93, numpy and torch.

Where people get burned

  • dtype and shape. Contours out of findContours are int32 (N,1,2). Point clouds you built yourself may be float32 (N,2). Both generally work, but a flat squeezed array or an (N,3) from a homogeneous-coordinate detour will not - reach for CV Reshape Array or cv2.convertPointsFromHomogeneous rather than guessing.
  • Comparing raw areas across differently-shaped pipelines. A contour from a thresholded mask and one you built by hand are different objects. Same source, same winding, same scale, or the numbers don't line up.
  • Assuming a self-intersecting or figure-eight contour has a meaningful area. The polygon formula gives you the signed sum of lobes. Real shapes from real masks are almost never like this, but simulated geometry can be.
  • Expecting it to accept an image. It won't. Turn a mask into contours first (CV Find Contours), or you're looking for the connected-components family instead - same "keep the big blobs" job, different route, and arguably a better one for pure masks.
  • The pack's caveats, in brief, because you should calibrate trust per node. LLM-assisted development is stated openly, along with a real overfitting incident and a "not recommended for production without review" warning, no planned updates, and no community corpus (a Reddit search for the pack returns nothing). Utility maths like the shoelace formula is the safest kind of node in such a pack; the surrounding plumbing is where you'd still verify.
Categoryimage/CV/low-level/cv2 C

Inputs (2)

NameTypeDefaultDescription
contourNPARRAY - - - A data array (points / matrix), NOT an image - only an NPARRAY link is accepted here.
orientedoptBOOLEANfalse - - - Preset to the OpenCV default (False).

Outputs (1)

NameTypeDescription
floatFLOAT—