# Extracting circle centre coordinates using cv2.HoughCircles

Hi there!

I want to extract centre coordinates of detected circles using cv2. HoughCircles, here's the code-

import cv2
import numpy as np
def f(x): return

cimg = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
edges = cv2.Canny(cimg,50,150,apertureSize=3)
circles = cv2.HoughCircles(edges, cv2.HOUGH_GRADIENT, 1.2, 200,param2=30)

if circles is not None:
circles = np.round(circles[0, :]).astype("int")
X = circles

print X


When I run this code, in X I get the centre coordinates as well as the radius of the circle. How do I store the coordinates and radius in separate variables?

Also if my code detects more than one circle, I get an error -

Need more than one value to repack.

Can anybody help me?

edit retag close merge delete

@jmbapps Didn't get a word of that... can you please elaborate? I think that C

Sort by » oldest newest most voted

this is a java solution

more

Mat circles = new Mat();

    Imgproc.HoughCircles(turned, circles, Imgproc.CV_HOUGH_GRADIENT, 2, turned.height() / 4,
100, 70, 0, 0);

Mat circleroi = new Mat();

if (circles.cols() > 0)
for (int x = 0; x < circles.cols(); x++)
{
double vCircle[] = circles.get(0,x);

if (vCircle == null)
break;

Point pt = new Point(Math.round(vCircle), Math.round(vCircle));

//inner most circle center
Imgproc.circle(img, pt, 3, new Scalar(255, 255, 255), 5);
//inner most outer circle
Imgproc.circle(img, pt, radius, new Scalar(255, 255, 2


Official site

GitHub

Wiki

Documentation