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.
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".
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.
You can subtract two images by OpenCV function, cv. subtract(). res = img1 - img2.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With