0

如何在opencv-python中使用mog背景減法器修復前景?我想有一個更穩定的前景,可以保持顯示的前景,一旦能夠正確減去前景和背景(例如固定前景約5秒) 這裏是我的代碼:OpenCV - 在背景減法器中修復前景MOG

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() 
+0

你有沒有檢查此https://gist.github.com/lakshayg/3c23267527f3b2894398了嗎?它基於https://www.youtube.com/watch?v=KRKKektCcok –

+0

不,我沒有檢查,請讓我檢查。那是天真的減法器,這個算法以及MOG? – Maryam

+0

它只是將明亮的物體變成白色和黑色的物體變成黑色,它是做什麼的? – Maryam

回答

0
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() 
+0

謝謝,我正在運行此代碼,它根本不顯示任何內容,而是黑屏。 – Maryam

+0

ops,一些錯字。它應該是time.sleep(5) – VICTOR

+0

它也會返回這個錯誤:NameError:名稱'sleep'未定義 – Maryam