Foreground detection algorithm-sacon (sample consensus)

Source: Internet
Author: User

Sacon is an algorithm in the paper "background subtraction based on a robust consensu method> completed by Hanzi Wang and David Suter. At first glance, the idea of the algorithm is very simple. The later vibe algorithm seems to be similar to it. Below is a brief introduction to this algorithm:

(1) Establish a background model

In this article, the establishment of the background model is relatively simple, that is, to save n background samples for each pixel, which can easily extract the first N frames of the video sequence.

(2) Foreground Extraction

This part is divided into three phases, in fact, each phase is integrated: the first step is to use the neighbor frame difference method to extract the possible foreground mask (fmforeground mask), that is, two consecutive frames in the time domain are subtracted, extract possible moving pixels. The second part is to process the possible moving pixels and background samples obtained in the first step to the sacon algorithm. The main processing process is obtained according to the following two formulas:

 

Here, TR is a constant, n is the number of background samples (sample number), K represents the number of channels (channel number), and the default value is 15 in the Implementation Program. the value of TN is related to N. The value can be expressed as tn ~ Cntr (C is a constant ). The default value of the program TN is 2n.

(3) shadow removal (this shadow Removal Algorithm draws on other articles. For details, refer to section 3.1)

(4) Empty Filling

Considering that there may be some small holes in the extracted foreground area, this article uses the cavity filling technology. In implementation, the morphology of collision corrosion (dilate and erode) is used for filling.

(5) Considering the removal of the background or the suspension of the foreground, this article introduces Tom (time of MAP) to process it, it can be said that the area left by the removed background is quickly included in the background, and the target in the scenario can be quickly integrated into the background.

Header file. h:

Class sacon {public: sacon (); // constructor, where iteration indicates the number of samples, that is, the number of frames used for learning n sacon (iplimage * IMG, int iteration ); /* Description: to implement the function to distinguish whether a pixel is foreground or background and then update the background model */void detect (iplimage * IMG ); /* set whether the current pixel is the background or foreground, that is, the number of large differences between the current pixel and the background sample is related to the number of samples giteration, the default value is giteration * 2 */void setalpha (double Al) {galpha = Al;} // set two pixels. Value difference. The default value is 15 void setvaldiff (INT ival) {valdiff = ival;} // used to mark a pixel as void settomth (INT count) {th_tom = count ;} iplimage * getforeground () {return pforeimg;} virtual ~ Sacon (); Private: void innerdetect (iplimage * IMG); // fill the holes in the foreground area. The idea is filling. Void validateforeground (iplimage * foreimg); iplimage ** pbkimgarr; iplimage * pforeimg; iplimage * ppreimg; iplimage * pgray; iplimage * pdiffimg; int gheight; int gwidth; cvsize gsize; int giteration; int frmnum; int valdiff; // double galpha; // the value that determines a pixel whether a foreground or background int ** Tom; // used to record the number of consecutive foreground values of a pixel int th_tom; // used to mark a maximum number of foreground values of a pixel. The default value is 100 };

The following is the implementation code CPP file:

Sacon: sacon (iplimage * IMG, int iteration) {frmnum = 0; th_tom = 100; gheight = IMG-> height; gwidth = IMG-> width; giteration = iteration; galpha = giteration * 2; // the setting of the value is still to be discussed. gsize = cvgetsize (IMG); pforeimg = cvcreateimage (gsize, 8, 1); pgray = cvcreateimage (gsize, 8, 1 ); ppreimg = cvcreateimage (gsize, 8, 3); pdiffimg = cvcreateimage (gsize, 8, 3); int I = 0, j = 0; Tom = new int * [gheight]; for (I = 0; I <gheight; I ++) {Tom [I] = new int [gwidth]; for (j = 0; j <gwidth; j ++) {Tom [I] [J] = 0 ;}} pbkimgarr = new iplimage * [giteration]; for (I = 0; I <giteration; I ++) {pbkimgarr [I] = cvcreateimage (gsize, ipl_depth_8u, 3);} cvcopyimage (IMG, pbkimgarr [frmnum]); frmnum ++;} // implements foreground detection, if the learning is not complete, continue to learn void sacon: Detect (iplimage * IMG) {If (frmnum <giteration) {cvcopyimage (IMG, pbkimgarr [frmnum]); frmnum ++; if (frmnum = giteration) {cvcopyimage (IMG, ppreimg) ;}} else {innerdetect (IMG); validateforeground (pforeimg) ;}}// Implement Foreground detection, void sacon: innerdetect (iplimage * IMG) {cvzero (pforeimg); cvzero (pgray); average (IMG, ppreimg, pdiffimg); cvcvcvtcolor (pdiffimg, pgray, cv_bgr2gray); cvthreshold (pgray, pgray, 20,255, cv_thresh_binary); cvcopyimage (IMG, ppreimg); int I = 0, j = 0, K = 0, P = 0; int CNT = 0, itom; cvscalar CS0, CS1, CS2, CS3; cs3.val [0] = 255; for (I = 0; I <gheight; I ++) {for (j = 0; j <gwidth; j ++) {CS0 = cvget2d (pgray, I, j); If (cs0.val [0] = 255) {CS1 = cvget2d (IMG, I, j); CNT = 0; For (k = 0; k <giteration; k ++) {CS2 = cvget2d (pbkimgarr [K], i, j); For (P = 0; P <3; p ++) {If (FABS (cs1.val [p]-cs2.val [p])> valdiff) {CNT ++ ;}}// if (CNT> galpha) {itom = Tom [I] [J]; If (itom> th_tom) // if the number of foreground pixels exceeds th_tom, the background is set to {Tom [I] [J] = 0; // how to update the background} else {Tom [I] [J] ++; cvset2d (pforeimg, I, j, CS3) ;}}} else {continue ;}}}} void sacon: validateforeground (iplimage * foreimg) {// The default structure element of expanded corrosion is a 3 × 3 rectangular cvdilate (foreimg, foreimg, null, 1); cverode (foreimg, foreimg, null, 1);/* int I = 0, j = 0, P = 0, q = 0; int CNT; cvscalar CS0, Cs; CS. val [0] = 255; for (I = 1; I <gHeight-1; I ++) {for (j = 1; j <gWidth-1; j ++) {CNT = 0; For (P = I-1; P <= I + 1; p ++) {for (q = J-1; q <= J + 1; q ++) {CS0 = cvget2d (foreimg, p, q); If (cs0.val [0] = 255) {CNT ++ ;}} if (CNT> 5) {cvset2d (foreimg, I, j, CS );}}}*/}

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.