optflow.hpp 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309
  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) 2013, 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. #ifndef __OPENCV_OPTFLOW_HPP__
  32. #define __OPENCV_OPTFLOW_HPP__
  33. #include "opencv2/core.hpp"
  34. #include "opencv2/video.hpp"
  35. /**
  36. @defgroup optflow Optical Flow Algorithms
  37. Dense optical flow algorithms compute motion for each point:
  38. - cv::optflow::calcOpticalFlowSF
  39. - cv::optflow::createOptFlow_DeepFlow
  40. Motion templates is alternative technique for detecting motion and computing its direction.
  41. See samples/motempl.py.
  42. - cv::motempl::updateMotionHistory
  43. - cv::motempl::calcMotionGradient
  44. - cv::motempl::calcGlobalOrientation
  45. - cv::motempl::segmentMotion
  46. Functions reading and writing .flo files in "Middlebury" format, see: <http://vision.middlebury.edu/flow/code/flow-code/README.txt>
  47. - cv::optflow::readOpticalFlow
  48. - cv::optflow::writeOpticalFlow
  49. */
  50. #include "opencv2/optflow/pcaflow.hpp"
  51. #include "opencv2/optflow/sparse_matching_gpc.hpp"
  52. #include "opencv2/optflow/rlofflow.hpp"
  53. namespace cv
  54. {
  55. namespace optflow
  56. {
  57. //! @addtogroup optflow
  58. //! @{
  59. /** @overload */
  60. CV_EXPORTS_W void calcOpticalFlowSF( InputArray from, InputArray to, OutputArray flow,
  61. int layers, int averaging_block_size, int max_flow);
  62. /** @brief Calculate an optical flow using "SimpleFlow" algorithm.
  63. @param from First 8-bit 3-channel image.
  64. @param to Second 8-bit 3-channel image of the same size as prev
  65. @param flow computed flow image that has the same size as prev and type CV_32FC2
  66. @param layers Number of layers
  67. @param averaging_block_size Size of block through which we sum up when calculate cost function
  68. for pixel
  69. @param max_flow maximal flow that we search at each level
  70. @param sigma_dist vector smooth spatial sigma parameter
  71. @param sigma_color vector smooth color sigma parameter
  72. @param postprocess_window window size for postprocess cross bilateral filter
  73. @param sigma_dist_fix spatial sigma for postprocess cross bilateralf filter
  74. @param sigma_color_fix color sigma for postprocess cross bilateral filter
  75. @param occ_thr threshold for detecting occlusions
  76. @param upscale_averaging_radius window size for bilateral upscale operation
  77. @param upscale_sigma_dist spatial sigma for bilateral upscale operation
  78. @param upscale_sigma_color color sigma for bilateral upscale operation
  79. @param speed_up_thr threshold to detect point with irregular flow - where flow should be
  80. recalculated after upscale
  81. See @cite Tao2012 . And site of project - <http://graphics.berkeley.edu/papers/Tao-SAN-2012-05/>.
  82. @note
  83. - An example using the simpleFlow algorithm can be found at samples/simpleflow_demo.cpp
  84. */
  85. CV_EXPORTS_W void calcOpticalFlowSF( InputArray from, InputArray to, OutputArray flow, int layers,
  86. int averaging_block_size, int max_flow,
  87. double sigma_dist, double sigma_color, int postprocess_window,
  88. double sigma_dist_fix, double sigma_color_fix, double occ_thr,
  89. int upscale_averaging_radius, double upscale_sigma_dist,
  90. double upscale_sigma_color, double speed_up_thr );
  91. /** @brief Fast dense optical flow based on PyrLK sparse matches interpolation.
  92. @param from first 8-bit 3-channel or 1-channel image.
  93. @param to second 8-bit 3-channel or 1-channel image of the same size as from
  94. @param flow computed flow image that has the same size as from and CV_32FC2 type
  95. @param grid_step stride used in sparse match computation. Lower values usually
  96. result in higher quality but slow down the algorithm.
  97. @param k number of nearest-neighbor matches considered, when fitting a locally affine
  98. model. Lower values can make the algorithm noticeably faster at the cost of
  99. some quality degradation.
  100. @param sigma parameter defining how fast the weights decrease in the locally-weighted affine
  101. fitting. Higher values can help preserve fine details, lower values can help to get rid
  102. of the noise in the output flow.
  103. @param use_post_proc defines whether the ximgproc::fastGlobalSmootherFilter() is used
  104. for post-processing after interpolation
  105. @param fgs_lambda see the respective parameter of the ximgproc::fastGlobalSmootherFilter()
  106. @param fgs_sigma see the respective parameter of the ximgproc::fastGlobalSmootherFilter()
  107. */
  108. CV_EXPORTS_W void calcOpticalFlowSparseToDense ( InputArray from, InputArray to, OutputArray flow,
  109. int grid_step = 8, int k = 128, float sigma = 0.05f,
  110. bool use_post_proc = true, float fgs_lambda = 500.0f,
  111. float fgs_sigma = 1.5f );
  112. /** @brief DeepFlow optical flow algorithm implementation.
  113. The class implements the DeepFlow optical flow algorithm described in @cite Weinzaepfel2013 . See
  114. also <http://lear.inrialpes.fr/src/deepmatching/> .
  115. Parameters - class fields - that may be modified after creating a class instance:
  116. - member float alpha
  117. Smoothness assumption weight
  118. - member float delta
  119. Color constancy assumption weight
  120. - member float gamma
  121. Gradient constancy weight
  122. - member float sigma
  123. Gaussian smoothing parameter
  124. - member int minSize
  125. Minimal dimension of an image in the pyramid (next, smaller images in the pyramid are generated
  126. until one of the dimensions reaches this size)
  127. - member float downscaleFactor
  128. Scaling factor in the image pyramid (must be \< 1)
  129. - member int fixedPointIterations
  130. How many iterations on each level of the pyramid
  131. - member int sorIterations
  132. Iterations of Succesive Over-Relaxation (solver)
  133. - member float omega
  134. Relaxation factor in SOR
  135. */
  136. CV_EXPORTS_W Ptr<DenseOpticalFlow> createOptFlow_DeepFlow();
  137. //! Additional interface to the SimpleFlow algorithm - calcOpticalFlowSF()
  138. CV_EXPORTS_W Ptr<DenseOpticalFlow> createOptFlow_SimpleFlow();
  139. //! Additional interface to the Farneback's algorithm - calcOpticalFlowFarneback()
  140. CV_EXPORTS_W Ptr<DenseOpticalFlow> createOptFlow_Farneback();
  141. //! Additional interface to the SparseToDenseFlow algorithm - calcOpticalFlowSparseToDense()
  142. CV_EXPORTS_W Ptr<DenseOpticalFlow> createOptFlow_SparseToDense();
  143. /** @brief "Dual TV L1" Optical Flow Algorithm.
  144. The class implements the "Dual TV L1" optical flow algorithm described in @cite Zach2007 and
  145. @cite Javier2012 .
  146. Here are important members of the class that control the algorithm, which you can set after
  147. constructing the class instance:
  148. - member double tau
  149. Time step of the numerical scheme.
  150. - member double lambda
  151. Weight parameter for the data term, attachment parameter. This is the most relevant
  152. parameter, which determines the smoothness of the output. The smaller this parameter is,
  153. the smoother the solutions we obtain. It depends on the range of motions of the images, so
  154. its value should be adapted to each image sequence.
  155. - member double theta
  156. Weight parameter for (u - v)\^2, tightness parameter. It serves as a link between the
  157. attachment and the regularization terms. In theory, it should have a small value in order
  158. to maintain both parts in correspondence. The method is stable for a large range of values
  159. of this parameter.
  160. - member int nscales
  161. Number of scales used to create the pyramid of images.
  162. - member int warps
  163. Number of warpings per scale. Represents the number of times that I1(x+u0) and grad(
  164. I1(x+u0) ) are computed per scale. This is a parameter that assures the stability of the
  165. method. It also affects the running time, so it is a compromise between speed and
  166. accuracy.
  167. - member double epsilon
  168. Stopping criterion threshold used in the numerical scheme, which is a trade-off between
  169. precision and running time. A small value will yield more accurate solutions at the
  170. expense of a slower convergence.
  171. - member int iterations
  172. Stopping criterion iterations number used in the numerical scheme.
  173. C. Zach, T. Pock and H. Bischof, "A Duality Based Approach for Realtime TV-L1 Optical Flow".
  174. Javier Sanchez, Enric Meinhardt-Llopis and Gabriele Facciolo. "TV-L1 Optical Flow Estimation".
  175. */
  176. class CV_EXPORTS_W DualTVL1OpticalFlow : public DenseOpticalFlow
  177. {
  178. public:
  179. //! @brief Time step of the numerical scheme
  180. /** @see setTau */
  181. CV_WRAP virtual double getTau() const = 0;
  182. /** @copybrief getTau @see getTau */
  183. CV_WRAP virtual void setTau(double val) = 0;
  184. //! @brief Weight parameter for the data term, attachment parameter
  185. /** @see setLambda */
  186. CV_WRAP virtual double getLambda() const = 0;
  187. /** @copybrief getLambda @see getLambda */
  188. CV_WRAP virtual void setLambda(double val) = 0;
  189. //! @brief Weight parameter for (u - v)^2, tightness parameter
  190. /** @see setTheta */
  191. CV_WRAP virtual double getTheta() const = 0;
  192. /** @copybrief getTheta @see getTheta */
  193. CV_WRAP virtual void setTheta(double val) = 0;
  194. //! @brief coefficient for additional illumination variation term
  195. /** @see setGamma */
  196. CV_WRAP virtual double getGamma() const = 0;
  197. /** @copybrief getGamma @see getGamma */
  198. CV_WRAP virtual void setGamma(double val) = 0;
  199. //! @brief Number of scales used to create the pyramid of images
  200. /** @see setScalesNumber */
  201. CV_WRAP virtual int getScalesNumber() const = 0;
  202. /** @copybrief getScalesNumber @see getScalesNumber */
  203. CV_WRAP virtual void setScalesNumber(int val) = 0;
  204. //! @brief Number of warpings per scale
  205. /** @see setWarpingsNumber */
  206. CV_WRAP virtual int getWarpingsNumber() const = 0;
  207. /** @copybrief getWarpingsNumber @see getWarpingsNumber */
  208. CV_WRAP virtual void setWarpingsNumber(int val) = 0;
  209. //! @brief Stopping criterion threshold used in the numerical scheme, which is a trade-off between precision and running time
  210. /** @see setEpsilon */
  211. CV_WRAP virtual double getEpsilon() const = 0;
  212. /** @copybrief getEpsilon @see getEpsilon */
  213. CV_WRAP virtual void setEpsilon(double val) = 0;
  214. //! @brief Inner iterations (between outlier filtering) used in the numerical scheme
  215. /** @see setInnerIterations */
  216. CV_WRAP virtual int getInnerIterations() const = 0;
  217. /** @copybrief getInnerIterations @see getInnerIterations */
  218. CV_WRAP virtual void setInnerIterations(int val) = 0;
  219. //! @brief Outer iterations (number of inner loops) used in the numerical scheme
  220. /** @see setOuterIterations */
  221. CV_WRAP virtual int getOuterIterations() const = 0;
  222. /** @copybrief getOuterIterations @see getOuterIterations */
  223. CV_WRAP virtual void setOuterIterations(int val) = 0;
  224. //! @brief Use initial flow
  225. /** @see setUseInitialFlow */
  226. CV_WRAP virtual bool getUseInitialFlow() const = 0;
  227. /** @copybrief getUseInitialFlow @see getUseInitialFlow */
  228. CV_WRAP virtual void setUseInitialFlow(bool val) = 0;
  229. //! @brief Step between scales (<1)
  230. /** @see setScaleStep */
  231. CV_WRAP virtual double getScaleStep() const = 0;
  232. /** @copybrief getScaleStep @see getScaleStep */
  233. CV_WRAP virtual void setScaleStep(double val) = 0;
  234. //! @brief Median filter kernel size (1 = no filter) (3 or 5)
  235. /** @see setMedianFiltering */
  236. CV_WRAP virtual int getMedianFiltering() const = 0;
  237. /** @copybrief getMedianFiltering @see getMedianFiltering */
  238. CV_WRAP virtual void setMedianFiltering(int val) = 0;
  239. /** @brief Creates instance of cv::DualTVL1OpticalFlow*/
  240. CV_WRAP static Ptr<DualTVL1OpticalFlow> create(
  241. double tau = 0.25,
  242. double lambda = 0.15,
  243. double theta = 0.3,
  244. int nscales = 5,
  245. int warps = 5,
  246. double epsilon = 0.01,
  247. int innnerIterations = 30,
  248. int outerIterations = 10,
  249. double scaleStep = 0.8,
  250. double gamma = 0.0,
  251. int medianFiltering = 5,
  252. bool useInitialFlow = false);
  253. };
  254. /** @brief Creates instance of cv::DenseOpticalFlow
  255. */
  256. CV_EXPORTS_W Ptr<DualTVL1OpticalFlow> createOptFlow_DualTVL1();
  257. //! @}
  258. } //optflow
  259. }
  260. #include "opencv2/optflow/motempl.hpp"
  261. #endif