Recently in the project of target tracking, a function using the OpenCV library is called Cvrunningavg. Let me make a simple description of the use of the CVRUNNINGAVG function, and then provide a background moving average function that I apply to my own project.
The declaration of the CVRUNINGAVG function is as follows:
void Cvrunningavg (const Cvarr * image, cvarr* acc, Double alpha, const cvarr* mask=null)
Parameter description:
Image: Enter images or sequences
ACC: an image or sequence used for accumulation
Alpha: The weight of the image when moving average
Mask: operator Mask
The result of this function is that the value of the image is added to the ACC by the alpha weight. The function of the alpha parameter is to tell the program, how quickly I forget the front frame of the image, the current frame into the background faster.
I used to run into a program error when I was using this function, and I believe many first-time users are prone to make this mistake. The two parameters of the function, both image and ACC, must be of type float, and a run error occurs if the input parameter image is of type Uchar. For example, if you create an image parameter like this:
Iplimage pframe = Cvcreateimage (Cvsize (Pframe->width, pframe->height), ipl_depth_8u,1);
Passing pframe as an input image to the CVRUNNINGAVG function parameter image will cause a run error.
Specifically why errors occur, you can view the source code of the Cvrunningavg.
For the mask parameter, I usually use this parameter 0,mask this parameter will not work. As for the specific function of the mask parameter, I think it is input an operation matrix, can be based on the value of the matrix to set the background updated pixels, but in a few simple experiments, I did not test how to use this parameter.
In my project to let the target pixel point does not fit into the background, so modeled the function implemented by the CVRUNNINGAVG function, made some changes. Below is the code for the MYRUNNINGAVG function I wrote. Where the parameter M_objectinfo stores the location information of the moving target
void Myrunningavg (cvmat* srcmat, cvmat* Dstmat, int* maskframe, object_info* m_objectinfo, float alpha, int n)
{
int i,j,k;
int height = srcmat->rows;
int width = srcmat->cols;
memset (maskframe, 0, sizeof (int) * (height*width));
for (i=n; i>0; i--)
{for
(k=m_objectinfo[i].y1; k<=m_objectinfo[i].y2; k++) for
(J=m_objectinfo[i] . x1; j<=m_objectinfo[i].x2; J + +)
maskframe[k*width+j] = 1;
}
for (j=0; j
If you use the CVRUNNINGAVG function to update the background, the background model of the resume will see the residual shadow of the moving target, the situation of the residual shadow can be seen in Figure 1, the right of the figure is the background gray map, you can see the moving target remnants.
Figure 1
If you use the Myrunning function, the shadow will be eliminated, as shown in Figure 2:
Figure 2