【问题标题】:frame difference using python使用python的帧差异
【发布时间】:2016-02-28 19:16:48
【问题描述】:

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

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

现在我收到此错误:

current_frame_gray = cv2.cvtColor(current_frame, cv2.COLOR_BGR2GRAY) TypeError: src 不是 numpy 数组,也不是标量

那我该怎么办呢?

感谢您的帮助

【问题讨论】:

    标签: python opencv numpy


    【解决方案1】:

    您已经混合了 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()
    

    【讨论】:

      猜你喜欢
      • 2022-07-01
      • 2019-06-19
      • 2016-09-08
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-07-16
      • 1970-01-01
      相关资源
      最近更新 更多