我正在尝试创建一个简单的程序来检测球在图像中的位置。问题是我从网上找到的任何东西都得到了一些奇怪的输出。我一直主要使用 pyimagesearch 的 adrian 提供的示例。链接如下 www.pyimagesearch.com/2015/09/14/ball-tracking-with-opencv/
这是 这是另一个 .
我不知道该尝试什么。到目前为止,我已经尝试编辑程序来绘制所有轮廓,只是为了查看是否检测到球。我看着规范化图像,但这似乎没有任何帮助。任何帮助,将不胜感激。
按照建议,我尝试了一种非基于颜色的方法。 Hough Circles 最终为我工作。它只有大约 66% 的时间看到球,但其他 33% 的时间它只是看不到任何改进(因为我正在使用它生成的数据,我不能有误报)。下面是霍夫圆的魔力。
while True:
# grab the current frame
(grabbed, frame) = camera.read()
frame = cv2.flip(frame, -1)
frame = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
frame = cv2.GaussianBlur(frame, (9,9), 2)
circles = cv2.HoughCircles(frame, cv2.cv.CV_HOUGH_GRADIENT, 1, 100, 50, 10)
if circles is not None:
# convert the (x, y) coordinates and radius of the circles to integers
circles = np.round(circles[0, :]).astype("int")
# loop over the (x, y) coordinates and radius of the circles
for (x, y, r) in circles:
# draw the circle in the output image, then draw a rectangle
# corresponding to the center of the circle
cv2.circle(frame, (x, y), r, (0, 255, 0), 4)
cv2.rectangle(frame, (x - 5, y - 5), (x + 5, y + 5), (0, 128, 255), -1)
cv2.imshow("Frame", frame)
最佳答案
写出您的蒙版图像的副本。我会说图像顶部的毯子(?)与您的颜色范围重叠,因此它将其用作检测球位置的数据的一部分。这似乎与目标的大小和位置有关。
关于python - 使用 opencv 识别球时遇到问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36347152/