通过屏蔽标记检测到的对象

时间:2017-08-09 11:24:20

标签: python opencv tracking object-detection

我正在关注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()

谢谢!

1 个答案:

答案 0 :(得分:0)

首先,拍摄没有物体的参考的图像,背景相同。然后减去对象所在的图像。它将为您提供一些值,期望零,对象存在的位置。对其执行二进制阈值。现在你可以单独拥有这个对象了。将其用作面具