【发布时间】:2018-01-17 06:19:15
【问题描述】:
我正在关注this 链接,以解决problem of mine。在第一个链接中,它提到,我们可以创建一个“掩码”,如果下一个对象非常靠近那个“掩码”,我们将不会检测到它,因为它可能是同一个对象。但是如果它有距离,我们应该检测它,然后跟踪它直到最后。
但我不知道如何创建掩码并使用条件语句将其排除。所以,第一个问题是创建面具。如果我可以创建对象的掩码,那么我可以尝试将其从下一次检测中排除。
import cv2
import numpy as np
cap = cv2.VideoCapture('anyvideo.mp4')
while(1):
# Take each frame
ret, frame = cap.read()
if ret:
# detect the object and get contour of the image. I'm calling it bbox
tracker = cv2.Tracker_create("KCF")
ok = tracker.init(frame, tuple(bbox))
ok, bbox = tracker.update(frame)
p1 = (int(bbox[0]), int(bbox[1]))
p2 = (int(bbox[0] + bbox[2]), int(bbox[1] + bbox[3]))
cv2.rectangle(frame, p1, p2, (0,0,255))
cv2.putText(frame, 'Tracked', (x + 25,y + 10), cv2.FONT_HERSHEY_SIMPLEX,1, (255,255,255), 2, cv2.LINE_AA)
mask = np.full((frame.shape[0], frame.shape[1]), 0, dtype=np.uint8)
res = res.append(cv2.bitwise_and(frame,frame,mask = mask))
k = cv2.waitKey(5) & 0xFF
if k == 27:
break
else:
break
cap.release()
cv2.destroyAllWindows()
谢谢!
【问题讨论】:
标签: python opencv tracking object-detection