openTLD算法在opencv3的PatchGenerator

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


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



PatchGenerator.h

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


PatchGenerator.cpp

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

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

你可能感兴趣的:(dsst)