user1708997
user1708997

Reputation: 45

OpenCV houghcircles deteting nothing C++

I am having a fairly annoying issue using OpenCV's HoughCircle method to detect circles in an image. I copied the code found in the official documentation and thus far I have been unable to detect anything. The circles vector has a size of 0 after calling the function, therefore there were no circles detected.

I have tried it with multiple images, such as .ppm images, .jpg and none of which end up with circles being detected. I really have no idea what could be going wrong.

If anyone has any idea of what I should try I'd be extremely thankful.

using namespace cv;

Mat src = imread("Images/balls.jpg");

if(! src.data )            
{
    std::cout <<  "Could not open or find the image" << std::endl ;
    return -1;
}


Mat src_gray2;
cvtColor(src, src_gray2, CV_BGR2GRAY );

GaussianBlur( src_gray2, src_gray2, cv::Size(9, 9), 2, 2 );

vector<Vec3f> circles;

HoughCircles(src_gray2, circles, CV_HOUGH_GRADIENT, 1, src_gray2.rows/8, 200, 100, 0, 0 );

std::cout << circles.size();

Upvotes: 2

Views: 9664

Answers (2)

Algold
Algold

Reputation: 1003

You need to change param2 to a lower value to find more circles. For example in the image posted above in the comments to the question with param2 = 20 I found a circle around the tennis ball.

HoughCircles(src_gray2, circles, CV_HOUGH_GRADIENT, 1, src_gray2.rows/8, 200, 20, 0, 0 );

Upvotes: 4

andy mcevoy
andy mcevoy

Reputation: 398

This works for me. I adjusted the arguments to the HoughCircles function. See below. Also, I used this book to help me out: OpenCV 2

#include <cstdio>
#include <iostream>

#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/features2d/features2d.hpp>


int main(int argc, char** argv) {
    using namespace cv;

    cv::Mat src=cv::imread("JGRiM.jpg"); 

    if (!src.data) {
        std::cout << "ERROR:\topening image" <<std::endl;
        return -1;
    }
    cv::namedWindow("image",CV_WINDOW_AUTOSIZE);
    cv::imshow("image",src);

    Mat src_gray2;
    cvtColor(src, src_gray2, CV_BGR2GRAY );

    GaussianBlur( src_gray2, src_gray2, cv::Size(9, 9), 2, 2 );

    vector<Vec3f> circles;

    HoughCircles(src_gray2, circles, CV_HOUGH_GRADIENT,
          2,   // accumulator resolution (size of the image / 2)
          5,  // minimum distance between two circles
          100, // Canny high threshold
          100, // minimum number of votes
          0, 1000); // min and max radius

    std::cout << circles.size() <<std::endl;
    std::cout << "end of test" << std::endl;

       std::vector<cv::Vec3f>::
              const_iterator itc= circles.begin();

       while (itc!=circles.end()) {

         cv::circle(src_gray2,
            cv::Point((*itc)[0], (*itc)[1]), // circle centre
            (*itc)[2],       // circle radius
            cv::Scalar(255), // color
            2);              // thickness

         ++itc;
       }
        cv::namedWindow("image",CV_WINDOW_AUTOSIZE);
        cv::imshow("image",src_gray2);
        cv::waitKey(0);
    return 0;
}

Upvotes: 5

Related Questions