#include "core/core.hpp"
#include "highgui/highgui.hpp"
#include "imgproc/imgproc.hpp"
#include
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 element2 = getStructuringElement(MORPH_RECT, Size(15, 15));
erode(diff, diff, element);
//imshow("erode", diff);
dilate(diff, diff, element2);
//imshow("dilate", diff);
*/
//二值化后使用中值滤波+膨胀
Mat element = getStructuringElement(MORPH_RECT, Size(11, 11));
medianBlur(diff, diff, 5);//中值滤波
//imshow("medianBlur", diff);
dilate(diff, diff, element);
//blur(diff, diff, Size(10, 10)); //均值滤波
//imshow("dilate", diff);
//查找并绘制轮廓
vector> contours;
vector hierarcy;
findContours(diff, contours, hierarcy, CV_RETR_EXTERNAL, CHAIN_APPROX_NONE); //查找轮廓
vector boundRect(contours.size()); //定义外接矩形集合
//drawContours(img2, contours, -1, Scalar(0, 0, 255), 1, 8); //绘制轮廓
//查找正外接矩形
int x0 = 0, y0 = 0, w0 = 0, h0 = 0;
double Area = 0, AreaAll = 0;
for (int i = 0; iw0>100 && 500>h0>100)
rectangle(result, Point(x0, y0), Point(x0 + w0, y0 + h0), Scalar(0, 255, 0), 2, 8); //绘制第i个外接矩形
//文字输出
Point org(10, 35);
if (i >= 1 && AreaAll >= 10000)
putText(result, "Is Blocked ", org, CV_FONT_HERSHEY_SCRIPT_SIMPLEX, 1.5f, Scalar(0, 255, 0), 2);
}
return result;
}
int main()
{
VideoCapture cap;
cap.open(0);
if (!cap.isOpened())
return 0;
double fps = cap.get(CV_CAP_PROP_FPS); //获取视频帧率
double pauseTime = 1000 / fps; //两幅画面中间间隔
namedWindow("Cap");
setMouseCallback("Cap", onMouse);
Mat background;
Mat image;
Mat ImageROI;
Mat result;
int count = 0;
int flag = 0;
while (1)
{
if (!leftButtonDownFlag) //判定鼠标左键没有按下,采取播放视频,否则暂停
{
cap >> frame;
}
if (waitKey(50) == 27) //Esc键按下退出播放
{
break;
}
if (originalPoint != processPoint &&!leftButtonDownFlag)
{
rectangle(frame, originalPoint, processPoint, Scalar(255, 0, 0), 2);
ImageROI = frame(Rect(originalPoint, processPoint)); //子图像显示
imshow("ROI", ImageROI);
flag = 1;
}
cap >> frame;
if (frame.empty())
break;
else
{
count++;
if (count == 1)
background = frame.clone();
if (flag == 1)
{
rectangle(background, originalPoint, processPoint, Scalar(255, 0, 0), 2);
image = background(Rect(originalPoint, processPoint));
result = MoveDetect(image, ImageROI);
imshow("ROI", result);
}
imshow("Cap",frame);
}
}
cap.release();
}
#include "core/core.hpp"
#include "highgui/highgui.hpp"
#include "imgproc/imgproc.hpp"
#include
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 element2 = getStructuringElement(MORPH_RECT, Size(15, 15));
erode(diff, diff, element);
//imshow("erode", diff);
dilate(diff, diff, element2);
//imshow("dilate", diff);
*/
//二值化后使用中值滤波+膨胀
Mat element = getStructuringElement(MORPH_RECT, Size(11, 11));
medianBlur(diff, diff, 5);//中值滤波
//imshow("medianBlur", diff);
dilate(diff, diff, element);
//blur(diff, diff, Size(10, 10)); //均值滤波
//imshow("dilate", diff);
//查找并绘制轮廓
vector> contours;
vector hierarcy;
findContours(diff, contours, hierarcy, CV_RETR_EXTERNAL, CHAIN_APPROX_NONE); //查找轮廓
vector boundRect(contours.size()); //定义外接矩形集合
//drawContours(img2, contours, -1, Scalar(0, 0, 255), 1, 8); //绘制轮廓
//查找正外接矩形
int x0 = 0, y0 = 0, w0 = 0, h0 = 0;
double Area = 0, AreaAll = 0;
for (int i = 0; i80 && h0>80)
rectangle(result, Point(x0, y0), Point(x0 + w0, y0 + h0), Scalar(0, 255, 0), 2, 8); //绘制第i个外接矩形
//文字输出
Point org(10, 35);
if (i >= 1 && AreaAll >= 6400)
putText(result, "Is Blocked ", org, CV_FONT_HERSHEY_SIMPLEX, 0.8f, Scalar(0, 255, 0), 2);
}
return result;
}
int main()
{
VideoCapture cap;
cap.open(0);
if (!cap.isOpened())
return 0;
double fps = cap.get(CV_CAP_PROP_FPS); //获取视频帧率
double pauseTime = 1000 / fps; //两幅画面中间间隔
namedWindow("Cap");
setMouseCallback("Cap", onMouse);
Mat background;
Mat result;
int count = 0;
while (1)
{
if (!leftButtonDownFlag) //判定鼠标左键没有按下,采取播放视频,否则暂停
{
cap >> frame;
}
if (waitKey(50) == 27) //Esc键按下退出播放
{
break;
}
if (originalPoint != processPoint&&!leftButtonDownFlag)
{
rectangle(frame, originalPoint, processPoint, Scalar(255, 0, 0), 2);
Mat ImageROI= frame(Rect(originalPoint, processPoint)); //子图像显示
imshow("ROI", ImageROI);
}
cap >> frame;
if (frame.empty())
break;
else
{
count++;
if (count == 1)
background = frame.clone();
result = MoveDetect(background, frame);
imshow("Cap",result);
}
}
cap.release();
}