閱讀574 返回首頁    go 阿裏雲 go 技術社區[雲棲]


視頻前景提取 (III)【Mat版本】

這是(II)中的Mat版本,特別注意一下accumulateWeighted這個函數的用法。

我將官方文檔中的函數說明貼出來:

accumulateWeighted

Updates a running average.

C++: void accumulateWeighted(InputArray src, InputOutputArray dst, double alpha, InputArray mask=noArray() )
Python: cv2.accumulateWeighted(src, dst, alpha[, mask]) → None
C: void cvRunningAvg(const CvArr* image, CvArr* acc, double alpha, const CvArr* mask=NULL )
Python: cv.RunningAvg(image, acc, alpha, mask=None) → None
Parameters:
  • src – Input image as 1- or 3-channel, 8-bit or 32-bit floating point.
  • dst – Accumulator image with the same number of channels as input image, 32-bit or 64-bit floating-point.
  • alpha – Weight of the input image.
  • mask – Optional operation mask.

The function calculates the weighted sum of the input image src and the accumulator dst so that dst becomes a running average of a frame sequence:

\texttt{dst} (x,y)  \leftarrow (1- \texttt{alpha} )  \cdot \texttt{dst} (x,y) +  \texttt{alpha} \cdot \texttt{src} (x,y)  \quad \text{if} \quad \texttt{mask} (x,y)  \ne 0

That is, alpha regulates the update speed (how fast the accumulator “forgets” about earlier images). The function supports multi-channel images. Each channel is processed independently.

See also

 

accumulate()accumulateSquare()accumulateProduct()


代碼:

//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):

//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;	// 控製背景累積學習的速率
	int nThreshold; //二值化閾值
	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);

				// 對得到的前景進行閾值選取,去掉偽前景
				nThreshold=30;
				threshold(img1, output, nThreshold, 255, THRESH_BINARY_INV);

				learningRate = 0.01;
				accumulateWeighted(gray, frame_copy,learningRate,output);

				imshow("src", frame);
				imshow("result", output);

			}
			else
			{ 
				printf(" --(!) No captured frame -- Break!");
				break;
			}


			//10ms中按任意鍵進入此if塊
			if( cvWaitKey( 10 ) >= 0 )
				break;
		}
	}

	return 0;
}



最後更新:2017-04-03 05:39:33

  上一篇:go 賬單支付寶代扣服務上線啦
  下一篇:go JAVA Annotation