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.

Marciano
  • 73
  • 1
  • 4
  • 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. – RussellStewart Nov 06 '14 at 04:46

1 Answers1

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

Alex Riley
  • 169,130
  • 45
  • 262
  • 238
user3491525
  • 91
  • 1
  • 3