Ask Your Question

Revision history [back]


Yes you can use both BackgroundSubtractorMOG and BackgroundSubtractorMOG2 with still images. The trick is to feed the algorithm with one of more background images before you start the detection (using learning rate > 0), and then apply the background extraction algorithm using learning rate 0. I have done it for a project on which I work with my team. I don't have the C++ code, but have the code in Python, I hope it will help...


import numpy as np
import cv2
import sys

# both MOG and MOG2 can be used, with different parameter values
backgroundSubtractor = cv2.BackgroundSubtractorMOG()

#backgroundSubtractor = cv2.BackgroundSubtractorMOG(history=100, nmixtures=5, backgroundRatio=0.7, noiseSigma=0)
#backgroundSubtractor = cv2.BackgroundSubtractorMOG2(history=500, varThreshold=500)
#backgroundSubtractor = cv2.BackgroundSubtractorMOG2()

# apply the algorithm for background images using learning rate > 0
for i in range(1, 16):
    bgImageFile = "data/backgrounds/bg_%03d.jpg" % i
    print "Opening background", bgImageFile
    bg = cv2.imread(bgImageFile)
    backgroundSubtractor.apply(bg, learningRate=0.5)

# apply the algorithm for detection image using learning rate 0
stillFrame = cv2.imread("data/test_bg.jpg")
fgmask = backgroundSubtractor.apply(stillFrame, learningRate=0)

# show both images
cv2.imshow("original", cv2.resize(stillFrame, (0, 0), fx=0.5, fy=0.5))
cv2.imshow("mask", cv2.resize(fgmask, (0, 0), fx=0.5, fy=0.5))

Here is an example result...

One of the background images used:

image description

Detection image:

image description


image description