I need to use a low-resolution (320 x 240) image in OpenCV and find a large exercise ball, either blue or red. The ball is 25 inches wide and is NOT guaranteed to be perfectly circular. I tried to use HoughCircles with a Canny-thresholded image. I had no success. What am I doing wrong and what is the best way to get the size of the ball in pixels and where it is? It'll let me calculate things like how far it is from the camera!
Asked
Active
Viewed 3,092 times
3
-
If you know the ball color, simple color recognition + edge detection + basic math should do the trick here. – Paweł Stawarz Feb 14 '14 at 04:38
-
i'm already filtering the color. now, i need to detect the ball! – yash101 Feb 14 '14 at 04:39
-
And my comment mentioned more things than just filtering the color, didn't it? :) There's an topic on SO about edge recognition: http://stackoverflow.com/questions/11987483/opencvs-canny-edge-detection-in-c and I'm sure you know how to calculate the center of the ball if you have its edges... – Paweł Stawarz Feb 14 '14 at 04:42
-
should i preform an HSV inrange before the Canny? That's what I've been doing. Canny shows up with an outline of the ball, but HoughCircles doen't budge. BTW, I'm using a vector
. I believe that's what is used in the example! – yash101 Feb 14 '14 at 04:47 -
3If you threshold the image, there's no need for using Canny. There's - at least in my opinion - no real reason to use the HoughCircles also. Just get the position of ANY pixel of the ball, then positions of the most left/right/top/bottom ball pixels. Your case sounds simple enough to do it in a manual way. If it's an exercise ball, it's gonna be moving. so you won't get accurate results anyway. Use HoughCircles only when you observe __many__ balls at once. – Paweł Stawarz Feb 14 '14 at 04:55
-
So this is for an FRC vision application. I would like to measure distances so I'll need to use trig functions to do so. Whenever I google something about opencv and circles, I only get info about HoughCircles. I'd like to stray away from findcontours and approxpolydp because the distance measurements are hard to acquire and require hours of calibration. The last time I got it to work, there was just a small box at the center of the ball! – yash101 Feb 14 '14 at 05:03
-
Pawel is giving you some good advice. – Junuxx Feb 14 '14 at 09:55
-
I agree that his advice is good. I actually have already been doing it all in my application. However, I think I'm doing something wrong with my HoughCircles! – yash101 Feb 14 '14 at 18:24
1 Answers
2
Let me collect all the other advice in one answer:
- Use cv::inRange() to get the correct color (More Information on that). You might want to use something like RGB Normalization beforehand to make sure you retreive the complete ball.
- Now you have all the pixel that relate to the ball (and maybe some noise that you have to get rid of). Retrieve the pixels that are farthest apart left/right and top/bottom in your ball (aka your connected Component that has a plausible size) to get the size of the ball. (If the ball doesn't have to be round you probably want to take the bigger value)
- Compute the distance from the camera with the now known size of the ball in the picture. (You have to know the "real" size beforehand for this computation obviously ;))
There obviously are other ways (f.e. use edge detection), but this is imo the easiest.
It is easier to give you an answer if you post an example picture.

Community
- 1
- 1

Sebastian Schmitz
- 1,884
- 3
- 21
- 41
-
https://www.dropbox.com/s/s8cyth1hg3jqe4u/image%20%282%29.jpg That is one image of the ball. I have 1000 more if you need but they are basically the same – yash101 Feb 14 '14 at 23:14
-
I got FindContours to work, but it only highlights a box at the center of the ball. the rest of the ball is just ignored though it shows up in the thresholded HSV image. Is there a way how I can threshold that image into a binary image? It has shades of gray and white, not black and white! – yash101 Feb 16 '14 at 01:50
-
@yash101 did you get it to work? Maybe post a new more specific question, also you could accept my question if you are happy with it. – Sebastian Schmitz May 07 '14 at 09:14
-
Sorry, I forgot about this thread. I got ball detection to work. I did do a threshold and used findContours for the final detection. For the calculations, I used the bounding rectangle, though, now that I think of it, I should have used minEnclosingEllipse! – yash101 May 08 '14 at 14:47