How can you prevent the BackgroundSubtractorGSOC from updating the background model?

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 =
    if frame is None:

    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'):

Is there any other way to achieve the same behavior? Any help would be highly appreciated.

Thank you in advance . . . :smiling_face_with_tear:

sounds like a bug. file a bug report.

docs explicitly promise that learning rate 0 does not update the model. if it does anyway, that is definitely a bug.

you should include a MRE “reproducer” in the bug report. make it easy for the opencv devs to pin this down.

if you like, explore the source code and find the place where the learning rate is broken.

1 Like

that’s a weird idea. how do you expect it to pick up new movement, then ?

can it be, you’re trying to use this for object detection / segmentation , NOT to detect motion ?

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.

your use case is fine. don’t worry.

bgsubs that don’t respect a zero learning rate are faulty. you should open an issue about this on the github.

The same issue exists with cv2.bgsegm.BackgroundSubtractorLSBP and cv2.BackgroundSubtractorKNN.

I just filed a report to github:

1 Like