这方面的资料不多,自己搞了一天才搞出来,给小伙伴们参考一把
#include "opencv2/opencv.hpp"
#include <iostream>
using namespace cv;
int main( int argc, char** argv )
{
Mat src1, src2, dst;
int FirstNmean = 20; //前n帧平均
double alpha = (double)1/FirstNmean; //这里计算alpha时一定要在前面加上强制转换符号,否则该变量一直为0
double beta = 0 ;
Mat frame, gray, mask;
Mat tem_sum ;
int img_height , img_weight ;
//img1 = (IplImage *)tem_mean ;
VideoCapture capture;
capture.open("2.avi");//campus sequence
if (!capture.isOpened())
{
std::cout<<"No camera or video input!\n"<<std::endl;
return -1;
}
capture.set( CV_CAP_PROP_POS_FRAMES,100);
capture >> frame;
img_height = frame.rows ;
img_weight = frame.cols;
tem_sum = Mat::zeros(frame.size(), CV_8UC3);
dst = Mat::zeros(frame.size(), CV_8UC3);
for(int i = 1;i < (FirstNmean+1);i++ )
{
capture.set( CV_CAP_PROP_POS_FRAMES,i);
capture >> frame;
beta = (double)(i-1)/FirstNmean ;
addWeighted( frame, alpha, tem_sum,1 , 0.0,dst);
tem_sum = dst ;
//tem_sum = frame ;
imshow( "frame", frame );
//imshow( "tem_sum", tem_sum );
imshow( "Linear Blend", dst );
waitKey(60);
}
waitKey(0);
return 0;
}