Ask Your Question
0

Extracting circle centre coordinates using cv2.HoughCircles

asked 2016-06-09 02:50:02 -0600

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?

edit retag flag offensive close merge delete

Comments

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

YaddyVirus gravatar imageYaddyVirus ( 2016-06-15 15:40:15 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
0

answered 2018-01-18 04:49:56 -0600

popo gravatar image

this is a java solution

edit flag offensive delete link more

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 ( 2018-01-18 04:51:22 -0600 )edit

Question Tools

1 follower

Stats

Asked: 2016-06-09 02:50:02 -0600

Seen: 3,228 times

Last updated: Jun 09 '16