BackgroundSubtractorMOG still keep the object after it left the frame
Asked Answered
E

1

5

I tried to use BackgroundSubtractorMOG to remove the background but there are some object that already left the frame but the result from BackgroundSubtractorMOG.apply() still show that the object is still on the scene.

Here is my code

inputVideo = cv2.VideoCapture('input.avi')
fgbg = cv2.BackgroundSubtractorMOG()

while inputVideo.isOpened():
    retVal, frame = inputVideo.read()

    fgmask = fgbg.apply(frame)

    cv2.imshow('Foreground', fgmask)
    cv2.imshow('Original', frame)
    if cv2.waitKey(1) & 0xFF == 27:
        break

I've also tried BackgroundSubtractorMOG with custom parameters (history = 200, nmixtures = 5, ratio = 0.8) but result is the same. Did I do something wrong or any recommedation? Please help.

Eveland answered 6/9, 2014 at 15:11 Comment(1)
I'm having the same problem. The history parameter is seemingly ignored. Using two different values produces exactly the same image. I think the history parameter is defaulting to 0, which means that the initial background image is the only image used, and the algorithm never learns.Kierakieran
B
9

The problem is in fgbg.apply. For some reason, the learningRate is set to 0. Make call like this:

history = 10   # or whatever you want it to be

fgmask = fgbg.apply(frame, learningRate=1.0/history)

Credit should go to Sebastian Ramirez who started a ticket in opencv and found the solution

Bassist answered 16/12, 2014 at 16:20 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.