cudaoptflow.hpp 28 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594
  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_W 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. CV_WRAP 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_W 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. CV_WRAP 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. /** @brief Base Interface for optical flow algorithms using NVIDIA Optical Flow SDK.
  95. */
  96. class CV_EXPORTS_W NvidiaHWOpticalFlow : public Algorithm
  97. {
  98. public:
  99. /** @brief Calculates Optical Flow using NVIDIA Optical Flow SDK.
  100. * NVIDIA GPUs starting with Turing contain a dedicated hardware accelerator for computing optical flow vectors between pairs of images.
  101. * The optical flow hardware accelerator generates block-based optical flow vectors.
  102. * The size of the block depends on hardware in use, and can be queried using the function getGridSize().
  103. * The block-based flow vectors generated by the hardware can be converted to dense representation (i.e. per-pixel flow vectors) using upSampler() helper function, if needed.
  104. * The flow vectors are stored in CV_16SC2 format with x and y components of each flow vector in 16-bit signed fixed point representation S10.5.
  105. @param inputImage Input image.
  106. @param referenceImage Reference image of the same size and the same type as input image.
  107. @param flow A buffer consisting of inputImage.Size() / getGridSize() flow vectors in CV_16SC2 format.
  108. @param stream It is highly recommended that CUDA streams for pre and post processing of optical flow vectors should be set once per session in create() function as a part of optical flow session creation.
  109. This parameter is left here for backward compatibility and may be removed in the future.
  110. Default value is NULL stream;
  111. @param hint Hint buffer if client provides external hints. Must have same size as flow buffer.
  112. Caller can provide flow vectors as hints for optical flow calculation.
  113. @param cost Cost buffer contains numbers indicating the confidence associated with each of the generated flow vectors.
  114. Higher the cost, lower the confidence. Cost buffer is of type CV_32SC1.
  115. @note
  116. - Client must use critical sections around each calc() function if calling it from multiple threads.
  117. */
  118. CV_WRAP virtual void calc(
  119. InputArray inputImage,
  120. InputArray referenceImage,
  121. InputOutputArray flow,
  122. Stream& stream = Stream::Null(),
  123. InputArray hint = cv::noArray(),
  124. OutputArray cost = cv::noArray()) = 0;
  125. /** @brief Releases all buffers, contexts and device pointers.
  126. */
  127. CV_WRAP virtual void collectGarbage() = 0;
  128. /** @brief Returns grid size of output buffer as per the hardware's capability.
  129. */
  130. CV_WRAP virtual int getGridSize() const = 0;
  131. };
  132. //
  133. // BroxOpticalFlow
  134. //
  135. /** @brief Class computing the optical flow for two images using Brox et al Optical Flow algorithm (@cite Brox2004).
  136. */
  137. class CV_EXPORTS_W BroxOpticalFlow : public DenseOpticalFlow
  138. {
  139. public:
  140. CV_WRAP virtual double getFlowSmoothness() const = 0;
  141. CV_WRAP virtual void setFlowSmoothness(double alpha) = 0;
  142. CV_WRAP virtual double getGradientConstancyImportance() const = 0;
  143. CV_WRAP virtual void setGradientConstancyImportance(double gamma) = 0;
  144. CV_WRAP virtual double getPyramidScaleFactor() const = 0;
  145. CV_WRAP virtual void setPyramidScaleFactor(double scale_factor) = 0;
  146. //! number of lagged non-linearity iterations (inner loop)
  147. CV_WRAP virtual int getInnerIterations() const = 0;
  148. CV_WRAP virtual void setInnerIterations(int inner_iterations) = 0;
  149. //! number of warping iterations (number of pyramid levels)
  150. CV_WRAP virtual int getOuterIterations() const = 0;
  151. CV_WRAP virtual void setOuterIterations(int outer_iterations) = 0;
  152. //! number of linear system solver iterations
  153. CV_WRAP virtual int getSolverIterations() const = 0;
  154. CV_WRAP virtual void setSolverIterations(int solver_iterations) = 0;
  155. CV_WRAP static Ptr<BroxOpticalFlow> create(
  156. double alpha = 0.197,
  157. double gamma = 50.0,
  158. double scale_factor = 0.8,
  159. int inner_iterations = 5,
  160. int outer_iterations = 150,
  161. int solver_iterations = 10);
  162. };
  163. //
  164. // PyrLKOpticalFlow
  165. //
  166. /** @brief Class used for calculating a sparse optical flow.
  167. The class can calculate an optical flow for a sparse feature set using the
  168. iterative Lucas-Kanade method with pyramids.
  169. @sa calcOpticalFlowPyrLK
  170. @note
  171. - An example of the Lucas Kanade optical flow algorithm can be found at
  172. opencv_source_code/samples/gpu/pyrlk_optical_flow.cpp
  173. */
  174. class CV_EXPORTS_W SparsePyrLKOpticalFlow : public SparseOpticalFlow
  175. {
  176. public:
  177. CV_WRAP virtual Size getWinSize() const = 0;
  178. CV_WRAP virtual void setWinSize(Size winSize) = 0;
  179. CV_WRAP virtual int getMaxLevel() const = 0;
  180. CV_WRAP virtual void setMaxLevel(int maxLevel) = 0;
  181. CV_WRAP virtual int getNumIters() const = 0;
  182. CV_WRAP virtual void setNumIters(int iters) = 0;
  183. CV_WRAP virtual bool getUseInitialFlow() const = 0;
  184. CV_WRAP virtual void setUseInitialFlow(bool useInitialFlow) = 0;
  185. CV_WRAP static Ptr<cuda::SparsePyrLKOpticalFlow> create(
  186. Size winSize = Size(21, 21),
  187. int maxLevel = 3,
  188. int iters = 30,
  189. bool useInitialFlow = false);
  190. };
  191. /** @brief Class used for calculating a dense optical flow.
  192. The class can calculate an optical flow for a dense optical flow using the
  193. iterative Lucas-Kanade method with pyramids.
  194. */
  195. class CV_EXPORTS_W DensePyrLKOpticalFlow : public DenseOpticalFlow
  196. {
  197. public:
  198. CV_WRAP virtual Size getWinSize() const = 0;
  199. CV_WRAP virtual void setWinSize(Size winSize) = 0;
  200. CV_WRAP virtual int getMaxLevel() const = 0;
  201. CV_WRAP virtual void setMaxLevel(int maxLevel) = 0;
  202. CV_WRAP virtual int getNumIters() const = 0;
  203. CV_WRAP virtual void setNumIters(int iters) = 0;
  204. CV_WRAP virtual bool getUseInitialFlow() const = 0;
  205. CV_WRAP virtual void setUseInitialFlow(bool useInitialFlow) = 0;
  206. CV_WRAP static Ptr<DensePyrLKOpticalFlow> create(
  207. Size winSize = Size(13, 13),
  208. int maxLevel = 3,
  209. int iters = 30,
  210. bool useInitialFlow = false);
  211. };
  212. //
  213. // FarnebackOpticalFlow
  214. //
  215. /** @brief Class computing a dense optical flow using the Gunnar Farneback's algorithm.
  216. */
  217. class CV_EXPORTS_W FarnebackOpticalFlow : public DenseOpticalFlow
  218. {
  219. public:
  220. CV_WRAP virtual int getNumLevels() const = 0;
  221. CV_WRAP virtual void setNumLevels(int numLevels) = 0;
  222. CV_WRAP virtual double getPyrScale() const = 0;
  223. CV_WRAP virtual void setPyrScale(double pyrScale) = 0;
  224. CV_WRAP virtual bool getFastPyramids() const = 0;
  225. CV_WRAP virtual void setFastPyramids(bool fastPyramids) = 0;
  226. CV_WRAP virtual int getWinSize() const = 0;
  227. CV_WRAP virtual void setWinSize(int winSize) = 0;
  228. CV_WRAP virtual int getNumIters() const = 0;
  229. CV_WRAP virtual void setNumIters(int numIters) = 0;
  230. CV_WRAP virtual int getPolyN() const = 0;
  231. CV_WRAP virtual void setPolyN(int polyN) = 0;
  232. CV_WRAP virtual double getPolySigma() const = 0;
  233. CV_WRAP virtual void setPolySigma(double polySigma) = 0;
  234. CV_WRAP virtual int getFlags() const = 0;
  235. CV_WRAP virtual void setFlags(int flags) = 0;
  236. CV_WRAP static Ptr<cuda::FarnebackOpticalFlow> create(
  237. int numLevels = 5,
  238. double pyrScale = 0.5,
  239. bool fastPyramids = false,
  240. int winSize = 13,
  241. int numIters = 10,
  242. int polyN = 5,
  243. double polySigma = 1.1,
  244. int flags = 0);
  245. };
  246. //
  247. // OpticalFlowDual_TVL1
  248. //
  249. /** @brief Implementation of the Zach, Pock and Bischof Dual TV-L1 Optical Flow method.
  250. *
  251. * @note C. Zach, T. Pock and H. Bischof, "A Duality Based Approach for Realtime TV-L1 Optical Flow".
  252. * @note Javier Sanchez, Enric Meinhardt-Llopis and Gabriele Facciolo. "TV-L1 Optical Flow Estimation".
  253. */
  254. class CV_EXPORTS_W OpticalFlowDual_TVL1 : public DenseOpticalFlow
  255. {
  256. public:
  257. /**
  258. * Time step of the numerical scheme.
  259. */
  260. CV_WRAP virtual double getTau() const = 0;
  261. CV_WRAP virtual void setTau(double tau) = 0;
  262. /**
  263. * Weight parameter for the data term, attachment parameter.
  264. * This is the most relevant parameter, which determines the smoothness of the output.
  265. * The smaller this parameter is, the smoother the solutions we obtain.
  266. * It depends on the range of motions of the images, so its value should be adapted to each image sequence.
  267. */
  268. CV_WRAP virtual double getLambda() const = 0;
  269. CV_WRAP virtual void setLambda(double lambda) = 0;
  270. /**
  271. * Weight parameter for (u - v)^2, tightness parameter.
  272. * It serves as a link between the attachment and the regularization terms.
  273. * In theory, it should have a small value in order to maintain both parts in correspondence.
  274. * The method is stable for a large range of values of this parameter.
  275. */
  276. CV_WRAP virtual double getGamma() const = 0;
  277. CV_WRAP virtual void setGamma(double gamma) = 0;
  278. /**
  279. * parameter used for motion estimation. It adds a variable allowing for illumination variations
  280. * Set this parameter to 1. if you have varying illumination.
  281. * See: Chambolle et al, A First-Order Primal-Dual Algorithm for Convex Problems with Applications to Imaging
  282. * Journal of Mathematical imaging and vision, may 2011 Vol 40 issue 1, pp 120-145
  283. */
  284. CV_WRAP virtual double getTheta() const = 0;
  285. CV_WRAP virtual void setTheta(double theta) = 0;
  286. /**
  287. * Number of scales used to create the pyramid of images.
  288. */
  289. CV_WRAP virtual int getNumScales() const = 0;
  290. CV_WRAP virtual void setNumScales(int nscales) = 0;
  291. /**
  292. * Number of warpings per scale.
  293. * Represents the number of times that I1(x+u0) and grad( I1(x+u0) ) are computed per scale.
  294. * This is a parameter that assures the stability of the method.
  295. * It also affects the running time, so it is a compromise between speed and accuracy.
  296. */
  297. CV_WRAP virtual int getNumWarps() const = 0;
  298. CV_WRAP virtual void setNumWarps(int warps) = 0;
  299. /**
  300. * Stopping criterion threshold used in the numerical scheme, which is a trade-off between precision and running time.
  301. * A small value will yield more accurate solutions at the expense of a slower convergence.
  302. */
  303. CV_WRAP virtual double getEpsilon() const = 0;
  304. CV_WRAP virtual void setEpsilon(double epsilon) = 0;
  305. /**
  306. * Stopping criterion iterations number used in the numerical scheme.
  307. */
  308. CV_WRAP virtual int getNumIterations() const = 0;
  309. CV_WRAP virtual void setNumIterations(int iterations) = 0;
  310. CV_WRAP virtual double getScaleStep() const = 0;
  311. CV_WRAP virtual void setScaleStep(double scaleStep) = 0;
  312. CV_WRAP virtual bool getUseInitialFlow() const = 0;
  313. CV_WRAP virtual void setUseInitialFlow(bool useInitialFlow) = 0;
  314. CV_WRAP static Ptr<OpticalFlowDual_TVL1> create(
  315. double tau = 0.25,
  316. double lambda = 0.15,
  317. double theta = 0.3,
  318. int nscales = 5,
  319. int warps = 5,
  320. double epsilon = 0.01,
  321. int iterations = 300,
  322. double scaleStep = 0.8,
  323. double gamma = 0.0,
  324. bool useInitialFlow = false);
  325. };
  326. //
  327. // NvidiaOpticalFlow
  328. //
  329. /** @brief Class for computing the optical flow vectors between two images using NVIDIA Optical Flow hardware and Optical Flow SDK 1.0.
  330. @note
  331. - A sample application demonstrating the use of NVIDIA Optical Flow can be found at
  332. opencv_contrib_source_code/modules/cudaoptflow/samples/nvidia_optical_flow.cpp
  333. - An example application comparing accuracy and performance of NVIDIA Optical Flow with other optical flow algorithms in OpenCV can be found at
  334. opencv_contrib_source_code/modules/cudaoptflow/samples/optical_flow.cpp
  335. */
  336. class CV_EXPORTS_W NvidiaOpticalFlow_1_0 : public NvidiaHWOpticalFlow
  337. {
  338. public:
  339. /**
  340. * Supported optical flow performance levels.
  341. */
  342. enum NVIDIA_OF_PERF_LEVEL
  343. {
  344. NV_OF_PERF_LEVEL_UNDEFINED,
  345. NV_OF_PERF_LEVEL_SLOW = 5, /**< Slow perf level results in lowest performance and best quality */
  346. NV_OF_PERF_LEVEL_MEDIUM = 10, /**< Medium perf level results in low performance and medium quality */
  347. NV_OF_PERF_LEVEL_FAST = 20, /**< Fast perf level results in high performance and low quality */
  348. NV_OF_PERF_LEVEL_MAX
  349. };
  350. /** @brief The NVIDIA optical flow hardware generates flow vectors at granularity gridSize, which can be queried via function getGridSize().
  351. * Upsampler() helper function converts the hardware-generated flow vectors to dense representation (1 flow vector for each pixel)
  352. * using nearest neighbour upsampling method.
  353. @param flow Buffer of type CV_16FC2 containing flow vectors generated by calc().
  354. @param imageSize Size of the input image in pixels for which these flow vectors were generated.
  355. @param gridSize Granularity of the optical flow vectors returned by calc() function. Can be queried using getGridSize().
  356. @param upsampledFlow Buffer of type CV_32FC2, containing upsampled flow vectors, each flow vector for 1 pixel, in the pitch-linear layout.
  357. */
  358. CV_WRAP virtual void upSampler(InputArray flow, cv::Size imageSize,
  359. int gridSize, InputOutputArray upsampledFlow) = 0;
  360. /** @brief Instantiate NVIDIA Optical Flow
  361. @param imageSize Size of input image in pixels.
  362. @param perfPreset Optional parameter. Refer [NV OF SDK documentation](https://developer.nvidia.com/opticalflow-sdk) for details about presets.
  363. Defaults to NV_OF_PERF_LEVEL_SLOW.
  364. @param enableTemporalHints Optional parameter. Flag to enable temporal hints. When set to true, the hardware uses the flow vectors
  365. generated in previous call to calc() as internal hints for the current call to calc().
  366. Useful when computing flow vectors between successive video frames. Defaults to false.
  367. @param enableExternalHints Optional Parameter. Flag to enable passing external hints buffer to calc(). Defaults to false.
  368. @param enableCostBuffer Optional Parameter. Flag to enable cost buffer output from calc(). Defaults to false.
  369. @param gpuId Optional parameter to select the GPU ID on which the optical flow should be computed. Useful in multi-GPU systems. Defaults to 0.
  370. @param inputStream Optical flow algorithm may optionally involve cuda preprocessing on the input buffers.
  371. The input cuda stream can be used to pipeline and synchronize the cuda preprocessing tasks with OF HW engine.
  372. If input stream is not set, the execute function will use default stream which is NULL stream;
  373. @param outputStream Optical flow algorithm may optionally involve cuda post processing on the output flow vectors.
  374. The output cuda stream can be used to pipeline and synchronize the cuda post processing tasks with OF HW engine.
  375. If output stream is not set, the execute function will use default stream which is NULL stream;
  376. */
  377. CV_WRAP static Ptr<NvidiaOpticalFlow_1_0> create(
  378. cv::Size imageSize,
  379. cv::cuda::NvidiaOpticalFlow_1_0::NVIDIA_OF_PERF_LEVEL perfPreset
  380. = cv::cuda::NvidiaOpticalFlow_1_0::NV_OF_PERF_LEVEL_SLOW,
  381. bool enableTemporalHints = false,
  382. bool enableExternalHints = false,
  383. bool enableCostBuffer = false,
  384. int gpuId = 0,
  385. Stream& inputStream = Stream::Null(),
  386. Stream& outputStream = Stream::Null());
  387. };
  388. /** @brief Class for computing the optical flow vectors between two images using NVIDIA Optical Flow hardware and Optical Flow SDK 2.0.
  389. @note
  390. - A sample application demonstrating the use of NVIDIA Optical Flow can be found at
  391. opencv_contrib_source_code/modules/cudaoptflow/samples/nvidia_optical_flow.cpp
  392. - An example application comparing accuracy and performance of NVIDIA Optical Flow with other optical flow algorithms in OpenCV can be found at
  393. opencv_contrib_source_code/modules/cudaoptflow/samples/optical_flow.cpp
  394. */
  395. class CV_EXPORTS_W NvidiaOpticalFlow_2_0 : public NvidiaHWOpticalFlow
  396. {
  397. public:
  398. /**
  399. * Supported optical flow performance levels.
  400. */
  401. enum NVIDIA_OF_PERF_LEVEL
  402. {
  403. NV_OF_PERF_LEVEL_UNDEFINED,
  404. NV_OF_PERF_LEVEL_SLOW = 5, /**< Slow perf level results in lowest performance and best quality */
  405. NV_OF_PERF_LEVEL_MEDIUM = 10, /**< Medium perf level results in low performance and medium quality */
  406. NV_OF_PERF_LEVEL_FAST = 20, /**< Fast perf level results in high performance and low quality */
  407. NV_OF_PERF_LEVEL_MAX
  408. };
  409. /**
  410. * Supported grid size for output buffer.
  411. */
  412. enum NVIDIA_OF_OUTPUT_VECTOR_GRID_SIZE
  413. {
  414. NV_OF_OUTPUT_VECTOR_GRID_SIZE_UNDEFINED,
  415. NV_OF_OUTPUT_VECTOR_GRID_SIZE_1 = 1, /**< Output buffer grid size is 1x1 */
  416. NV_OF_OUTPUT_VECTOR_GRID_SIZE_2 = 2, /**< Output buffer grid size is 2x2 */
  417. NV_OF_OUTPUT_VECTOR_GRID_SIZE_4 = 4, /**< Output buffer grid size is 4x4 */
  418. NV_OF_OUTPUT_VECTOR_GRID_SIZE_MAX
  419. };
  420. /**
  421. * Supported grid size for hint buffer.
  422. */
  423. enum NVIDIA_OF_HINT_VECTOR_GRID_SIZE
  424. {
  425. NV_OF_HINT_VECTOR_GRID_SIZE_UNDEFINED,
  426. NV_OF_HINT_VECTOR_GRID_SIZE_1 = 1, /**< Hint buffer grid size is 1x1.*/
  427. NV_OF_HINT_VECTOR_GRID_SIZE_2 = 2, /**< Hint buffer grid size is 2x2.*/
  428. NV_OF_HINT_VECTOR_GRID_SIZE_4 = 4, /**< Hint buffer grid size is 4x4.*/
  429. NV_OF_HINT_VECTOR_GRID_SIZE_8 = 8, /**< Hint buffer grid size is 8x8.*/
  430. NV_OF_HINT_VECTOR_GRID_SIZE_MAX
  431. };
  432. /** @brief convertToFloat() helper function converts the hardware-generated flow vectors to floating point representation (1 flow vector for gridSize).
  433. * gridSize can be queried via function getGridSize().
  434. @param flow Buffer of type CV_16FC2 containing flow vectors generated by calc().
  435. @param floatFlow Buffer of type CV_32FC2, containing flow vectors in floating point representation, each flow vector for 1 pixel per gridSize, in the pitch-linear layout.
  436. */
  437. CV_WRAP virtual void convertToFloat(InputArray flow, InputOutputArray floatFlow) = 0;
  438. /** @brief Instantiate NVIDIA Optical Flow
  439. @param imageSize Size of input image in pixels.
  440. @param perfPreset Optional parameter. Refer [NV OF SDK documentation](https://developer.nvidia.com/opticalflow-sdk) for details about presets.
  441. Defaults to NV_OF_PERF_LEVEL_SLOW.
  442. @param outputGridSize Optional parameter. Refer [NV OF SDK documentation](https://developer.nvidia.com/opticalflow-sdk) for details about output grid sizes.
  443. Defaults to NV_OF_OUTPUT_VECTOR_GRID_SIZE_1.
  444. @param hintGridSize Optional parameter. Refer [NV OF SDK documentation](https://developer.nvidia.com/opticalflow-sdk) for details about hint grid sizes.
  445. Defaults to NV_OF_HINT_VECTOR_GRID_SIZE_1.
  446. @param enableTemporalHints Optional parameter. Flag to enable temporal hints. When set to true, the hardware uses the flow vectors
  447. generated in previous call to calc() as internal hints for the current call to calc().
  448. Useful when computing flow vectors between successive video frames. Defaults to false.
  449. @param enableExternalHints Optional Parameter. Flag to enable passing external hints buffer to calc(). Defaults to false.
  450. @param enableCostBuffer Optional Parameter. Flag to enable cost buffer output from calc(). Defaults to false.
  451. @param gpuId Optional parameter to select the GPU ID on which the optical flow should be computed. Useful in multi-GPU systems. Defaults to 0.
  452. @param inputStream Optical flow algorithm may optionally involve cuda preprocessing on the input buffers.
  453. The input cuda stream can be used to pipeline and synchronize the cuda preprocessing tasks with OF HW engine.
  454. If input stream is not set, the execute function will use default stream which is NULL stream;
  455. @param outputStream Optical flow algorithm may optionally involve cuda post processing on the output flow vectors.
  456. The output cuda stream can be used to pipeline and synchronize the cuda post processing tasks with OF HW engine.
  457. If output stream is not set, the execute function will use default stream which is NULL stream;
  458. */
  459. CV_WRAP static Ptr<NvidiaOpticalFlow_2_0> create(
  460. cv::Size imageSize,
  461. cv::cuda::NvidiaOpticalFlow_2_0::NVIDIA_OF_PERF_LEVEL perfPreset
  462. = cv::cuda::NvidiaOpticalFlow_2_0::NV_OF_PERF_LEVEL_SLOW,
  463. cv::cuda::NvidiaOpticalFlow_2_0::NVIDIA_OF_OUTPUT_VECTOR_GRID_SIZE outputGridSize
  464. = cv::cuda::NvidiaOpticalFlow_2_0::NV_OF_OUTPUT_VECTOR_GRID_SIZE_1,
  465. cv::cuda::NvidiaOpticalFlow_2_0::NVIDIA_OF_HINT_VECTOR_GRID_SIZE hintGridSize
  466. = cv::cuda::NvidiaOpticalFlow_2_0::NV_OF_HINT_VECTOR_GRID_SIZE_1,
  467. bool enableTemporalHints = false,
  468. bool enableExternalHints = false,
  469. bool enableCostBuffer = false,
  470. int gpuId = 0,
  471. Stream& inputStream = Stream::Null(),
  472. Stream& outputStream = Stream::Null());
  473. /** @brief Instantiate NVIDIA Optical Flow with ROI Feature
  474. @param imageSize Size of input image in pixels.
  475. @param roiData Pointer to ROI data.
  476. @param perfPreset Optional parameter. Refer [NV OF SDK documentation](https://developer.nvidia.com/opticalflow-sdk) for details about presets.
  477. Defaults to NV_OF_PERF_LEVEL_SLOW.
  478. @param outputGridSize Optional parameter. Refer [NV OF SDK documentation](https://developer.nvidia.com/opticalflow-sdk) for details about output grid sizes.
  479. Defaults to NV_OF_OUTPUT_VECTOR_GRID_SIZE_1.
  480. @param hintGridSize Optional parameter. Refer [NV OF SDK documentation](https://developer.nvidia.com/opticalflow-sdk) for details about hint grid sizes.
  481. Defaults to NV_OF_HINT_VECTOR_GRID_SIZE_1.
  482. @param enableTemporalHints Optional parameter. Flag to enable temporal hints. When set to true, the hardware uses the flow vectors
  483. generated in previous call to calc() as internal hints for the current call to calc().
  484. Useful when computing flow vectors between successive video frames. Defaults to false.
  485. @param enableExternalHints Optional Parameter. Flag to enable passing external hints buffer to calc(). Defaults to false.
  486. @param enableCostBuffer Optional Parameter. Flag to enable cost buffer output from calc(). Defaults to false.
  487. @param gpuId Optional parameter to select the GPU ID on which the optical flow should be computed. Useful in multi-GPU systems. Defaults to 0.
  488. @param inputStream Optical flow algorithm may optionally involve cuda preprocessing on the input buffers.
  489. The input cuda stream can be used to pipeline and synchronize the cuda preprocessing tasks with OF HW engine.
  490. If input stream is not set, the execute function will use default stream which is NULL stream;
  491. @param outputStream Optical flow algorithm may optionally involve cuda post processing on the output flow vectors.
  492. The output cuda stream can be used to pipeline and synchronize the cuda post processing tasks with OF HW engine.
  493. If output stream is not set, the execute function will use default stream which is NULL stream;
  494. */
  495. CV_WRAP static Ptr<NvidiaOpticalFlow_2_0> create(
  496. cv::Size imageSize,
  497. std::vector<Rect> roiData,
  498. cv::cuda::NvidiaOpticalFlow_2_0::NVIDIA_OF_PERF_LEVEL perfPreset
  499. = cv::cuda::NvidiaOpticalFlow_2_0::NV_OF_PERF_LEVEL_SLOW,
  500. cv::cuda::NvidiaOpticalFlow_2_0::NVIDIA_OF_OUTPUT_VECTOR_GRID_SIZE outputGridSize
  501. = cv::cuda::NvidiaOpticalFlow_2_0::NV_OF_OUTPUT_VECTOR_GRID_SIZE_1,
  502. cv::cuda::NvidiaOpticalFlow_2_0::NVIDIA_OF_HINT_VECTOR_GRID_SIZE hintGridSize
  503. = cv::cuda::NvidiaOpticalFlow_2_0::NV_OF_HINT_VECTOR_GRID_SIZE_1,
  504. bool enableTemporalHints = false,
  505. bool enableExternalHints = false,
  506. bool enableCostBuffer = false,
  507. int gpuId = 0,
  508. Stream& inputStream = Stream::Null(),
  509. Stream& outputStream = Stream::Null());
  510. };
  511. //! @}
  512. }} // namespace cv { namespace cuda {
  513. #endif /* OPENCV_CUDAOPTFLOW_HPP */