opencv学习之路(37)、运动物体检测(二)

时间:2021-10-29 19:41:15

一、运动物体轮廓椭圆拟合及中心

 #include "opencv2/opencv.hpp"
#include<iostream>
using namespace std;
using namespace cv; Mat MoveDetect(Mat frame1, Mat frame2)
{
Mat result = frame2.clone();
Mat gray1, gray2;
cvtColor(frame1, gray1, CV_BGR2GRAY);
cvtColor(frame2, gray2, CV_BGR2GRAY); Mat diff;
absdiff(gray1, gray2, diff);
imshow("absdiss", diff);
threshold(diff, diff, , , CV_THRESH_BINARY);
imshow("threshold", diff); Mat element = getStructuringElement(MORPH_RECT, Size(, ));
Mat element2 = getStructuringElement(MORPH_RECT, Size(, ));
erode(diff, diff, element);
imshow("erode", diff); dilate(diff, diff, element2);
imshow("dilate", diff); vector<vector<Point>> contours;
vector<Vec4i> hierarcy;
//画椭圆及中心
findContours(diff, contours, hierarcy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_NONE);
cout<<"num="<<contours.size()<<endl;
vector<RotatedRect> box(contours.size());
for(int i=; i<contours.size(); i++)
{
box[i] = fitEllipse(Mat(contours[i]));
ellipse(result, box[i], Scalar(, , ), , );
circle(result, box[i].center, , Scalar(, , ), -, );
}
return result;
} void main()
{
VideoCapture cap("E://man.avi");
if(!cap.isOpened()) //检查打开是否成功
return;
Mat frame;
Mat result;
Mat background;
int count=;
while()
{
cap>>frame;
if(frame.empty())
break;
else{
count++;
if(count==)
background = frame.clone(); //提取第一帧为背景帧
imshow("video", frame);
result = MoveDetect(background, frame);
imshow("result", result);
if(waitKey()==)
break;
}
}
cap.release();
}

和上一篇文章代码的不同点在30-38行,天台行人视频适合用背景减法处理,自行车视频适合帧差法处理

opencv学习之路(37)、运动物体检测(二)

二、滤波方法去除噪声

上篇文章中使用腐蚀膨胀消除噪声,这次使用滤波方法去除噪声

中值滤波

  //二值化后使用中值滤波+膨胀
  Mat element = getStructuringElement(MORPH_RECT, Size(, ));
medianBlur(diff, diff, );//中值滤波
imshow("medianBlur", diff);
dilate(diff, diff, element);
imshow("dilate", diff);

opencv学习之路(37)、运动物体检测(二)

均值滤波

#include "opencv2/opencv.hpp"
#include<iostream>
using namespace std;
using namespace cv; //int to string helper function
string intToString(int number)
{
stringstream ss;
ss << number;
return ss.str();
} Mat MoveDetect(Mat background, Mat img)
{
Mat result = img.clone();
Mat gray1, gray2;
cvtColor(background, gray1, CV_BGR2GRAY);
cvtColor(img, gray2, CV_BGR2GRAY); Mat diff;
absdiff(gray1, gray2, diff);
threshold(diff, diff, , , CV_THRESH_BINARY);
imshow("threshold", diff);
blur(diff, diff, Size(, ));//均值滤波
imshow("blur", diff); vector<vector<Point>> contours;
vector<Vec4i> hierarcy;
findContours(diff, contours, hierarcy, CV_RETR_EXTERNAL, CHAIN_APPROX_NONE); //查找轮廓
vector<Rect> boundRect(contours.size()); //定义外接矩形集合
//drawContours(img2, contours, -1, Scalar(0, 0, 255), 1, 8); //绘制轮廓
int x0=, y0=, w0=, h0=;
for(int i=; i<contours.size(); i++)
{
boundRect[i] = boundingRect((Mat)contours[i]); //查找每个轮廓的外接矩形 x0 = boundRect[i].x; //获得第i个外接矩形的左上角的x坐标
y0 = boundRect[i].y; //获得第i个外接矩形的左上角的y坐标
w0 = boundRect[i].width; //获得第i个外接矩形的宽度
h0 = boundRect[i].height; //获得第i个外接矩形的高度
//rectangle(result, Point(x0, y0), Point(x0+w0, y0+h0), Scalar(0, 255, 0), 2, 8); //绘制第i个外接矩形
circle(result, Point(x0+w0/, y0+h0/), , Scalar(, , ), , );
line(result, Point(x0+w0/-, y0+h0/), Point(x0+w0/+, y0+h0/), Scalar(, , ), , );
line(result, Point(x0+w0/, y0+h0/-), Point(x0+w0/, y0+h0/+), Scalar(, , ), , );
putText(result,"(" + intToString(x0+w0/)+","+intToString(y0+h0/)+")",Point(x0+w0/+, y0+h0/), , ,Scalar(,,),);
}
return result;
} void main()
{
VideoCapture cap("E://ball.avi");
if(!cap.isOpened()) //检查打开是否成功
return;
Mat frame;
Mat result;
Mat background;
int count=;
while()
{
cap>>frame;
if(frame.empty())
break;
else{
count++;
if(count==)
background = frame.clone(); //提取第一帧为背景帧
imshow("video", frame);
result = MoveDetect(background, frame);
imshow("result", result);
if(waitKey()==)
break;
}
}
cap.release();
}

opencv学习之路(37)、运动物体检测(二)

三、轮廓筛选去除噪声(效果挺好的)

    //其余代码相同
int x0=, y0=, w0=, h0=;
for(int i=; i<contours.size(); i++)
{
boundRect[i] = boundingRect((Mat)contours[i]); //查找每个轮廓的外接矩形 x0 = boundRect[i].x; //获得第i个外接矩形的左上角的x坐标
y0 = boundRect[i].y; //获得第i个外接矩形的左上角的y坐标
w0 = boundRect[i].width; //获得第i个外接矩形的宽度
h0 = boundRect[i].height; //获得第i个外接矩形的高度
//筛选
if(w0> && h0>)
rectangle(result, Point(x0, y0), Point(x0+w0, y0+h0), Scalar(, , ), , ); //绘制第i个外接矩形
}

opencv学习之路(37)、运动物体检测(二)

四、运动轨迹绘制

#include "opencv2/opencv.hpp"
#include<iostream>
using namespace std;
using namespace cv; Point center;
Point fre_center;//存储前一帧中心坐标
int num=;
vector<Point> points; Mat MoveDetect(Mat background, Mat img)
{
Mat result = img.clone();
Mat gray1, gray2;
cvtColor(background, gray1, CV_BGR2GRAY);
cvtColor(img, gray2, CV_BGR2GRAY); Mat diff;
absdiff(gray1, gray2, diff);
imshow("absdiss", diff);
threshold(diff, diff, , , CV_THRESH_BINARY);
imshow("threshold", diff); Mat element = getStructuringElement(MORPH_RECT, Size(, ));
Mat element2 = getStructuringElement(MORPH_RECT, Size(, ));
erode(diff, diff, element);
imshow("erode", diff);
dilate(diff, diff, element2);
imshow("dilate", diff); vector<vector<Point>> contours;
vector<Vec4i> hierarcy;
findContours(diff, contours, hierarcy, CV_RETR_EXTERNAL, CHAIN_APPROX_NONE); //查找轮廓
vector<Rect> boundRect(contours.size()); //定义外接矩形集合
//drawContours(img2, contours, -1, Scalar(0, 0, 255), 1, 8); //绘制轮廓
vector<RotatedRect> box(contours.size());
int x0=, y0=, w0=, h0=;
for(int i=; i<contours.size(); i++)
{
boundRect[i] = boundingRect((Mat)contours[i]); //查找每个轮廓的外接矩形 x0 = boundRect[i].x; //获得第i个外接矩形的左上角的x坐标
y0 = boundRect[i].y; //获得第i个外接矩形的左上角的y坐标
w0 = boundRect[i].width; //获得第i个外接矩形的宽度
h0 = boundRect[i].height; //获得第i个外接矩形的高度
if(w0> && h0>)//筛选长宽大于30的轮廓
{
num++;
//rectangle(result, Point(x0, y0), Point(x0+w0, y0+h0), Scalar(0, 255, 0), 2, 8); //绘制第i个外接矩形
box[i] = fitEllipse(Mat(contours[i]));
ellipse(result, box[i], Scalar(, , ), , ); //椭圆轮廓
circle(result, box[i].center, , Scalar(, , ), -, ); //画中心
center = box[i].center;//当前帧的中心坐标
points.push_back(center);//中心塞进points向量集
if(num !=)
{
//line(result, fre_center, center, Scalar(255, 0, 0), 2, 8);
for(int j=; j<points.size()-; j++)
line(result, points[j], points[j+], Scalar(, , ), , );
}
//fre_center = center;
}
}
return result;
} void main()
{
VideoCapture cap("E://man.avi");
if(!cap.isOpened()) //检查打开是否成功
return;
Mat frame;
Mat background;
Mat result;
int count=;
while()
{
cap>>frame;
if(!frame.empty())
{
count++;
if(count==)
background = frame.clone(); //提取第一帧为背景帧
imshow("video", frame);
result = MoveDetect(background, frame);
imshow("result", result);
if(waitKey()==)
break;
}
else
break;
}
cap.release();
}

opencv学习之路(37)、运动物体检测(二)

五、车辆数量检测

1.帧差法检测运动目标

2.预处理:a.转灰度图,绝对值做差  b.二值化,腐蚀,中值滤波,膨胀  c.查找轮廓,筛选轮廓,绘制外接矩形,计数,输出

 #include "opencv2/opencv.hpp"
#include<iostream>
using namespace std;
using namespace cv; int CarNum = ;
//int to string helper function
string intToString(int number)
{
//this function has a number input and string output
stringstream ss;
ss << number;
return ss.str();
} Mat MoveDetect(Mat frame1, Mat frame2) {
Mat result = frame2.clone();
Mat gray1, gray2;
cvtColor(frame1, gray1, CV_BGR2GRAY);
cvtColor(frame2, gray2, CV_BGR2GRAY); Mat diff;
absdiff(gray1, gray2, diff);
//imshow("absdiss", diff);
threshold(diff, diff, , , CV_THRESH_BINARY);
imshow("threshold", diff); Mat element = getStructuringElement(MORPH_RECT, Size(, ));
Mat element2 = getStructuringElement(MORPH_RECT, Size(, ));
erode(diff, diff, element);
//imshow("erode", dst);
medianBlur(diff, diff, );
imshow("medianBlur", diff);
dilate(diff, diff, element2);
imshow("dilate", diff); vector<vector<Point>> contours;
vector<Vec4i> hierarcy;
findContours(diff, contours, hierarcy, CV_RETR_EXTERNAL, CV_CHAIN_APPROX_SIMPLE, Point(, ));//查找轮廓
vector<vector<Point>>contours_poly(contours.size());
vector<Rect> boundRect(contours.size()); //定义外接矩形集合
//drawContours(img2, contours, -1, Scalar(0, 0, 255), 1, 8); //绘制轮廓
int x0 = , y0 = , w0 = , h0 = ;
for (int i = ; i<contours.size(); i++)
{
approxPolyDP(Mat(contours[i]), contours_poly[i], , true);//对图像轮廓点进行多边形拟合:轮廓点组成的点集,输出的多边形点集,精度(即两个轮廓点之间的距离),输出多边形是否封闭
boundRect[i] = boundingRect(Mat(contours_poly[i]));
if (boundRect[i].width> && boundRect[i].width< && boundRect[i].height> && boundRect[i].height<) {//轮廓筛选
x0 = boundRect[i].x;
y0 = boundRect[i].y;
w0 = boundRect[i].width;
h0 = boundRect[i].height; rectangle(result, Point(x0, y0), Point(x0 + w0, y0 + h0), Scalar(, , ), , , );
if ((y0 + h0 / + ) >= && (y0 + h0 / - ) <= ) {//经过这条线(区间),车辆数量+1
CarNum++;
}
}
line(result, Point(, ), Point(, ), Scalar(, , ), , );//画红线
Point org(, );
putText(result, "CarNum=" + intToString(CarNum), org, CV_FONT_HERSHEY_SIMPLEX, 0.8f, Scalar(, , ), );
}
return result;
} void main()
{
VideoCapture cap("E://2.avi");
if (!cap.isOpened()) //检查打开是否成功
return;
Mat frame;
Mat tmp;
Mat result;
int count = ;
while ()
{
cap >> frame;
if(frame.empty())//检查视频是否结束
break;
else{
count++;
if (count == )
result = MoveDetect(frame, frame);
else result = MoveDetect(tmp, frame);
imshow("video", frame);
imshow("result", result);
tmp = frame.clone();
if (waitKey() == )
break;
}
}
cap.release();
}

opencv学习之路(37)、运动物体检测(二)