Search code examples
c++opencvhough-transformthreshold

How can I apply the hough circle method to a thresholded image?


I currently have a program that can detect colored balls with openCV and I'd like to add the HoughCircle method to make it better.

I had the idea to apply the HoughCircle method on the thresholded image that i got from the inRange method but unfortunately it does not work. I saw that the HoughCircle method is only taking gray-scale images, is there a way to pass the thresholded image to it ?

this is how I do :

int isBall(Mat threshold){
    Mat temp;
    threshold.copyTo(temp);
    std::vector<Vec3f> circles;
    HoughCircles(temp, circles, CV_HOUGH_GRADIENT, 1, temp.rows/8 
                                                    , 100
                                                    , 50 
                                                    , 15 /* min radius */
                                                    , 200 /* max radius */
                                                    );
    printf("nb circles = %d\n", circles.size());
    if(circles.size() > 0){
        return 1;
    }
    return 0;

}

the thresholded image is coming from :

inRange(HSV,Scalar(H_MIN,S_MIN,V_MIN),Scalar(H_MAX,S_MAX,V_MAX),threshold);

Here is an example of a thresholded image that i can get : https://www.noelshack.com/2019-39-5-1569586501-threshold.png

Thanks in advance for your help.


Solution

  • The problem you are facing is related to the parameters used in HoughCircles. You can use the example image just fine if you convert it to a gray-scale one.

    The main culprit is param2=50, that is a threshold for circle centers at detection stage. If you set it to a smaller value, the algorithm will be able to return the circle.

    I have tested HoughCircles() using param2=10 and the result is this:

    enter image description here