# I can't paint the circle into image after using Hough transform :(

Hi everybody ! I used Hough transform to detect circle and then I will paint the circle into my input image ( tennis ball ) depend on those parameter that Hough issued ( center,radius ). Here is my code :

            Mat src1 = imread("banhtennis.jpg");
Mat src2 = src1.clone();
Mat gray,canny;

cvtColor(src1,gray,CV_BGR2GRAY);
Canny(gray,canny,20,30,3,false);
vector<Vec3f> circles;
for(int i=0;i<circles.size();i++)
{
Point center(cvRound(circles[i][0]),cvRound(circles[i][1]));
}
imshow("Anh sau khi tim thay duong tron",src2);


The result I received after running my program was many red circles that were painted on the input image but not the circle I need. I need the circle cover my ball in this image. Can everyone help me about this problem ? I will attach this image at here C:\fakepath\banhtennis.jpg Thanks very much ^^

edit retag close merge delete

Those params are (0,0) because I don't know the radius of ball, so I put (0,0) into those params to search automatically all circles in image

( 2014-09-10 11:26:39 -0500 )edit

Sort by » oldest newest most voted

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);

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

int canny_threshold = 400;

vector<Vec3f> circles;
1, // dp
50, // min_dist_px
canny_threshold, // higher canny
20, // acc threshold

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

for(int i=0;i<circles.size(); i++)
{
Point center(cvRound(circles[i][0]),cvRound(circles[i][1]));
}


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.

more

@FooBar thanks very much...but can you indicate me how to recognize "canny_threshold" and "acc threshold" ? I think maximum value of threshold is 255

( 2014-09-10 23:36:17 -0500 )edit

I want to write value of "radius" and "center" in Window but I get a mistake that I don't know why. Here is my code : cout <<"The radius of circle" << radius << endl; cout <<"The center of circle" << center <<endl; Can you help me ? @FooBar

( 2014-09-11 00:15:21 -0500 )edit

Which error message do you get? It's important to be as accurate as possible, otherwise others can just guess. Are you using the std:: namespace?

( 2014-09-11 03:04:07 -0500 )edit

i use my codes and your codes in a lot of image. I always get an error when drawing detected circle on the image.

C:\fakepath\error.png

more

@FooBar I got a good result when I used your code, but when I used webcam to track the tennis ball, I got a bad result. There were many wrong circle drew arround this ball but no right circle drew cover this ball. Here is my code : capture >> col; cvtColor(col.clone(),src,CV_BGR2GRAY); equalizeHist(src,src); GaussianBlur( src, src, Size(9, 9), 2, 2 ); if(src.empty()) { cout<< "can't load file from webcam" <<endl; }<="" p="">

        int canny_threshold = 200;

vector<Vec3f> circles;
1, // dp
50, // min_dist_px
canny_threshold, // higher canny
20, // acc threshold

for(int i=0;i<circles.size(); i++)
{
Point center(cvRound(circles[i][0]),cvRound(circles[i][1]));
}

imshow("Window1",col);
char c = waitKey(33);
if(c==27) break;
}


Can you help me about this problem ? I will attach the bad result I got when I run my program at here C:\fakepath\Untitled2.png The original Image C:\fakepath\ball.jpg Have a great day !

more

This is a very different situation. You maybe now also need some kind of color segmentation. How does your original image look like?

( 2014-09-13 06:55:49 -0500 )edit

@FooBar I attached this image in my answer.

( 2014-09-13 10:32:11 -0500 )edit

Official site

GitHub

Wiki

Documentation