Ask Your Question

Revision history [back]

cv::HoughCircles already applies cv::canny internally, so you apply it on the original image. (Have a look at its param1: http://docs.opencv.org/modules/imgproc/doc/feature_detection.html?highlight=canny#houghcircles

Your canny-parameters also produce many edges. It is good practice to smooth the image a bit before calling canny (e.g. via cv::GaussianBlur). I get a good result with this program:

Mat src = cv::imread("ball.jpg", CV_LOAD_IMAGE_GRAYSCALE);

Mat col = cv::imread("ball.jpg", CV_LOAD_IMAGE_COLOR);

if(src.empty()) {
    cout << "can't open file" << endl;
    return -1;
}

int canny_threshold = 400;

vector<Vec3f> circles;
HoughCircles(src,circles,CV_HOUGH_GRADIENT,
1, // dp
50, // min_dist_px
canny_threshold, // higher canny
20, // acc threshold
0, // min radius
0); // max radius

cout << "found circles " << circles.size() << endl;

for(int i=0;i<circles.size(); i++)
{
    Point center(cvRound(circles[i][0]),cvRound(circles[i][1]));
    int radius = cvRound(circles[i][2]);
    circle(col,center,radius,Scalar(0,0,255),2,8,0);
}

cv::HoughCircles already applies cv::canny internally, so you apply it on the original image. (Have a look at its param1: http://docs.opencv.org/modules/imgproc/doc/feature_detection.html?highlight=canny#houghcircles

Your canny-parameters also produce many edges. It is good practice to smooth the image a bit before calling canny (e.g. via cv::GaussianBlur). cv::GaussianBlur) (I just use a higher threshold). I get a good result with this program:

Mat src = cv::imread("ball.jpg", CV_LOAD_IMAGE_GRAYSCALE);

Mat col = cv::imread("ball.jpg", CV_LOAD_IMAGE_COLOR);

if(src.empty()) {
    cout << "can't open file" << endl;
    return -1;
}

int canny_threshold = 400;

vector<Vec3f> circles;
HoughCircles(src,circles,CV_HOUGH_GRADIENT,
1, // dp
50, // min_dist_px
canny_threshold, // higher canny
20, // acc threshold
0, // min radius
0); // max radius

cout << "found circles " << circles.size() << endl;

for(int i=0;i<circles.size(); i++)
{
    Point center(cvRound(circles[i][0]),cvRound(circles[i][1]));
    int radius = cvRound(circles[i][2]);
    circle(col,center,radius,Scalar(0,0,255),2,8,0);
}

cv::HoughCircles already applies cv::canny internally, so you apply it on the original image. (Have a look at its param1: http://docs.opencv.org/modules/imgproc/doc/feature_detection.html?highlight=canny#houghcircles

Your canny-parameters also produce many edges. It is good practice to smooth the image a bit before calling canny (e.g. via cv::GaussianBlur) (I just use a higher threshold). I get a good result with this program:

Mat src = cv::imread("ball.jpg", CV_LOAD_IMAGE_GRAYSCALE);

Mat col = cv::imread("ball.jpg", CV_LOAD_IMAGE_COLOR);

if(src.empty()) {
    cout << "can't open file" << endl;
    return -1;
}

int canny_threshold = 400;

vector<Vec3f> circles;
HoughCircles(src,circles,CV_HOUGH_GRADIENT,
1, // dp
50, // min_dist_px
canny_threshold, // higher canny
20, // acc threshold
0, // min radius
0); // max radius

cout << "found circles " << circles.size() << endl;

for(int i=0;i<circles.size(); i++)
{
    Point center(cvRound(circles[i][0]),cvRound(circles[i][1]));
    int radius = cvRound(circles[i][2]);
    circle(col,center,radius,Scalar(0,0,255),2,8,0);
}

BTW: Your title is completely misleading. Your problem is not that you can't draw the circle, but that you can't find the right circle to draw.