Problem with HoughCircle() method

439 views
Skip to first unread message

Ben G

unread,
Apr 15, 2012, 7:04:24 PM4/15/12
to android...@googlegroups.com
Hi !

I am beginner in opencv and I try to use HoughCircle() method to detect some circles in Bitmap. But it seems that it doesn't work ...

Here is my code :

                //Grey level
                 bmpSource = ImageUtils.ConvertToGrayscale( bmpSource  );

                //Median filter
bmpSource  = ImageUtils.ApplyMedianFilter( bmpSource  );

                //convert to format RGB 8888
bmpSource  = ImageUtils.JPEGtoRGB888( bmpSource  );
//Get MAT from my bitmap
Mat imgSource = new Mat();
Utils.bitmapToMat( bmpSource  , imgSource); 

                //grey opencv
Imgproc.cvtColor(imgSource, imgSource, Imgproc.COLOR_BGR2GRAY);
Mat imgCirclesOut = new Mat();
                float rayon = (float)( bmpSource.getHeight() / 4) ;

                Imgproc.HoughCircles(imgSource, imgCirclesOut, Imgproc.CV_HOUGH_GRADIENT, 1, 10);//, 150, 75, (int)rayon-10, (int)rayon+10);

                float circle[] = new float[3];

//retrieve circles
float[][] cercles = new float[imgCirclesOut.cols()][3];

                for (int i = 0; i < imgCirclesOut.cols(); i++)
{
    imgCirclesOut.get(0, i, circle);
    cercles[i][0] = circle[2];// rayon
        cercles[i][1] = circle[0];// x center
        cercles[i][2] = circle[1];// y center
}

And imgCirclesOut.cols(); is always empty !

Did I do some mistakes ?

Thanks for help

Andrey Pavlenko

unread,
Apr 18, 2012, 11:05:55 AM4/18/12
to android...@googlegroups.com
are you sure there are circles on your image?

Rui Marques

unread,
Apr 18, 2012, 12:01:15 PM4/18/12
to android...@googlegroups.com
Usually it is a good idea to do some kind of thresholding or Canny before the HoughCircles.

Ben G

unread,
Apr 18, 2012, 4:10:23 PM4/18/12
to android...@googlegroups.com
Ok guys. So I try to detect circles on simple image (red circle on white background) and that detect one circle, good !

But on my picture, it is more complicated (human eye), and I don't detect any circle ...

This is my new code :
Saisissez le code ici
bmpSource = ImageUtils.JPEGtoRGB888(bmpSource );
 
Mat imgSource = new Mat(), imgCirclesOut = new Mat();
 
Utils.bitmapToMat(bmpSource , imgSource);
 
//grey opencv
Imgproc.cvtColor(imgSource, imgSource, Imgproc.COLOR_BGR2GRAY);
 
Imgproc.GaussianBlur( imgSource, imgSource, new Size(9, 9), 2, 2 );
Imgproc.HoughCircles( imgSource, imgCirclesOut, Imgproc.CV_HOUGH_GRADIENT, 1, imgSource.rows()/8, 200, 100, 0, 0 );

 
float circle[] = new float[3];
Log.w("circles", imgCirclesOut.cols()+""); // Renvoi tjrs 0 !!!!!!

 
for (int i = 0; i < imgCirclesOut.cols(); i++)
{
        imgCirclesOut
.get(0, i, circle);

        org
.opencv.core.Point center = new org.opencv.core.Point();
        center
.x = circle[0];
        center
.y = circle[1];
       
Core.circle(imgSource, center, (int) circle[2], new Scalar(255, 255, 0, 255), 4);
}
 
Bitmap bmp = Bitmap.createBitmap(bmpSource.getWidth(), bmpSource.getHeight(), Bitmap.Config.ARGB_8888);
 
Utils.matToBitmap(imgSource, bmp);
...

I should probably used filters before, or even adjust the parameters of Canny filter (200, 100) of this HoughCircle () method.

But because I'm a beginner in image processing domain, your advices on this matter will be welcome :)

Rui Marques

unread,
Apr 18, 2012, 5:11:32 PM4/18/12
to android...@googlegroups.com
Honestly i had more luck with the approaches you find by reading the following stackoverflow topic than using HoughCircle() : 


Reply all
Reply to author
Forward
0 new messages