申明:本文非笔者原创,原文转载自:http://blog.csdn.NET/crzy_sparrow/article/details/7407604

本文目录:

一.基于特征点的目标跟踪的一般方法

二.光流法

三.OpenCV中的光流法函数

四.用类封装基于光流法的目标跟踪方法

五.完整代码

六.参考文献

一.基于特征点的目标跟踪的一般方法

基于特征点的跟踪算法大致可以分为两个步骤:

1)探测当前帧的特征点;

2)通过当前帧和下一帧灰度比较,估计当前帧特征点在下一帧的位置;

3)过滤位置不变的特征点,余下的点就是目标了。

很显然,基于特征点的目标跟踪算法和1),2)两个步骤有关。特征点可以是Harris角点(见我的另外一篇博文),也可以是边缘点等等,而估计下一帧位置的方法也有不少,比如这里要讲的光流法,也可以是卡尔曼滤波法(咱是控制系的,上课经常遇到这个,所以看光流法看着看着就想到这个了)。

本文中,用改进的Harris角点提取特征点(见我另一篇博文:http://blog.csdn.Net/crzy_sparrow/article/details/7391511),用Lucas-Kanade光流法实现目标跟踪。

二.光流法

这一部分《learing opencv》一书的第10章Lucas-Kanade光流部分写得非常详细,推荐大家看书。我这里也粘帖一些选自书中的内容。

另外我对这一部分附上一些个人的看法(谬误之处还望不吝指正):

1.首先是假设条件:

(1)亮度恒定,就是同一点随着时间的变化,其亮度不会发生改变。这是基本光流法的假定(所有光流法变种都必须满足),用于得到光流法基本方程;

(2)小运动,这个也必须满足,就是时间的变化不会引起位置的剧烈变化,这样灰度才能对位置求偏导(换句话说,小运动情况下我们才能用前后帧之间单位位置变化引起的灰度变化去近似灰度对位置的偏导数),这也是光流法不可或缺的假定;

(3)空间一致,一个场景上邻近的点投影到图像上也是邻近点,且邻近点速度一致。这是Lucas-Kanade光流法特有的假定,因为光流法基本方程约束只有一个,而要求x,y方向的速度,有两个未知变量。我们假定特征点邻域内做相似运动,就可以连立n多个方程求取x,y方向的速度(n为特征点邻域总点数,包括该特征点)。

2.方程求解

多个方程求两个未知变量,又是线性方程,很容易就想到用最小二乘法,事实上opencv也是这么做的。其中,最小误差平方和为最优化指标。

3.好吧,前面说到了小运动这个假定,聪明的你肯定很不爽了,目标速度很快那这货不是二掉了。幸运的是多尺度能解决这个问题。首先,对每一帧建立一个高斯金字塔,最大尺度图片在最顶层,原始图片在底层。然后,从顶层开始估计下一帧所在位置,作为下一层的初始位置,沿着金字塔向下搜索,重复估计动作,直到到达金字塔的底层。聪明的你肯定发现了:这样搜索不仅可以解决大运动目标跟踪,也可以一定程度上解决孔径问题(相同大小的窗口能覆盖大尺度图片上尽量多的角点,而这些角点无法在原始图片上被覆盖)。

三.opencv中的光流法函数

opencv2.3.1中已经实现了基于光流法的特征点位置估计函数(当前帧位置已知,前后帧灰度已知),介绍如下(摘自opencv2.3.1参考手册):

[cpp] view plaincopy
  1. calcOpticalFlowPyrLK
  2. Calculates an optical flow for a sparse feature set using the iterative Lucas-Kanade method with pyramids.
  3. void calcOpticalFlowPyrLK(InputArray prevImg, InputArray nextImg, InputArray prevPts,
  4. InputOutputArray nextPts, OutputArray status, OutputArray err,
  5. Size winSize=Size(15,15), int maxLevel=3, TermCriteria crite-
  6. ria=TermCriteria(TermCriteria::COUNT+TermCriteria::EPS, 30, 0.01),
  7. double derivLambda=0.5, int flags=0 )
  8. Parameters
  9. prevImg – First 8-bit single-channel or 3-channel input image.
  10. nextImg – Second input image of the same size and the same type as prevImg .
  11. prevPts – Vector of 2D points for which the flow needs to be found. The point coordinates
  12. must be single-precision floating-point numbers.
  13. nextPts – Output vector of 2D points (with single-precision floating-point coordinates)
  14. containing the calculated new positions of input features in the second image. When
  15. OPTFLOW_USE_INITIAL_FLOW flag is passed, the vector must have the same size as in the
  16. input.
  17. status – Output status vector. Each element of the vector is set to 1 if the flow for the
  18. corresponding features has been found. Otherwise, it is set to 0.
  19. err – Output vector that contains the difference between patches around the original and
  20. moved points.
  21. winSize – Size of the search window at each pyramid level.
  22. maxLevel – 0-based maximal pyramid level number. If set to 0, pyramids are not used
  23. (single level). If set to 1, two levels are used, and so on.
  24. criteria – Parameter specifying the termination criteria of the iterative search algorithm
  25. (after the specified maximum number of iterations criteria.maxCount or when the search
  26. window moves by less than criteria.epsilon .
  27. derivLambda – Not used.
  28. flags – Operation flags:
  29. – OPTFLOW_USE_INITIAL_FLOW Use initial estimations stored in nextPts . If the
  30. flag is not set, then prevPts is copied to nextPts and is considered as the initial estimate.

四.用类封装基于光流法的目标跟踪方法

废话少说,附上代码,包括特征点提取,跟踪特征点,标记特征点等。

[cpp] view plaincopy
  1. <span style="font-size:18px;">//帧处理基类
  2. class FrameProcessor{
  3. public:
  4. virtual void process(Mat &input,Mat &ouput)=0;
  5. };
  6. //特征跟踪类,继承自帧处理基类
  7. class FeatureTracker :  public FrameProcessor{
  8. Mat gray;  //当前灰度图
  9. Mat gray_prev;  //之前的灰度图
  10. vector<Point2f> points[2];//前后两帧的特征点
  11. vector<Point2f> initial;//初始特征点
  12. vector<Point2f> features;//检测到的特征
  13. int max_count; //要跟踪特征的最大数目
  14. double qlevel; //特征检测的指标
  15. double minDist;//特征点之间最小容忍距离
  16. vector<uchar> status; //特征点被成功跟踪的标志
  17. vector<float> err; //跟踪时的特征点小区域误差和
  18. public:
  19. FeatureTracker():max_count(500),qlevel(0.01),minDist(10.){}
  20. void process(Mat &frame,Mat &output){
  21. //得到灰度图
  22. cvtColor (frame,gray,CV_BGR2GRAY);
  23. frame.copyTo (output);
  24. //特征点太少了,重新检测特征点
  25. if(addNewPoint()){
  26. detectFeaturePoint ();
  27. //插入检测到的特征点
  28. points[0].insert (points[0].end (),features.begin (),features.end ());
  29. initial.insert (initial.end (),features.begin (),features.end ());
  30. }
  31. //第一帧
  32. if(gray_prev.empty ()){
  33. gray.copyTo (gray_prev);
  34. }
  35. //根据前后两帧灰度图估计前一帧特征点在当前帧的位置
  36. //默认窗口是15*15
  37. calcOpticalFlowPyrLK (
  38. gray_prev,//前一帧灰度图
  39. gray,//当前帧灰度图
  40. points[0],//前一帧特征点位置
  41. points[1],//当前帧特征点位置
  42. status,//特征点被成功跟踪的标志
  43. err);//前一帧特征点点小区域和当前特征点小区域间的差,根据差的大小可删除那些运动变化剧烈的点
  44. int k = 0;
  45. //去除那些未移动的特征点
  46. for(int i=0;i<points[1].size ();i++){
  47. if(acceptTrackedPoint (i)){
  48. initial[k]=initial[i];
  49. points[1][k++] = points[1][i];
  50. }
  51. }
  52. points[1].resize (k);
  53. initial.resize (k);
  54. //标记被跟踪的特征点
  55. handleTrackedPoint (frame,output);
  56. //为下一帧跟踪初始化特征点集和灰度图像
  57. std::swap(points[1],points[0]);
  58. cv::swap(gray_prev,gray);
  59. }
  60. void detectFeaturePoint(){
  61. goodFeaturesToTrack (gray,//输入图片
  62. features,//输出特征点
  63. max_count,//特征点最大数目
  64. qlevel,//质量指标
  65. minDist);//最小容忍距离
  66. }
  67. bool addNewPoint(){
  68. //若特征点数目少于10,则决定添加特征点
  69. return points[0].size ()<=10;
  70. }
  71. //若特征点在前后两帧移动了,则认为该点是目标点,且可被跟踪
  72. bool acceptTrackedPoint(int i){
  73. return status[i]&&
  74. (abs(points[0][i].x-points[1][i].x)+
  75. abs(points[0][i].y-points[1][i].y) >2);
  76. }
  77. //画特征点
  78. void  handleTrackedPoint(Mat &frame,Mat &output){
  79. for(int i=0;i<points[i].size ();i++){
  80. //当前特征点到初始位置用直线表示
  81. line(output,initial[i],points[1][i],Scalar::all (0));
  82. //当前位置用圈标出
  83. circle(output,points[1][i],3,Scalar::all(0),(-1));
  84. }
  85. }
  86. };</span>

五.完整代码
  完整的运行代码有300+行,粘上来太多了,大家去我传的资源里下载吧。

下载地址:http://download.csdn.net/detail/crzy_sparrow/4183674

运行结果:

六.参考文献

【1】The classic article by B. Lucas and T. Kanade, An iterative image registration technique with an application to stereo vision in Int. Joint Conference in Artificial Intelligence, pp. 674-679,1981, that describes the original feature point tracking algorithm.
【2】The article by J. Shi and C. Tomasi, Good Features to Track in IEEE Conference on Computer Vision and Pattern Recognition, pp. 593-600, 1994, that describes an improved version of the original feature point tracking algorithm.

Optical_Flow(4)相关推荐

  1. HALCON示例程序optical_flow.hdev如何使用optical_flow_mg计算图像序列中的光流以及如何分割光流。

    HALCON示例程序optical_flow.hdev如何使用optical_flow_mg计算图像序列中的光流以及如何分割光流. 示例程序源码(加注释) 关于显示类函数解释 dev_update_o ...

  2. Optical_Flow(3)

    本文转自:http://blog.csdn.net/zouxy09/article/details/8683859 光流Optical Flow介绍与OpenCV实现 光流(optic flow)是什 ...

  3. Optical_Flow(2)

    本文转自:http://blog.csdn.net/justremind/article/details/23662273 1. 光流概念的介绍 光流的概念是Gibson在1950年首先提出来的,它是 ...

  4. Optical_Flow(1)

    申明:本文非笔者原创,原文转载自:http://blog.csdn.net/carson2005/article/details/7581642 光流的概念是Gibson在1950年首先提出来的.它是 ...

  5. PX4 - position_estimator_inav

    by luoshi006 欢迎交流~ 个人 Gitter 交流平台,点击直达: 参考: 1. http://dev.px4.io/advanced-switching_state_estimators ...

  6. pixhawk position_estimator_inav.cpp思路整理及数据流

    写在前面: 这篇blog主要参考pixhawk的高度解算算法解读,并且加以扩展,扩展到其他传感器,其实里面处理好多只是记录了流程,至于里面实际物理意义并不是很清楚,也希望大牛能够指导一下. 概述: 整 ...

  7. pixhawk 整体架构的认识

     此篇blog的目的是对px4工程有一个整体认识,对各个信号的流向有个了解,以及控制算法采用的控制框架. PX4自动驾驶仪软件可分为三大部分:实时操作系统.中间件和飞行控制栈. 1.NuttX实时 ...

  8. Px4源码框架结构图

     此篇blog的目的是对px4工程有一个整体认识,对各个信号的流向有个了解,以及控制算法采用的控制框架. PX4自动驾驶仪软件可分为三大部分:实时操作系统.中间件和飞行控制栈. 1.NuttX实时 ...

  9. opencv光流例程_OpenCV 4.4 发布!新增YOLOv4 和 EfficientDet 推断支持

    新增特性: 1. 支持 YOLOv4! OpenCV一直对较为实用的YOLO系列算法情有独钟. 2. ONNX增加支持Resnet_backbone 3. 支持谷歌目标检测算法 EfficientDe ...

最新文章

  1. 面试?莫慌--- 教你如何“秀技”摩擦面试官
  2. 关于在hdfs上对数据创建外部表的原因
  3. linux redis 删除_Redis-安装amp;删除【Linux 版】
  4. 文巾解题 182. 查找重复的电子邮箱
  5. AI公开课:19.03.06何晓冬博士《自然语言与多模态交互前沿技术》课堂笔记以及个人感悟
  6. 【STM32】 keil软件介绍--工程目标选项配置(上)
  7. 中石油训练赛 - 奎奎发红包(贪心)
  8. jquery 文件预览功能
  9. 停牌17个月 汉能薄膜真的要复牌了?
  10. 重绘CButton控件
  11. 最让人心动的十大互联网界广告语+超笑评语
  12. MySQL中的mysqldump命令使用详解
  13. python判断是否登录成功_python-42: 怎么判断模拟登录是否成功
  14. 中标麒麟安装mysql教程_中标麒麟操作系统安装MySQL5.7.22的步骤教程
  15. php原创度检测工具,推荐一款不错的伪原创工具
  16. crt软件(crt软件安装)
  17. chrome清楚缓存并硬性重新加载
  18. 游戏对战平台编写流程
  19. SKYPE的BUG 7/8
  20. 从Hadoop到Spark、Flink,大数据处理框架十年激荡发展史!

热门文章

  1. 计算机桌面组成部分教案,计算机基础 教案设计(完整版).doc
  2. 体验 vue cli 3.0
  3. 转载,关于缓存穿透、缓存并发、缓存雪崩那些事
  4. 如何开启一个Django项目
  5. html中的div span和frameset框架标签
  6. web.xml配置(转)
  7. Apache配置(转载)
  8. 绝对经典的滑动门特效代码
  9. ATL学习笔记〔一〕
  10. iframe嵌套改变url地址