C++实现运动检测

#include <opencv2/opencv.hpp>
using namespace std;
using namespace cv;

int main(int argc, char** argv)
{
	VideoCapture capture("E:/2.mp4");//修改为自己的视频地址
	if (!capture.isOpened())
	{
		cout << "No camera or video input!\n" << endl;
		return -1;
	}
	Mat frame;
	Mat gray;
	Mat gray_dilate1;
	Mat gray_dilate2;
	Mat gray_dilate3;
	Mat background, foreground, foreground_BW;
	Mat mid_filer;   //中值滤波法后的照片
	//---------------------------------------------------------------------
	//获取视频的宽度、高度、帧率、总的帧数
	int frameH = capture.get(CV_CAP_PROP_FRAME_HEIGHT); //获取帧高
	int frameW = capture.get(CV_CAP_PROP_FRAME_WIDTH);  //获取帧宽
	int fps = capture.get(CV_CAP_PROP_FPS);          //获取帧率
	int numFrames = capture.get(CV_CAP_PROP_FRAME_COUNT);  //获取整个帧数
	int num = numFrames;
	printf("vedio's \nwidth = %d\t height = %d\n video's fps = %d\t nums = %d", frameW, frameH, fps, numFrames);
	//---------------------------------------------------------------------
	Mat frame_0, frame_1;//Mat m(3, 5, CV_32FC1, 1);
	//---------------------------------------------------------------------
	while (1)
	{
		capture >> frame;
		imshow("frame_resize", frame);
		cvtColor(frame, gray, CV_RGB2GRAY);
		//-----------------------------------------------------------------------------------
		//选择前一帧作为背景(读入第一帧时,第一帧作为背景)
		if (num == numFrames)
		{
			background = gray.clone();
			frame_0 = background;
		}
		else
		{
			background = frame_0;
		}
		//------------------------------------------------------------------------------------
		absdiff(gray, background, foreground);//用帧差法求前景
		imshow("foreground", foreground);
		Mat element = getStructuringElement(MORPH_RECT, Size(5, 5));


		threshold(foreground, foreground_BW, 30, 255, 0);//二值化通常设置为50  255
		//threshold(foreground, foreground_BW, 0, 255 ,CV_THRESH_BINARY | CV_THRESH_OTSU) ;  //此处使用大津法  自适应取阈值
		imshow("foreground_BW", foreground_BW);
		medianBlur(foreground_BW, mid_filer, 3);     //中值滤波法
		imshow("mid_filer", mid_filer);
		dilate(mid_filer, gray_dilate1, element);
		imshow("gray_dilate1", gray_dilate1);
		dilate(gray_dilate1, gray_dilate2, element);
		imshow("gray_dilate2", gray_dilate2);
		dilate(gray_dilate2, gray_dilate3, element);
		imshow("gray_dilate3", gray_dilate3);
		frame_0 = gray.clone();
		num--;
		char c = waitKey(10);
		if (c == 27) 
         {
          break;
         } 
		if (num < 1)
		{
			return -1;
		}
	}
	system("pause");
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值