0

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.

Yunus Temurlenk
  • 4,085
  • 4
  • 18
  • 39
Chaman Shukla
  • 169
  • 1
  • 5
  • 16
  • 3
    Please post an example image and the code you use to detect the circles. It’ll be useful to anyone wanting to help you improve your detection. – Cris Luengo Feb 18 '20 at 08:16
  • As @CrisLuengo said. Post some code as well. So far it looks like the parameters of Hough are too strict, otherwise it should not be a problem to detect all of them. – Croolman Feb 18 '20 at 09:26
  • ransac circle detection might work but will be some effort to implement it for your specific use case: https://stackoverflow.com/questions/20698613/detect-semicircle-in-opencv/20734263#20734263 – Micka Feb 18 '20 at 10:00

1 Answers1

2

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

Yunus Temurlenk
  • 4,085
  • 4
  • 18
  • 39