
这是(II)中的Mat版本,特别注意一下accumulateWeighted这个函数的用法。
我将官网文档中的函数表明贴起来:
accumulateWeighted
Updates a running average.

C++:voidaccumulateWeighted(InputArraysrc,
InputOutputArraydst, doublealpha, InputArraymask=noArray())
Python:cv2.accumulateWeighted(src,
dst, alpha[, mask])→ None

C:voidcvRunningAvg(const
CvArr*image, CvArr*acc, doublealpha, const CvArr*mask=NULL)
Python:cv.RunningAvg(image,
acc, alpha, mask=None)→ None

Parameters:
The function calculates the weighted sum of the input imagesrcand the accumulatordstso
thatdstbecomes a running average of a frame sequence:


That is,alpharegulates the update speed (how fast the accumulator “forgets” about earlier images). The function supports multi-channel
images. Each channel is processed independently.
See also
,,
代码:
//opencv2.0风格的视频前景提取
#include "cv.h"
#include "highgui.h"
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/imgproc/imgproc.hpp>
#include <opencv2/core/core.hpp>
#include <iostream>
#include <cstdio>
using namespace std;
using namespace cv;
int main()
{
Mat frame, frame_copy,img1,output,gray,frame_copy_8U;
double learningRate = 0.01; // 控制背景累积学习的速率
char* input_name = "001.avi";
//从视频读入
VideoCapture capture(input_name);
cvNamedWindow( "result", 1 );
if(capture.isOpened()/*capture*/) // 读取文件开关
{
//对每一帧做处理
for(;;)
{
//frame = cvQueryFrame( capture ); // 读取文件开关
capture >> frame;
if(!frame.empty())
{
cvtColor(frame, gray, CV_BGR2GRAY);
//进行处理
if (frame_copy.empty())
{
//记录第一帧
gray.convertTo(frame_copy, CV_32F);
}
frame_copy.convertTo(frame_copy_8U, CV_8U);
//做差分
absdiff(frame_copy_8U, gray, img1);
// 对得到的前景进行阈值选取,去掉伪前景
threshold(img1, output, 30, 255, THRESH_BINARY_INV);
accumulateWeighted(gray, frame_copy,0.01,output);
imshow("src", frame);
imshow("result", output);
}
else
{
printf(" --(!) No captured frame -- Break!");
break;
}
//10ms中按任意键进入此if块
if( cvWaitKey( 10 ) >= 0 )
break;
}
}
return 0;
}
更好版本(II):
本文来自电脑杂谈,转载请注明本文网址:
http://www.pc-fly.com/a/bofangqi/article-140825-1.html
没办法
此人完了
来宣示霸权的美国军舰
高速成长的时代已经永远结束