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

**URL:** <https://forum.opencv.org/t/how-can-you-prevent-the-backgroundsubtractorgsoc-from-updating-the-background-model/14441>\
**Category:** Python\
**Tags:** backgroundsubtractor, bgsegm\
**Created:** [September 4, 2023, 8:30pm UTC](https://forum.opencv.org/t/how-can-you-prevent-the-backgroundsubtractorgsoc-from-updating-the-background-model/14441 "2023-09-04T20:30:35Z")\
**Posts on this page:** 6\
**Page:** 1

<div class="post-metadata">

**Author:** ![ArdooTala](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/ardootala/32/8651_2.png) [@ArdooTala](https://forum.opencv.org/u/ArdooTala)\
**Post date:** [September 4, 2023, 8:30pm UTC](https://forum.opencv.org/t/how-can-you-prevent-the-backgroundsubtractorgsoc-from-updating-the-background-model/14441/1 "2023-09-04T20:30:36Z")

</div>

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.

```auto
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.

Thank you in advance . . . 🥲

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [September 5, 2023, 9:03am UTC](https://forum.opencv.org/t/how-can-you-prevent-the-backgroundsubtractorgsoc-from-updating-the-background-model/14441/2 "2023-09-05T09:03:46Z")

</div>

> [@ArdooTala](#):
>
> the **learningRate** param in `BackgroundSubtractorGSOC.apply(frame, None, learningRate=0)` does not work

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](https://stackoverflow.com/help/minimal-reproducible-example) “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.

---

<div class="post-metadata">

**Author:** ![berak](https://avatars.discourse-cdn.com/v4/letter/b/85f322/32.png) [@berak](https://forum.opencv.org/u/berak)\
**Post date:** [September 5, 2023, 10:48am UTC](https://forum.opencv.org/t/how-can-you-prevent-the-backgroundsubtractorgsoc-from-updating-the-background-model/14441/3 "2023-09-05T10:48:52Z")

</div>

> [@ArdooTala](#):
>
> 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.

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_ ?

---

<div class="post-metadata">

**Author:** ![ArdooTala](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/ardootala/32/8651_2.png) [@ArdooTala](https://forum.opencv.org/u/ArdooTala)\
**Post date:** [September 5, 2023, 11:17am UTC](https://forum.opencv.org/t/how-can-you-prevent-the-backgroundsubtractorgsoc-from-updating-the-background-model/14441/5 "2023-09-05T11:17:46Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![crackwitz](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/crackwitz/32/14_2.png) [@crackwitz](https://forum.opencv.org/u/crackwitz)\
**Post date:** [September 5, 2023, 12:13pm UTC](https://forum.opencv.org/t/how-can-you-prevent-the-backgroundsubtractorgsoc-from-updating-the-background-model/14441/6 "2023-09-05T12:13:11Z")

</div>

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.

---

<div class="post-metadata">

**Author:** ![ArdooTala](https://sea2.discourse-cdn.com/flex020/user_avatar/forum.opencv.org/ardootala/32/8651_2.png) [@ArdooTala](https://forum.opencv.org/u/ArdooTala)\
**Post date:** [September 5, 2023, 12:55pm UTC](https://forum.opencv.org/t/how-can-you-prevent-the-backgroundsubtractorgsoc-from-updating-the-background-model/14441/7 "2023-09-05T12:55:31Z")

</div>

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

I just filed a report to github:

> <https://github.com/opencv/opencv/issues/24226>
>
> \### System Information
> 
> OpenCV python version: 4.8.0.76 opencv-contrib-python
> O…perating System / Platform: Ubuntu 22.04
> Python version: 3.10.12
> 
> \### Detailed description
> 
> The \`BackgroundSubtractorKNN()\` does \*\*not\*\* stop updating the background model regardless of the zero \`learningRate\` factor when calling the \`BackgroundSubtractorKNN.apply(\<image\>, None, learningRate=0)\`.
> It works fine when using the \`BackgroundSubtractorMOG2\`, but the learningRate param in \`BackgroundSubtractorKNN\` does not respect the learning rate. The background model keeps updating regardless of the value.
> 
> The same issue exists with \`BackgroundSubtractorLSBP\` and \`BackgroundSubtractorGSOC\` from the \*cv2.bgsegm\* package. Although \`BackgroundSubtractorMOG\` and \`BackgroundSubtractorMGM\` work fine.
> 
> \### Steps to reproduce
> 
> \`\`\`python
> import cv2
> 
> 
> back\_sub = cv2.createBackgroundSubtractorKNN()
> \# back\_sub = cv2.bgsegm.createBackgroundSubtractorLSBP() # Has the same problem
> \# back\_sub = cv2.bgsegm.createBackgroundSubtractorGSOC() # Has the same problem
> 
> cap = cv2.VideoCapture(0)
> if not cap.isOpened():
> raise (Exception("Could not open capture"))
> 
> learning\_rate = 0.1
> while True:
> ret, frame = cap.read()
> if not ret:
> break
> 
> fg\_mask = back\_sub.apply(frame, None, learning\_rate)
> print(learning\_rate)
> 
> cv2.imshow("frame", frame)
> cv2.imshow("foreground", fg\_mask)
> cv2.imshow("background", back\_sub.getBackgroundImage())
> 
> k = cv2.waitKey(1)
> if k == ord("q"):
> break
> # Press "d" to switch learning\_rate between 0.1 and 0.0
> elif k == ord("d"):
> if not learning\_rate:
> learning\_rate = 0.1
> else:
> learning\_rate = 0.0
> 
> cap.release()
> cv2.destroyAllWindows()
> \`\`\`
> 
> \### Issue submission checklist
> 
> \- \[X\] I report the issue, it's not a question
> \- \[X\] I checked the problem with documentation, FAQ, open issues, forum.opencv.org, Stack Overflow, etc and have not found any solution
> \- \[X\] I updated to the latest OpenCV version and the issue is still there
> \- \[X\] There is reproducer code and related data files (videos, images, onnx, etc)
