Chaman Shukla
Chaman Shukla

Reputation: 169

Circle detection in a Image

I am trying to use HoughCircles method to detect a circle from an Image, but it looks like that this method is not useful to detect all circle with almost the same centres. For example, if I have 3 circles with almost the same centre it detects as a single circle. Please suggest if there is any way around to find all circles. Here is the source image:

Please check image

I might be wrong with HoughCircle Methos assumption.

Thanks in advance.

Upvotes: 0

Views: 277

Answers (1)

Yunus Temurlenk
Yunus Temurlenk

Reputation: 4352

The reason is that when you call HoughCircles you should decide the minimum distance between the detected circle centerces. The same center you mentioned means that zero distance between them. So in this case you should set the minimum distance parameter almost 0.

void cv::HoughCircles   (   InputArray  image,
OutputArray     circles,
int     method,
double  dp,
double  minDist, // You should set this parameter almost zero cos 0 not accepted.
double  param1 = 100,
double  param2 = 100,
int     minRadius = 0,
int     maxRadius = 0 
)

When I tried with these parameters:

  HoughCircles( input, output, CV_HOUGH_GRADIENT, 1, 0.5, 60, 30, 1, 200 );

I get this:

enter image description here

Edit: When I tried some more playing on this, I got 12 circles but the reality is 18 circles(edge circles not included). The reason could be about the image quality. Here is my code and result:

#include "opencv2/highgui/highgui.hpp"
#include "opencv2/imgproc/imgproc.hpp"
#include <iostream>
#include <stdio.h>
#include <stdlib.h>

using namespace cv;
using namespace std;

int main()
{
    /// Load source image and convert it to gray
    Mat src_gray,dst,src = imread("/ur/source/image/image.jpg", 1 );
    imshow("Source",src);
    int i = 50;
    bilateralFilter(src,dst,i,i*2,i/2);

    imshow("Output",dst);

    cvtColor( dst, src_gray, CV_BGR2GRAY );

    vector<Vec3f> circles;

    /// Apply the Hough Transform to find the circles
    HoughCircles( src_gray, circles, CV_HOUGH_GRADIENT, 1, 0.01, 80, 55, 0, 100 );

    Mat zero_mask = Mat::zeros(src.rows,src.cols,CV_8UC3);
    /// 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( zero_mask, center, 3, Scalar(0,255,0), -1, 8, 0 );
        // circle outline
        circle( zero_mask, center, radius, Scalar(0,0,255), 1, 8, 0 );
    }

    cout<<circles.size()<<endl;
    imshow("Output2",src_gray);
    imshow("outt",zero_mask);
    waitKey(0);
    return(0);
}

Output:

enter image description here

Upvotes: 2

Related Questions