opencv实现运动目标检测

#include "cv.h"
#include "highgui.h"
#include

using namespace cv;
using namespace std;


int main()
{
	//读取图像
	Mat frame,frameBK,frameFR;
	VideoCapture capture("walk.avi");
	if(!capture.isOpened())
		return -1;

	int deley=1000/capture.get(CV_CAP_PROP_FPS);

	while(capture.read(frame))
	{
		cvtColor(frame,frame,CV_RGB2GRAY);

		if(i==0)
		{
			frameBK=frame.clone();
			frameFR=frame.clone();
		}
		else
		{
			absdiff(frame,frameBK,frameFR);
			threshold(frameFR,frameFR,60,255.0,CV_THRESH_BINARY);
			//更新背景模型——滑动平均滤波 background = background + (frame - background) *  alpha  = background * (1 - alpha) + frame * alpha
			addWeighted(frameBK, 0.003, frame, 1-0.003, 0, frameBK);
		}

		imshow("test",frameFR);

		cvWaitKey(deley);
	}

	waitKey(0);
	return 0;
}

你可能感兴趣的:(图像处理)