Home | History | Annotate | Download | only in opencv2
      1 /*M///////////////////////////////////////////////////////////////////////////////////////
      2 //
      3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
      4 //
      5 //  By downloading, copying, installing or using the software you agree to this license.
      6 //  If you do not agree to this license, do not download, install,
      7 //  copy or use the software.
      8 //
      9 //
     10 //                           License Agreement
     11 //                For Open Source Computer Vision Library
     12 //
     13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
     14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
     15 // Third party copyrights are property of their respective owners.
     16 //
     17 // Redistribution and use in source and binary forms, with or without modification,
     18 // are permitted provided that the following conditions are met:
     19 //
     20 //   * Redistribution's of source code must retain the above copyright notice,
     21 //     this list of conditions and the following disclaimer.
     22 //
     23 //   * Redistribution's in binary form must reproduce the above copyright notice,
     24 //     this list of conditions and the following disclaimer in the documentation
     25 //     and/or other materials provided with the distribution.
     26 //
     27 //   * The name of the copyright holders may not be used to endorse or promote products
     28 //     derived from this software without specific prior written permission.
     29 //
     30 // This software is provided by the copyright holders and contributors "as is" and
     31 // any express or implied warranties, including, but not limited to, the implied
     32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
     33 // In no event shall the Intel Corporation or contributors be liable for any direct,
     34 // indirect, incidental, special, exemplary, or consequential damages
     35 // (including, but not limited to, procurement of substitute goods or services;
     36 // loss of use, data, or profits; or business interruption) however caused
     37 // and on any theory of liability, whether in contract, strict liability,
     38 // or tort (including negligence or otherwise) arising in any way out of
     39 // the use of this software, even if advised of the possibility of such damage.
     40 //
     41 //M*/
     42 
     43 #ifndef __OPENCV_CUDAOPTFLOW_HPP__
     44 #define __OPENCV_CUDAOPTFLOW_HPP__
     45 
     46 #ifndef __cplusplus
     47 #  error cudaoptflow.hpp header must be compiled as C++
     48 #endif
     49 
     50 #include "opencv2/core/cuda.hpp"
     51 
     52 /**
     53   @addtogroup cuda
     54   @{
     55     @defgroup cudaoptflow Optical Flow
     56   @}
     57  */
     58 
     59 namespace cv { namespace cuda {
     60 
     61 //! @addtogroup cudaoptflow
     62 //! @{
     63 
     64 //
     65 // Interface
     66 //
     67 
     68 /** @brief Base interface for dense optical flow algorithms.
     69  */
     70 class CV_EXPORTS DenseOpticalFlow : public Algorithm
     71 {
     72 public:
     73     /** @brief Calculates a dense optical flow.
     74 
     75     @param I0 first input image.
     76     @param I1 second input image of the same size and the same type as I0.
     77     @param flow computed flow image that has the same size as I0 and type CV_32FC2.
     78     @param stream Stream for the asynchronous version.
     79      */
     80     virtual void calc(InputArray I0, InputArray I1, InputOutputArray flow, Stream& stream = Stream::Null()) = 0;
     81 };
     82 
     83 /** @brief Base interface for sparse optical flow algorithms.
     84  */
     85 class CV_EXPORTS SparseOpticalFlow : public Algorithm
     86 {
     87 public:
     88     /** @brief Calculates a sparse optical flow.
     89 
     90     @param prevImg First input image.
     91     @param nextImg Second input image of the same size and the same type as prevImg.
     92     @param prevPts Vector of 2D points for which the flow needs to be found.
     93     @param nextPts Output vector of 2D points containing the calculated new positions of input features in the second image.
     94     @param status Output status vector. Each element of the vector is set to 1 if the
     95                   flow for the corresponding features has been found. Otherwise, it is set to 0.
     96     @param err Optional output vector that contains error response for each point (inverse confidence).
     97     @param stream Stream for the asynchronous version.
     98      */
     99     virtual void calc(InputArray prevImg, InputArray nextImg,
    100                       InputArray prevPts, InputOutputArray nextPts,
    101                       OutputArray status,
    102                       OutputArray err = cv::noArray(),
    103                       Stream& stream = Stream::Null()) = 0;
    104 };
    105 
    106 //
    107 // BroxOpticalFlow
    108 //
    109 
    110 /** @brief Class computing the optical flow for two images using Brox et al Optical Flow algorithm (@cite Brox2004).
    111  */
    112 class CV_EXPORTS BroxOpticalFlow : public DenseOpticalFlow
    113 {
    114 public:
    115     virtual double getFlowSmoothness() const = 0;
    116     virtual void setFlowSmoothness(double alpha) = 0;
    117 
    118     virtual double getGradientConstancyImportance() const = 0;
    119     virtual void setGradientConstancyImportance(double gamma) = 0;
    120 
    121     virtual double getPyramidScaleFactor() const = 0;
    122     virtual void setPyramidScaleFactor(double scale_factor) = 0;
    123 
    124     //! number of lagged non-linearity iterations (inner loop)
    125     virtual int getInnerIterations() const = 0;
    126     virtual void setInnerIterations(int inner_iterations) = 0;
    127 
    128     //! number of warping iterations (number of pyramid levels)
    129     virtual int getOuterIterations() const = 0;
    130     virtual void setOuterIterations(int outer_iterations) = 0;
    131 
    132     //! number of linear system solver iterations
    133     virtual int getSolverIterations() const = 0;
    134     virtual void setSolverIterations(int solver_iterations) = 0;
    135 
    136     static Ptr<BroxOpticalFlow> create(
    137             double alpha = 0.197,
    138             double gamma = 50.0,
    139             double scale_factor = 0.8,
    140             int inner_iterations = 5,
    141             int outer_iterations = 150,
    142             int solver_iterations = 10);
    143 };
    144 
    145 //
    146 // PyrLKOpticalFlow
    147 //
    148 
    149 /** @brief Class used for calculating a sparse optical flow.
    150 
    151 The class can calculate an optical flow for a sparse feature set using the
    152 iterative Lucas-Kanade method with pyramids.
    153 
    154 @sa calcOpticalFlowPyrLK
    155 
    156 @note
    157    -   An example of the Lucas Kanade optical flow algorithm can be found at
    158         opencv_source_code/samples/gpu/pyrlk_optical_flow.cpp
    159  */
    160 class CV_EXPORTS SparsePyrLKOpticalFlow : public SparseOpticalFlow
    161 {
    162 public:
    163     virtual Size getWinSize() const = 0;
    164     virtual void setWinSize(Size winSize) = 0;
    165 
    166     virtual int getMaxLevel() const = 0;
    167     virtual void setMaxLevel(int maxLevel) = 0;
    168 
    169     virtual int getNumIters() const = 0;
    170     virtual void setNumIters(int iters) = 0;
    171 
    172     virtual bool getUseInitialFlow() const = 0;
    173     virtual void setUseInitialFlow(bool useInitialFlow) = 0;
    174 
    175     static Ptr<SparsePyrLKOpticalFlow> create(
    176             Size winSize = Size(21, 21),
    177             int maxLevel = 3,
    178             int iters = 30,
    179             bool useInitialFlow = false);
    180 };
    181 
    182 /** @brief Class used for calculating a dense optical flow.
    183 
    184 The class can calculate an optical flow for a dense optical flow using the
    185 iterative Lucas-Kanade method with pyramids.
    186  */
    187 class CV_EXPORTS DensePyrLKOpticalFlow : public DenseOpticalFlow
    188 {
    189 public:
    190     virtual Size getWinSize() const = 0;
    191     virtual void setWinSize(Size winSize) = 0;
    192 
    193     virtual int getMaxLevel() const = 0;
    194     virtual void setMaxLevel(int maxLevel) = 0;
    195 
    196     virtual int getNumIters() const = 0;
    197     virtual void setNumIters(int iters) = 0;
    198 
    199     virtual bool getUseInitialFlow() const = 0;
    200     virtual void setUseInitialFlow(bool useInitialFlow) = 0;
    201 
    202     static Ptr<DensePyrLKOpticalFlow> create(
    203             Size winSize = Size(13, 13),
    204             int maxLevel = 3,
    205             int iters = 30,
    206             bool useInitialFlow = false);
    207 };
    208 
    209 //
    210 // FarnebackOpticalFlow
    211 //
    212 
    213 /** @brief Class computing a dense optical flow using the Gunnar Farnebacks algorithm.
    214  */
    215 class CV_EXPORTS FarnebackOpticalFlow : public DenseOpticalFlow
    216 {
    217 public:
    218     virtual int getNumLevels() const = 0;
    219     virtual void setNumLevels(int numLevels) = 0;
    220 
    221     virtual double getPyrScale() const = 0;
    222     virtual void setPyrScale(double pyrScale) = 0;
    223 
    224     virtual bool getFastPyramids() const = 0;
    225     virtual void setFastPyramids(bool fastPyramids) = 0;
    226 
    227     virtual int getWinSize() const = 0;
    228     virtual void setWinSize(int winSize) = 0;
    229 
    230     virtual int getNumIters() const = 0;
    231     virtual void setNumIters(int numIters) = 0;
    232 
    233     virtual int getPolyN() const = 0;
    234     virtual void setPolyN(int polyN) = 0;
    235 
    236     virtual double getPolySigma() const = 0;
    237     virtual void setPolySigma(double polySigma) = 0;
    238 
    239     virtual int getFlags() const = 0;
    240     virtual void setFlags(int flags) = 0;
    241 
    242     static Ptr<FarnebackOpticalFlow> create(
    243             int numLevels = 5,
    244             double pyrScale = 0.5,
    245             bool fastPyramids = false,
    246             int winSize = 13,
    247             int numIters = 10,
    248             int polyN = 5,
    249             double polySigma = 1.1,
    250             int flags = 0);
    251 };
    252 
    253 //
    254 // OpticalFlowDual_TVL1
    255 //
    256 
    257 /** @brief Implementation of the Zach, Pock and Bischof Dual TV-L1 Optical Flow method.
    258  *
    259  * @sa C. Zach, T. Pock and H. Bischof, "A Duality Based Approach for Realtime TV-L1 Optical Flow".
    260  * @sa Javier Sanchez, Enric Meinhardt-Llopis and Gabriele Facciolo. "TV-L1 Optical Flow Estimation".
    261  */
    262 class CV_EXPORTS OpticalFlowDual_TVL1 : public DenseOpticalFlow
    263 {
    264 public:
    265     /**
    266      * Time step of the numerical scheme.
    267      */
    268     virtual double getTau() const = 0;
    269     virtual void setTau(double tau) = 0;
    270 
    271     /**
    272      * Weight parameter for the data term, attachment parameter.
    273      * This is the most relevant parameter, which determines the smoothness of the output.
    274      * The smaller this parameter is, the smoother the solutions we obtain.
    275      * It depends on the range of motions of the images, so its value should be adapted to each image sequence.
    276      */
    277     virtual double getLambda() const = 0;
    278     virtual void setLambda(double lambda) = 0;
    279 
    280     /**
    281      * Weight parameter for (u - v)^2, tightness parameter.
    282      * It serves as a link between the attachment and the regularization terms.
    283      * In theory, it should have a small value in order to maintain both parts in correspondence.
    284      * The method is stable for a large range of values of this parameter.
    285      */
    286     virtual double getGamma() const = 0;
    287     virtual void setGamma(double gamma) = 0;
    288 
    289     /**
    290      * parameter used for motion estimation. It adds a variable allowing for illumination variations
    291      * Set this parameter to 1. if you have varying illumination.
    292      * See: Chambolle et al, A First-Order Primal-Dual Algorithm for Convex Problems with Applications to Imaging
    293      * Journal of Mathematical imaging and vision, may 2011 Vol 40 issue 1, pp 120-145
    294      */
    295     virtual double getTheta() const = 0;
    296     virtual void setTheta(double theta) = 0;
    297 
    298     /**
    299      * Number of scales used to create the pyramid of images.
    300      */
    301     virtual int getNumScales() const = 0;
    302     virtual void setNumScales(int nscales) = 0;
    303 
    304     /**
    305      * Number of warpings per scale.
    306      * Represents the number of times that I1(x+u0) and grad( I1(x+u0) ) are computed per scale.
    307      * This is a parameter that assures the stability of the method.
    308      * It also affects the running time, so it is a compromise between speed and accuracy.
    309      */
    310     virtual int getNumWarps() const = 0;
    311     virtual void setNumWarps(int warps) = 0;
    312 
    313     /**
    314      * Stopping criterion threshold used in the numerical scheme, which is a trade-off between precision and running time.
    315      * A small value will yield more accurate solutions at the expense of a slower convergence.
    316      */
    317     virtual double getEpsilon() const = 0;
    318     virtual void setEpsilon(double epsilon) = 0;
    319 
    320     /**
    321      * Stopping criterion iterations number used in the numerical scheme.
    322      */
    323     virtual int getNumIterations() const = 0;
    324     virtual void setNumIterations(int iterations) = 0;
    325 
    326     virtual double getScaleStep() const = 0;
    327     virtual void setScaleStep(double scaleStep) = 0;
    328 
    329     virtual bool getUseInitialFlow() const = 0;
    330     virtual void setUseInitialFlow(bool useInitialFlow) = 0;
    331 
    332     static Ptr<OpticalFlowDual_TVL1> create(
    333             double tau = 0.25,
    334             double lambda = 0.15,
    335             double theta = 0.3,
    336             int nscales = 5,
    337             int warps = 5,
    338             double epsilon = 0.01,
    339             int iterations = 300,
    340             double scaleStep = 0.8,
    341             double gamma = 0.0,
    342             bool useInitialFlow = false);
    343 };
    344 
    345 //! @}
    346 
    347 }} // namespace cv { namespace cuda {
    348 
    349 #endif /* __OPENCV_CUDAOPTFLOW_HPP__ */
    350