Video.cs 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240
  1. using OpenCVForUnity.CoreModule;
  2. using OpenCVForUnity.UtilsModule;
  3. using System;
  4. using System.Collections.Generic;
  5. using System.Runtime.InteropServices;
  6. namespace OpenCVForUnity.VideoModule
  7. {
  8. // C++: class Video
  9. public class Video
  10. {
  11. private const int CV_LKFLOW_INITIAL_GUESSES = 4;
  12. private const int CV_LKFLOW_GET_MIN_EIGENVALS = 8;
  13. // C++: enum <unnamed>
  14. public const int OPTFLOW_USE_INITIAL_FLOW = 4;
  15. public const int OPTFLOW_LK_GET_MIN_EIGENVALS = 8;
  16. public const int OPTFLOW_FARNEBACK_GAUSSIAN = 256;
  17. public const int MOTION_TRANSLATION = 0;
  18. public const int MOTION_EUCLIDEAN = 1;
  19. public const int MOTION_AFFINE = 2;
  20. public const int MOTION_HOMOGRAPHY = 3;
  21. // C++: enum cv.detail.TrackerSamplerCSC.MODE
  22. public const int TrackerSamplerCSC_MODE_INIT_POS = 1;
  23. public const int TrackerSamplerCSC_MODE_INIT_NEG = 2;
  24. public const int TrackerSamplerCSC_MODE_TRACK_POS = 3;
  25. public const int TrackerSamplerCSC_MODE_TRACK_NEG = 4;
  26. public const int TrackerSamplerCSC_MODE_DETECT = 5;
  27. //
  28. // C++: Ptr_BackgroundSubtractorMOG2 cv::createBackgroundSubtractorMOG2(int history = 500, double varThreshold = 16, bool detectShadows = true)
  29. //
  30. /**
  31. * Creates MOG2 Background Subtractor
  32. *
  33. * param history Length of the history.
  34. * param varThreshold Threshold on the squared Mahalanobis distance between the pixel and the model
  35. * to decide whether a pixel is well described by the background model. This parameter does not
  36. * affect the background update.
  37. * param detectShadows If true, the algorithm will detect shadows and mark them. It decreases the
  38. * speed a bit, so if you do not need this feature, set the parameter to false.
  39. * return automatically generated
  40. */
  41. public static BackgroundSubtractorMOG2 createBackgroundSubtractorMOG2(int history, double varThreshold, bool detectShadows)
  42. {
  43. return BackgroundSubtractorMOG2.__fromPtr__(DisposableObject.ThrowIfNullIntPtr(video_Video_createBackgroundSubtractorMOG2_10(history, varThreshold, detectShadows)));
  44. }
  45. /**
  46. * Creates MOG2 Background Subtractor
  47. *
  48. * param history Length of the history.
  49. * param varThreshold Threshold on the squared Mahalanobis distance between the pixel and the model
  50. * to decide whether a pixel is well described by the background model. This parameter does not
  51. * affect the background update.
  52. * speed a bit, so if you do not need this feature, set the parameter to false.
  53. * return automatically generated
  54. */
  55. public static BackgroundSubtractorMOG2 createBackgroundSubtractorMOG2(int history, double varThreshold)
  56. {
  57. return BackgroundSubtractorMOG2.__fromPtr__(DisposableObject.ThrowIfNullIntPtr(video_Video_createBackgroundSubtractorMOG2_11(history, varThreshold)));
  58. }
  59. /**
  60. * Creates MOG2 Background Subtractor
  61. *
  62. * param history Length of the history.
  63. * to decide whether a pixel is well described by the background model. This parameter does not
  64. * affect the background update.
  65. * speed a bit, so if you do not need this feature, set the parameter to false.
  66. * return automatically generated
  67. */
  68. public static BackgroundSubtractorMOG2 createBackgroundSubtractorMOG2(int history)
  69. {
  70. return BackgroundSubtractorMOG2.__fromPtr__(DisposableObject.ThrowIfNullIntPtr(video_Video_createBackgroundSubtractorMOG2_12(history)));
  71. }
  72. /**
  73. * Creates MOG2 Background Subtractor
  74. *
  75. * to decide whether a pixel is well described by the background model. This parameter does not
  76. * affect the background update.
  77. * speed a bit, so if you do not need this feature, set the parameter to false.
  78. * return automatically generated
  79. */
  80. public static BackgroundSubtractorMOG2 createBackgroundSubtractorMOG2()
  81. {
  82. return BackgroundSubtractorMOG2.__fromPtr__(DisposableObject.ThrowIfNullIntPtr(video_Video_createBackgroundSubtractorMOG2_13()));
  83. }
  84. //
  85. // C++: Ptr_BackgroundSubtractorKNN cv::createBackgroundSubtractorKNN(int history = 500, double dist2Threshold = 400.0, bool detectShadows = true)
  86. //
  87. /**
  88. * Creates KNN Background Subtractor
  89. *
  90. * param history Length of the history.
  91. * param dist2Threshold Threshold on the squared distance between the pixel and the sample to decide
  92. * whether a pixel is close to that sample. This parameter does not affect the background update.
  93. * param detectShadows If true, the algorithm will detect shadows and mark them. It decreases the
  94. * speed a bit, so if you do not need this feature, set the parameter to false.
  95. * return automatically generated
  96. */
  97. public static BackgroundSubtractorKNN createBackgroundSubtractorKNN(int history, double dist2Threshold, bool detectShadows)
  98. {
  99. return BackgroundSubtractorKNN.__fromPtr__(DisposableObject.ThrowIfNullIntPtr(video_Video_createBackgroundSubtractorKNN_10(history, dist2Threshold, detectShadows)));
  100. }
  101. /**
  102. * Creates KNN Background Subtractor
  103. *
  104. * param history Length of the history.
  105. * param dist2Threshold Threshold on the squared distance between the pixel and the sample to decide
  106. * whether a pixel is close to that sample. This parameter does not affect the background update.
  107. * speed a bit, so if you do not need this feature, set the parameter to false.
  108. * return automatically generated
  109. */
  110. public static BackgroundSubtractorKNN createBackgroundSubtractorKNN(int history, double dist2Threshold)
  111. {
  112. return BackgroundSubtractorKNN.__fromPtr__(DisposableObject.ThrowIfNullIntPtr(video_Video_createBackgroundSubtractorKNN_11(history, dist2Threshold)));
  113. }
  114. /**
  115. * Creates KNN Background Subtractor
  116. *
  117. * param history Length of the history.
  118. * whether a pixel is close to that sample. This parameter does not affect the background update.
  119. * speed a bit, so if you do not need this feature, set the parameter to false.
  120. * return automatically generated
  121. */
  122. public static BackgroundSubtractorKNN createBackgroundSubtractorKNN(int history)
  123. {
  124. return BackgroundSubtractorKNN.__fromPtr__(DisposableObject.ThrowIfNullIntPtr(video_Video_createBackgroundSubtractorKNN_12(history)));
  125. }
  126. /**
  127. * Creates KNN Background Subtractor
  128. *
  129. * whether a pixel is close to that sample. This parameter does not affect the background update.
  130. * speed a bit, so if you do not need this feature, set the parameter to false.
  131. * return automatically generated
  132. */
  133. public static BackgroundSubtractorKNN createBackgroundSubtractorKNN()
  134. {
  135. return BackgroundSubtractorKNN.__fromPtr__(DisposableObject.ThrowIfNullIntPtr(video_Video_createBackgroundSubtractorKNN_13()));
  136. }
  137. //
  138. // C++: RotatedRect cv::CamShift(Mat probImage, Rect& window, TermCriteria criteria)
  139. //
  140. /**
  141. * Finds an object center, size, and orientation.
  142. *
  143. * param probImage Back projection of the object histogram. See calcBackProject.
  144. * param window Initial search window.
  145. * param criteria Stop criteria for the underlying meanShift.
  146. * returns
  147. * (in old interfaces) Number of iterations CAMSHIFT took to converge
  148. * The function implements the CAMSHIFT object tracking algorithm CITE: Bradski98 . First, it finds an
  149. * object center using meanShift and then adjusts the window size and finds the optimal rotation. The
  150. * function returns the rotated rectangle structure that includes the object position, size, and
  151. * orientation. The next position of the search window can be obtained with RotatedRect::boundingRect()
  152. *
  153. * See the OpenCV sample camshiftdemo.c that tracks colored objects.
  154. *
  155. * <b>Note:</b>
  156. * <ul>
  157. * <li>
  158. * (Python) A sample explaining the camshift tracking algorithm can be found at
  159. * opencv_source_code/samples/python/camshift.py
  160. * </li>
  161. * </ul>
  162. * return automatically generated
  163. */
  164. public static RotatedRect CamShift(Mat probImage, Rect window, TermCriteria criteria)
  165. {
  166. if (probImage != null) probImage.ThrowIfDisposed();
  167. double[] window_out = new double[4];
  168. double[] tmpArray = new double[5];
  169. video_Video_CamShift_10(probImage.nativeObj, window.x, window.y, window.width, window.height, window_out, criteria.type, criteria.maxCount, criteria.epsilon, tmpArray);
  170. RotatedRect retVal = new RotatedRect(tmpArray);
  171. if (window != null) { window.x = (int)window_out[0]; window.y = (int)window_out[1]; window.width = (int)window_out[2]; window.height = (int)window_out[3]; }
  172. return retVal;
  173. }
  174. //
  175. // C++: int cv::meanShift(Mat probImage, Rect& window, TermCriteria criteria)
  176. //
  177. /**
  178. * Finds an object on a back projection image.
  179. *
  180. * param probImage Back projection of the object histogram. See calcBackProject for details.
  181. * param window Initial search window.
  182. * param criteria Stop criteria for the iterative search algorithm.
  183. * returns
  184. * : Number of iterations CAMSHIFT took to converge.
  185. * The function implements the iterative object search algorithm. It takes the input back projection of
  186. * an object and the initial position. The mass center in window of the back projection image is
  187. * computed and the search window center shifts to the mass center. The procedure is repeated until the
  188. * specified number of iterations criteria.maxCount is done or until the window center shifts by less
  189. * than criteria.epsilon. The algorithm is used inside CamShift and, unlike CamShift , the search
  190. * window size or orientation do not change during the search. You can simply pass the output of
  191. * calcBackProject to this function. But better results can be obtained if you pre-filter the back
  192. * projection and remove the noise. For example, you can do this by retrieving connected components
  193. * with findContours , throwing away contours with small area ( contourArea ), and rendering the
  194. * remaining contours with drawContours.
  195. * return automatically generated
  196. */
  197. public static int meanShift(Mat probImage, Rect window, TermCriteria criteria)
  198. {
  199. if (probImage != null) probImage.ThrowIfDisposed();
  200. double[] window_out = new double[4];
  201. int retVal = video_Video_meanShift_10(probImage.nativeObj, window.x, window.y, window.width, window.height, window_out, criteria.type, criteria.maxCount, criteria.epsilon);
  202. if (window != null) { window.x = (int)window_out[0]; window.y = (int)window_out[1]; window.width = (int)window_out[2]; window.height = (int)window_out[3]; }
  203. return retVal;
  204. }
  205. //
  206. // C++: int cv::buildOpticalFlowPyramid(Mat img, vector_Mat& pyramid, Size winSize, int maxLevel, bool withDerivatives = true, int pyrBorder = BORDER_REFLECT_101, int derivBorder = BORDER_CONSTANT, bool tryReuseInputImage = true)
  207. //
  208. /**
  209. * Constructs the image pyramid which can be passed to calcOpticalFlowPyrLK.
  210. *
  211. * param img 8-bit input image.
  212. * param pyramid output pyramid.
  213. * param winSize window size of optical flow algorithm. Must be not less than winSize argument of
  214. * calcOpticalFlowPyrLK. It is needed to calculate required padding for pyramid levels.
  215. * param maxLevel 0-based maximal pyramid level number.
  216. * param withDerivatives set to precompute gradients for the every pyramid level. If pyramid is
  217. * constructed without the gradients then calcOpticalFlowPyrLK will calculate them internally.
  218. * param pyrBorder the border mode for pyramid layers.
  219. * param derivBorder the border mode for gradients.
  220. * param tryReuseInputImage put ROI of input image into the pyramid if possible. You can pass false
  221. * to force data copying.
  222. * return number of levels in constructed pyramid. Can be less than maxLevel.
  223. */
  224. public static int buildOpticalFlowPyramid(Mat img, List<Mat> pyramid, Size winSize, int maxLevel, bool withDerivatives, int pyrBorder, int derivBorder, bool tryReuseInputImage)
  225. {
  226. if (img != null) img.ThrowIfDisposed();
  227. Mat pyramid_mat = new Mat();
  228. int retVal = video_Video_buildOpticalFlowPyramid_10(img.nativeObj, pyramid_mat.nativeObj, winSize.width, winSize.height, maxLevel, withDerivatives, pyrBorder, derivBorder, tryReuseInputImage);
  229. Converters.Mat_to_vector_Mat(pyramid_mat, pyramid);
  230. pyramid_mat.release();
  231. return retVal;
  232. }
  233. /**
  234. * Constructs the image pyramid which can be passed to calcOpticalFlowPyrLK.
  235. *
  236. * param img 8-bit input image.
  237. * param pyramid output pyramid.
  238. * param winSize window size of optical flow algorithm. Must be not less than winSize argument of
  239. * calcOpticalFlowPyrLK. It is needed to calculate required padding for pyramid levels.
  240. * param maxLevel 0-based maximal pyramid level number.
  241. * param withDerivatives set to precompute gradients for the every pyramid level. If pyramid is
  242. * constructed without the gradients then calcOpticalFlowPyrLK will calculate them internally.
  243. * param pyrBorder the border mode for pyramid layers.
  244. * param derivBorder the border mode for gradients.
  245. * to force data copying.
  246. * return number of levels in constructed pyramid. Can be less than maxLevel.
  247. */
  248. public static int buildOpticalFlowPyramid(Mat img, List<Mat> pyramid, Size winSize, int maxLevel, bool withDerivatives, int pyrBorder, int derivBorder)
  249. {
  250. if (img != null) img.ThrowIfDisposed();
  251. Mat pyramid_mat = new Mat();
  252. int retVal = video_Video_buildOpticalFlowPyramid_11(img.nativeObj, pyramid_mat.nativeObj, winSize.width, winSize.height, maxLevel, withDerivatives, pyrBorder, derivBorder);
  253. Converters.Mat_to_vector_Mat(pyramid_mat, pyramid);
  254. pyramid_mat.release();
  255. return retVal;
  256. }
  257. /**
  258. * Constructs the image pyramid which can be passed to calcOpticalFlowPyrLK.
  259. *
  260. * param img 8-bit input image.
  261. * param pyramid output pyramid.
  262. * param winSize window size of optical flow algorithm. Must be not less than winSize argument of
  263. * calcOpticalFlowPyrLK. It is needed to calculate required padding for pyramid levels.
  264. * param maxLevel 0-based maximal pyramid level number.
  265. * param withDerivatives set to precompute gradients for the every pyramid level. If pyramid is
  266. * constructed without the gradients then calcOpticalFlowPyrLK will calculate them internally.
  267. * param pyrBorder the border mode for pyramid layers.
  268. * to force data copying.
  269. * return number of levels in constructed pyramid. Can be less than maxLevel.
  270. */
  271. public static int buildOpticalFlowPyramid(Mat img, List<Mat> pyramid, Size winSize, int maxLevel, bool withDerivatives, int pyrBorder)
  272. {
  273. if (img != null) img.ThrowIfDisposed();
  274. Mat pyramid_mat = new Mat();
  275. int retVal = video_Video_buildOpticalFlowPyramid_12(img.nativeObj, pyramid_mat.nativeObj, winSize.width, winSize.height, maxLevel, withDerivatives, pyrBorder);
  276. Converters.Mat_to_vector_Mat(pyramid_mat, pyramid);
  277. pyramid_mat.release();
  278. return retVal;
  279. }
  280. /**
  281. * Constructs the image pyramid which can be passed to calcOpticalFlowPyrLK.
  282. *
  283. * param img 8-bit input image.
  284. * param pyramid output pyramid.
  285. * param winSize window size of optical flow algorithm. Must be not less than winSize argument of
  286. * calcOpticalFlowPyrLK. It is needed to calculate required padding for pyramid levels.
  287. * param maxLevel 0-based maximal pyramid level number.
  288. * param withDerivatives set to precompute gradients for the every pyramid level. If pyramid is
  289. * constructed without the gradients then calcOpticalFlowPyrLK will calculate them internally.
  290. * to force data copying.
  291. * return number of levels in constructed pyramid. Can be less than maxLevel.
  292. */
  293. public static int buildOpticalFlowPyramid(Mat img, List<Mat> pyramid, Size winSize, int maxLevel, bool withDerivatives)
  294. {
  295. if (img != null) img.ThrowIfDisposed();
  296. Mat pyramid_mat = new Mat();
  297. int retVal = video_Video_buildOpticalFlowPyramid_13(img.nativeObj, pyramid_mat.nativeObj, winSize.width, winSize.height, maxLevel, withDerivatives);
  298. Converters.Mat_to_vector_Mat(pyramid_mat, pyramid);
  299. pyramid_mat.release();
  300. return retVal;
  301. }
  302. /**
  303. * Constructs the image pyramid which can be passed to calcOpticalFlowPyrLK.
  304. *
  305. * param img 8-bit input image.
  306. * param pyramid output pyramid.
  307. * param winSize window size of optical flow algorithm. Must be not less than winSize argument of
  308. * calcOpticalFlowPyrLK. It is needed to calculate required padding for pyramid levels.
  309. * param maxLevel 0-based maximal pyramid level number.
  310. * constructed without the gradients then calcOpticalFlowPyrLK will calculate them internally.
  311. * to force data copying.
  312. * return number of levels in constructed pyramid. Can be less than maxLevel.
  313. */
  314. public static int buildOpticalFlowPyramid(Mat img, List<Mat> pyramid, Size winSize, int maxLevel)
  315. {
  316. if (img != null) img.ThrowIfDisposed();
  317. Mat pyramid_mat = new Mat();
  318. int retVal = video_Video_buildOpticalFlowPyramid_14(img.nativeObj, pyramid_mat.nativeObj, winSize.width, winSize.height, maxLevel);
  319. Converters.Mat_to_vector_Mat(pyramid_mat, pyramid);
  320. pyramid_mat.release();
  321. return retVal;
  322. }
  323. //
  324. // C++: void cv::calcOpticalFlowPyrLK(Mat prevImg, Mat nextImg, vector_Point2f prevPts, vector_Point2f& nextPts, vector_uchar& status, vector_float& err, Size winSize = Size(21,21), int maxLevel = 3, TermCriteria criteria = TermCriteria(TermCriteria::COUNT+TermCriteria::EPS, 30, 0.01), int flags = 0, double minEigThreshold = 1e-4)
  325. //
  326. /**
  327. * Calculates an optical flow for a sparse feature set using the iterative Lucas-Kanade method with
  328. * pyramids.
  329. *
  330. * param prevImg first 8-bit input image or pyramid constructed by buildOpticalFlowPyramid.
  331. * param nextImg second input image or pyramid of the same size and the same type as prevImg.
  332. * param prevPts vector of 2D points for which the flow needs to be found; point coordinates must be
  333. * single-precision floating-point numbers.
  334. * param nextPts output vector of 2D points (with single-precision floating-point coordinates)
  335. * containing the calculated new positions of input features in the second image; when
  336. * OPTFLOW_USE_INITIAL_FLOW flag is passed, the vector must have the same size as in the input.
  337. * param status output status vector (of unsigned chars); each element of the vector is set to 1 if
  338. * the flow for the corresponding features has been found, otherwise, it is set to 0.
  339. * param err output vector of errors; each element of the vector is set to an error for the
  340. * corresponding feature, type of the error measure can be set in flags parameter; if the flow wasn't
  341. * found then the error is not defined (use the status parameter to find such cases).
  342. * param winSize size of the search window at each pyramid level.
  343. * param maxLevel 0-based maximal pyramid level number; if set to 0, pyramids are not used (single
  344. * level), if set to 1, two levels are used, and so on; if pyramids are passed to input then
  345. * algorithm will use as many levels as pyramids have but no more than maxLevel.
  346. * param criteria parameter, specifying the termination criteria of the iterative search algorithm
  347. * (after the specified maximum number of iterations criteria.maxCount or when the search window
  348. * moves by less than criteria.epsilon.
  349. * param flags operation flags:
  350. * <ul>
  351. * <li>
  352. * <b>OPTFLOW_USE_INITIAL_FLOW</b> uses initial estimations, stored in nextPts; if the flag is
  353. * not set, then prevPts is copied to nextPts and is considered the initial estimate.
  354. * </li>
  355. * <li>
  356. * <b>OPTFLOW_LK_GET_MIN_EIGENVALS</b> use minimum eigen values as an error measure (see
  357. * minEigThreshold description); if the flag is not set, then L1 distance between patches
  358. * around the original and a moved point, divided by number of pixels in a window, is used as a
  359. * error measure.
  360. * </li>
  361. * </ul>
  362. * param minEigThreshold the algorithm calculates the minimum eigen value of a 2x2 normal matrix of
  363. * optical flow equations (this matrix is called a spatial gradient matrix in CITE: Bouguet00), divided
  364. * by number of pixels in a window; if this value is less than minEigThreshold, then a corresponding
  365. * feature is filtered out and its flow is not processed, so it allows to remove bad points and get a
  366. * performance boost.
  367. *
  368. * The function implements a sparse iterative version of the Lucas-Kanade optical flow in pyramids. See
  369. * CITE: Bouguet00 . The function is parallelized with the TBB library.
  370. *
  371. * <b>Note:</b>
  372. *
  373. * <ul>
  374. * <li>
  375. * An example using the Lucas-Kanade optical flow algorithm can be found at
  376. * opencv_source_code/samples/cpp/lkdemo.cpp
  377. * </li>
  378. * <li>
  379. * (Python) An example using the Lucas-Kanade optical flow algorithm can be found at
  380. * opencv_source_code/samples/python/lk_track.py
  381. * </li>
  382. * <li>
  383. * (Python) An example using the Lucas-Kanade tracker for homography matching can be found at
  384. * opencv_source_code/samples/python/lk_homography.py
  385. * </li>
  386. * </ul>
  387. */
  388. public static void calcOpticalFlowPyrLK(Mat prevImg, Mat nextImg, MatOfPoint2f prevPts, MatOfPoint2f nextPts, MatOfByte status, MatOfFloat err, Size winSize, int maxLevel, TermCriteria criteria, int flags, double minEigThreshold)
  389. {
  390. if (prevImg != null) prevImg.ThrowIfDisposed();
  391. if (nextImg != null) nextImg.ThrowIfDisposed();
  392. if (prevPts != null) prevPts.ThrowIfDisposed();
  393. if (nextPts != null) nextPts.ThrowIfDisposed();
  394. if (status != null) status.ThrowIfDisposed();
  395. if (err != null) err.ThrowIfDisposed();
  396. Mat prevPts_mat = prevPts;
  397. Mat nextPts_mat = nextPts;
  398. Mat status_mat = status;
  399. Mat err_mat = err;
  400. video_Video_calcOpticalFlowPyrLK_10(prevImg.nativeObj, nextImg.nativeObj, prevPts_mat.nativeObj, nextPts_mat.nativeObj, status_mat.nativeObj, err_mat.nativeObj, winSize.width, winSize.height, maxLevel, criteria.type, criteria.maxCount, criteria.epsilon, flags, minEigThreshold);
  401. }
  402. /**
  403. * Calculates an optical flow for a sparse feature set using the iterative Lucas-Kanade method with
  404. * pyramids.
  405. *
  406. * param prevImg first 8-bit input image or pyramid constructed by buildOpticalFlowPyramid.
  407. * param nextImg second input image or pyramid of the same size and the same type as prevImg.
  408. * param prevPts vector of 2D points for which the flow needs to be found; point coordinates must be
  409. * single-precision floating-point numbers.
  410. * param nextPts output vector of 2D points (with single-precision floating-point coordinates)
  411. * containing the calculated new positions of input features in the second image; when
  412. * OPTFLOW_USE_INITIAL_FLOW flag is passed, the vector must have the same size as in the input.
  413. * param status output status vector (of unsigned chars); each element of the vector is set to 1 if
  414. * the flow for the corresponding features has been found, otherwise, it is set to 0.
  415. * param err output vector of errors; each element of the vector is set to an error for the
  416. * corresponding feature, type of the error measure can be set in flags parameter; if the flow wasn't
  417. * found then the error is not defined (use the status parameter to find such cases).
  418. * param winSize size of the search window at each pyramid level.
  419. * param maxLevel 0-based maximal pyramid level number; if set to 0, pyramids are not used (single
  420. * level), if set to 1, two levels are used, and so on; if pyramids are passed to input then
  421. * algorithm will use as many levels as pyramids have but no more than maxLevel.
  422. * param criteria parameter, specifying the termination criteria of the iterative search algorithm
  423. * (after the specified maximum number of iterations criteria.maxCount or when the search window
  424. * moves by less than criteria.epsilon.
  425. * param flags operation flags:
  426. * <ul>
  427. * <li>
  428. * <b>OPTFLOW_USE_INITIAL_FLOW</b> uses initial estimations, stored in nextPts; if the flag is
  429. * not set, then prevPts is copied to nextPts and is considered the initial estimate.
  430. * </li>
  431. * <li>
  432. * <b>OPTFLOW_LK_GET_MIN_EIGENVALS</b> use minimum eigen values as an error measure (see
  433. * minEigThreshold description); if the flag is not set, then L1 distance between patches
  434. * around the original and a moved point, divided by number of pixels in a window, is used as a
  435. * error measure.
  436. * </li>
  437. * </ul>
  438. * optical flow equations (this matrix is called a spatial gradient matrix in CITE: Bouguet00), divided
  439. * by number of pixels in a window; if this value is less than minEigThreshold, then a corresponding
  440. * feature is filtered out and its flow is not processed, so it allows to remove bad points and get a
  441. * performance boost.
  442. *
  443. * The function implements a sparse iterative version of the Lucas-Kanade optical flow in pyramids. See
  444. * CITE: Bouguet00 . The function is parallelized with the TBB library.
  445. *
  446. * <b>Note:</b>
  447. *
  448. * <ul>
  449. * <li>
  450. * An example using the Lucas-Kanade optical flow algorithm can be found at
  451. * opencv_source_code/samples/cpp/lkdemo.cpp
  452. * </li>
  453. * <li>
  454. * (Python) An example using the Lucas-Kanade optical flow algorithm can be found at
  455. * opencv_source_code/samples/python/lk_track.py
  456. * </li>
  457. * <li>
  458. * (Python) An example using the Lucas-Kanade tracker for homography matching can be found at
  459. * opencv_source_code/samples/python/lk_homography.py
  460. * </li>
  461. * </ul>
  462. */
  463. public static void calcOpticalFlowPyrLK(Mat prevImg, Mat nextImg, MatOfPoint2f prevPts, MatOfPoint2f nextPts, MatOfByte status, MatOfFloat err, Size winSize, int maxLevel, TermCriteria criteria, int flags)
  464. {
  465. if (prevImg != null) prevImg.ThrowIfDisposed();
  466. if (nextImg != null) nextImg.ThrowIfDisposed();
  467. if (prevPts != null) prevPts.ThrowIfDisposed();
  468. if (nextPts != null) nextPts.ThrowIfDisposed();
  469. if (status != null) status.ThrowIfDisposed();
  470. if (err != null) err.ThrowIfDisposed();
  471. Mat prevPts_mat = prevPts;
  472. Mat nextPts_mat = nextPts;
  473. Mat status_mat = status;
  474. Mat err_mat = err;
  475. video_Video_calcOpticalFlowPyrLK_11(prevImg.nativeObj, nextImg.nativeObj, prevPts_mat.nativeObj, nextPts_mat.nativeObj, status_mat.nativeObj, err_mat.nativeObj, winSize.width, winSize.height, maxLevel, criteria.type, criteria.maxCount, criteria.epsilon, flags);
  476. }
  477. /**
  478. * Calculates an optical flow for a sparse feature set using the iterative Lucas-Kanade method with
  479. * pyramids.
  480. *
  481. * param prevImg first 8-bit input image or pyramid constructed by buildOpticalFlowPyramid.
  482. * param nextImg second input image or pyramid of the same size and the same type as prevImg.
  483. * param prevPts vector of 2D points for which the flow needs to be found; point coordinates must be
  484. * single-precision floating-point numbers.
  485. * param nextPts output vector of 2D points (with single-precision floating-point coordinates)
  486. * containing the calculated new positions of input features in the second image; when
  487. * OPTFLOW_USE_INITIAL_FLOW flag is passed, the vector must have the same size as in the input.
  488. * param status output status vector (of unsigned chars); each element of the vector is set to 1 if
  489. * the flow for the corresponding features has been found, otherwise, it is set to 0.
  490. * param err output vector of errors; each element of the vector is set to an error for the
  491. * corresponding feature, type of the error measure can be set in flags parameter; if the flow wasn't
  492. * found then the error is not defined (use the status parameter to find such cases).
  493. * param winSize size of the search window at each pyramid level.
  494. * param maxLevel 0-based maximal pyramid level number; if set to 0, pyramids are not used (single
  495. * level), if set to 1, two levels are used, and so on; if pyramids are passed to input then
  496. * algorithm will use as many levels as pyramids have but no more than maxLevel.
  497. * param criteria parameter, specifying the termination criteria of the iterative search algorithm
  498. * (after the specified maximum number of iterations criteria.maxCount or when the search window
  499. * moves by less than criteria.epsilon.
  500. * <ul>
  501. * <li>
  502. * <b>OPTFLOW_USE_INITIAL_FLOW</b> uses initial estimations, stored in nextPts; if the flag is
  503. * not set, then prevPts is copied to nextPts and is considered the initial estimate.
  504. * </li>
  505. * <li>
  506. * <b>OPTFLOW_LK_GET_MIN_EIGENVALS</b> use minimum eigen values as an error measure (see
  507. * minEigThreshold description); if the flag is not set, then L1 distance between patches
  508. * around the original and a moved point, divided by number of pixels in a window, is used as a
  509. * error measure.
  510. * </li>
  511. * </ul>
  512. * optical flow equations (this matrix is called a spatial gradient matrix in CITE: Bouguet00), divided
  513. * by number of pixels in a window; if this value is less than minEigThreshold, then a corresponding
  514. * feature is filtered out and its flow is not processed, so it allows to remove bad points and get a
  515. * performance boost.
  516. *
  517. * The function implements a sparse iterative version of the Lucas-Kanade optical flow in pyramids. See
  518. * CITE: Bouguet00 . The function is parallelized with the TBB library.
  519. *
  520. * <b>Note:</b>
  521. *
  522. * <ul>
  523. * <li>
  524. * An example using the Lucas-Kanade optical flow algorithm can be found at
  525. * opencv_source_code/samples/cpp/lkdemo.cpp
  526. * </li>
  527. * <li>
  528. * (Python) An example using the Lucas-Kanade optical flow algorithm can be found at
  529. * opencv_source_code/samples/python/lk_track.py
  530. * </li>
  531. * <li>
  532. * (Python) An example using the Lucas-Kanade tracker for homography matching can be found at
  533. * opencv_source_code/samples/python/lk_homography.py
  534. * </li>
  535. * </ul>
  536. */
  537. public static void calcOpticalFlowPyrLK(Mat prevImg, Mat nextImg, MatOfPoint2f prevPts, MatOfPoint2f nextPts, MatOfByte status, MatOfFloat err, Size winSize, int maxLevel, TermCriteria criteria)
  538. {
  539. if (prevImg != null) prevImg.ThrowIfDisposed();
  540. if (nextImg != null) nextImg.ThrowIfDisposed();
  541. if (prevPts != null) prevPts.ThrowIfDisposed();
  542. if (nextPts != null) nextPts.ThrowIfDisposed();
  543. if (status != null) status.ThrowIfDisposed();
  544. if (err != null) err.ThrowIfDisposed();
  545. Mat prevPts_mat = prevPts;
  546. Mat nextPts_mat = nextPts;
  547. Mat status_mat = status;
  548. Mat err_mat = err;
  549. video_Video_calcOpticalFlowPyrLK_12(prevImg.nativeObj, nextImg.nativeObj, prevPts_mat.nativeObj, nextPts_mat.nativeObj, status_mat.nativeObj, err_mat.nativeObj, winSize.width, winSize.height, maxLevel, criteria.type, criteria.maxCount, criteria.epsilon);
  550. }
  551. /**
  552. * Calculates an optical flow for a sparse feature set using the iterative Lucas-Kanade method with
  553. * pyramids.
  554. *
  555. * param prevImg first 8-bit input image or pyramid constructed by buildOpticalFlowPyramid.
  556. * param nextImg second input image or pyramid of the same size and the same type as prevImg.
  557. * param prevPts vector of 2D points for which the flow needs to be found; point coordinates must be
  558. * single-precision floating-point numbers.
  559. * param nextPts output vector of 2D points (with single-precision floating-point coordinates)
  560. * containing the calculated new positions of input features in the second image; when
  561. * OPTFLOW_USE_INITIAL_FLOW flag is passed, the vector must have the same size as in the input.
  562. * param status output status vector (of unsigned chars); each element of the vector is set to 1 if
  563. * the flow for the corresponding features has been found, otherwise, it is set to 0.
  564. * param err output vector of errors; each element of the vector is set to an error for the
  565. * corresponding feature, type of the error measure can be set in flags parameter; if the flow wasn't
  566. * found then the error is not defined (use the status parameter to find such cases).
  567. * param winSize size of the search window at each pyramid level.
  568. * param maxLevel 0-based maximal pyramid level number; if set to 0, pyramids are not used (single
  569. * level), if set to 1, two levels are used, and so on; if pyramids are passed to input then
  570. * algorithm will use as many levels as pyramids have but no more than maxLevel.
  571. * (after the specified maximum number of iterations criteria.maxCount or when the search window
  572. * moves by less than criteria.epsilon.
  573. * <ul>
  574. * <li>
  575. * <b>OPTFLOW_USE_INITIAL_FLOW</b> uses initial estimations, stored in nextPts; if the flag is
  576. * not set, then prevPts is copied to nextPts and is considered the initial estimate.
  577. * </li>
  578. * <li>
  579. * <b>OPTFLOW_LK_GET_MIN_EIGENVALS</b> use minimum eigen values as an error measure (see
  580. * minEigThreshold description); if the flag is not set, then L1 distance between patches
  581. * around the original and a moved point, divided by number of pixels in a window, is used as a
  582. * error measure.
  583. * </li>
  584. * </ul>
  585. * optical flow equations (this matrix is called a spatial gradient matrix in CITE: Bouguet00), divided
  586. * by number of pixels in a window; if this value is less than minEigThreshold, then a corresponding
  587. * feature is filtered out and its flow is not processed, so it allows to remove bad points and get a
  588. * performance boost.
  589. *
  590. * The function implements a sparse iterative version of the Lucas-Kanade optical flow in pyramids. See
  591. * CITE: Bouguet00 . The function is parallelized with the TBB library.
  592. *
  593. * <b>Note:</b>
  594. *
  595. * <ul>
  596. * <li>
  597. * An example using the Lucas-Kanade optical flow algorithm can be found at
  598. * opencv_source_code/samples/cpp/lkdemo.cpp
  599. * </li>
  600. * <li>
  601. * (Python) An example using the Lucas-Kanade optical flow algorithm can be found at
  602. * opencv_source_code/samples/python/lk_track.py
  603. * </li>
  604. * <li>
  605. * (Python) An example using the Lucas-Kanade tracker for homography matching can be found at
  606. * opencv_source_code/samples/python/lk_homography.py
  607. * </li>
  608. * </ul>
  609. */
  610. public static void calcOpticalFlowPyrLK(Mat prevImg, Mat nextImg, MatOfPoint2f prevPts, MatOfPoint2f nextPts, MatOfByte status, MatOfFloat err, Size winSize, int maxLevel)
  611. {
  612. if (prevImg != null) prevImg.ThrowIfDisposed();
  613. if (nextImg != null) nextImg.ThrowIfDisposed();
  614. if (prevPts != null) prevPts.ThrowIfDisposed();
  615. if (nextPts != null) nextPts.ThrowIfDisposed();
  616. if (status != null) status.ThrowIfDisposed();
  617. if (err != null) err.ThrowIfDisposed();
  618. Mat prevPts_mat = prevPts;
  619. Mat nextPts_mat = nextPts;
  620. Mat status_mat = status;
  621. Mat err_mat = err;
  622. video_Video_calcOpticalFlowPyrLK_13(prevImg.nativeObj, nextImg.nativeObj, prevPts_mat.nativeObj, nextPts_mat.nativeObj, status_mat.nativeObj, err_mat.nativeObj, winSize.width, winSize.height, maxLevel);
  623. }
  624. /**
  625. * Calculates an optical flow for a sparse feature set using the iterative Lucas-Kanade method with
  626. * pyramids.
  627. *
  628. * param prevImg first 8-bit input image or pyramid constructed by buildOpticalFlowPyramid.
  629. * param nextImg second input image or pyramid of the same size and the same type as prevImg.
  630. * param prevPts vector of 2D points for which the flow needs to be found; point coordinates must be
  631. * single-precision floating-point numbers.
  632. * param nextPts output vector of 2D points (with single-precision floating-point coordinates)
  633. * containing the calculated new positions of input features in the second image; when
  634. * OPTFLOW_USE_INITIAL_FLOW flag is passed, the vector must have the same size as in the input.
  635. * param status output status vector (of unsigned chars); each element of the vector is set to 1 if
  636. * the flow for the corresponding features has been found, otherwise, it is set to 0.
  637. * param err output vector of errors; each element of the vector is set to an error for the
  638. * corresponding feature, type of the error measure can be set in flags parameter; if the flow wasn't
  639. * found then the error is not defined (use the status parameter to find such cases).
  640. * param winSize size of the search window at each pyramid level.
  641. * level), if set to 1, two levels are used, and so on; if pyramids are passed to input then
  642. * algorithm will use as many levels as pyramids have but no more than maxLevel.
  643. * (after the specified maximum number of iterations criteria.maxCount or when the search window
  644. * moves by less than criteria.epsilon.
  645. * <ul>
  646. * <li>
  647. * <b>OPTFLOW_USE_INITIAL_FLOW</b> uses initial estimations, stored in nextPts; if the flag is
  648. * not set, then prevPts is copied to nextPts and is considered the initial estimate.
  649. * </li>
  650. * <li>
  651. * <b>OPTFLOW_LK_GET_MIN_EIGENVALS</b> use minimum eigen values as an error measure (see
  652. * minEigThreshold description); if the flag is not set, then L1 distance between patches
  653. * around the original and a moved point, divided by number of pixels in a window, is used as a
  654. * error measure.
  655. * </li>
  656. * </ul>
  657. * optical flow equations (this matrix is called a spatial gradient matrix in CITE: Bouguet00), divided
  658. * by number of pixels in a window; if this value is less than minEigThreshold, then a corresponding
  659. * feature is filtered out and its flow is not processed, so it allows to remove bad points and get a
  660. * performance boost.
  661. *
  662. * The function implements a sparse iterative version of the Lucas-Kanade optical flow in pyramids. See
  663. * CITE: Bouguet00 . The function is parallelized with the TBB library.
  664. *
  665. * <b>Note:</b>
  666. *
  667. * <ul>
  668. * <li>
  669. * An example using the Lucas-Kanade optical flow algorithm can be found at
  670. * opencv_source_code/samples/cpp/lkdemo.cpp
  671. * </li>
  672. * <li>
  673. * (Python) An example using the Lucas-Kanade optical flow algorithm can be found at
  674. * opencv_source_code/samples/python/lk_track.py
  675. * </li>
  676. * <li>
  677. * (Python) An example using the Lucas-Kanade tracker for homography matching can be found at
  678. * opencv_source_code/samples/python/lk_homography.py
  679. * </li>
  680. * </ul>
  681. */
  682. public static void calcOpticalFlowPyrLK(Mat prevImg, Mat nextImg, MatOfPoint2f prevPts, MatOfPoint2f nextPts, MatOfByte status, MatOfFloat err, Size winSize)
  683. {
  684. if (prevImg != null) prevImg.ThrowIfDisposed();
  685. if (nextImg != null) nextImg.ThrowIfDisposed();
  686. if (prevPts != null) prevPts.ThrowIfDisposed();
  687. if (nextPts != null) nextPts.ThrowIfDisposed();
  688. if (status != null) status.ThrowIfDisposed();
  689. if (err != null) err.ThrowIfDisposed();
  690. Mat prevPts_mat = prevPts;
  691. Mat nextPts_mat = nextPts;
  692. Mat status_mat = status;
  693. Mat err_mat = err;
  694. video_Video_calcOpticalFlowPyrLK_14(prevImg.nativeObj, nextImg.nativeObj, prevPts_mat.nativeObj, nextPts_mat.nativeObj, status_mat.nativeObj, err_mat.nativeObj, winSize.width, winSize.height);
  695. }
  696. /**
  697. * Calculates an optical flow for a sparse feature set using the iterative Lucas-Kanade method with
  698. * pyramids.
  699. *
  700. * param prevImg first 8-bit input image or pyramid constructed by buildOpticalFlowPyramid.
  701. * param nextImg second input image or pyramid of the same size and the same type as prevImg.
  702. * param prevPts vector of 2D points for which the flow needs to be found; point coordinates must be
  703. * single-precision floating-point numbers.
  704. * param nextPts output vector of 2D points (with single-precision floating-point coordinates)
  705. * containing the calculated new positions of input features in the second image; when
  706. * OPTFLOW_USE_INITIAL_FLOW flag is passed, the vector must have the same size as in the input.
  707. * param status output status vector (of unsigned chars); each element of the vector is set to 1 if
  708. * the flow for the corresponding features has been found, otherwise, it is set to 0.
  709. * param err output vector of errors; each element of the vector is set to an error for the
  710. * corresponding feature, type of the error measure can be set in flags parameter; if the flow wasn't
  711. * found then the error is not defined (use the status parameter to find such cases).
  712. * level), if set to 1, two levels are used, and so on; if pyramids are passed to input then
  713. * algorithm will use as many levels as pyramids have but no more than maxLevel.
  714. * (after the specified maximum number of iterations criteria.maxCount or when the search window
  715. * moves by less than criteria.epsilon.
  716. * <ul>
  717. * <li>
  718. * <b>OPTFLOW_USE_INITIAL_FLOW</b> uses initial estimations, stored in nextPts; if the flag is
  719. * not set, then prevPts is copied to nextPts and is considered the initial estimate.
  720. * </li>
  721. * <li>
  722. * <b>OPTFLOW_LK_GET_MIN_EIGENVALS</b> use minimum eigen values as an error measure (see
  723. * minEigThreshold description); if the flag is not set, then L1 distance between patches
  724. * around the original and a moved point, divided by number of pixels in a window, is used as a
  725. * error measure.
  726. * </li>
  727. * </ul>
  728. * optical flow equations (this matrix is called a spatial gradient matrix in CITE: Bouguet00), divided
  729. * by number of pixels in a window; if this value is less than minEigThreshold, then a corresponding
  730. * feature is filtered out and its flow is not processed, so it allows to remove bad points and get a
  731. * performance boost.
  732. *
  733. * The function implements a sparse iterative version of the Lucas-Kanade optical flow in pyramids. See
  734. * CITE: Bouguet00 . The function is parallelized with the TBB library.
  735. *
  736. * <b>Note:</b>
  737. *
  738. * <ul>
  739. * <li>
  740. * An example using the Lucas-Kanade optical flow algorithm can be found at
  741. * opencv_source_code/samples/cpp/lkdemo.cpp
  742. * </li>
  743. * <li>
  744. * (Python) An example using the Lucas-Kanade optical flow algorithm can be found at
  745. * opencv_source_code/samples/python/lk_track.py
  746. * </li>
  747. * <li>
  748. * (Python) An example using the Lucas-Kanade tracker for homography matching can be found at
  749. * opencv_source_code/samples/python/lk_homography.py
  750. * </li>
  751. * </ul>
  752. */
  753. public static void calcOpticalFlowPyrLK(Mat prevImg, Mat nextImg, MatOfPoint2f prevPts, MatOfPoint2f nextPts, MatOfByte status, MatOfFloat err)
  754. {
  755. if (prevImg != null) prevImg.ThrowIfDisposed();
  756. if (nextImg != null) nextImg.ThrowIfDisposed();
  757. if (prevPts != null) prevPts.ThrowIfDisposed();
  758. if (nextPts != null) nextPts.ThrowIfDisposed();
  759. if (status != null) status.ThrowIfDisposed();
  760. if (err != null) err.ThrowIfDisposed();
  761. Mat prevPts_mat = prevPts;
  762. Mat nextPts_mat = nextPts;
  763. Mat status_mat = status;
  764. Mat err_mat = err;
  765. video_Video_calcOpticalFlowPyrLK_15(prevImg.nativeObj, nextImg.nativeObj, prevPts_mat.nativeObj, nextPts_mat.nativeObj, status_mat.nativeObj, err_mat.nativeObj);
  766. }
  767. //
  768. // C++: void cv::calcOpticalFlowFarneback(Mat prev, Mat next, Mat& flow, double pyr_scale, int levels, int winsize, int iterations, int poly_n, double poly_sigma, int flags)
  769. //
  770. /**
  771. * Computes a dense optical flow using the Gunnar Farneback's algorithm.
  772. *
  773. * param prev first 8-bit single-channel input image.
  774. * param next second input image of the same size and the same type as prev.
  775. * param flow computed flow image that has the same size as prev and type CV_32FC2.
  776. * param pyr_scale parameter, specifying the image scale (&lt;1) to build pyramids for each image;
  777. * pyr_scale=0.5 means a classical pyramid, where each next layer is twice smaller than the previous
  778. * one.
  779. * param levels number of pyramid layers including the initial image; levels=1 means that no extra
  780. * layers are created and only the original images are used.
  781. * param winsize averaging window size; larger values increase the algorithm robustness to image
  782. * noise and give more chances for fast motion detection, but yield more blurred motion field.
  783. * param iterations number of iterations the algorithm does at each pyramid level.
  784. * param poly_n size of the pixel neighborhood used to find polynomial expansion in each pixel;
  785. * larger values mean that the image will be approximated with smoother surfaces, yielding more
  786. * robust algorithm and more blurred motion field, typically poly_n =5 or 7.
  787. * param poly_sigma standard deviation of the Gaussian that is used to smooth derivatives used as a
  788. * basis for the polynomial expansion; for poly_n=5, you can set poly_sigma=1.1, for poly_n=7, a
  789. * good value would be poly_sigma=1.5.
  790. * param flags operation flags that can be a combination of the following:
  791. * <ul>
  792. * <li>
  793. * <b>OPTFLOW_USE_INITIAL_FLOW</b> uses the input flow as an initial flow approximation.
  794. * </li>
  795. * <li>
  796. * <b>OPTFLOW_FARNEBACK_GAUSSIAN</b> uses the Gaussian \(\texttt{winsize}\times\texttt{winsize}\)
  797. * filter instead of a box filter of the same size for optical flow estimation; usually, this
  798. * option gives z more accurate flow than with a box filter, at the cost of lower speed;
  799. * normally, winsize for a Gaussian window should be set to a larger value to achieve the same
  800. * level of robustness.
  801. * </li>
  802. * </ul>
  803. *
  804. * The function finds an optical flow for each prev pixel using the CITE: Farneback2003 algorithm so that
  805. *
  806. * \(\texttt{prev} (y,x) \sim \texttt{next} ( y + \texttt{flow} (y,x)[1], x + \texttt{flow} (y,x)[0])\)
  807. *
  808. * <b>Note:</b>
  809. *
  810. * <ul>
  811. * <li>
  812. * An example using the optical flow algorithm described by Gunnar Farneback can be found at
  813. * opencv_source_code/samples/cpp/fback.cpp
  814. * </li>
  815. * <li>
  816. * (Python) An example using the optical flow algorithm described by Gunnar Farneback can be
  817. * found at opencv_source_code/samples/python/opt_flow.py
  818. * </li>
  819. * </ul>
  820. */
  821. public static void calcOpticalFlowFarneback(Mat prev, Mat next, Mat flow, double pyr_scale, int levels, int winsize, int iterations, int poly_n, double poly_sigma, int flags)
  822. {
  823. if (prev != null) prev.ThrowIfDisposed();
  824. if (next != null) next.ThrowIfDisposed();
  825. if (flow != null) flow.ThrowIfDisposed();
  826. video_Video_calcOpticalFlowFarneback_10(prev.nativeObj, next.nativeObj, flow.nativeObj, pyr_scale, levels, winsize, iterations, poly_n, poly_sigma, flags);
  827. }
  828. //
  829. // C++: double cv::computeECC(Mat templateImage, Mat inputImage, Mat inputMask = Mat())
  830. //
  831. /**
  832. * Computes the Enhanced Correlation Coefficient value between two images CITE: EP08 .
  833. *
  834. * param templateImage single-channel template image; CV_8U or CV_32F array.
  835. * param inputImage single-channel input image to be warped to provide an image similar to
  836. * templateImage, same type as templateImage.
  837. * param inputMask An optional mask to indicate valid values of inputImage.
  838. *
  839. * SEE:
  840. * findTransformECC
  841. * return automatically generated
  842. */
  843. public static double computeECC(Mat templateImage, Mat inputImage, Mat inputMask)
  844. {
  845. if (templateImage != null) templateImage.ThrowIfDisposed();
  846. if (inputImage != null) inputImage.ThrowIfDisposed();
  847. if (inputMask != null) inputMask.ThrowIfDisposed();
  848. return video_Video_computeECC_10(templateImage.nativeObj, inputImage.nativeObj, inputMask.nativeObj);
  849. }
  850. /**
  851. * Computes the Enhanced Correlation Coefficient value between two images CITE: EP08 .
  852. *
  853. * param templateImage single-channel template image; CV_8U or CV_32F array.
  854. * param inputImage single-channel input image to be warped to provide an image similar to
  855. * templateImage, same type as templateImage.
  856. *
  857. * SEE:
  858. * findTransformECC
  859. * return automatically generated
  860. */
  861. public static double computeECC(Mat templateImage, Mat inputImage)
  862. {
  863. if (templateImage != null) templateImage.ThrowIfDisposed();
  864. if (inputImage != null) inputImage.ThrowIfDisposed();
  865. return video_Video_computeECC_11(templateImage.nativeObj, inputImage.nativeObj);
  866. }
  867. //
  868. // C++: double cv::findTransformECC(Mat templateImage, Mat inputImage, Mat& warpMatrix, int motionType, TermCriteria criteria, Mat inputMask, int gaussFiltSize)
  869. //
  870. /**
  871. * Finds the geometric transform (warp) between two images in terms of the ECC criterion CITE: EP08 .
  872. *
  873. * param templateImage single-channel template image; CV_8U or CV_32F array.
  874. * param inputImage single-channel input image which should be warped with the final warpMatrix in
  875. * order to provide an image similar to templateImage, same type as templateImage.
  876. * param warpMatrix floating-point \(2\times 3\) or \(3\times 3\) mapping matrix (warp).
  877. * param motionType parameter, specifying the type of motion:
  878. * <ul>
  879. * <li>
  880. * <b>MOTION_TRANSLATION</b> sets a translational motion model; warpMatrix is \(2\times 3\) with
  881. * the first \(2\times 2\) part being the unity matrix and the rest two parameters being
  882. * estimated.
  883. * </li>
  884. * <li>
  885. * <b>MOTION_EUCLIDEAN</b> sets a Euclidean (rigid) transformation as motion model; three
  886. * parameters are estimated; warpMatrix is \(2\times 3\).
  887. * </li>
  888. * <li>
  889. * <b>MOTION_AFFINE</b> sets an affine motion model (DEFAULT); six parameters are estimated;
  890. * warpMatrix is \(2\times 3\).
  891. * </li>
  892. * <li>
  893. * <b>MOTION_HOMOGRAPHY</b> sets a homography as a motion model; eight parameters are
  894. * estimated;\{code warpMatrix\} is \(3\times 3\).
  895. * </li>
  896. * </ul>
  897. * param criteria parameter, specifying the termination criteria of the ECC algorithm;
  898. * criteria.epsilon defines the threshold of the increment in the correlation coefficient between two
  899. * iterations (a negative criteria.epsilon makes criteria.maxcount the only termination criterion).
  900. * Default values are shown in the declaration above.
  901. * param inputMask An optional mask to indicate valid values of inputImage.
  902. * param gaussFiltSize An optional value indicating size of gaussian blur filter; (DEFAULT: 5)
  903. *
  904. * The function estimates the optimum transformation (warpMatrix) with respect to ECC criterion
  905. * (CITE: EP08), that is
  906. *
  907. * \(\texttt{warpMatrix} = \arg\max_{W} \texttt{ECC}(\texttt{templateImage}(x,y),\texttt{inputImage}(x',y'))\)
  908. *
  909. * where
  910. *
  911. * \(\begin{bmatrix} x' \\ y' \end{bmatrix} = W \cdot \begin{bmatrix} x \\ y \\ 1 \end{bmatrix}\)
  912. *
  913. * (the equation holds with homogeneous coordinates for homography). It returns the final enhanced
  914. * correlation coefficient, that is the correlation coefficient between the template image and the
  915. * final warped input image. When a \(3\times 3\) matrix is given with motionType =0, 1 or 2, the third
  916. * row is ignored.
  917. *
  918. * Unlike findHomography and estimateRigidTransform, the function findTransformECC implements an
  919. * area-based alignment that builds on intensity similarities. In essence, the function updates the
  920. * initial transformation that roughly aligns the images. If this information is missing, the identity
  921. * warp (unity matrix) is used as an initialization. Note that if images undergo strong
  922. * displacements/rotations, an initial transformation that roughly aligns the images is necessary
  923. * (e.g., a simple euclidean/similarity transform that allows for the images showing the same image
  924. * content approximately). Use inverse warping in the second image to take an image close to the first
  925. * one, i.e. use the flag WARP_INVERSE_MAP with warpAffine or warpPerspective. See also the OpenCV
  926. * sample image_alignment.cpp that demonstrates the use of the function. Note that the function throws
  927. * an exception if algorithm does not converges.
  928. *
  929. * SEE:
  930. * computeECC, estimateAffine2D, estimateAffinePartial2D, findHomography
  931. * return automatically generated
  932. */
  933. public static double findTransformECC(Mat templateImage, Mat inputImage, Mat warpMatrix, int motionType, TermCriteria criteria, Mat inputMask, int gaussFiltSize)
  934. {
  935. if (templateImage != null) templateImage.ThrowIfDisposed();
  936. if (inputImage != null) inputImage.ThrowIfDisposed();
  937. if (warpMatrix != null) warpMatrix.ThrowIfDisposed();
  938. if (inputMask != null) inputMask.ThrowIfDisposed();
  939. return video_Video_findTransformECC_10(templateImage.nativeObj, inputImage.nativeObj, warpMatrix.nativeObj, motionType, criteria.type, criteria.maxCount, criteria.epsilon, inputMask.nativeObj, gaussFiltSize);
  940. }
  941. //
  942. // C++: double cv::findTransformECC(Mat templateImage, Mat inputImage, Mat& warpMatrix, int motionType = MOTION_AFFINE, TermCriteria criteria = TermCriteria(TermCriteria::COUNT+TermCriteria::EPS, 50, 0.001), Mat inputMask = Mat())
  943. //
  944. public static double findTransformECC(Mat templateImage, Mat inputImage, Mat warpMatrix, int motionType, TermCriteria criteria, Mat inputMask)
  945. {
  946. if (templateImage != null) templateImage.ThrowIfDisposed();
  947. if (inputImage != null) inputImage.ThrowIfDisposed();
  948. if (warpMatrix != null) warpMatrix.ThrowIfDisposed();
  949. if (inputMask != null) inputMask.ThrowIfDisposed();
  950. return video_Video_findTransformECC_11(templateImage.nativeObj, inputImage.nativeObj, warpMatrix.nativeObj, motionType, criteria.type, criteria.maxCount, criteria.epsilon, inputMask.nativeObj);
  951. }
  952. public static double findTransformECC(Mat templateImage, Mat inputImage, Mat warpMatrix, int motionType, TermCriteria criteria)
  953. {
  954. if (templateImage != null) templateImage.ThrowIfDisposed();
  955. if (inputImage != null) inputImage.ThrowIfDisposed();
  956. if (warpMatrix != null) warpMatrix.ThrowIfDisposed();
  957. return video_Video_findTransformECC_12(templateImage.nativeObj, inputImage.nativeObj, warpMatrix.nativeObj, motionType, criteria.type, criteria.maxCount, criteria.epsilon);
  958. }
  959. public static double findTransformECC(Mat templateImage, Mat inputImage, Mat warpMatrix, int motionType)
  960. {
  961. if (templateImage != null) templateImage.ThrowIfDisposed();
  962. if (inputImage != null) inputImage.ThrowIfDisposed();
  963. if (warpMatrix != null) warpMatrix.ThrowIfDisposed();
  964. return video_Video_findTransformECC_13(templateImage.nativeObj, inputImage.nativeObj, warpMatrix.nativeObj, motionType);
  965. }
  966. public static double findTransformECC(Mat templateImage, Mat inputImage, Mat warpMatrix)
  967. {
  968. if (templateImage != null) templateImage.ThrowIfDisposed();
  969. if (inputImage != null) inputImage.ThrowIfDisposed();
  970. if (warpMatrix != null) warpMatrix.ThrowIfDisposed();
  971. return video_Video_findTransformECC_14(templateImage.nativeObj, inputImage.nativeObj, warpMatrix.nativeObj);
  972. }
  973. //
  974. // C++: Mat cv::readOpticalFlow(String path)
  975. //
  976. /**
  977. * Read a .flo file
  978. *
  979. * param path Path to the file to be loaded
  980. *
  981. * The function readOpticalFlow loads a flow field from a file and returns it as a single matrix.
  982. * Resulting Mat has a type CV_32FC2 - floating-point, 2-channel. First channel corresponds to the
  983. * flow in the horizontal direction (u), second - vertical (v).
  984. * return automatically generated
  985. */
  986. public static Mat readOpticalFlow(string path)
  987. {
  988. return new Mat(DisposableObject.ThrowIfNullIntPtr(video_Video_readOpticalFlow_10(path)));
  989. }
  990. //
  991. // C++: bool cv::writeOpticalFlow(String path, Mat flow)
  992. //
  993. /**
  994. * Write a .flo to disk
  995. *
  996. * param path Path to the file to be written
  997. * param flow Flow field to be stored
  998. *
  999. * The function stores a flow field in a file, returns true on success, false otherwise.
  1000. * The flow field must be a 2-channel, floating-point matrix (CV_32FC2). First channel corresponds
  1001. * to the flow in the horizontal direction (u), second - vertical (v).
  1002. * return automatically generated
  1003. */
  1004. public static bool writeOpticalFlow(string path, Mat flow)
  1005. {
  1006. if (flow != null) flow.ThrowIfDisposed();
  1007. return video_Video_writeOpticalFlow_10(path, flow.nativeObj);
  1008. }
  1009. #if (UNITY_IOS || UNITY_WEBGL) && !UNITY_EDITOR
  1010. const string LIBNAME = "__Internal";
  1011. #else
  1012. const string LIBNAME = "opencvforunity";
  1013. #endif
  1014. // C++: Ptr_BackgroundSubtractorMOG2 cv::createBackgroundSubtractorMOG2(int history = 500, double varThreshold = 16, bool detectShadows = true)
  1015. [DllImport(LIBNAME)]
  1016. private static extern IntPtr video_Video_createBackgroundSubtractorMOG2_10(int history, double varThreshold, [MarshalAs(UnmanagedType.U1)] bool detectShadows);
  1017. [DllImport(LIBNAME)]
  1018. private static extern IntPtr video_Video_createBackgroundSubtractorMOG2_11(int history, double varThreshold);
  1019. [DllImport(LIBNAME)]
  1020. private static extern IntPtr video_Video_createBackgroundSubtractorMOG2_12(int history);
  1021. [DllImport(LIBNAME)]
  1022. private static extern IntPtr video_Video_createBackgroundSubtractorMOG2_13();
  1023. // C++: Ptr_BackgroundSubtractorKNN cv::createBackgroundSubtractorKNN(int history = 500, double dist2Threshold = 400.0, bool detectShadows = true)
  1024. [DllImport(LIBNAME)]
  1025. private static extern IntPtr video_Video_createBackgroundSubtractorKNN_10(int history, double dist2Threshold, [MarshalAs(UnmanagedType.U1)] bool detectShadows);
  1026. [DllImport(LIBNAME)]
  1027. private static extern IntPtr video_Video_createBackgroundSubtractorKNN_11(int history, double dist2Threshold);
  1028. [DllImport(LIBNAME)]
  1029. private static extern IntPtr video_Video_createBackgroundSubtractorKNN_12(int history);
  1030. [DllImport(LIBNAME)]
  1031. private static extern IntPtr video_Video_createBackgroundSubtractorKNN_13();
  1032. // C++: RotatedRect cv::CamShift(Mat probImage, Rect& window, TermCriteria criteria)
  1033. [DllImport(LIBNAME)]
  1034. private static extern void video_Video_CamShift_10(IntPtr probImage_nativeObj, int window_x, int window_y, int window_width, int window_height, double[] window_out, int criteria_type, int criteria_maxCount, double criteria_epsilon, double[] retVal);
  1035. // C++: int cv::meanShift(Mat probImage, Rect& window, TermCriteria criteria)
  1036. [DllImport(LIBNAME)]
  1037. private static extern int video_Video_meanShift_10(IntPtr probImage_nativeObj, int window_x, int window_y, int window_width, int window_height, double[] window_out, int criteria_type, int criteria_maxCount, double criteria_epsilon);
  1038. // C++: int cv::buildOpticalFlowPyramid(Mat img, vector_Mat& pyramid, Size winSize, int maxLevel, bool withDerivatives = true, int pyrBorder = BORDER_REFLECT_101, int derivBorder = BORDER_CONSTANT, bool tryReuseInputImage = true)
  1039. [DllImport(LIBNAME)]
  1040. private static extern int video_Video_buildOpticalFlowPyramid_10(IntPtr img_nativeObj, IntPtr pyramid_mat_nativeObj, double winSize_width, double winSize_height, int maxLevel, [MarshalAs(UnmanagedType.U1)] bool withDerivatives, int pyrBorder, int derivBorder, [MarshalAs(UnmanagedType.U1)] bool tryReuseInputImage);
  1041. [DllImport(LIBNAME)]
  1042. private static extern int video_Video_buildOpticalFlowPyramid_11(IntPtr img_nativeObj, IntPtr pyramid_mat_nativeObj, double winSize_width, double winSize_height, int maxLevel, [MarshalAs(UnmanagedType.U1)] bool withDerivatives, int pyrBorder, int derivBorder);
  1043. [DllImport(LIBNAME)]
  1044. private static extern int video_Video_buildOpticalFlowPyramid_12(IntPtr img_nativeObj, IntPtr pyramid_mat_nativeObj, double winSize_width, double winSize_height, int maxLevel, [MarshalAs(UnmanagedType.U1)] bool withDerivatives, int pyrBorder);
  1045. [DllImport(LIBNAME)]
  1046. private static extern int video_Video_buildOpticalFlowPyramid_13(IntPtr img_nativeObj, IntPtr pyramid_mat_nativeObj, double winSize_width, double winSize_height, int maxLevel, [MarshalAs(UnmanagedType.U1)] bool withDerivatives);
  1047. [DllImport(LIBNAME)]
  1048. private static extern int video_Video_buildOpticalFlowPyramid_14(IntPtr img_nativeObj, IntPtr pyramid_mat_nativeObj, double winSize_width, double winSize_height, int maxLevel);
  1049. // C++: void cv::calcOpticalFlowPyrLK(Mat prevImg, Mat nextImg, vector_Point2f prevPts, vector_Point2f& nextPts, vector_uchar& status, vector_float& err, Size winSize = Size(21,21), int maxLevel = 3, TermCriteria criteria = TermCriteria(TermCriteria::COUNT+TermCriteria::EPS, 30, 0.01), int flags = 0, double minEigThreshold = 1e-4)
  1050. [DllImport(LIBNAME)]
  1051. private static extern void video_Video_calcOpticalFlowPyrLK_10(IntPtr prevImg_nativeObj, IntPtr nextImg_nativeObj, IntPtr prevPts_mat_nativeObj, IntPtr nextPts_mat_nativeObj, IntPtr status_mat_nativeObj, IntPtr err_mat_nativeObj, double winSize_width, double winSize_height, int maxLevel, int criteria_type, int criteria_maxCount, double criteria_epsilon, int flags, double minEigThreshold);
  1052. [DllImport(LIBNAME)]
  1053. private static extern void video_Video_calcOpticalFlowPyrLK_11(IntPtr prevImg_nativeObj, IntPtr nextImg_nativeObj, IntPtr prevPts_mat_nativeObj, IntPtr nextPts_mat_nativeObj, IntPtr status_mat_nativeObj, IntPtr err_mat_nativeObj, double winSize_width, double winSize_height, int maxLevel, int criteria_type, int criteria_maxCount, double criteria_epsilon, int flags);
  1054. [DllImport(LIBNAME)]
  1055. private static extern void video_Video_calcOpticalFlowPyrLK_12(IntPtr prevImg_nativeObj, IntPtr nextImg_nativeObj, IntPtr prevPts_mat_nativeObj, IntPtr nextPts_mat_nativeObj, IntPtr status_mat_nativeObj, IntPtr err_mat_nativeObj, double winSize_width, double winSize_height, int maxLevel, int criteria_type, int criteria_maxCount, double criteria_epsilon);
  1056. [DllImport(LIBNAME)]
  1057. private static extern void video_Video_calcOpticalFlowPyrLK_13(IntPtr prevImg_nativeObj, IntPtr nextImg_nativeObj, IntPtr prevPts_mat_nativeObj, IntPtr nextPts_mat_nativeObj, IntPtr status_mat_nativeObj, IntPtr err_mat_nativeObj, double winSize_width, double winSize_height, int maxLevel);
  1058. [DllImport(LIBNAME)]
  1059. private static extern void video_Video_calcOpticalFlowPyrLK_14(IntPtr prevImg_nativeObj, IntPtr nextImg_nativeObj, IntPtr prevPts_mat_nativeObj, IntPtr nextPts_mat_nativeObj, IntPtr status_mat_nativeObj, IntPtr err_mat_nativeObj, double winSize_width, double winSize_height);
  1060. [DllImport(LIBNAME)]
  1061. private static extern void video_Video_calcOpticalFlowPyrLK_15(IntPtr prevImg_nativeObj, IntPtr nextImg_nativeObj, IntPtr prevPts_mat_nativeObj, IntPtr nextPts_mat_nativeObj, IntPtr status_mat_nativeObj, IntPtr err_mat_nativeObj);
  1062. // C++: void cv::calcOpticalFlowFarneback(Mat prev, Mat next, Mat& flow, double pyr_scale, int levels, int winsize, int iterations, int poly_n, double poly_sigma, int flags)
  1063. [DllImport(LIBNAME)]
  1064. private static extern void video_Video_calcOpticalFlowFarneback_10(IntPtr prev_nativeObj, IntPtr next_nativeObj, IntPtr flow_nativeObj, double pyr_scale, int levels, int winsize, int iterations, int poly_n, double poly_sigma, int flags);
  1065. // C++: double cv::computeECC(Mat templateImage, Mat inputImage, Mat inputMask = Mat())
  1066. [DllImport(LIBNAME)]
  1067. private static extern double video_Video_computeECC_10(IntPtr templateImage_nativeObj, IntPtr inputImage_nativeObj, IntPtr inputMask_nativeObj);
  1068. [DllImport(LIBNAME)]
  1069. private static extern double video_Video_computeECC_11(IntPtr templateImage_nativeObj, IntPtr inputImage_nativeObj);
  1070. // C++: double cv::findTransformECC(Mat templateImage, Mat inputImage, Mat& warpMatrix, int motionType, TermCriteria criteria, Mat inputMask, int gaussFiltSize)
  1071. [DllImport(LIBNAME)]
  1072. private static extern double video_Video_findTransformECC_10(IntPtr templateImage_nativeObj, IntPtr inputImage_nativeObj, IntPtr warpMatrix_nativeObj, int motionType, int criteria_type, int criteria_maxCount, double criteria_epsilon, IntPtr inputMask_nativeObj, int gaussFiltSize);
  1073. // C++: double cv::findTransformECC(Mat templateImage, Mat inputImage, Mat& warpMatrix, int motionType = MOTION_AFFINE, TermCriteria criteria = TermCriteria(TermCriteria::COUNT+TermCriteria::EPS, 50, 0.001), Mat inputMask = Mat())
  1074. [DllImport(LIBNAME)]
  1075. private static extern double video_Video_findTransformECC_11(IntPtr templateImage_nativeObj, IntPtr inputImage_nativeObj, IntPtr warpMatrix_nativeObj, int motionType, int criteria_type, int criteria_maxCount, double criteria_epsilon, IntPtr inputMask_nativeObj);
  1076. [DllImport(LIBNAME)]
  1077. private static extern double video_Video_findTransformECC_12(IntPtr templateImage_nativeObj, IntPtr inputImage_nativeObj, IntPtr warpMatrix_nativeObj, int motionType, int criteria_type, int criteria_maxCount, double criteria_epsilon);
  1078. [DllImport(LIBNAME)]
  1079. private static extern double video_Video_findTransformECC_13(IntPtr templateImage_nativeObj, IntPtr inputImage_nativeObj, IntPtr warpMatrix_nativeObj, int motionType);
  1080. [DllImport(LIBNAME)]
  1081. private static extern double video_Video_findTransformECC_14(IntPtr templateImage_nativeObj, IntPtr inputImage_nativeObj, IntPtr warpMatrix_nativeObj);
  1082. // C++: Mat cv::readOpticalFlow(String path)
  1083. [DllImport(LIBNAME)]
  1084. private static extern IntPtr video_Video_readOpticalFlow_10(string path);
  1085. // C++: bool cv::writeOpticalFlow(String path, Mat flow)
  1086. [DllImport(LIBNAME)]
  1087. [return: MarshalAs(UnmanagedType.U1)]
  1088. private static extern bool video_Video_writeOpticalFlow_10(string path, IntPtr flow_nativeObj);
  1089. }
  1090. }