【OpenCV】OpenCV3的第二天——光流法(Optial Flow)运动目标检测
2016-03-30 09:47
405 查看
基于特征点的跟踪算法大致可以分为两个步骤:
1)探测当前帧的特征点;
2)通过当前帧和下一帧灰度比较,估计当前帧特征点在下一帧的位置;
3)过滤位置不变的特征点,余下的点就是目标了。
特征点包括:
1、Harris
2、SURF
3、FAST
4、STAR
5、SIFT
6、ORB
7、MSER
8、GETT
9、Dense
10、SimpleBlob
光流的基本方法:
1、基于梯度
2、基于匹配
3、基于能量
4、基于相位
光流法首先假设的条件:
1、亮度恒定
2、小运动
3、空间一致
使用函数:
goodFeaturesToTrack()——寻找图像中具有大特征的角点
calcOpticalFlowPyrLK()——计算一个稀疏特征集的光流
两篇博客链接:
/article/1358133.html
OpenCV3编程入门——光流法的代码
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/video/video.hpp>
using namespace cv;
using namespace std;
void duan_OpticalFlow( Mat &frame, Mat & result);
bool addNewPoints();
bool acceptTrackedPoint(int i);
int main()
{
VideoCapture video(0);
Mat matSrc;
Mat matRst;
long lNum = 0;
if (video.isOpened())
{
while(1)
{
video >> matSrc;
if ( !matSrc.empty())
{
lNum++;
duan_OpticalFlow(matSrc, matRst);
cout << "This picture is " << lNum << endl;
}
else
{
cout << "Error : Get picture is empty!" << endl;
}
}
}
else
{
cout << "Error : Open this camera is Fail!\n" << endl;
}
return 0;
}
Mat curgray; //Current mat
Mat pregray; // next mat
vector<Point2f> point[2];
vector<Point2f> initPoint;
vector<Point2f> features;
int maxCount = 500; // max feature number
double qLevel = 0.01; // level
double minDist = 10.0; // Two features distance
vector<uchar> status; // find optical flow is 1
vector<float> err;
void duan_OpticalFlow( Mat &frame, Mat & result)
{
cvtColor(frame, curgray, CV_BGR2GRAY);
frame.copyTo(result);
if (addNewPoints())
{
goodFeaturesToTrack(curgray, features, maxCount,qLevel,minDist);
point[0].insert(point[0].end(), features.begin(), features.end());
initPoint.insert(initPoint.end(), features.begin(), features.end());
}
if (pregray.empty())
{
curgray.copyTo(pregray);
}
calcOpticalFlowPyrLK(pregray, curgray, point[0], point[1], status, err);
int k = 0;
for (size_t i=0; i<point[1].size(); i++)
{
if (acceptTrackedPoint(i))
{
initPoint[k] = initPoint[i];
point[1][k++] = point[1][i];
}
}
point[1].resize(k);
initPoint.resize(k);
for (size_t i=0; i<point[1].size(); i++)
{
line(result, initPoint[i], point[1][i], Scalar(0, 0, 255));
circle(result, point[1][i], 3, Scalar(0, 255, 0), -1);
}
swap(point[1], point[0]);
swap(pregray, curgray);
imshow("Optical Flow Demo", result);
waitKey(50);
}
bool addNewPoints()
{
return point[0].size() <= 10;
}
bool acceptTrackedPoint(int i)
{
return status[i] && ((abs(point[0][i].x - point[1][i].x) + abs(point[0][i].y - point[1][i].y)) > 2);
}
1)探测当前帧的特征点;
2)通过当前帧和下一帧灰度比较,估计当前帧特征点在下一帧的位置;
3)过滤位置不变的特征点,余下的点就是目标了。
特征点包括:
1、Harris
2、SURF
3、FAST
4、STAR
5、SIFT
6、ORB
7、MSER
8、GETT
9、Dense
10、SimpleBlob
光流的基本方法:
1、基于梯度
2、基于匹配
3、基于能量
4、基于相位
光流法首先假设的条件:
1、亮度恒定
2、小运动
3、空间一致
使用函数:
goodFeaturesToTrack()——寻找图像中具有大特征的角点
calcOpticalFlowPyrLK()——计算一个稀疏特征集的光流
两篇博客链接:
Opencv学习笔记(九)光流法
/article/1358133.html【算法分析】Lucas–Kanade光流算法
/article/4750676.htmlOpenCV3编程入门——光流法的代码
#include <iostream>
#include <opencv2/core/core.hpp>
#include <opencv2/highgui/highgui.hpp>
#include <opencv2/video/video.hpp>
using namespace cv;
using namespace std;
void duan_OpticalFlow( Mat &frame, Mat & result);
bool addNewPoints();
bool acceptTrackedPoint(int i);
int main()
{
VideoCapture video(0);
Mat matSrc;
Mat matRst;
long lNum = 0;
if (video.isOpened())
{
while(1)
{
video >> matSrc;
if ( !matSrc.empty())
{
lNum++;
duan_OpticalFlow(matSrc, matRst);
cout << "This picture is " << lNum << endl;
}
else
{
cout << "Error : Get picture is empty!" << endl;
}
}
}
else
{
cout << "Error : Open this camera is Fail!\n" << endl;
}
return 0;
}
Mat curgray; //Current mat
Mat pregray; // next mat
vector<Point2f> point[2];
vector<Point2f> initPoint;
vector<Point2f> features;
int maxCount = 500; // max feature number
double qLevel = 0.01; // level
double minDist = 10.0; // Two features distance
vector<uchar> status; // find optical flow is 1
vector<float> err;
void duan_OpticalFlow( Mat &frame, Mat & result)
{
cvtColor(frame, curgray, CV_BGR2GRAY);
frame.copyTo(result);
if (addNewPoints())
{
goodFeaturesToTrack(curgray, features, maxCount,qLevel,minDist);
point[0].insert(point[0].end(), features.begin(), features.end());
initPoint.insert(initPoint.end(), features.begin(), features.end());
}
if (pregray.empty())
{
curgray.copyTo(pregray);
}
calcOpticalFlowPyrLK(pregray, curgray, point[0], point[1], status, err);
int k = 0;
for (size_t i=0; i<point[1].size(); i++)
{
if (acceptTrackedPoint(i))
{
initPoint[k] = initPoint[i];
point[1][k++] = point[1][i];
}
}
point[1].resize(k);
initPoint.resize(k);
for (size_t i=0; i<point[1].size(); i++)
{
line(result, initPoint[i], point[1][i], Scalar(0, 0, 255));
circle(result, point[1][i], 3, Scalar(0, 255, 0), -1);
}
swap(point[1], point[0]);
swap(pregray, curgray);
imshow("Optical Flow Demo", result);
waitKey(50);
}
bool addNewPoints()
{
return point[0].size() <= 10;
}
bool acceptTrackedPoint(int i)
{
return status[i] && ((abs(point[0][i].x - point[1][i].x) + abs(point[0][i].y - point[1][i].y)) > 2);
}
相关文章推荐
- 使用unbound在RHEL7上搭建DNS服务
- linux一键安装包的使用教程
- CentOS 安装Java环境
- runloop的理解
- 自学鸟哥linux服务-NTP时间服务器
- 将要改变IT世界的的docker技术是什么?
- XSHELL 绝佳配色方案, 保护你的眼睛
- Window.open的用法及乱码解决
- iOS- 系统架构的区别
- iptables详解
- 性别字段存储时应该使用的字符串,还是数字?
- myeclipse导入工程没有错误之后,tomcat运行也没错误,但是浏览器访问该工程时报错。
- Linux安装Tomcat
- CenOS7修改软件安装源到阿里云
- LINUX 段错误查找记录 -- segfault at fffffffffffffff9 ip 0000003c97e7b81c sp 00007fffad7c0638 error 4 in lib
- Linux_用户级_常用命令(1)
- java tomcat内存溢出
- Linux安装jdk_8
- DropDownList 绑定DataTable并给默认值
- DRBD 高可用配置详解(转)