opencv-python中的运动跟踪

2024-06-28 11:15:30 发布

您现在位置:Python中文网/ 问答频道 /正文

所以我一直在尝试制作一个运动跟踪器来跟踪一只狗在一段视频中的移动(自上而下录制),检索一段显示狗的剪辑视频,忽略其余的背景。在

我首先尝试使用opencv 3中可用的算法(BOOSTING、MIL、KCF、TLD、MEDIANFLOW、GOTURN(返回错误,还不能解决问题))从this link开始尝试了一个基本的运动跟踪算法,但是没有一个能给出好的结果。Link

我更喜欢一个带有预设矩形框的代码,一旦检测到运动区域,就会包围它。类似于这个video

我不太熟悉OPENCV,但我相信单运动跟踪不应该是一个问题,因为已经做了很多工作。我应该考虑其他的库/api,还是有更好的代码/教程来完成这项工作?我的重点是稍后将其用于神经网络(这就是为什么我尝试使用python/opencv解决它)

谢谢你的帮助/建议

编辑:

我删除了以前的代码,使帖子更干净。在

另外,根据我得到的反馈和进一步的研究,我能够修改一些代码,使之接近我想要的结果。然而,我仍然有一个烦人的跟踪问题。似乎第一帧会影响到跟踪的其余部分,因为即使在狗移动之后,它仍然会检测到它的第一个位置。我试图用一个标志将跟踪限制在一个动作上,但是检测结果搞砸了。这是显示结果的代码和图片:

jimport imutils
import time
import cv2

previousFrame = None

def searchForMovement(cnts, frame, min_area):

    text = "Undetected"

    flag = 0

    for c in cnts:
        # if the contour is too small, ignore it
        if cv2.contourArea(c) < min_area:
            continue

        #Use the flag to prevent the detection of other motions in the video
        if flag == 0:
            (x, y, w, h) = cv2.boundingRect(c)

            #print("x y w h")
            #print(x,y,w,h) 
            cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)
            text = "Detected"
            flag = 1

    return frame, text

def trackMotion(ret,frame, gaussian_kernel, sensitivity_value, min_area):


    if ret:

        # Convert to grayscale and blur it for better frame difference
        gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
        gray = cv2.GaussianBlur(gray, (gaussian_kernel, gaussian_kernel), 0)



        global previousFrame

        if previousFrame is None:
            previousFrame = gray
            return frame, "Uninitialized", frame, frame



        frameDiff = cv2.absdiff(previousFrame, gray)
        thresh = cv2.threshold(frameDiff, sensitivity_value, 255, cv2.THRESH_BINARY)[1]

        thresh = cv2.dilate(thresh, None, iterations=2)
        _, cnts, _ = cv2.findContours(thresh.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

        frame, text = searchForMovement(cnts, frame, min_area)
        #previousFrame = gray

    return frame, text, thresh, frameDiff




if __name__ == '__main__':

    video = "Track.avi"
    video0 = "Track.mp4"
    video1= "Ntest1.avi"
    video2= "Ntest2.avi"

    camera = cv2.VideoCapture(video1)
    time.sleep(0.25)
    min_area = 5000 #int(sys.argv[1])

    cv2.namedWindow("Security Camera Feed")


    while camera.isOpened():

        gaussian_kernel = 27
        sensitivity_value = 5
        min_area = 2500

        ret, frame = camera.read()

        #Check if the next camera read is not null
        if ret:
            frame, text, thresh, frameDiff = trackMotion(ret,frame, gaussian_kernel, sensitivity_value, min_area)

        else:
            print("Video Finished")
            break


        cv2.namedWindow('Thresh',cv2.WINDOW_NORMAL)
        cv2.namedWindow('Frame Difference',cv2.WINDOW_NORMAL)
        cv2.namedWindow('Security Camera Feed',cv2.WINDOW_NORMAL)

        cv2.resizeWindow('Thresh', 800,600)
        cv2.resizeWindow('Frame Difference', 800,600)
        cv2.resizeWindow('Security Camera Feed', 800,600)
      # uncomment to see the tresh and framedifference displays                  
        cv2.imshow("Thresh", thresh)
        cv2.imshow("Frame Difference", frameDiff)



        cv2.putText(frame, text, (10, 20), cv2.FONT_HERSHEY_SIMPLEX, 0.5, (0, 0, 255), 2)
        cv2.imshow("Security Camera Feed", frame)

        key = cv2.waitKey(3) & 0xFF
        if key == 27 or key == ord('q'):
            print("Bye")
            break

    camera.release()
cv2.destroyAllWindows()

这张图片显示了第一帧是如何影响帧差结果的,这将迫使方框覆盖区域而不移动。在

Result showing the frame difference and video display

这一个例子显示了当运动被忽略时,不再存在的运动(与视频的第二和第一帧的帧差)被错误地检测到。当我允许多个跟踪时,它会同时跟踪这两个,这仍然是错误的,因为它检测到一个空白区域。在

enter image description here

有人知道代码在哪里出错或缺失吗?我一直在努力,但无法使它正常工作。在

提前谢谢你!!在


Tags: the代码textifareagaussianmincv2