Detection of table tennis balls and color correction
Hello
I am making a robot project and I am trying to detect the tables tennis balls in pictures (from a webcam) like this.
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.
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.
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.
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.
Thanks for the comment. I will look into it and tell if it works.
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.