Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

BackgroundSubtractorMOG still keep the object after it left the frame

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.

like image 932
Marciano Avatar asked Sep 06 '14 15:09

Marciano


People also ask

What is background subtraction in image processing?

Background subtraction is a widely used approach for detecting moving objects in videos from static cameras. The rationale in the approach is that of detecting the moving objects from the difference between the current frame and a reference frame, often called "background image", or "background model".

What is background subtraction algorithms?

The background subtraction method (BSM) is one of the most popular approaches to detecting objects. This algorithm works by comparing moving parts of a video to a background image and foreground image.

How do you subtract two images in python?

You can subtract two images by OpenCV function, cv. subtract(). res = img1 - img2.


1 Answers

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

like image 82
user3491525 Avatar answered Oct 29 '22 23:10

user3491525