Foreground detection algorithm _ 2 (Frame Difference Method 1)

Source: Internet
Author: User

The frame difference method is a type of background subtraction method, but the frame difference method does not need to be modeled because its background model is the image of the previous frame, so the speed is very fast, in addition, the frame difference method is not very sensitive to the light of the slow transformation, so its use is still available, and many scholars have made outstanding improvements to it.

The basic principle can be seen in the following formula:

| I (t)-I (t-1) | <t background

| I (t)-I (t-1) |> = T foreground

Where I (t), I (t-1) are T, T-1 time corresponds to the pixel value, T is the threshold value.

Of course, there are also many shortcomings, which are prone to "dual shadows" and "holes.

Opencv2.3.1 + vs2010 was used for a simple experiment. The experiment code is as follows:

1 // frame_diff.cpp: defines the entry point of the console application. 2 // 3 4 # include "stdafx. H "5 # include <opencv2/highgui. HPP> 6 # include <opencv2/imgproc. HPP> 7 # include <opencv2/CORE/core. HPP> 8 9 # define threshold_diff 20 // set the simple Frame Difference Threshold Value of 10 11 using namespace CV; 12 using namespace STD; 13 14 int main (INT argc, unsigned char * argv []) 15 {16 mat img_src1, img_src2, img_dst, gray1, gray2, gray_diff; 17 bool pause = false; 18 19 videocapture vido_file ("indoorgtte St1.avi "); // change the corresponding file name here. 20 namedwindow (" foreground ", 0); 21 for (;) 22 {23 if (! Pause) 24 {25 vido_file> img_src1; // because the number of video file frames has been fixed, the number of adjacent frames is read every time this statement arrives, when the video does not arrive at the time, the video does not move forward 26 cvtcolor (img_src1, gray1, cv_bgr2gray); 27 imshow ("video_src", img_src1); // you do not need to create a window 28 waitkey (5 ); 29 30 vido_file> img_src2; 31 cvtcolor (img_src2, gray2, cv_bgr2gray); 32 imshow ("video_src", img_src2 ); // you do not need to create a window 33 34 waitkey (5); 35 subtract (gray1, gray2, gray_diff); 36 for (INT I = 0; I <gray_diff.rows; I ++) 37 for (Int J = 0; j <gr Ay_diff.cols; j ++) 38 If (ABS (gray_diff.at <unsigned char> (I, j)> = threshold_diff) // The template parameter must use unsigned char, otherwise, 39 gray_diff.at <unsigned char> (I, j) = 255; 40 else gray_diff.at <unsigned char> (I, j) = 0; 41 42 imshow ("foreground", gray_diff); 43} 44 char c = (char) waitkey (10); 45 if (C = 27) 46 {47 break; 48} 49 If (C = '') 50 pause =! Pause; 51} 52 return 0; 53}

The experiment results are as follows:

    

It can be seen that its "Double shadows" and "holes" are obvious. The dual shadow method has two shadows due to the frame difference. In this experiment, the contour is very rough, and the "Hollow" is caused by the similarity of the internal color of the object, which cannot be detected. Of course, the frame difference method also has a fatal disadvantage, that is, the threshold t needs to be manually set.

For the "dual-Shadow" Phenomenon of the frame difference method, someone proposed the three-frame difference method. The principle is as follows:

1. Obtain the foreground diagram F1 from I (t)-I (t-1)

2. the foreground graph F2 is obtained from I (t + 1)-I (t ).

3. F1 then F2 get the foreground diagram F3

4. Morphological Processing

That is to say, the real shadow is obtained by using the difference between two adjacent frames and then removing them from the operation.

To a certain extent, this can solve the "double shadow" phenomenon.

We also made a simple experiment with the Code as follows:

1 // frame_3diff.cpp: defines the entry point of the console application. 2 // 3 4 # include "stdafx. H "5 # include <opencv2/highgui. HPP> 6 # include <opencv2/imgproc. HPP> 7 # include <opencv2/CORE/core. HPP> 8 9 # define threshold_diff1 10 // set the simple Frame Difference Threshold 10 # define threshold_diff2 10 // set the simple Frame Difference Threshold 11 12 using namespace CV; 13 using namespace STD; 14 15 int main (INT argc, unsigned char * argv []) 16 {17 mat img_src1, img_src2, img_src3; // three frames are required for the 18 mat img_dst, gray1, gray2, G Ray3; 19 mat gray_diff1, gray_diff2; // store the 20 mat gray image subtracted twice; // The 21 bool pause = false that is used to display the foreground; 22 23 videocapture vido_file ("indoorgttest1.avi"); // change the corresponding file name here: 24 namedwindow ("foreground", 0); 25 for (;) 26 {27 if (! False) 28 {29 vido_file> img_src1; 30 cvtcolor (img_src1, gray1, latency); 31 32 waitkey (5); 33 vido_file> img_src2; 34 cvtcolor (img_src2, gray2, cv_bgr2gray); 35 imshow ("video_src", img_src2); // 36 37 waitkey (5); 38 vido_file> img_src3; 39 cvtcolor (img_src3, gray3, cv_bgr2gray ); 40 41 subtract (gray2, gray1, gray_diff1); // The second frame minus the first frame 42 subtract (gray3, gray2, gray_diff2 ); // The third frame minus the second frame 43 44 for (INT I = 0; I <gray_diff1.rows; I + +) 45 for (Int J = 0; j <gray_diff1.cols; j ++) 46 {47 If (ABS (gray_diff1.at <unsigned char> (I, j)> = threshold_diff1) // here, the template parameter must use unsigned char; otherwise, the error 48 gray_diff1.at <unsigned char> (I, j) = 255 is reported; // The first time the subtraction threshold is processed for 49 else gray_diff1.at <unsigned char> (I, j) = 0; 50 51 if (ABS (gray_diff2.at <unsigned char> (I, j)> = threshold_diff2) // The second subtraction threshold value processing 52 gray_diff2.at <unsigned char> (I, j) = 255; 53 else gray_diff2.at <unsigned char> (I, j) = 0; 54} 55 bitwise_and (gray_diff1, gray_diff2, gray); 56 imshow ("foreground", gray); 57} 58 char c = (char) waitkey (10 ); 59 If (C = 27) 60 {61 break; 62} 63 If (C = '') 64 pause =! Pause; // Why can't I pause it ?? 65} 66 return 0; 67}

The experiment results are as follows:

  

It can be seen that the effect is not very good, but the shadow profile is indeed fine and the "empty" phenomenon is not improved. Of course, this is only a simple experiment, and it has not been optimized or improved, it uses the most primitive idea and does not use morphology for post-processing.

 

 

 

 

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.