我正在尝试做帧差异这是我的下面的代码

import numpy as np
import cv2

current_frame =cv2.VideoCapture(0)


previous_frame=current_frame


while(current_frame.isOpened()):


current_frame_gray = cv2.cvtColor(current_frame, cv2.COLOR_BGR2GRAY)
previous_frame_gray= cv2.cvtColor(previous_frame, cv2.COLOR_BGR2GRAY)

frame_diff=cv2.absdiff(current_frame_gray,previous_frame_gray)


cv2.imshow('frame diff ',frame_diff)


cv2.waitKey(1)

current_frame.copyto(previous_frame)
ret, current_frame = current_frame.read()


current_frame.release()
cv2.destroyAllWindows()

我的问题是我尝试创建空框架以保存current_frame中的第一帧
previous_frame=np.zeros(current_frame.shape,dtype=current_frame.dtype)

但是我认为这是不正确的,然后我尝试像这样传递current_frame:
previous_frame=current_frame

现在我得到这个错误:



那我该怎么办呢?

感谢帮助

最佳答案

您已经混合了VideoCapture对象和框架。

我还对框架副本和等待键做了一些小的更改。

import cv2

cap = cv2.VideoCapture(0)
ret, current_frame = cap.read()
previous_frame = current_frame

while(cap.isOpened()):
    current_frame_gray = cv2.cvtColor(current_frame, cv2.COLOR_BGR2GRAY)
    previous_frame_gray = cv2.cvtColor(previous_frame, cv2.COLOR_BGR2GRAY)

    frame_diff = cv2.absdiff(current_frame_gray,previous_frame_gray)

    cv2.imshow('frame diff ',frame_diff)
    if cv2.waitKey(1) & 0xFF == ord('q'):
        break

    previous_frame = current_frame.copy()
    ret, current_frame = cap.read()

cap.release()
cv2.destroyAllWindows()

关于python - 使用python的帧差异,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/33926000/

10-12 19:07