使用OpenCV进行球检测

问题描述:

我需要在OpenCV中使用低分辨率(320 x 240)图像,并找到一个大的练习球,蓝色或红色。球是25英寸宽,不保证是完美的圆形。我试图用一个Canny阈值图像使用HoughCircles。我没有成功。我做错了什么是什么是最好的方式来获得球的大小的像素和它在哪里?

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!

让我收集所有其他建议在一个answer:

Let me collect all the other advice in one answer:


  1. 使用 cv :: inRange (有关详情)。您可能需要事先使用 RGB正规化,以确保

  2. 现在你有了所有与球相关的像素(也许有一些噪音,你必须摆脱)。检索在你的球(也就是你的连接组件,具有合理的大小)的左/右和顶/底最远的像素,以获得球的大小。 (如果球不一定是圆的,你可能想要取更大的值)

  3. 计算从相机到现在已知的球的大小的距离。 (你必须事先知道这个计算的真实大小明显;))

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

显然有其他方法