Search code examples
pythonopencvbackground-subtraction

openCV background subtraction with GSOC


im trying to implement the GSOC background subtractor from openCV.

fgbg = cv.bgsegm_BackgroundSubtractorGSOC()
fgmask = fgbg.apply(frame)

but this gives me following error:

fgmask = fgbg.apply(frame)
TypeError: Incorrect type of self (must be 'bgsegm_BackgroundSubtractorGSOC' or its derivative)

and

fgmask = cv.bgsegm_BackgroundSubtractorGSOC.apply(frame)

gives me this error:

fgmask = cv.bgsegm_BackgroundSubtractorGSOC.apply(frame)
TypeError: descriptor 'apply' requires a 'cv2.bgsegm_BackgroundSubtractorGSOC' object but received a 'numpy.ndarray'

The documentation for .apply() says i only need to supply an inputarray (the frame), the output location and the learning rate. Changing .apply(frame) to .apply(frame, output, -1) does not fix the error

how do i correctly implement a bgsegm_BackgroundSubtractorGSOC object and use it on my image?

i read this post but it seems i am failing a step before that already


Solution

  • GSOC and the other background subtraction methods (other than MOG2 and KNN) are located in the extra modules and require the opencv-contrib library to be installed. Once it is installed, the module can be used by writing:

    backSub = cv.bgsegm.createBackgroundSubtractorGSOC()