Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Ball detection with OpenCV

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!

like image 753
yash101 Avatar asked Dec 01 '25 03:12

yash101


1 Answers

Let me collect all the other advice in one answer:

  1. 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.
  2. 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)
  3. 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.

like image 183
Sebastian Schmitz Avatar answered Dec 02 '25 19:12

Sebastian Schmitz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!