由于opencv3的各种版本相对于opencv2的版本已经改变了很多内容,openTLD跟踪算法所依赖的一些函数在opencv3中已经消失了,为此需要对openTLD进行适当修改才能使之在opencv3的各种版本中运行。

加入如下文件,并在对应的地方include头文件即可。

PatchGenerator.h

[cpp] view plaincopy
  1. #include <opencv2/opencv.hpp>
  2. #ifndef PATCHGENERATOR_H
[cpp] view plaincopy
  1. #define <span style="font-family: Arial, Helvetica, sans-serif;">PATCHGENERATOR_H</span>
  2. namespace cv
  3. {
  4. class CV_EXPORTS PatchGenerator
  5. {
  6. public:
  7. PatchGenerator();
  8. PatchGenerator(double _backgroundMin, double _backgroundMax,
  9. double _noiseRange, bool _randomBlur=true,
  10. double _lambdaMin=0.6, double _lambdaMax=1.5,
  11. double _thetaMin=-CV_PI, double _thetaMax=CV_PI,
  12. double _phiMin=-CV_PI, double _phiMax=CV_PI );
  13. void operator()(const Mat& image, Point2f pt, Mat& patch, Size patchSize, RNG& rng) const;
  14. void operator()(const Mat& image, const Mat& transform, Mat& patch,
  15. Size patchSize, RNG& rng) const;
  16. void warpWholeImage(const Mat& image, Mat& matT, Mat& buf,
  17. CV_OUT Mat& warped, int border, RNG& rng) const;
  18. void generateRandomTransform(Point2f srcCenter, Point2f dstCenter,
  19. CV_OUT Mat& transform, RNG& rng,
  20. bool inverse=false) const;
  21. void setAffineParam(double lambda, double theta, double phi);
  22. double backgroundMin, backgroundMax;
  23. double noiseRange;
  24. bool randomBlur;
  25. double lambdaMin, lambdaMax;
  26. double thetaMin, thetaMax;
  27. double phiMin, phiMax;
  28. };
  29. };
[cpp] view plaincopy
  1. #endif

PatchGenerator.cpp

[cpp] view plaincopy
  1. #include <opencv2/opencv.hpp>
  2. #include <PatchGenerator.h>
  3. namespace cv
  4. {
  5. /*
  6. The code below implements keypoint detector, fern-based point classifier and a planar object detector.
  7. References:
  8. 1. Mustafa Özuysal, Michael Calonder, Vincent Lepetit, Pascal Fua,
  9. "Fast KeyPoint Recognition Using Random Ferns,"
  10. IEEE Transactions on Pattern Analysis and Machine Intelligence, 15 Jan. 2009.
  11. 2. Vincent Lepetit, Pascal Fua,
  12. "Towards Recognizing Feature Points Using Classification Trees,"
  13. Technical Report IC/2004/74, EPFL, 2004.
  14. */
  15. const int progressBarSize = 50;
  16. Patch Generator //
  17. static const double DEFAULT_BACKGROUND_MIN = 0;
  18. static const double DEFAULT_BACKGROUND_MAX = 256;
  19. static const double DEFAULT_NOISE_RANGE = 5;
  20. static const double DEFAULT_LAMBDA_MIN = 0.6;
  21. static const double DEFAULT_LAMBDA_MAX = 1.5;
  22. static const double DEFAULT_THETA_MIN = -CV_PI;
  23. static const double DEFAULT_THETA_MAX = CV_PI;
  24. static const double DEFAULT_PHI_MIN = -CV_PI;
  25. static const double DEFAULT_PHI_MAX = CV_PI;
  26. PatchGenerator::PatchGenerator()
  27. : backgroundMin(DEFAULT_BACKGROUND_MIN), backgroundMax(DEFAULT_BACKGROUND_MAX),
  28. noiseRange(DEFAULT_NOISE_RANGE), randomBlur(true), lambdaMin(DEFAULT_LAMBDA_MIN),
  29. lambdaMax(DEFAULT_LAMBDA_MAX), thetaMin(DEFAULT_THETA_MIN),
  30. thetaMax(DEFAULT_THETA_MAX), phiMin(DEFAULT_PHI_MIN),
  31. phiMax(DEFAULT_PHI_MAX)
  32. {
  33. }
  34. PatchGenerator::PatchGenerator(double _backgroundMin, double _backgroundMax,
  35. double _noiseRange, bool _randomBlur,
  36. double _lambdaMin, double _lambdaMax,
  37. double _thetaMin, double _thetaMax,
  38. double _phiMin, double _phiMax )
  39. : backgroundMin(_backgroundMin), backgroundMax(_backgroundMax),
  40. noiseRange(_noiseRange), randomBlur(_randomBlur),
  41. lambdaMin(_lambdaMin), lambdaMax(_lambdaMax),
  42. thetaMin(_thetaMin), thetaMax(_thetaMax),
  43. phiMin(_phiMin), phiMax(_phiMax)
  44. {
  45. }
  46. void PatchGenerator::generateRandomTransform(Point2f srcCenter, Point2f dstCenter,
  47. Mat& transform, RNG& rng, bool inverse) const
  48. {
  49. double lambda1 = rng.uniform(lambdaMin, lambdaMax);
  50. double lambda2 = rng.uniform(lambdaMin, lambdaMax);
  51. double theta = rng.uniform(thetaMin, thetaMax);
  52. double phi = rng.uniform(phiMin, phiMax);
  53. // Calculate random parameterized affine transformation A,
  54. // A = T(patch center) * R(theta) * R(phi)' *
  55. //     S(lambda1, lambda2) * R(phi) * T(-pt)
  56. double st = sin(theta);
  57. double ct = cos(theta);
  58. double sp = sin(phi);
  59. double cp = cos(phi);
  60. double c2p = cp*cp;
  61. double s2p = sp*sp;
  62. double A = lambda1*c2p + lambda2*s2p;
  63. double B = (lambda2 - lambda1)*sp*cp;
  64. double C = lambda1*s2p + lambda2*c2p;
  65. double Ax_plus_By = A*srcCenter.x + B*srcCenter.y;
  66. double Bx_plus_Cy = B*srcCenter.x + C*srcCenter.y;
  67. transform.create(2, 3, CV_64F);
  68. Mat_<double>& T = (Mat_<double>&)transform;
  69. T(0,0) = A*ct - B*st;
  70. T(0,1) = B*ct - C*st;
  71. T(0,2) = -ct*Ax_plus_By + st*Bx_plus_Cy + dstCenter.x;
  72. T(1,0) = A*st + B*ct;
  73. T(1,1) = B*st + C*ct;
  74. T(1,2) = -st*Ax_plus_By - ct*Bx_plus_Cy + dstCenter.y;
  75. if( inverse )
  76. invertAffineTransform(T, T);
  77. }
  78. void PatchGenerator::operator ()(const Mat& image, Point2f pt, Mat& patch, Size patchSize, RNG& rng) const
  79. {
  80. double buffer[6];
  81. Mat_<double> T(2, 3, buffer);
  82. generateRandomTransform(pt, Point2f((patchSize.width-1)*0.5f, (patchSize.height-1)*0.5f), T, rng);
  83. (*this)(image, T, patch, patchSize, rng);
  84. }
  85. void PatchGenerator::operator ()(const Mat& image, const Mat& T,
  86. Mat& patch, Size patchSize, RNG& rng) const
  87. {
  88. patch.create( patchSize, image.type() );
  89. if( backgroundMin != backgroundMax )
  90. {
  91. rng.fill(patch, RNG::UNIFORM, Scalar::all(backgroundMin), Scalar::all(backgroundMax));
  92. warpAffine(image, patch, T, patchSize, INTER_LINEAR, BORDER_TRANSPARENT);
  93. }
  94. else
  95. warpAffine(image, patch, T, patchSize, INTER_LINEAR, BORDER_CONSTANT, Scalar::all(backgroundMin));
  96. int ksize = randomBlur ? (unsigned)rng % 9 - 5 : 0;
  97. if( ksize > 0 )
  98. {
  99. ksize = ksize*2 + 1;
  100. GaussianBlur(patch, patch, Size(ksize, ksize), 0, 0);
  101. }
  102. if( noiseRange > 0 )
  103. {
  104. AutoBuffer<uchar> _noiseBuf( patchSize.width*patchSize.height*image.elemSize() );
  105. Mat noise(patchSize, image.type(), (uchar*)_noiseBuf);
  106. int delta = image.depth() == CV_8U ? 128 : image.depth() == CV_16U ? 32768 : 0;
  107. rng.fill(noise, RNG::NORMAL, Scalar::all(delta), Scalar::all(noiseRange));
  108. if( backgroundMin != backgroundMax )
  109. addWeighted(patch, 1, noise, 1, -delta, patch);
  110. else
  111. {
  112. for( int i = 0; i < patchSize.height; i++ )
  113. {
  114. uchar* prow = patch.ptr<uchar>(i);
  115. const uchar* nrow =  noise.ptr<uchar>(i);
  116. for( int j = 0; j < patchSize.width; j++ )
  117. if( prow[j] != backgroundMin )
  118. prow[j] = saturate_cast<uchar>(prow[j] + nrow[j] - delta);
  119. }
  120. }
  121. }
  122. }
  123. void PatchGenerator::warpWholeImage(const Mat& image, Mat& matT, Mat& buf,
  124. Mat& warped, int border, RNG& rng) const
  125. {
  126. Mat_<double> T = matT;
  127. Rect roi(INT_MAX, INT_MAX, INT_MIN, INT_MIN);
  128. for( int k = 0; k < 4; k++ )
  129. {
  130. Point2f pt0, pt1;
  131. pt0.x = (float)(k == 0 || k == 3 ? 0 : image.cols);
  132. pt0.y = (float)(k < 2 ? 0 : image.rows);
  133. pt1.x = (float)(T(0,0)*pt0.x + T(0,1)*pt0.y + T(0,2));
  134. pt1.y = (float)(T(1,0)*pt0.x + T(1,1)*pt0.y + T(1,2));
  135. roi.x = std::min(roi.x, cvFloor(pt1.x));
  136. roi.y = std::min(roi.y, cvFloor(pt1.y));
  137. roi.width = std::max(roi.width, cvCeil(pt1.x));
  138. roi.height = std::max(roi.height, cvCeil(pt1.y));
  139. }
  140. roi.width -= roi.x - 1;
  141. roi.height -= roi.y - 1;
  142. int dx = border - roi.x;
  143. int dy = border - roi.y;
  144. if( (roi.width+border*2)*(roi.height+border*2) > buf.cols )
  145. buf.create(1, (roi.width+border*2)*(roi.height+border*2), image.type());
  146. warped = Mat(roi.height + border*2, roi.width + border*2,
  147. image.type(), buf.data);
  148. T(0,2) += dx;
  149. T(1,2) += dy;
  150. (*this)(image, T, warped, warped.size(), rng);
  151. if( T.data != matT.data )
  152. T.convertTo(matT, matT.type());
  153. }
  154. // Params are assumed to be symmetrical: lambda w.r.t. 1, theta and phi w.r.t. 0
  155. void PatchGenerator::setAffineParam(double lambda, double theta, double phi)
  156. {
  157. lambdaMin = 1. - lambda;
  158. lambdaMax = 1. + lambda;
  159. thetaMin = -theta;
  160. thetaMax = theta;
  161. phiMin = -phi;
  162. phiMax = phi;
  163. }
  164. };

原文 http://blog.csdn.net/j10527/article/details/51305087

openTLD算法在opencv3的PatchGenerator相关推荐

  1. BRISK算法在OpenCV3.0中的使用

    BRISK算法在OpenCV3.0中的使用 在OpenCV3.0中一些局部特征相关的接口还是有一些改动的,因为最近在测试BRISK部分内容,所以将OpenCV3.0中BRISK的使用贴出来,其他局部特 ...

  2. 多个物体轮廓c语言提取算法,C++ opencv-3.4.1 提取不规则物体的轮廓

    在学习opencv的时候,对一张照片,需要标注照片上物体的不规则轮廓. 如图: 使用opencv进行物体的轮廓处理,关键在于对照片的理解,前期的照片处理的越好最后调用api出来的结果就越接近理想值. ...

  3. 在opencv3中的机器学习算法

    在opencv3.0中,提供了一个ml.cpp的文件,这里面全是机器学习的算法,共提供了这么几种: 1.正态贝叶斯:normal Bayessian classifier    我已在另外一篇博文中介 ...

  4. 在opencv3中实现机器学习之:利用svm(支持向量机)分类

    svm分类算法在opencv3中有了很大的变动,取消了CvSVMParams这个类,因此在参数设定上会有些改变. opencv中的svm分类代码,来源于libsvm. #include "s ...

  5. openCV学习笔记(十二) —— 人脸识别算法(1/3)—— 特征脸 EigenFaces

    一.原理 opencv支持3种人脸识别的算法,分别是: 1.    Eigen Faces    PCA(特征脸方法) 2.    Fisher Faces    LDA(线性判别分析) 3.     ...

  6. 运动目标的背景建模-混合高斯背景建模和KNN模型建模的OpenCV代码实现

    图像处理开发需求.图像处理接私活挣零花钱,请加微信/QQ 2487872782 图像处理开发资料.图像处理技术交流请加QQ群,群号 271891601 运动检测通常用于分析视频序列中的移动目标,如车辆 ...

  7. 一种基于人脸追踪和特征分析的疲劳驾驶预警平台

    目录 整体描述 1. 嵌入式端: 2.程序端: 3. 辅助功能: 4. 项目整体工作流程 5. 不足之处 效果演示 程序源码 算法篇 基于AdaBoost级联分类器的人脸/眼睛位置检测算法 基于特征脸 ...

  8. python 立体匹配算法_OpenCV3.4两种立体匹配算法效果对比

    以OpenCV自带的Aloe图像对为例: 1.BM算法(Block Matching) 参数设置如下: int numberOfDisparities = ((imgSize.width / 8) + ...

  9. OpenCV编程入门——启程前的准备

    一.OpenCV官方例程与赏析 1.彩色目标跟踪:Camshift(Continuously Adaptive Mean-SHIFT) 说明:程序的用法是根据鼠标框选区域的色度光谱来进行摄像头读入视频 ...

最新文章

  1. MYSQL出错代码列表
  2. 美空管官员:政府停摆致人手不足 危及航空安全
  3. 中国血液制品行业供给预测与投资风险分析报告2022版
  4. EasyUI combobox
  5. 【经验分享】产品、运营人如何告别重复的数据分析工作?
  6. 前端学习(675):if else if
  7. Android手势锁实现
  8. java 数据队列_Java 数据结构 - 队列
  9. SQLi LABS Less 23 联合注入+报错注入+布尔盲注
  10. 搜索 + 剪枝 --- POJ 1101 : Sticks
  11. JAVA中RGB字串转换为颜色
  12. 系统辨识总论(System Identification)
  13. 物联网全栈教程(1)——总目录
  14. Python中的shuffle()函数
  15. 给本本换硬盘,直接克隆旧盘!
  16. [HTML] kbd 标签
  17. 程序自动修复相关工作总结
  18. php 统计汉字,PHP 统计实时统计汉字个数和区别
  19. Oracle索引(Index)创建使用
  20. Android Wi-Fi 修改国家码(QCOM平台)

热门文章

  1. linux 中断机制的处理过程
  2. java spring context_java将对象注册到spring context中 | 学步园
  3. Ubunt php连接oracle,uBuntu PHP Oracle扩展 OCI8安装
  4. linux 软件装到hone,如何在Linux系统安装Apollo
  5. php 安装oracle扩展,win PHP7安装oracle扩展
  6. chrome 适配调试_移动端适配
  7. 过年回家抢票不求人,试试这个开源抢票神器吧!
  8. 手下两个应届生,一个踏实喜欢加班,一个技术强挑活,怎么选?
  9. 皮一皮:我也想做这样的房东,善解人意、为他人着想...
  10. 人类快感程度体验等级