opencv_contrib icon indicating copy to clipboard operation
opencv_contrib copied to clipboard

BackgroundSubtractorGSOC and LSBP does not respect a zero learning rate

Open ArdooTala opened this issue 2 years ago • 1 comments

System Information

OpenCV python version: 4.8.0.76 opencv-contrib-python Operating System / Platform: Ubuntu 22.04 Python version: 3.10.12

Detailed description

The BackgroundSubtractorGSOC() does not stop updating the background model regardless of the zero learningRate factor when calling the BackgroundSubtractorGSOC.apply(<image>, None, learningRate=0). It works fine when using the BackgroundSubtractorMOG and BackgroundSubtractorMGM, but BackgroundSubtractorGSOC and BackgroundSubtractorLSBP does not respect the learning rate. The background model keeps updating regardless of the learningRate value.

The same issue exists with BackgroundSubtractorKNN from the standard OpenCV.

Steps to reproduce

import cv2


# back_sub = cv2.createBackgroundSubtractorKNN()    # Has the same problem
# back_sub = cv2.bgsegm.createBackgroundSubtractorLSBP()    # Has the same problem
back_sub = cv2.bgsegm.createBackgroundSubtractorGSOC()

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)

ArdooTala avatar Sep 05 '23 13:09 ArdooTala

I can handle this where is the file of code?

Tarunnagpal7 avatar Sep 05 '23 17:09 Tarunnagpal7