OpenCV - 修复背景减法器MOG中的前景

时间:2016-05-20 07:00:17

标签: opencv image-processing machine-learning computer-vision feature-detection

如何在opencv-python中使用mog背景减法器修复前景?我试图有一个更稳定的前景,一旦它可以正确地从背景中减去前景,就可以继续显示前景(例如修复前景约5秒) 这是我的代码:

cap = cv2.VideoCapture(0)
history = 500   # or whatever you want it to be
accelerate = 5

fgbg = cv2.createBackgroundSubtractorMOG2(history=500,  varThreshold=20,  detectShadows=True)

count=0
while(1):
    for i in (1, accelerate):
        ret, frame = cap.read()
    fgmask = fgbg.apply(frame, learningRate=1.0/history)
    imageproc(fgmask,count)

#    time.sleep(5)
    k = cv2.waitKey(0) & 0xff
    if k == 27:
        break   
cap.release()
cv2.destroyAllWindows()

1 个答案:

答案 0 :(得分:1)

import numpy as np
import cv2
import time

cap = cv2.VideoCapture('video.avi')

fgbg = cv2.createBackgroundSubtractorMOG()
while(1):
    ret, frame = cap.read()

    fgmask = fgbg.apply(frame)

    cv2.imshow('frame',fgmask)
    time.sleep(5)
    k = cv2.waitKey(30) & 0xff
    if k == 27:
        break

cap.release()
cv2.destroyAllWindows()