opencv学习之路(37)、运动物体检测(二)
一、运动物体轮廓椭圆拟合及中心
#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行,天台行人视频适合用背景减法处理,自行车视频适合帧差法处理
二、滤波方法去除噪声
上篇文章中使用腐蚀膨胀消除噪声,这次使用滤波方法去除噪声
中值滤波
//二值化后使用中值滤波+膨胀
Mat element = getStructuringElement(MORPH_RECT, Size(, ));
medianBlur(diff, diff, );//中值滤波
imshow("medianBlur", diff);
dilate(diff, diff, element);
imshow("dilate", diff);
均值滤波
#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();
}
三、轮廓筛选去除噪声(效果挺好的)
//其余代码相同
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个外接矩形
}
四、运动轨迹绘制
#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();
}
五、车辆数量检测
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)、运动物体检测(二)的更多相关文章
- opencv学习之路【四】——opencv文件结构介绍
这里要感谢这篇博主的文章 部分内容转载自此 opencv在2.3版本之前 都是用的c语言实现的 而在2.3以后的版本 做了很多重大的改变 其中最主要的是用c++重写大部分结构 然后文件的结构和2.0之 ...
- opencv学习之路(36)、运动物体检测(一)
一.简介 二.背景减法 图片说明 #include "opencv2/opencv.hpp"using namespace cv; void main() { Mat img1 = ...
- opencv学习之路(32)、角点检测
一.角点检测的相关概念 二.Harris角点检测——cornerHarris() 参考网址: http://www.cnblogs.com/ronny/p/4009425.html #include ...
- OpenCV 学习笔记03 直线和圆检测
检测边缘和轮廓不仅重要,还经常用到,它们也是构成其他复杂操作的基础. 直线和形状检测与边缘和轮廓检测有密切的关系. 霍夫hough 变换是直线和形状检测背后的理论基础.霍夫变化是基于极坐标和向量开展的 ...
- Opencv学习之路—Opencv下基于HOG特征的KNN算法分类训练
在计算机视觉研究当中,HOG算法和LBP算法算是基础算法,但是却十分重要.后期很多图像特征提取的算法都是基于HOG和LBP,所以了解和掌握HOG,是学习计算机视觉的前提和基础. HOG算法的原理很多资 ...
- opencv学习之路(41)、人脸识别
一.人脸检测并采集个人图像 //take_photo.cpp #include<opencv2/opencv.hpp> using namespace cv; using namespac ...
- opencv学习之路(35)、SURF特征点提取与匹配(三)
一.简介 二.opencv中的SURF算法接口 三.特征点匹配方法 四.代码 1.特征点提取 #include "opencv2/opencv.hpp" #include < ...
- opencv学习之路(34)、SIFT特征匹配(二)
一.特征匹配简介 二.暴力匹配 1.nth_element筛选 #include "opencv2/opencv.hpp" #include <opencv2/nonfree ...
- opencv学习之路(33)、SIFT特征点提取(一)
一.简介 二.OpenCV中的SIFT算法接口 #include "opencv2/opencv.hpp" #include <opencv2/nonfree/nonfree ...
随机推荐
- Jmeter响应数据中文乱码
在用Jmeter测试的时候吸纳供应数据如果出现中文乱码解决方法: 1.如下图在Content encoding输入框内输入 UTF-8
- JAVA生成(可执行)Jar包的全面详解说明 [打包][SpringBoot][Eclipse][IDEA][Maven][Gradle][分离][可执行]
辛苦所得,转载还请注明: https://www.cnblogs.com/applerosa/p/9739007.html 得空整理了关于java 开发中,所有打包方式的 一个操作方法, 有基于ID ...
- HMAC-SHA256 签名方法各个语音的实现方式之前端JavaScriptes6
sha256和16进制输出,网上很多种后端的验证方法,几乎没有前端的,所以自己写了个,希望给类似需求的人一个帮助,适用场景 腾讯云接口鉴权 v3签名 npm install sha256npm ins ...
- nginx上通过ssl证书将http转发为https
环境:阿里云linux,ngnix 1.16.0 ,ssl证书,XXXX.jar 0.自行在阿里云上下载免费的ssl证书.里面有2个文件.key和pem后面要用到. 1.首先将项目在linux上跑起来 ...
- MGR
单主模式 参数修改 server_id=1 gtid_mode=ON enforce_gtid_consistency=ON binlog_checksum=NONE log_bin=binlog l ...
- 严重:one or more listeners failed. Full details will be found in the appropriate container log file
one or more listeners failed. Full details will be found in the appropriate container log file 这句话 ...
- 博客搬家 https://hanwang945.github.io/
博客搬家 https://hanwang945.github.io/
- 1.C++基础(C、C++)
1.命名空间 所谓namespace,是指标识符的各种可见范围.C++标准程序库中的所有标识符都被定 义于一个名为std的namespace中. 命名空间std封装的是标准程序库的名称,标准程序库为了 ...
- Install Superset from Python3.6
本文安装Superset大致分为以下部分: 在操作系统中安装相关依赖,我所用的操作系统为Centos6.5 安装Python3.6.6 安装Superset 详细步骤如下: 相关依赖的安装 yum i ...
- python 批量ping脚本不能用os.system
os.system(cmd)通过执行命令会得到返回值. ping通的情况下返回值为0. ping不通的情况: 1.请求超时,返回值1 2.无法访问目标主机,返回值为 0,和ping通返回值相同 所 ...