cudaoptflow.hpp 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349
  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. #ifndef OPENCV_CUDAOPTFLOW_HPP
  43. #define OPENCV_CUDAOPTFLOW_HPP
  44. #ifndef __cplusplus
  45. # error cudaoptflow.hpp header must be compiled as C++
  46. #endif
  47. #include "opencv2/core/cuda.hpp"
  48. /**
  49. @addtogroup cuda
  50. @{
  51. @defgroup cudaoptflow Optical Flow
  52. @}
  53. */
  54. namespace cv { namespace cuda {
  55. //! @addtogroup cudaoptflow
  56. //! @{
  57. //
  58. // Interface
  59. //
  60. /** @brief Base interface for dense optical flow algorithms.
  61. */
  62. class CV_EXPORTS DenseOpticalFlow : public Algorithm
  63. {
  64. public:
  65. /** @brief Calculates a dense optical flow.
  66. @param I0 first input image.
  67. @param I1 second input image of the same size and the same type as I0.
  68. @param flow computed flow image that has the same size as I0 and type CV_32FC2.
  69. @param stream Stream for the asynchronous version.
  70. */
  71. virtual void calc(InputArray I0, InputArray I1, InputOutputArray flow, Stream& stream = Stream::Null()) = 0;
  72. };
  73. /** @brief Base interface for sparse optical flow algorithms.
  74. */
  75. class CV_EXPORTS SparseOpticalFlow : public Algorithm
  76. {
  77. public:
  78. /** @brief Calculates a sparse optical flow.
  79. @param prevImg First input image.
  80. @param nextImg Second input image of the same size and the same type as prevImg.
  81. @param prevPts Vector of 2D points for which the flow needs to be found.
  82. @param nextPts Output vector of 2D points containing the calculated new positions of input features in the second image.
  83. @param status Output status vector. Each element of the vector is set to 1 if the
  84. flow for the corresponding features has been found. Otherwise, it is set to 0.
  85. @param err Optional output vector that contains error response for each point (inverse confidence).
  86. @param stream Stream for the asynchronous version.
  87. */
  88. virtual void calc(InputArray prevImg, InputArray nextImg,
  89. InputArray prevPts, InputOutputArray nextPts,
  90. OutputArray status,
  91. OutputArray err = cv::noArray(),
  92. Stream& stream = Stream::Null()) = 0;
  93. };
  94. //
  95. // BroxOpticalFlow
  96. //
  97. /** @brief Class computing the optical flow for two images using Brox et al Optical Flow algorithm (@cite Brox2004).
  98. */
  99. class CV_EXPORTS BroxOpticalFlow : public DenseOpticalFlow
  100. {
  101. public:
  102. virtual double getFlowSmoothness() const = 0;
  103. virtual void setFlowSmoothness(double alpha) = 0;
  104. virtual double getGradientConstancyImportance() const = 0;
  105. virtual void setGradientConstancyImportance(double gamma) = 0;
  106. virtual double getPyramidScaleFactor() const = 0;
  107. virtual void setPyramidScaleFactor(double scale_factor) = 0;
  108. //! number of lagged non-linearity iterations (inner loop)
  109. virtual int getInnerIterations() const = 0;
  110. virtual void setInnerIterations(int inner_iterations) = 0;
  111. //! number of warping iterations (number of pyramid levels)
  112. virtual int getOuterIterations() const = 0;
  113. virtual void setOuterIterations(int outer_iterations) = 0;
  114. //! number of linear system solver iterations
  115. virtual int getSolverIterations() const = 0;
  116. virtual void setSolverIterations(int solver_iterations) = 0;
  117. static Ptr<BroxOpticalFlow> create(
  118. double alpha = 0.197,
  119. double gamma = 50.0,
  120. double scale_factor = 0.8,
  121. int inner_iterations = 5,
  122. int outer_iterations = 150,
  123. int solver_iterations = 10);
  124. };
  125. //
  126. // PyrLKOpticalFlow
  127. //
  128. /** @brief Class used for calculating a sparse optical flow.
  129. The class can calculate an optical flow for a sparse feature set using the
  130. iterative Lucas-Kanade method with pyramids.
  131. @sa calcOpticalFlowPyrLK
  132. @note
  133. - An example of the Lucas Kanade optical flow algorithm can be found at
  134. opencv_source_code/samples/gpu/pyrlk_optical_flow.cpp
  135. */
  136. class CV_EXPORTS SparsePyrLKOpticalFlow : public SparseOpticalFlow
  137. {
  138. public:
  139. virtual Size getWinSize() const = 0;
  140. virtual void setWinSize(Size winSize) = 0;
  141. virtual int getMaxLevel() const = 0;
  142. virtual void setMaxLevel(int maxLevel) = 0;
  143. virtual int getNumIters() const = 0;
  144. virtual void setNumIters(int iters) = 0;
  145. virtual bool getUseInitialFlow() const = 0;
  146. virtual void setUseInitialFlow(bool useInitialFlow) = 0;
  147. static Ptr<SparsePyrLKOpticalFlow> create(
  148. Size winSize = Size(21, 21),
  149. int maxLevel = 3,
  150. int iters = 30,
  151. bool useInitialFlow = false);
  152. };
  153. /** @brief Class used for calculating a dense optical flow.
  154. The class can calculate an optical flow for a dense optical flow using the
  155. iterative Lucas-Kanade method with pyramids.
  156. */
  157. class CV_EXPORTS DensePyrLKOpticalFlow : public DenseOpticalFlow
  158. {
  159. public:
  160. virtual Size getWinSize() const = 0;
  161. virtual void setWinSize(Size winSize) = 0;
  162. virtual int getMaxLevel() const = 0;
  163. virtual void setMaxLevel(int maxLevel) = 0;
  164. virtual int getNumIters() const = 0;
  165. virtual void setNumIters(int iters) = 0;
  166. virtual bool getUseInitialFlow() const = 0;
  167. virtual void setUseInitialFlow(bool useInitialFlow) = 0;
  168. static Ptr<DensePyrLKOpticalFlow> create(
  169. Size winSize = Size(13, 13),
  170. int maxLevel = 3,
  171. int iters = 30,
  172. bool useInitialFlow = false);
  173. };
  174. //
  175. // FarnebackOpticalFlow
  176. //
  177. /** @brief Class computing a dense optical flow using the Gunnar Farneback's algorithm.
  178. */
  179. class CV_EXPORTS FarnebackOpticalFlow : public DenseOpticalFlow
  180. {
  181. public:
  182. virtual int getNumLevels() const = 0;
  183. virtual void setNumLevels(int numLevels) = 0;
  184. virtual double getPyrScale() const = 0;
  185. virtual void setPyrScale(double pyrScale) = 0;
  186. virtual bool getFastPyramids() const = 0;
  187. virtual void setFastPyramids(bool fastPyramids) = 0;
  188. virtual int getWinSize() const = 0;
  189. virtual void setWinSize(int winSize) = 0;
  190. virtual int getNumIters() const = 0;
  191. virtual void setNumIters(int numIters) = 0;
  192. virtual int getPolyN() const = 0;
  193. virtual void setPolyN(int polyN) = 0;
  194. virtual double getPolySigma() const = 0;
  195. virtual void setPolySigma(double polySigma) = 0;
  196. virtual int getFlags() const = 0;
  197. virtual void setFlags(int flags) = 0;
  198. static Ptr<FarnebackOpticalFlow> create(
  199. int numLevels = 5,
  200. double pyrScale = 0.5,
  201. bool fastPyramids = false,
  202. int winSize = 13,
  203. int numIters = 10,
  204. int polyN = 5,
  205. double polySigma = 1.1,
  206. int flags = 0);
  207. };
  208. //
  209. // OpticalFlowDual_TVL1
  210. //
  211. /** @brief Implementation of the Zach, Pock and Bischof Dual TV-L1 Optical Flow method.
  212. *
  213. * @note C. Zach, T. Pock and H. Bischof, "A Duality Based Approach for Realtime TV-L1 Optical Flow".
  214. * @note Javier Sanchez, Enric Meinhardt-Llopis and Gabriele Facciolo. "TV-L1 Optical Flow Estimation".
  215. */
  216. class CV_EXPORTS OpticalFlowDual_TVL1 : public DenseOpticalFlow
  217. {
  218. public:
  219. /**
  220. * Time step of the numerical scheme.
  221. */
  222. virtual double getTau() const = 0;
  223. virtual void setTau(double tau) = 0;
  224. /**
  225. * Weight parameter for the data term, attachment parameter.
  226. * This is the most relevant parameter, which determines the smoothness of the output.
  227. * The smaller this parameter is, the smoother the solutions we obtain.
  228. * It depends on the range of motions of the images, so its value should be adapted to each image sequence.
  229. */
  230. virtual double getLambda() const = 0;
  231. virtual void setLambda(double lambda) = 0;
  232. /**
  233. * Weight parameter for (u - v)^2, tightness parameter.
  234. * It serves as a link between the attachment and the regularization terms.
  235. * In theory, it should have a small value in order to maintain both parts in correspondence.
  236. * The method is stable for a large range of values of this parameter.
  237. */
  238. virtual double getGamma() const = 0;
  239. virtual void setGamma(double gamma) = 0;
  240. /**
  241. * parameter used for motion estimation. It adds a variable allowing for illumination variations
  242. * Set this parameter to 1. if you have varying illumination.
  243. * See: Chambolle et al, A First-Order Primal-Dual Algorithm for Convex Problems with Applications to Imaging
  244. * Journal of Mathematical imaging and vision, may 2011 Vol 40 issue 1, pp 120-145
  245. */
  246. virtual double getTheta() const = 0;
  247. virtual void setTheta(double theta) = 0;
  248. /**
  249. * Number of scales used to create the pyramid of images.
  250. */
  251. virtual int getNumScales() const = 0;
  252. virtual void setNumScales(int nscales) = 0;
  253. /**
  254. * Number of warpings per scale.
  255. * Represents the number of times that I1(x+u0) and grad( I1(x+u0) ) are computed per scale.
  256. * This is a parameter that assures the stability of the method.
  257. * It also affects the running time, so it is a compromise between speed and accuracy.
  258. */
  259. virtual int getNumWarps() const = 0;
  260. virtual void setNumWarps(int warps) = 0;
  261. /**
  262. * Stopping criterion threshold used in the numerical scheme, which is a trade-off between precision and running time.
  263. * A small value will yield more accurate solutions at the expense of a slower convergence.
  264. */
  265. virtual double getEpsilon() const = 0;
  266. virtual void setEpsilon(double epsilon) = 0;
  267. /**
  268. * Stopping criterion iterations number used in the numerical scheme.
  269. */
  270. virtual int getNumIterations() const = 0;
  271. virtual void setNumIterations(int iterations) = 0;
  272. virtual double getScaleStep() const = 0;
  273. virtual void setScaleStep(double scaleStep) = 0;
  274. virtual bool getUseInitialFlow() const = 0;
  275. virtual void setUseInitialFlow(bool useInitialFlow) = 0;
  276. static Ptr<OpticalFlowDual_TVL1> create(
  277. double tau = 0.25,
  278. double lambda = 0.15,
  279. double theta = 0.3,
  280. int nscales = 5,
  281. int warps = 5,
  282. double epsilon = 0.01,
  283. int iterations = 300,
  284. double scaleStep = 0.8,
  285. double gamma = 0.0,
  286. bool useInitialFlow = false);
  287. };
  288. //! @}
  289. }} // namespace cv { namespace cuda {
  290. #endif /* OPENCV_CUDAOPTFLOW_HPP */