首页 > 解决方案 > python opencv - 对象跟踪和坐标

问题描述

我正在使用对象跟踪代码。https://www.pyimagesearch.com/2015/09/21/opencv-track-object-movement/。有几次它工作得很好。它跟踪球很好。然后,打开相机没有问题,但是当我在相机的视野范围内移动网球时,出现以下错误

if counter >= 10 and i == 1 and pts[-10] is not None:IndexError: deque index out of range

dX = pts[-10][0] - pts[i][0]
        dY = pts[-10][1] - pts[i][1]
        (dirX, dirY) = ("", "")

如果我删除if counter >= 10 and i == 1 and pts[-10] is not None:

该代码可以在不显示 x 和 y 的位置的情况下工作,但这不是我想要的。可能是什么问题呢?

标签: pythonopencv

解决方案


检测轮廓后重置帧计数器:

# only proceed if at least one contour was found
if len(cnts) > 0:
    # find the largest contour in the mask, then use
    # it to compute the minimum enclosing circle and
    # centroid
    c = max(cnts, key=cv2.contourArea)
    ((x, y), radius) = cv2.minEnclosingCircle(c)
    M = cv2.moments(c)
    center = (int(M["m10"] / M["m00"]), int(M["m01"] / M["m00"]))


if len(pts) >= 60:
       cap.set(cv2.CAP_PROP_POS_FRAMES, 0)

推荐阅读