Detect/Fitting Circles using Hough Transform in OpenCV 2.4.6

asked 2013-07-28 15:51:27 -0600

Robionic gravatar image

updated 2013-07-28 15:53:21 -0600

Hi All,

The main objective is ti detect the main 5-point white circles in the image.The test image in which the circles have to be detected is the one shown here 640x480. It is a low resolution image, please download the original image here,1280x1024

I am using different methods to bring out a evaluation of various circle/ellipse detection methods. But somehow I am not able to fix my simple Hough transform code. It does not detect any circles. I am not clear whether the problem is with pre-processing step, or the parameters of the HoughCircle. This is my code. Please help me in this regards,

Header file


// OpenCV Library
#include <opencv2\opencv.hpp>
#include <iostream>

using namespace cv;
using namespace std;

class ImgProcessor{
    Mat OpImg ;

    //aquire filter methods to image
    int  Do_Hough(Mat IpImg);

#endif /* ImgProcessor_H */

Source file

#include "ImgProcessor.h"
#include <opencv2\opencv.hpp>
#include "opencv2\imgproc\imgproc.hpp"
#include "opencv2\imgproc\imgproc_c.h"
#include <vector>

using namespace cv;


//Apply filtering for the input image
int ImgProcessor::Do_Hough(Mat IpImg)

    //Parameter Initialization________________________________________________________
    double sigma_x, sigma_y, thresh=250, max_thresh = 255;
    int ksize_w = 5 ;
    int ksize_h = 5;
    sigma_x = 0.3*((ksize_w-1)*0.5 - 1) + 0.8 ;
    sigma_y = 0.3*((ksize_h-1)*0.5 - 1) + 0.8 ;

    vector<Vec3f> circles;

    //Read the image as a matrix
    Mat TempImg;
    //resize(IpImg, IpImg ,Size(), 0.5,0.5, INTER_AREA);


    //Perform initial smoothing
    GaussianBlur( IpImg, TempImg, Size(ksize_w, ksize_h),2,2);

    //perform thresholding
    threshold(TempImg,TempImg, thresh,thresh, 0);

    //Remove noise by gaussian smoothing
    GaussianBlur( TempImg, TempImg, Size(ksize_w, ksize_h),2,2);
    /*imshow("Noisefree Image", TempImg);

    //Obtain edges
    Canny(TempImg, TempImg, 255,240 , 3);
    imshow("See Edges", TempImg);

    //Increase the line thickness

    //Hough Circle Method______________________________________________________________

    // Apply the Hough Transform to find the circles
    HoughCircles( TempImg, circles, 3, 1, TempImg.rows/32, 255, 240, 5, 0 );
    // Draw the circles detected
    for( size_t i = 0; i < circles.size(); i++ )
         Point center(cvRound(circles[i][0]), cvRound(circles[i][1]));
         int radius = cvRound(circles[i][2]);
         // circle center
         circle( IpImg, center, 3, Scalar(0,255,0), -1, 8, 0 );
         // circle outline
         circle( IpImg, center, radius, Scalar(0,0,255), 3, 8, 0 );

   // Show your results
    namedWindow( "Hough Circle Transform", WINDOW_AUTOSIZE );
    imshow( "Hough Circle Transform", IpImg );

   // waitKey(0);
   return 0;   


int main(int argc, char** argv)
    ImgProcessor Iclass;
    //char* imageName = argv[1];
    string imageName = "D:/Projects/test_2707/test_2707/1.bmp";
    Mat IpImg = imread( imageName );
    cvtColor(IpImg, IpImg,6,CV_8UC1);
    return 0;
edit retag flag offensive close merge delete


You have to play with the parameters. But I don't succeed to have something relevant for your detection... maybe it's not the right approach...

Mathieu Barnachon gravatar imageMathieu Barnachon ( 2013-07-28 18:25:49 -0600 )edit

I too think so.. I am actually also trying out various other methods like moments based, contours and fitellipse based techniques.. But I still do not understand, why does this method doesn't work for this specific image.. I will try for some more time, and skip to the next method..

Robionic gravatar imageRobionic ( 2013-07-29 03:09:27 -0600 )edit

Try first segmenting your image based on the points intensities. Then apply houghcircles on that binary image.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-07-29 03:50:08 -0600 )edit

@StevenPuttemans Can you please explain it in a more detailed manner.. I would be greatful..

Robionic gravatar imageRobionic ( 2013-07-29 04:33:15 -0600 )edit

Excuse me, it seems you were already working an a binary edge image:

 //perform thresholding
 threshold(TempImg,TempImg, thresh,thresh, 0);

However, you might want to visualize your images in every step, in order to be sure that all processing goes well. So after thresholding, after edge detection, ... this will give a more clear insight in your problem.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-07-29 04:41:56 -0600 )edit

Yes.. it is a binary image I am working upon...and I had observed the images at every step of the preprocessing.. The Canny edges can be seen here for your further examination, cannyedge Do I need to dilate them further ? Will it be of any help ?

Robionic gravatar imageRobionic ( 2013-07-29 05:01:02 -0600 )edit

Hmm I guess it is indeed a parameter search problem if you get there. However what you could do is the following maybe: apply findContours on the binary image. Use the information of the boundingbox of the contours to define the largest area elements. Then only remain the largest ones. You have a clean edge seperation in your sample image, so contours should work properly also.

StevenPuttemans gravatar imageStevenPuttemans ( 2013-07-29 05:08:08 -0600 )edit