Opencv-python 使用addWeighted图像背景更新

图像背景更新一般用createBackgroundSubtractorKNN算法来更新背景,但此算法耗时较长,在视频应用中会出现卡顿。使用图像融合的方法,速度较快。

import cv2
import numpy as np
 
cap = cv2.VideoCapture(0)
ret, privous_frame = cap.read()
while(1):
 ret, frame = cap.read()
 cv2.imshow("capture", frame)
 
 privous_frame=cv2.addWeighted(frame,0.7,privous_frame,0.3,0) 
 diff = cv2.absdiff(frame, privous_frame)
 fgmask = cv2.cvtColor(diff, cv2.COLOR_BGR2GRAY)
 th = cv2.threshold(fgmask.copy(), 10, 255, cv2.THRESH_BINARY)[1]
 cv2.imshow("diff", th)
 
 if cv2.waitKey(1) & 0xFF == ord('q'):
  break
cap.release()
cv2.destroyAllWindows()

 

你可能感兴趣的:(Python,opencv,python,背景更新)