This is driving me crazy. I am using OpenCV to monitor a robotic process. As long as the robot is moving, the background subtraction works fine, but sometimes, the robot needs to wait for a while and will become stationary. I am trying to prevent the Background Subtractor from updating the background model while the robot is waiting, to avoid the robot becoming part of the background.
It works fine when I am using the BackgroundSubtractorMOG2, but the learningRate param in BackgroundSubtractorGSOC.apply(frame, None, learningRate=0) does not work. The background model keeps updating regardless of the value.
Here is the code I am using for background subtraction.
import cv2 as cv
# backSub = cv.createBackgroundSubtractorMOG2() # This works ok
backSub = cv.bgsegm.createBackgroundSubtractorGSOC()
capture = cv.VideoCapture(0)
while True:
ret, frame = capture.read()
if frame is None:
break
fgMask = backSub.apply(frame, None, 0) # 0.0 ~ 1.0 - 0: do not update / 1: reinitialize background
cv.imshow('Frame', frame)
cv.imshow('FG Mask', fgMask)
keyboard = cv.waitKey(30)
if keyboard == ord('q'):
break
Is there any other way to achieve the same behavior? Any help would be highly appreciated.
I’m using it for subtracting the robot (foreground) from the background. Isn’t that what it’s supposed to do?
It is supposed to create a background model and be able to distinct the objects from that background, not only to detect motion. This is the intended behavior based on the documentation at least.
With MOG2, it’s totally ok. I keep updating the background, as long as the robot is moving around, and when it becomes stationary, I stop updating the background. It keeps detecting the stationary robot as a foreground then. I guess that is actually a segmentation.
With MOG and MGM, it works fine. With KNN, GSOC, and LSBP, it fails.