Background pruning and implementation in OpenCV, background clipping opencv
Reprinted, please specify the source !!!Http://blog.csdn.net/zhonghuan1992
Implementation of background pruning and OpenCV
The background and prospects are relative concepts. Taking highways as an example: Sometimes we are interested in cars coming and going on highways. At this time, cars are the foreground, while roads and the surrounding environment are the background; sometimes we are only interested in people who break into the highway. At this time, the break-in is the prospect, and other things, including cars, become the background. The background clipping function is used to detect moving objects in a widely used camera video. This kind of object that detects movement in Different frames is called the background model. In fact, background clipping is also a foreground detection.
A strong background pruning algorithm should be able to solve changes in light intensity, repetitive movement of clutter, and changes in long-term scenarios. In the following analysis, the function V (x, y, t) is used to represent the video stream, t is the time, and x and y represent the pixel position. For example, V (, 3) indicates the light intensity of a pixel () at t = 3. The following describes several background pruning methods.
1. Use different frames
This method assumes that the foreground is moving, but the background is not moving. The following formula can be used for different frames:
From the formula, we can understand the meaning of D (t + 1) and use it to represent the light intensity difference at different time points before and after the same position. As long as we extract the points where D is 0, it is our prospect and the background is cut. Of course, we can make some improvements here, not necessarily because the background will not change. A threshold value can be used to limit. See the following formula:
The Th threshold value is used to limit the number of vertices greater than Th. What we want is the future.
2 Mean filter)
First of all, there is a Mean in the name. It is generally related to the Mean. How can this relationship be linked? See the following formula:
The formula expresses the meaning of B (x, y) in a silent language. The average light intensity of the same point in the past N frames. Well, with this formula, we have the mean value. Let's look at how to use this B (x, y). The usage is very simple, that is, to put V (x, y, t + 1) in the above 1) change to B (x, y), so the company is as follows:
Well, I think you can feel this role, not by comparing the difference between the current frame and the previous frame, but by comparing the average difference between the current frame and the previous frame, at the same time, the threshold Th is used for control. When the point above the threshold value is removed, what is left behind is what we want.
3 Use Gaussian mean:
The mathematics is not very good. When I met Gauss's name, I thought it was very difficult. In fact, I am not very clear about it. I want to give a simple and easy-to-understand explanation. My approach to this situation is, there are some rules. I can't say the benefits of doing so. I will learn more in the future, and I will come back to supplement it. Let's take a look at how it works.
The first is the mean, which is the variance. See the following initialization:
This is quite clear. The mean at the beginning is I0 (at the beginning). The variance can make an initial value, which is used for updating later. The formula is as follows:
Well, the formula is updated in the above way. We can understand the above formula from the effect. Below is a diagram,
Each lattice above represents a part of a graph, and the situation of each pixel. You can see that each lattice is like a hill, I think Gaussian distribution is also used for this reason. The time is very close and the impact is relatively large, if you expand more layers of the above formula, you will feel high. The following is the judgment method:
It is similar to the above. However, there is an addition of variance. K is a free threshold. When K is large, some dynamic parts will be taken as the background; K is small, and there will be some static parts in the foreground.
Use OpenCV to Implement Foreground detection in the simplest 1.
# Include <opencv2/highgui. hpp> # include <opencv2/imgproc. hpp> # include <opencv2/core. hpp> # include <iostream> # define threshold_diff 10 // set the simple Frame Difference Threshold using namespace cv; using namespace std; int main (int argc, unsigned char * argv []) {Mat img1, img2, gray1, gray2, bac; bool pause = false; VideoCapture capture ("G: \ video analysis getting started exercise-attachment \ sample. avi "); // change the corresponding file name here // read the next frame if (! Capture. read (img1) {cout <"end" <endl; capture. release (); return 0;} while (true) {cvtColor (img1, gray1, CV_BGR2GRAY); // read the next frame if (! Capture. read (img2) {cout <"end" <endl; capture. release (); return 0;} cvtColor (img2, gray2, CV_BGR2GRAY); imshow ("original video", img2); subtract (gray1, gray2, bac ); for (int I = 0; I <bac. rows; I ++) for (int j = 0; j <bac. cols; j ++) if (abs (bac. at <unsigned char> (I, j)> = threshold_diff) // The template parameter must use unsigned char. Otherwise, an error is reported. at <unsigned char> (I, j) = 255; else bac. at <unsigned char> (I, j) = 0; imshow ("background image", bac); img1 = img2; waitKey (20);} return 0 ;}
Reference:
Http://en.wikipedia.org/wiki/Background_subtraction
Http://docs.opencv.org/modules/video/doc/motion_analysis_and_object_tracking.html
S
How to update the background of opencv
You can look at the Gaussian Mixture Model Method in Opencv. When a moving target in the foreground is stopped, the model will automatically process it as a background, but the Gaussian mixture model is time-consuming. Another method is to use the moving average, that is:
CvRunningAvg (const CvArr * image, CvArr * acc, double alpha, const CvArr * mask = NULL );
This function updates the background by setting the Alpha value. The larger the Alpha value, the faster the background is updated.
Kindly help to download the relevant information on PUDN: Using opencv to extract the background of the image, which is a good way to extract the background.
Kuai.xunlei.com/d/BaTCAuNEuxdWUwQAdba