运动追踪器正在检测整个屏幕

时间:2018-10-29 04:17:17

标签: python opencv motion

我正在尝试进行基本的运动跟踪,以便稍后在raspberrypi / arduino项目中使用。我还不太了解python,但是我可以完全围绕发生的事情的逻辑进行思考。我一直在使用一些示例来尝试使其与笔记本电脑的内置摄像头一起使用,但是即使在第一帧之外,它似乎仍在跟踪整个图像。我的猜测是,低分辨率(640x480)和帧速率(6 fps)会引起抖动,而这些帧与抖动之间的差异正是它试图跟踪的。从我阅读的内容来看,高斯模糊应该解决这个问题,但事实并非如此。该代码似乎可以编译,我可以看到在多个窗口中进行了多种处理,并且进行了一些运动检测,但是它非常不一致,因此无法解决问题。

import cv2,time

first_frame = None

video = cv2.VideoCapture(0)

a = 1

while True:
    a = a + 1

    check, frame = video.read()
    print (frame)

    gray = cv2.cvtColor(frame, cv2.COLOR_BGR2GRAY)
    gray = cv2.GaussianBlur(gray, (21, 21), 0)

    if first_frame is None:
        first_frame = gray
        continue

    delta_frame = cv2.absdiff(first_frame, gray)

    thresh_delta = cv2.threshold(delta_frame, 25, 255, cv2.THRESH_BINARY)[1]

    thresh_delta = cv2.dilate(thresh_delta, None, iterations=2)

    (_, cnts, _) = cv2.findContours(thresh_delta.copy(), cv2.RETR_EXTERNAL, cv2.CHAIN_APPROX_SIMPLE)

    for contour in cnts:
        if cv2.contourArea(contour) < 1000:
            continue

        (x, y, w, h) = cv2.boundingRect(contour)
        cv2.rectangle(frame, (x, y), (x+w, y+h), (0, 255, 0), 2)

    cv2.imshow('captureFrame', frame)
    cv2.imshow('captureGrey', gray)
    cv2.imshow('delta', delta_frame)
    cv2.imshow('thresh', thresh_delta)

    key = cv2.waitKey(1)

    if key == ord('q'):
        break

print(a)
video.release()
cv2.destroyAllWindows()

enter image description here

编辑:这似乎是有关自动照明的硬件问题?无法确认。但是,购买廉价的Microsoft lifecam VX 2000似乎可以解决问题。

0 个答案:

没有答案