First time here? Check out the FAQ!

Ask Your Question
0

Extracting circle centre coordinates using cv2.HoughCircles

asked Jun 9 '16

YaddyVirus gravatar image

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

img = cv2.imread('C:/Users/Yardy/img1.png')
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?

Preview: (hide)

Comments

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

YaddyVirus gravatar imageYaddyVirus (Jun 15 '16)edit

1 answer

Sort by » oldest newest most voted
0

answered Jan 18 '18

popo gravatar image

this is a java solution

Preview: (hide)

Comments

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[0]), Math.round(vCircle[1]));
            int radius = (int)Math.round(vCircle[2]);

            //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
popo gravatar imagepopo (Jan 18 '18)edit

Question Tools

1 follower

Stats

Asked: Jun 9 '16

Seen: 3,364 times

Last updated: Jun 09 '16