运动物体检测顾名思义就是在视频(视频文件、摄像头获取)中检测运动物体(目标)。
OpenCV中常用的运动物体检测有背景差法、帧差法、光流法,运动物体检测广泛应用于视频安全监控、车辆检测等方面。
本博文主要介绍背景差法与帧差法:
背景差法:就是用原图像减去背景模型,剩下的就是前景图像,即运动目标
帧差法:就是利用相邻的两帧或者三帧图像,利用像素之间的差异性,判断是否有运动目标
(视频就是一帧一帧图像组成的、求图像差异最基本的就是图像减法--suntract,absdiff)
背景减法基本步骤:原图-背景------阈值处理------去除噪声(腐蚀滤波)------膨胀连通-----查找轮廓-----外接矩形(椭圆/圆)
一个摄像头:
#include "opencv2/opencv.hpp"
#include
using namespace std;
using namespace cv;
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; i140 && h0>140)
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>=19600)
putText(result, "Is Blocked ", org , CV_FONT_HERSHEY_SIMPLEX,0.8f,Scalar(0, 255, 0),2);
}
return result;
}
void main()
{
VideoCapture cap;
cap.open(0);
if (!cap.isOpened()) //检查打开是否成功
return;
Mat frame;
Mat background;
Mat result;
int count = 0;
while (1)
{
cap >> frame;
if (!frame.empty())
{
count++;
if (count == 1)
background = frame.clone(); //提取第一帧为背景帧
//imshow("video", frame);
result = MoveDetect(background, frame);
imshow("result", result);
if (waitKey(50) == 27)
break;
}
else
continue;
}
cap.release();
}
两个摄像头:
#include "opencv2/opencv.hpp"
#include
using namespace std;
using namespace cv;
Mat MoveDetect01(Mat background01, Mat img)
{
//将background和img转为灰度图
Mat result = img.clone();
Mat gray1, gray2;
cvtColor(background01, gray1, CV_BGR2GRAY);
cvtColor(img, gray2, CV_BGR2GRAY);
//进行canny边缘检测
Canny(background01, background01, 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; i140 && h0>140)
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 >= 19600)
putText(result, "Is Blocked ", org, CV_FONT_HERSHEY_SIMPLEX, 0.8f, Scalar(0, 255, 0), 2);
}
return result;
}
Mat MoveDetect02(Mat background02, Mat img02)
{
//将background和img转为灰度图
Mat result02 = img02.clone();
Mat gray1, gray2;
cvtColor(background02, gray1, CV_BGR2GRAY);
cvtColor(img02, gray2, CV_BGR2GRAY);
//进行canny边缘检测
Canny(background02, background02, 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; i140 && h0>140)
rectangle(result02, Point(x0, y0), Point(x0 + w0, y0 + h0), Scalar(0, 255, 0), 2, 8); //绘制第i个外接矩形
//文字输出
Point org(10, 35);
if (i >= 1 && AreaAll >= 19600)
putText(result02, "Is Blocked ", org, CV_FONT_HERSHEY_SIMPLEX, 0.8f, Scalar(0, 255, 0), 2);
}
return result02;
}
void main()
{
VideoCapture cap01;
VideoCapture cap02;
cap01.open(0);
cap02.open(1);
if (!cap01.isOpened() && !cap02.isOpened()) //检查打开是否成功
return;
Mat frame01, frame02;
Mat background01, background02;
Mat result01, result02;
int count01 = 0;
int count02 = 0;
while (1)
{
cap01 >> frame01;
if (!frame01.empty())
{
count01++;
if (count01 == 1)
background01 = frame01.clone(); //提取第一帧为背景帧
//imshow("video", frame);
result01 = MoveDetect01(background01, frame01);
imshow("result01", result01);
}
cap02 >> frame02;
if (!frame02.empty())
{
count02++;
if (count02 == 1)
background02 = frame02.clone(); //提取第一帧为背景帧
//imshow("video", frame);
result02 = MoveDetect02(background02, frame02);
imshow("result02", result02);
if (waitKey(50) == 27)
break;
}
else
continue;
}
cap01.release();
cap02.release();
}