Nodes/opencv-comfyui/OpenCV HoughCircles_0
ComfyUI Node

OpenCV HoughCircles_0

Finding every coin in the photo, the OpenCV way

By geroldmeisinger·Created about a year ago·Updated about a year ago· 35
OpenCV HoughCircles_0
  • image
  • circles
  • nparray
method
dp
minDist
param1
param2
minRadius
maxRadius

HoughCircles_0 detects circles in an image and hands you back their centers and radii as a plain array. It's the classic computer-vision move for "find the coins / the pupil / the ball" - no ML, no training, just geometry voting. If your workflow needs to locate round things and you're already comfortable converting between Comfy images and OpenCV nparrays, this is the node.

How it works

The Hough circle transform works on the edges of an image. Every edge point votes for the circles that could pass through it (center at a distance r, at some angle), and the accumulator counts how many votes each candidate center receives. Real circles gather a strong consensus; noise doesn't. It's the same family of algorithm as the Hough line nodes in this pack, just voting in 3D space (x, y, radius) instead of 2D.

Important: OpenCV's HoughCircles takes a grayscale, 8-bit image and does its own Canny edge detection internally via param1. You can't feed it the 3-channel BGR nparray from Image2Nparray - convert with cvtColor first (code 6 for BGR2GRAY). Skipping that is the most common error, and it's exactly what the pack's README warns about with img.type() == CV_8UC1.

The inputs that matter

  • image (NPARRAY) - grayscale.
  • method (INT) - the detection method; 3 is HOUGH_GRADIENT, the one you want.
  • dp (FLOAT) - inverse accumulator resolution. 1.0 = same resolution as the input; 2.0 = half, faster but coarser. Start at 1.01.2.
  • minDist (FLOAT) - minimum distance between detected circle centers, in pixels. Too small and you get duplicate circles; scale it to your image.
  • param1 (FLOAT) - the higher Canny threshold for the internal edge detection.
  • param2 (FLOAT) - the accumulator threshold for center detection. Lower = more circles, more false positives. This is your main "sensitivity" knob.
  • minRadius, maxRadius (INT) - radius bounds in pixels. Narrowing these is the single best move for cutting false positives.

The optional circles out-parameter exists because the generator exposes OpenCV's in-out buffers, but you can leave it disconnected - the output you actually want is nparray, a shape (N, 1, 3) array where each row is [x, y, radius] in pixels.

What you get and what you don't

You get coordinates, not a picture. This is where the pack's sharp edges show: there's no built-in "draw the detected circles" convenience node (the README's todo list literally names "convert Hough lines to image" as unfinished). To visualize, you'd take the nparray and draw with the pack's own circle_0 node. That's a few extra nodes of plumbing, but it's all in-pack.

Install

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

or "OpenCV" via ComfyUI Manager. Then pip install opencv-contrib-python. No models.

Gotchas

Circle detection is fiddly: param2 too low floods you with false circles, minDist too small gives duplicates, and your radius bounds need to actually match the image scale. Start conservative (tight bounds, high param2) and loosen up. And remember the grayscale requirement - if you get an assertion error, the fix is cvtColor with code 6, not a different HoughCircles setting.

Categoryimage/OpenCV

Inputs (9)

NameTypeDefaultDescription
imageNPARRAY
methodINT
dpFLOAT
minDistFLOAT
param1FLOAT
param2FLOAT
minRadiusINT
maxRadiusINT
circlesoptNPARRAY

Outputs (1)

NameTypeDescription
nparrayNPARRAY