Nodes/opencv-comfyui/OpenCV convexityDefects_0
ComfyUI Node

OpenCV convexityDefects_0

The finger-valley detector, now in your graph

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV convexityDefects_0
  • contour
  • convexhull
  • convexityDefects
  • nparray

convexityDefects_0 finds the dents - the places where a contour dips inward away from its convex hull. It's the geometric counterpart to convexHull_0: the hull is the rubber band pulled tight around a shape, and the defects are the gaps between the rubber band and the actual outline. It's the classic primitive behind finger-counting and hand-gesture recognition - each gap between fingers is a defect, so counting them counts fingers. That's the demo every OpenCV tutorial ends with, and this node brings it into ComfyUI.

The inputs are contour (NPARRAY) and convexhull (NPARRAY). Here's the subtlety that bites everyone the first time: the convexhull input must be the index form of the hull, not the points form. So the pipeline is findContours_0convexHull_0 with returnPoints set to FalseconvexityDefects_0. If you feed it a points-form hull, OpenCV will throw an assertion error and you'll sit there wondering why, because nothing in the UI tells you.

The output is an NPARRAY of shape (N, 1, 4), where each row is [start_point_index, end_point_index, farthest_point_index, depth]. The last column - depth - is how deep the defect cuts, i.e. how far the contour sags from the hull. In hand-tracking terms, a real finger gap has a meaningful depth and tiny noise dimples don't; thresholding on depth is how you separate actual fingers from contour wobble. And the farthest-point index tells you where the bottom of the valley is, which is where the fingertip-camera people aim next.

Is this useful in ComfyUI?

Honestly, it's niche - hand-gesture counting inside a diffusion graph isn't most people's workflow. But if you're building an interactive or automation pipeline - say, using the "detect, mask, refine" loop from the detection docs to find where a hand is and then do something with the fingers - this is the missing piece. It's also just a nice demonstration of what the opencv-comfyui pack can do beyond pixel filters: real geometry, fully wired into the graph.

Install and troubleshooting

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

Or ComfyUI Manager → opencv-comfyui → restart.

  • Feed it the index hull. returnPoints = False on convexHull_0 first, or you'll get a shape assertion error. This is the number one failure mode for this node.
  • Contours and hulls are point arrays, not images - wire nparray outputs around, and convert real images with Image2Nparray (batch 1 only) at the start of the chain.
  • convexityDefects_0 and _1 are the identical overload pair. Either works; the suffix is just auto-generation noise.
Categoryimage/OpenCV

Inputs (3)

NameTypeDefaultDescription
contourNPARRAY
convexhullNPARRAY
convexityDefectsoptNPARRAY

Outputs (1)

NameTypeDescription
nparrayNPARRAY