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