【opencv3】帧差法检测运动物体C++

opencv3视频透视变换【C++】
上一篇博客通过用鼠标选择特征点,完成透视变换,实现了视频视角的校正。

实现目标

在透视变换的基础上,利用帧差法检测运动物体,并用矩形框出。

程序

#include<opencv2/opencv.hpp>
#include<opencv2/highgui/highgui.hpp>
#include<opencv2/core/core.hpp>
#include<opencv2/imgproc/imgproc.hpp>
#include <iostream>  
using namespace cv;
using namespace std;

Mat MoveDetect(Mat temp, Mat frame);
void onm ouse(int event, int x, int y, int flags, void *utsc);
Point2f srcTri[4], dstTri[4];
int clickTimes = 0;
Mat imageWarp;
Mat frame;

int main()
{
	VideoCapture video("H:\\test.mp4");
	if (!video.isOpened())
		return -1;

	video.set(CV_CAP_PROP_FRAME_WIDTH, 1280);
	video.set(CV_CAP_PROP_FRAME_HEIGHT, 720);

	while (1)
	{
		int frameCount = video.get(CV_CAP_PROP_FRAME_COUNT);//获取帧数  
		double FPS = video.get(CV_CAP_PROP_FPS);//获取FPS  

		Mat temp;//存储前一帧图像  
		Mat result;//存储结果图像  

		for (int i = 0; i < frameCount; i++)
		{
			video.read(frame);
			if (frame.empty())//异常检测  
			{
				cout << "frame is empty!" << endl;
				break;
			}
			
			if (i == 0)//如果为第一帧(temp为空)  
			{
				result = MoveDetect(frame, frame);
			}
			else//若不是第一帧(temp有值)  
			{
				result = MoveDetect(temp, frame);
			}
			temp = frame.clone();
			imshow("result", result);

			setMouseCallback("Source Image", onm ouse);
			imshow("Source Image", frame);
		}
	}
	return 0;
}

void onm ouse(int event, int x, int y, int flags, void *utsc)
{
	if (event == CV_EVENT_LBUTTONUP)//响应鼠标左键抬起事件
	{
		circle(frame, Point(x, y), 2.5, Scalar(0, 0, 255), 2.5);//标记选中点
		imshow("Source Image", frame);
		srcTri[clickTimes].x = x;
		srcTri[clickTimes].y = y;
		cout << "x: " << x << "  y: " << y << endl;
		clickTimes++;
	}
	if (clickTimes == 4)
	{
		//注意点的顺序:左上,右上,左下,右下
		dstTri[0].x = 0;
		dstTri[0].y = 0;
		dstTri[1].x = frame.rows - 1;
		dstTri[1].y = 0;
		dstTri[2].x = 0;
		dstTri[2].y = frame.cols - 161;
		dstTri[3].x = frame.rows - 1;
		dstTri[3].y = frame.cols - 161;

		Mat transform = Mat::zeros(3, 3, CV_32FC1);//透视变换矩阵
		transform = getPerspectiveTransform(srcTri, dstTri);//获取透视变换矩阵		
		warpPerspective(frame, imageWarp, transform, Size(frame.rows, frame.cols - 160));//透视变换
		imshow("After WarpPerspecttive", imageWarp);
	}
}

Mat MoveDetect(Mat temp, Mat frame)
{
	Mat result = frame.clone();
	//1.将background和frame转为灰度图  
	Mat gray1, gray2;
	cvtColor(temp, gray1, CV_BGR2GRAY);
	cvtColor(frame, gray2, CV_BGR2GRAY);

	//2.将background和frame做差  
	Mat diff;
	absdiff(gray1, gray2, diff);
	// imshow("2_diff", diff);

	//3.对差值图diff_thresh进行阈值化处理  
	Mat diff_thresh;
	threshold(diff, diff_thresh, 50, 255, CV_THRESH_BINARY);
	// imshow("3_diff_thresh", diff_thresh);

	//4.腐蚀  
	Mat kernel_erode = getStructuringElement(MORPH_RECT, Size(3, 3));
	Mat kernel_dilate = getStructuringElement(MORPH_RECT, Size(5, 5));
	erode(diff_thresh, diff_thresh, kernel_erode);
	// imshow("4_erode", diff_thresh);

	//5.膨胀  
	dilate(diff_thresh, diff_thresh, kernel_dilate);
	// imshow("5_dilate", diff_thresh);

	//6.查找轮廓并绘制轮廓  
	vector<vector<Point> > contours;
	findContours(diff_thresh, contours, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
	// drawContours(result, contours, -1, Scalar(0, 0, 255), 2);//在result上绘制轮廓  				

	//7.查找正外接矩形  
	vector<Rect> boundRect(contours.size());
	for (int i = 0; i < contours.size(); i++)
	{
		boundRect[i] = boundingRect(contours[i]);
		if (contours[i].size() > 500)//过滤掉较小的矩形
			rectangle(result, boundRect[i], Scalar(0, 255, 0), 2);//在result上绘制正外接矩形  
	}
	return result;
}
上一篇:opencv图像二值化操作


下一篇:opencv阈值处理——threshold函数、自适应阈值处理、Otsu处理(大津法)