pcaflow.hpp 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149
  1. /*
  2. By downloading, copying, installing or using the software you agree to this
  3. license. If you do not agree to this license, do not download, install,
  4. copy or use the software.
  5. License Agreement
  6. For Open Source Computer Vision Library
  7. (3-clause BSD License)
  8. Copyright (C) 2016, OpenCV Foundation, all rights reserved.
  9. Third party copyrights are property of their respective owners.
  10. Redistribution and use in source and binary forms, with or without modification,
  11. are permitted provided that the following conditions are met:
  12. * Redistributions of source code must retain the above copyright notice,
  13. this list of conditions and the following disclaimer.
  14. * Redistributions in binary form must reproduce the above copyright notice,
  15. this list of conditions and the following disclaimer in the documentation
  16. and/or other materials provided with the distribution.
  17. * Neither the names of the copyright holders nor the names of the contributors
  18. may be used to endorse or promote products derived from this software
  19. without specific prior written permission.
  20. This software is provided by the copyright holders and contributors "as is" and
  21. any express or implied warranties, including, but not limited to, the implied
  22. warranties of merchantability and fitness for a particular purpose are
  23. disclaimed. In no event shall copyright holders or contributors be liable for
  24. any direct, indirect, incidental, special, exemplary, or consequential damages
  25. (including, but not limited to, procurement of substitute goods or services;
  26. loss of use, data, or profits; or business interruption) however caused
  27. and on any theory of liability, whether in contract, strict liability,
  28. or tort (including negligence or otherwise) arising in any way out of
  29. the use of this software, even if advised of the possibility of such damage.
  30. */
  31. /**
  32. * @file pcaflow.hpp
  33. * @author Vladislav Samsonov <vvladxx@gmail.com>
  34. * @brief Implementation of the PCAFlow algorithm from the following paper:
  35. * http://files.is.tue.mpg.de/black/papers/cvpr2015_pcaflow.pdf
  36. *
  37. * @cite Wulff:CVPR:2015
  38. *
  39. * There are some key differences which distinguish this algorithm from the original PCAFlow (see paper):
  40. * - Discrete Cosine Transform basis is used instead of basis extracted with PCA.
  41. * Reasoning: DCT basis has comparable performance and it doesn't require additional storage space.
  42. * Also, this decision helps to avoid overloading the algorithm with a lot of external input.
  43. * - Usage of built-in OpenCV feature tracking instead of libviso.
  44. */
  45. #ifndef __OPENCV_OPTFLOW_PCAFLOW_HPP__
  46. #define __OPENCV_OPTFLOW_PCAFLOW_HPP__
  47. #include "opencv2/core.hpp"
  48. #include "opencv2/video.hpp"
  49. namespace cv
  50. {
  51. namespace optflow
  52. {
  53. //! @addtogroup optflow
  54. //! @{
  55. /** @brief
  56. * This class can be used for imposing a learned prior on the resulting optical flow.
  57. * Solution will be regularized according to this prior.
  58. * You need to generate appropriate prior file with "learn_prior.py" script beforehand.
  59. */
  60. class CV_EXPORTS_W PCAPrior
  61. {
  62. private:
  63. Mat L1;
  64. Mat L2;
  65. Mat c1;
  66. Mat c2;
  67. public:
  68. PCAPrior( const char *pathToPrior );
  69. int getPadding() const { return L1.size().height; }
  70. int getBasisSize() const { return L1.size().width; }
  71. void fillConstraints( float *A1, float *A2, float *b1, float *b2 ) const;
  72. };
  73. /** @brief PCAFlow algorithm.
  74. */
  75. class CV_EXPORTS_W OpticalFlowPCAFlow : public DenseOpticalFlow
  76. {
  77. protected:
  78. const Ptr<const PCAPrior> prior;
  79. const Size basisSize;
  80. const float sparseRate; // (0 .. 0.1)
  81. const float retainedCornersFraction; // [0 .. 1]
  82. const float occlusionsThreshold;
  83. const float dampingFactor;
  84. const float claheClip;
  85. bool useOpenCL;
  86. public:
  87. /** @brief Creates an instance of PCAFlow algorithm.
  88. * @param _prior Learned prior or no prior (default). @see cv::optflow::PCAPrior
  89. * @param _basisSize Number of basis vectors.
  90. * @param _sparseRate Controls density of sparse matches.
  91. * @param _retainedCornersFraction Retained corners fraction.
  92. * @param _occlusionsThreshold Occlusion threshold.
  93. * @param _dampingFactor Regularization term for solving least-squares. It is not related to the prior regularization.
  94. * @param _claheClip Clip parameter for CLAHE.
  95. */
  96. OpticalFlowPCAFlow( Ptr<const PCAPrior> _prior = Ptr<const PCAPrior>(), const Size _basisSize = Size( 18, 14 ),
  97. float _sparseRate = 0.024, float _retainedCornersFraction = 0.2,
  98. float _occlusionsThreshold = 0.0003, float _dampingFactor = 0.00002, float _claheClip = 14 );
  99. void calc( InputArray I0, InputArray I1, InputOutputArray flow ) CV_OVERRIDE;
  100. void collectGarbage() CV_OVERRIDE;
  101. private:
  102. void findSparseFeatures( UMat &from, UMat &to, std::vector<Point2f> &features,
  103. std::vector<Point2f> &predictedFeatures ) const;
  104. void removeOcclusions( UMat &from, UMat &to, std::vector<Point2f> &features,
  105. std::vector<Point2f> &predictedFeatures ) const;
  106. void getSystem( OutputArray AOut, OutputArray b1Out, OutputArray b2Out, const std::vector<Point2f> &features,
  107. const std::vector<Point2f> &predictedFeatures, const Size size );
  108. void getSystem( OutputArray A1Out, OutputArray A2Out, OutputArray b1Out, OutputArray b2Out,
  109. const std::vector<Point2f> &features, const std::vector<Point2f> &predictedFeatures,
  110. const Size size );
  111. OpticalFlowPCAFlow& operator=( const OpticalFlowPCAFlow& ); // make it non-assignable
  112. };
  113. /** @brief Creates an instance of PCAFlow
  114. */
  115. CV_EXPORTS_W Ptr<DenseOpticalFlow> createOptFlow_PCAFlow();
  116. //! @}
  117. }
  118. }
  119. #endif