可用版:
#include "core/core.hpp"
#include "highgui/highgui.hpp"
#include "imgproc/imgproc.hpp"
#include<iostream>
using namespace cv;
using namespace std;
Mat frame;
Mat frameCopy; //绘制矩形框时用来拷贝原图的图像
bool leftButtonDownFlag = false; //左键单击后视频暂停播放的标志位
Point originalPoint; //矩形框起点
Point processPoint; //矩形框终点
//*******************************************************************//
//鼠标回调函数
void onMouse(int event, int x, int y, int flags, void *ustc)
{
if (event == CV_EVENT_LBUTTONDOWN)
{
leftButtonDownFlag = true; //标志位
originalPoint = Point(x, y); //设置左键按下点的矩形起点
processPoint = originalPoint;
}
if (event == CV_EVENT_MOUSEMOVE&&leftButtonDownFlag)
{
frameCopy = frame.clone();
processPoint = Point(x, y);
if (originalPoint != processPoint)
{
//在复制的图像上绘制矩形
rectangle(frameCopy, originalPoint, processPoint, Scalar(255, 0, 0), 2);
}
imshow("Cap", frameCopy);
}
if (event == CV_EVENT_LBUTTONUP)
{
leftButtonDownFlag = false;
Mat rectImage = frame(Rect(originalPoint, processPoint)); //子图像显示
imshow("ROI", rectImage);
}
}
Mat MoveDetect(Mat background, Mat img)
{
//将background和img转为灰度图
Mat result = img.clone();
Mat gray1, gray2;
cvtColor(background, gray1, CV_BGR2GRAY);
cvtColor(img, gray2, CV_BGR2GRAY);
//进行canny边缘检测
Canny(background, background, 0, 30, 3);
//将background和img做差;对差值图diff进行阈值化处理
Mat diff;
absdiff(gray1, gray2, diff);
//imshow("absdiss", diff);
threshold(diff, diff, 50, 255, CV_THRESH_BINARY);
//imshow("threshold", diff);
/*
//腐蚀膨胀消除噪音
Mat element = getStructuringElement(MORPH_RECT, Size(3, 3));
Mat element