python - 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 的位置的情况下工作,但这不是我想要的。可能是什么问题呢?
解决方案
检测轮廓后重置帧计数器:
# 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)
推荐阅读
- python - 跟踪一组 IR LED 以找到它们的坐标
- r - 删除以 R 中特定字符结尾的值
- ios - Xcode11 或 Transporter 不擅长“使用应用商店进行身份验证”
- scala - 如何在 Scala LocalDateTime 中进行日期比较?
- c# - 如何在 bot v4 中保存用户状态,以便在整个对话期间保持持久性
- android - 带有包含recyclerview的片段的Android ViewPager2不滚动
- laravel - SQLSTATE [HY000]:一般错误:1364 字段 'user_id' 没有默认值 laravel 6.9.0
- selenium - 在使用 selenium-wire python 捕获网络流量时查看不同格式的日志
- sql - 查找给定周数周期的最接近的未来日期
- google-cloud-platform - 重试后 MQTT 未连接到 Google IOT 核心