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