Ask Your Question
0

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

asked 2014-09-10 10:56:22 -0600

nhat.truong gravatar image

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;
    HoughCircles(canny,circles,CV_HOUGH_GRADIENT,1,100,30,20,0,0);
    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(src2,center,radius,Scalar(0,0,255),2,8,0);
    }
            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 flag offensive close merge delete

Comments

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

nhat.truong gravatar imagenhat.truong ( 2014-09-10 11:26:39 -0600 )edit

3 answers

Sort by ยป oldest newest most voted
1

answered 2014-09-10 12:09:23 -0600

updated 2014-09-10 12:27:05 -0600

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.

edit flag offensive delete link more

Comments

@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

nhat.truong gravatar imagenhat.truong ( 2014-09-10 23:36:17 -0600 )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

nhat.truong gravatar imagenhat.truong ( 2014-09-11 00:15:21 -0600 )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?

FooBar gravatar imageFooBar ( 2014-09-11 03:04:07 -0600 )edit
0

answered 2015-04-28 22:32:57 -0600

Pinpin gravatar image

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

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(original,center,radius,scalar(0,0,255),2,8,0);="" }<="" p="">

C:\fakepath\error.png

edit flag offensive delete link more
0

answered 2014-09-12 11:15:24 -0600

nhat.truong gravatar image

updated 2014-09-13 10:30:57 -0600

@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;
        HoughCircles(src,circles,CV_HOUGH_GRADIENT,
            1, // dp
            50, // min_dist_px
            canny_threshold, // higher canny
            20, // acc threshold
            0, // min radius
            100); // max radius

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

        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 !

edit flag offensive delete link more

Comments

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

FooBar gravatar imageFooBar ( 2014-09-13 06:55:49 -0600 )edit

@FooBar I attached this image in my answer.

nhat.truong gravatar imagenhat.truong ( 2014-09-13 10:32:11 -0600 )edit

Question Tools

Stats

Asked: 2014-09-10 10:56:22 -0600

Seen: 643 times

Last updated: Sep 13 '14