Ask Your Question
1

Detection of table tennis balls and color correction

asked 2014-06-12 21:19:45 -0600

vkcelik gravatar image

updated 2015-09-26 13:04:41 -0600

Hello

I am making a robot project and I am trying to detect the tables tennis balls in pictures (from a webcam) like this.

Hej

I have tried different smoothing functions and tried a lot different numbers in the parameters to the functions, but the image (pre-processed) that gives best results look like this.

image description

The camera stand foot got detected as a circle but I removed that result and 4 balls are not found. This is the balls I find at the end.

So far

This is my code:

src = Highgui.imread("Picture 10.jpg",1);
Mat srcH = new Mat();
src.convertTo(srcH, -1, 0.7, 0);
Highgui.imwrite("contrast.jpg", srcH);

Imgproc.cvtColor(srcH, src_gray, Imgproc.COLOR_BGR2GRAY);
Imgproc.equalizeHist(src_gray, src_gray);
Highgui.imwrite("outgray.jpg", src_gray);
Imgproc.GaussianBlur(src_gray, smooth, new Size(11,11),4, 4);
Highgui.imwrite("blur.jpg", smooth);
Imgproc.HoughCircles(smooth, circles, Imgproc.CV_HOUGH_GRADIENT, 2, 20, 81, 29, 10, 13);

System.out.println("Found "+circles.cols() + " circles.");
for (int i = 0; i < circles.cols(); i++) {
    double[] circle = circles.get(0,i);
    if (src.get((int)circle[1], (int)circle[0])[2]>140){
        list.add(new Ball((int)circle[0],(int)circle[1]));
        Point center = new Point((int)circle[0], (int)circle[1]);

        int radius =  (int) circle[2];
        // circle center
        Core.circle( src, center, 3, new Scalar(0,255,0), -1, 8, 0 );
        // circle outline
        Core.circle( src, center, radius, new Scalar(0,0,255), 3, 8, 0 );
     }
}

Do you guys have any ideas about why the last 4 balls are not detected and how the pre-processing can be improved?

I am also having trouble detecting the colored circles on the robot. Sometimes it works, sometimes it doesn't. I think the sunlight affects the detection. I found this color balance technique which is implemented in Matlab (I think) and I have no idea how I would translate that to OpenCV. Any advice on how to translate that would also be appreciated.

edit retag flag offensive close merge delete

Comments

Have you Tried this link? http://opencv-srf.blogspot.in/2010/09/object-detection-using-color-seperation.html Try to make Mask by using HSV Color mask then find contours & Filter those with required contour Area to find the Center of the Ball then Draw Circle with required radius from that Center.

Balaji R gravatar imageBalaji R ( 2014-06-13 08:57:38 -0600 )edit

Thanks for the comment. I will look into it and tell if it works.

vkcelik gravatar imagevkcelik ( 2014-06-13 14:48:58 -0600 )edit

Thanks for the reply. I solved the problem by applying a GuassianBlur, adaptiveThreshold, erode, removed the noise (erode, dilate, dilate, erode) and findContours. For each contour I checked whether the area was in a acceptable interval and used boundingRect to find the center of the circle countour.

vkcelik gravatar imagevkcelik ( 2014-06-15 13:34:10 -0600 )edit

1 answer

Sort by ยป oldest newest most voted
2

answered 2014-06-13 17:36:06 -0600

Witek gravatar image

updated 2014-06-13 17:44:36 -0600

One way would be to use adaptiveThreshold instead of equalizing and blurring. But I don't think it will always work :(

Try this:

adaptiveThreshold(src_gray, bw, 255, 0, 0, 51, -25); 
HoughCircles( bw, circles, CV_HOUGH_GRADIENT, 2, 10, 100, 25, 9, 16 );

Experiment with threshold parameters to find the most robust set.

image description

If this fails too often, I would give up Hough and go for contour analysis.

edit flag offensive delete link more

Comments

Thanks for the reply. As you said in the answer, the method did not work on all my test images. I solved the problem by applying a GuassianBlur, adaptiveThreshold, erode, removed the noise (erode, dilate, dilate, erode) and findContours. For each contour I checked whether the area was in a acceptable interval and used boundingRect to find the center of the circle countour.

vkcelik gravatar imagevkcelik ( 2014-06-15 13:34:00 -0600 )edit

Your approach was exactly what I would have done as Hough is not robust enough for such small objects.

Witek gravatar imageWitek ( 2014-06-16 05:45:11 -0600 )edit

Question Tools

Stats

Asked: 2014-06-12 21:19:45 -0600

Seen: 5,612 times

Last updated: Jun 13 '14