Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Detecting Hough circles android

I am trying to detect circles using android. I succeeded to implement the detect lines algorithm but nothing gets displayed when trying the draw hough circles algoritm.

Here is my code:

Mat thresholdImage = new Mat(getFrameHeight() + getFrameHeight() / 2, getFrameWidth(), CvType.CV_8UC1);
            mYuv.put(0, 0, data);
            Imgproc.cvtColor(mYuv, destination, Imgproc.COLOR_YUV420sp2RGB, 4);
            Imgproc.cvtColor(destination, thresholdImage, Imgproc.COLOR_RGB2GRAY, 4);
            Imgproc.GaussianBlur(thresholdImage, thresholdImage, new Size(9, 9), 2, 2 );

        Mat circles = new Mat();


        Imgproc.HoughCircles(thresholdImage, circles, Imgproc.CV_HOUGH_GRADIENT, 1d, (double)thresholdImage.height()/70, 200d, 100d);

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

                Point center=new Point(Math.round(vCircle[0]), Math.round(vCircle[1]));
                int radius = (int)Math.round(vCircle[2]);
                // draw the circle center
                Core.circle(destination, center, 3,new Scalar(0,255,0), -1, 8, 0 );
                // draw the circle outline
                Core.circle( destination, center, radius, new Scalar(0,0,255), 3, 8, 0 );

        }
like image 839
Alin Turcu Avatar asked Feb 25 '12 15:02

Alin Turcu


People also ask

How does Hough circle detection work?

The circle Hough Transform (CHT) is a basic feature extraction technique used in digital image processing for detecting circles in imperfect images. The circle candidates are produced by “voting” in the Hough parameter space and then selecting local maxima in an accumulator matrix.

What algorithm is used to detect circles?

Automatic circle detection is an important element of many image processing algorithms. Traditionally the Hough transform has been used to find circular objects in images but more modern approaches that make use of heuristic optimisation techniques have been developed.

How do I find circles in OpenCV?

Use the OpenCV function HoughCircles() to detect circles in an image.


1 Answers

You may have got this sorted by now, but a few things. I'd check your circles mat actually has some results; sometimes vCircle seems to come back null; try one of the other versions of HoughCircles:

iCannyUpperThreshold = 100;
iMinRadius = 20;
iMaxRadius = 400;
iAccumulator = 300;

Imgproc.HoughCircles(thresholdImage, circles, Imgproc.CV_HOUGH_GRADIENT, 
         2.0, thresholdImage.rows() / 8, iCannyUpperThreshold, iAccumulator, 
         iMinRadius, iMaxRadius);

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]);

        // draw the found circle
        Core.circle(destination, pt, radius, new Scalar(0,255,0), iLineThickness);
        Core.circle(destination, pt, 3, new Scalar(0,0,255), iLineThickness);
        }

(I swapped your code into mine, renamed some stuff and swapped it back, I think I've got it back so it works...)

B.

like image 136
Barry Avatar answered Sep 28 '22 08:09

Barry