Nodes/ComfyUI CV/CV Select Contour
ComfyUI Node

CV Select Contour

Stop filtering by area, just ask for the biggest one

By bmad4ever·Created 4 months ago·Updated 15 days ago· 1
CV Select Contour
  • contours
  • contour
  • found
◄index0►

CV Find Contours hands you a set. Nine times out of ten you want one of them. CV Select Contour is the pick-one node - and because Find Contours emits its set sorted by area, largest first, the picking is as simple as typing an index.

How it works

Index 0 is the biggest blob. Index 2 is the third biggest. There's no area maths, no threshold, no "keep contours between X and Y pixels" filter to tune - you select by rank in the incoming order and the ordering does the work. The tooltip is explicit that the rank is "in the incoming order", and separately that Find Contours sorts by area, so if you feed contours from some other source (a filter node, a merge node) the rank means whatever that node's order is. Keep that in mind: index 0 is "the largest" only so long as nothing upstream reordered them.

The output is still a CV_CONTOURS set - a one-contour set, not a bare contour. That's a deliberate choice, and it's what makes the node composable: every other contour node in the pack takes a set, so the result of a selection drops straight into them without an adapter.

What you actually set

Two required inputs, and that's the whole node:

  • contours - from CV Find Contours.
  • index - INT, default 0, range 0 to 100000. 0 = biggest.

Outputs are contour (the one-contour set, empty if nothing matched) and found (BOOLEAN). The found flag is the useful half: an index past the end of the set returns an empty set and found = false instead of raising, so a graph that processes a batch of frames where some frames have no blobs keeps running. This is a house pattern across the whole pack - you'll see found on the pose solvers, the face nodes, the component nodes, the homography nodes. Branch on it with a control-flow node rather than letting an empty contour wander into a distance measure.

Where it goes in a graph

Three destinations, all common. Into CV ShapeMoments to get centroid, area, orientation and the Hu signature for one shape. Into CV ShapeDistance as the reference shape when you're comparing "this contour against the canonical one" - the distance node takes a whole set plus an index, so you can skip this node entirely there, but feeding it a one-contour set makes the graph read better. Or into CV Draw Contours for the preview that tells you whether index 0 really was the thing you meant.

If the answer to "which one" is more complicated than a rank - aspect ratio, solidity, mirroring - that's CV Filter Contours By Shape, which takes a reference contour and applies a policy. CV Select Contour selects; the filter node judges.

Install

ComfyUI Manager → ComfyUI CV → install → restart. Manual:

cd ComfyUI/custom_nodes
git clone https://github.com/bmad4ever/comfyui_cv
pip install "opencv-contrib-python-headless~=5.0.0.93"

Python ≥ 3.12 and a recent ComfyUI on the V3 node API. No model files.

Common issues

  • Index 0 is a giant blob of background. The classic: your threshold made the background foreground. Largest-blob selection fails loudly and usefully - draw the contour and you'll see it immediately. Fix the mask, not the index.
  • Index 1 and 2 are noise. Contour sets are full of one-pixel specks that outrank nothing but still occupy slots. If you're indexing past 0, filter the specks first (CV Filter Contours has an area floor) or you're indexing into a lottery.
  • Empty contour downstream and no error. That's found = false. Wire the flag somewhere visible if you're debugging a silent pipeline.
  • The whole image/CV category is gone after a pip install. A non-contrib OpenCV wheel landed in the shared site-packages/cv2 and wiped the contrib submodules. tools/repair_opencv_contrib.py --check then --apply.
Categoryimage/CV/contours

Inputs (2)

NameTypeDefaultDescription
contoursCV_CONTOURSFrom 'CV Find Contours'.
indexINT00–100000Rank in the incoming order (Find Contours sorts by area, largest first - 0 = biggest).

Outputs (2)

NameTypeDescription
contourCV_CONTOURSA 1-contour set (empty if not found).
foundBOOLEAN—