Ask Your Question
0

What does the history of this function “createBackgroundSubtractorMOG2” means?

asked 2018-01-11 02:21:29 -0600

MadFrog gravatar image

updated 2020-07-08 23:40:07 -0600

I only see this description in this link, it hasn't a very detailed explanation, so I'd like to know where can I find a more detailed explanation.The official web document says "Length of the history", what "Length of the history" is?

image description

My code:

import os
import time
import cv2

def main():
    img_src_dirpath = r'C:/Users/Shinelon/Desktop/SRC/'
    dir = r'D:/deal_pics/' + time.strftime('%Y-%m-%d') + '/'
    if not os.path.exists(dir):
        os.makedirs(dir)
    img_dst_dirpath = dir
    history = 60
    varThreshold = 16
    detectShadows = True
    mog2 = cv2.createBackgroundSubtractorMOG2( history, varThreshold, detectShadows )
    for f in os.listdir( img_src_dirpath ):
        if f.endswith( '.jpg' ):
            img = cv2.imread( img_src_dirpath + f )
            mog2.apply( img )
            bg = mog2.getBackgroundImage()
            cv2.imwrite( img_dst_dirpath + f, bg )
    cv2.destroyAllWindows()

if __name__ == '__main__':
    main()
edit retag flag offensive close merge delete

2 answers

Sort by » oldest newest most voted
0

answered 2018-01-11 06:41:21 -0600

It simply states how many previous frames are used for building the background model. So basically if an item is standing at a fixed position for as many frames as the history size, then it will disappear in the background.

edit flag offensive delete link more

Comments

Can you provide detailed information for this parameter? I have never found more.

MadFrog gravatar imageMadFrog ( 2018-01-11 07:06:58 -0600 )edit

Do you have the source code for this method? Or, for example, this parameter is 60, so is it means to generate a 61st frame of the present video with the front 60 pictures? If it is, is there a weight in the first 60 frames? Is the weight same?

MadFrog gravatar imageMadFrog ( 2018-01-11 07:17:09 -0600 )edit

The code is in the repository, and it is some kind of weighted sum, but you would have to read the paper to know the exact formula. I do not know those out of my head :D

StevenPuttemans gravatar imageStevenPuttemans ( 2018-01-11 07:20:51 -0600 )edit
1

Anyway,thank you for your help,

MadFrog gravatar imageMadFrog ( 2018-01-11 18:12:48 -0600 )edit
1

answered 2020-07-08 23:12:14 -0600

holzfigure gravatar image

TL;DR (short answer):

As I understand it, the history parameter just sets the learning rate alpha of the algorithm like: 'alpha = 1 / history', i.e. exponentially decaying the weights of older frames. E.g., setting history=2 means alpha=1/2=0.5, so the newest frame is weighted 50%, the previous frame 25%, the one before the previous 12.5% and so on.. Your history value is likely much larger (the default value is 500, meaning learning rate alpha=1/500=0.002), so the exponential decay of the weights is much flatter. A nice visualization of exponential decay and the associated half-lives (currently) can be found at https://upload.wikimedia.org/wikipedi... (where the flattest curve represents alpha=0.01, i.e. history=100)

Btw thanks for asking that question! I've been struggling with it myself recently (and generally with the confusing organization and incomplete nature of OpenCV documentation over the years.. disclaimer: I do deeply love and heavily use OpenCV!,).

Long explanation:

This answer comes 2.5 years late and i hope the original poster has solved their problem, but just to spare/ease others the trouble I attempt to justify/substantiate my answer. For a start, the most recent (and still as incomplete) documentation of the class can be found at https://docs.opencv.org/master/d7/d7b... (as long as they don't decide to change the organization of the docs again,)). There, two papers are cited that explain the algorithm, the published versions of which reside at https://www.doi.org/10.1109/ICPR.2004... ['Zivkovic2004'] and https://doi.org/10.1016/j.patrec.2005... ['Zivkovic2006'], none of which are Open-Access (PDFs can be found searching for the papers e.g. on Google Scholar..) [Side note: I do consider it questionable practice to outsource documentation of Open-Source code to Non-Open-Access publications (yet worlds better than close-sourcing the code,).. Also, imho, OpenCV could at least add such DOI-based permalinks to its bibliography to make things a teeny-weeny little less tedious..] In Zivkovic2004, it says "Instead of the time interval T [the "history" parameter] that was mentioned above, here constant α describes an exponentially decaying envelope that is used to limit the influence of the old data. We keep the same notation having in mind that approximately α = 1/T."

Looking at the source code (which [again at time of writing] be found at https://github.com/opencv/opencv/blob... ), I find the "approximately" refers to the number of already available frames: If fewer than the nframes specified in 'history' are available, the learning rate alpha is set to '1 / (2 * nframes)'. See lines 779 and 869 [at time of writing]:

"learningRate = learningRate >= 0 && nframes > 1 ? learningRate : 1./std::min( 2*nframes, history );"

[My understanding of C++ is limited (being a Python Monty for years now,), maybe it says that as soon nframes is bigger than one, the learning rate alpha is set to 1/history (depending on other parts of ... (more)

edit flag offensive delete link more

Question Tools

1 follower

Stats

Asked: 2018-01-11 02:21:29 -0600

Seen: 3,843 times

Last updated: Jul 08 '20