optflow.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  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 Read a .flo file
  112. @param path Path to the file to be loaded
  113. The function readOpticalFlow loads a flow field from a file and returns it as a single matrix.
  114. Resulting Mat has a type CV_32FC2 - floating-point, 2-channel. First channel corresponds to the
  115. flow in the horizontal direction (u), second - vertical (v).
  116. */
  117. CV_EXPORTS_W Mat readOpticalFlow( const String& path );
  118. /** @brief Write a .flo to disk
  119. @param path Path to the file to be written
  120. @param flow Flow field to be stored
  121. The function stores a flow field in a file, returns true on success, false otherwise.
  122. The flow field must be a 2-channel, floating-point matrix (CV_32FC2). First channel corresponds
  123. to the flow in the horizontal direction (u), second - vertical (v).
  124. */
  125. CV_EXPORTS_W bool writeOpticalFlow( const String& path, InputArray flow );
  126. /** @brief Variational optical flow refinement
  127. This class implements variational refinement of the input flow field, i.e.
  128. it uses input flow to initialize the minimization of the following functional:
  129. \f$E(U) = \int_{\Omega} \delta \Psi(E_I) + \gamma \Psi(E_G) + \alpha \Psi(E_S) \f$,
  130. where \f$E_I,E_G,E_S\f$ are color constancy, gradient constancy and smoothness terms
  131. respectively. \f$\Psi(s^2)=\sqrt{s^2+\epsilon^2}\f$ is a robust penalizer to limit the
  132. influence of outliers. A complete formulation and a description of the minimization
  133. procedure can be found in @cite Brox2004
  134. */
  135. class CV_EXPORTS_W VariationalRefinement : public DenseOpticalFlow
  136. {
  137. public:
  138. /** @brief @ref calc function overload to handle separate horizontal (u) and vertical (v) flow components
  139. (to avoid extra splits/merges) */
  140. CV_WRAP virtual void calcUV(InputArray I0, InputArray I1, InputOutputArray flow_u, InputOutputArray flow_v) = 0;
  141. /** @brief Number of outer (fixed-point) iterations in the minimization procedure.
  142. @see setFixedPointIterations */
  143. CV_WRAP virtual int getFixedPointIterations() const = 0;
  144. /** @copybrief getFixedPointIterations @see getFixedPointIterations */
  145. CV_WRAP virtual void setFixedPointIterations(int val) = 0;
  146. /** @brief Number of inner successive over-relaxation (SOR) iterations
  147. in the minimization procedure to solve the respective linear system.
  148. @see setSorIterations */
  149. CV_WRAP virtual int getSorIterations() const = 0;
  150. /** @copybrief getSorIterations @see getSorIterations */
  151. CV_WRAP virtual void setSorIterations(int val) = 0;
  152. /** @brief Relaxation factor in SOR
  153. @see setOmega */
  154. CV_WRAP virtual float getOmega() const = 0;
  155. /** @copybrief getOmega @see getOmega */
  156. CV_WRAP virtual void setOmega(float val) = 0;
  157. /** @brief Weight of the smoothness term
  158. @see setAlpha */
  159. CV_WRAP virtual float getAlpha() const = 0;
  160. /** @copybrief getAlpha @see getAlpha */
  161. CV_WRAP virtual void setAlpha(float val) = 0;
  162. /** @brief Weight of the color constancy term
  163. @see setDelta */
  164. CV_WRAP virtual float getDelta() const = 0;
  165. /** @copybrief getDelta @see getDelta */
  166. CV_WRAP virtual void setDelta(float val) = 0;
  167. /** @brief Weight of the gradient constancy term
  168. @see setGamma */
  169. CV_WRAP virtual float getGamma() const = 0;
  170. /** @copybrief getGamma @see getGamma */
  171. CV_WRAP virtual void setGamma(float val) = 0;
  172. };
  173. /** @brief Creates an instance of VariationalRefinement
  174. */
  175. CV_EXPORTS_W Ptr<VariationalRefinement> createVariationalFlowRefinement();
  176. /** @brief DeepFlow optical flow algorithm implementation.
  177. The class implements the DeepFlow optical flow algorithm described in @cite Weinzaepfel2013 . See
  178. also <http://lear.inrialpes.fr/src/deepmatching/> .
  179. Parameters - class fields - that may be modified after creating a class instance:
  180. - member float alpha
  181. Smoothness assumption weight
  182. - member float delta
  183. Color constancy assumption weight
  184. - member float gamma
  185. Gradient constancy weight
  186. - member float sigma
  187. Gaussian smoothing parameter
  188. - member int minSize
  189. Minimal dimension of an image in the pyramid (next, smaller images in the pyramid are generated
  190. until one of the dimensions reaches this size)
  191. - member float downscaleFactor
  192. Scaling factor in the image pyramid (must be \< 1)
  193. - member int fixedPointIterations
  194. How many iterations on each level of the pyramid
  195. - member int sorIterations
  196. Iterations of Succesive Over-Relaxation (solver)
  197. - member float omega
  198. Relaxation factor in SOR
  199. */
  200. CV_EXPORTS_W Ptr<DenseOpticalFlow> createOptFlow_DeepFlow();
  201. //! Additional interface to the SimpleFlow algorithm - calcOpticalFlowSF()
  202. CV_EXPORTS_W Ptr<DenseOpticalFlow> createOptFlow_SimpleFlow();
  203. //! Additional interface to the Farneback's algorithm - calcOpticalFlowFarneback()
  204. CV_EXPORTS_W Ptr<DenseOpticalFlow> createOptFlow_Farneback();
  205. //! Additional interface to the SparseToDenseFlow algorithm - calcOpticalFlowSparseToDense()
  206. CV_EXPORTS_W Ptr<DenseOpticalFlow> createOptFlow_SparseToDense();
  207. /** @brief DIS optical flow algorithm.
  208. This class implements the Dense Inverse Search (DIS) optical flow algorithm. More
  209. details about the algorithm can be found at @cite Kroeger2016 . Includes three presets with preselected
  210. parameters to provide reasonable trade-off between speed and quality. However, even the slowest preset is
  211. still relatively fast, use DeepFlow if you need better quality and don't care about speed.
  212. This implementation includes several additional features compared to the algorithm described in the paper,
  213. including spatial propagation of flow vectors (@ref getUseSpatialPropagation), as well as an option to
  214. utilize an initial flow approximation passed to @ref calc (which is, essentially, temporal propagation,
  215. if the previous frame's flow field is passed).
  216. */
  217. class CV_EXPORTS_W DISOpticalFlow : public DenseOpticalFlow
  218. {
  219. public:
  220. enum
  221. {
  222. PRESET_ULTRAFAST = 0,
  223. PRESET_FAST = 1,
  224. PRESET_MEDIUM = 2
  225. };
  226. /** @brief Finest level of the Gaussian pyramid on which the flow is computed (zero level
  227. corresponds to the original image resolution). The final flow is obtained by bilinear upscaling.
  228. @see setFinestScale */
  229. CV_WRAP virtual int getFinestScale() const = 0;
  230. /** @copybrief getFinestScale @see getFinestScale */
  231. CV_WRAP virtual void setFinestScale(int val) = 0;
  232. /** @brief Size of an image patch for matching (in pixels). Normally, default 8x8 patches work well
  233. enough in most cases.
  234. @see setPatchSize */
  235. CV_WRAP virtual int getPatchSize() const = 0;
  236. /** @copybrief getPatchSize @see getPatchSize */
  237. CV_WRAP virtual void setPatchSize(int val) = 0;
  238. /** @brief Stride between neighbor patches. Must be less than patch size. Lower values correspond
  239. to higher flow quality.
  240. @see setPatchStride */
  241. CV_WRAP virtual int getPatchStride() const = 0;
  242. /** @copybrief getPatchStride @see getPatchStride */
  243. CV_WRAP virtual void setPatchStride(int val) = 0;
  244. /** @brief Maximum number of gradient descent iterations in the patch inverse search stage. Higher values
  245. may improve quality in some cases.
  246. @see setGradientDescentIterations */
  247. CV_WRAP virtual int getGradientDescentIterations() const = 0;
  248. /** @copybrief getGradientDescentIterations @see getGradientDescentIterations */
  249. CV_WRAP virtual void setGradientDescentIterations(int val) = 0;
  250. /** @brief Number of fixed point iterations of variational refinement per scale. Set to zero to
  251. disable variational refinement completely. Higher values will typically result in more smooth and
  252. high-quality flow.
  253. @see setGradientDescentIterations */
  254. CV_WRAP virtual int getVariationalRefinementIterations() const = 0;
  255. /** @copybrief getGradientDescentIterations @see getGradientDescentIterations */
  256. CV_WRAP virtual void setVariationalRefinementIterations(int val) = 0;
  257. /** @brief Weight of the smoothness term
  258. @see setVariationalRefinementAlpha */
  259. CV_WRAP virtual float getVariationalRefinementAlpha() const = 0;
  260. /** @copybrief getVariationalRefinementAlpha @see getVariationalRefinementAlpha */
  261. CV_WRAP virtual void setVariationalRefinementAlpha(float val) = 0;
  262. /** @brief Weight of the color constancy term
  263. @see setVariationalRefinementDelta */
  264. CV_WRAP virtual float getVariationalRefinementDelta() const = 0;
  265. /** @copybrief getVariationalRefinementDelta @see getVariationalRefinementDelta */
  266. CV_WRAP virtual void setVariationalRefinementDelta(float val) = 0;
  267. /** @brief Weight of the gradient constancy term
  268. @see setVariationalRefinementGamma */
  269. CV_WRAP virtual float getVariationalRefinementGamma() const = 0;
  270. /** @copybrief getVariationalRefinementGamma @see getVariationalRefinementGamma */
  271. CV_WRAP virtual void setVariationalRefinementGamma(float val) = 0;
  272. /** @brief Whether to use mean-normalization of patches when computing patch distance. It is turned on
  273. by default as it typically provides a noticeable quality boost because of increased robustness to
  274. illumination variations. Turn it off if you are certain that your sequence doesn't contain any changes
  275. in illumination.
  276. @see setUseMeanNormalization */
  277. CV_WRAP virtual bool getUseMeanNormalization() const = 0;
  278. /** @copybrief getUseMeanNormalization @see getUseMeanNormalization */
  279. CV_WRAP virtual void setUseMeanNormalization(bool val) = 0;
  280. /** @brief Whether to use spatial propagation of good optical flow vectors. This option is turned on by
  281. default, as it tends to work better on average and can sometimes help recover from major errors
  282. introduced by the coarse-to-fine scheme employed by the DIS optical flow algorithm. Turning this
  283. option off can make the output flow field a bit smoother, however.
  284. @see setUseSpatialPropagation */
  285. CV_WRAP virtual bool getUseSpatialPropagation() const = 0;
  286. /** @copybrief getUseSpatialPropagation @see getUseSpatialPropagation */
  287. CV_WRAP virtual void setUseSpatialPropagation(bool val) = 0;
  288. };
  289. /** @brief Creates an instance of DISOpticalFlow
  290. @param preset one of PRESET_ULTRAFAST, PRESET_FAST and PRESET_MEDIUM
  291. */
  292. CV_EXPORTS_W Ptr<DISOpticalFlow> createOptFlow_DIS(int preset = DISOpticalFlow::PRESET_FAST);
  293. //! @}
  294. } //optflow
  295. }
  296. #include "opencv2/optflow/motempl.hpp"
  297. #endif