video.hpp 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364
  1. // This file is part of OpenCV project.
  2. // It is subject to the license terms in the LICENSE file found in the top-level directory
  3. // of this distribution and at http://opencv.org/license.html.
  4. //
  5. // Copyright (C) 2020 Intel Corporation
  6. #ifndef OPENCV_GAPI_VIDEO_HPP
  7. #define OPENCV_GAPI_VIDEO_HPP
  8. #include <utility> // std::tuple
  9. #include <opencv2/gapi/gkernel.hpp>
  10. /** \defgroup gapi_video G-API Video processing functionality
  11. */
  12. namespace cv { namespace gapi {
  13. /** @brief Structure for the Kalman filter's initialization parameters.*/
  14. struct GAPI_EXPORTS KalmanParams
  15. {
  16. // initial state
  17. //! corrected state (x(k)): x(k)=x'(k)+K(k)*(z(k)-H*x'(k))
  18. Mat state;
  19. //! posteriori error estimate covariance matrix (P(k)): P(k)=(I-K(k)*H)*P'(k)
  20. Mat errorCov;
  21. // dynamic system description
  22. //! state transition matrix (A)
  23. Mat transitionMatrix;
  24. //! measurement matrix (H)
  25. Mat measurementMatrix;
  26. //! process noise covariance matrix (Q)
  27. Mat processNoiseCov;
  28. //! measurement noise covariance matrix (R)
  29. Mat measurementNoiseCov;
  30. //! control matrix (B) (Optional: not used if there's no control)
  31. Mat controlMatrix;
  32. };
  33. /**
  34. * @brief This namespace contains G-API Operations and functions for
  35. * video-oriented algorithms, like optical flow and background subtraction.
  36. */
  37. namespace video
  38. {
  39. using GBuildPyrOutput = std::tuple<GArray<GMat>, GScalar>;
  40. using GOptFlowLKOutput = std::tuple<cv::GArray<cv::Point2f>,
  41. cv::GArray<uchar>,
  42. cv::GArray<float>>;
  43. G_TYPED_KERNEL(GBuildOptFlowPyramid, <GBuildPyrOutput(GMat,Size,GScalar,bool,int,int,bool)>,
  44. "org.opencv.video.buildOpticalFlowPyramid")
  45. {
  46. static std::tuple<GArrayDesc,GScalarDesc>
  47. outMeta(GMatDesc,const Size&,GScalarDesc,bool,int,int,bool)
  48. {
  49. return std::make_tuple(empty_array_desc(), empty_scalar_desc());
  50. }
  51. };
  52. G_TYPED_KERNEL(GCalcOptFlowLK,
  53. <GOptFlowLKOutput(GMat,GMat,cv::GArray<cv::Point2f>,cv::GArray<cv::Point2f>,Size,
  54. GScalar,TermCriteria,int,double)>,
  55. "org.opencv.video.calcOpticalFlowPyrLK")
  56. {
  57. static std::tuple<GArrayDesc,GArrayDesc,GArrayDesc> outMeta(GMatDesc,GMatDesc,GArrayDesc,
  58. GArrayDesc,const Size&,GScalarDesc,
  59. const TermCriteria&,int,double)
  60. {
  61. return std::make_tuple(empty_array_desc(), empty_array_desc(), empty_array_desc());
  62. }
  63. };
  64. G_TYPED_KERNEL(GCalcOptFlowLKForPyr,
  65. <GOptFlowLKOutput(cv::GArray<cv::GMat>,cv::GArray<cv::GMat>,
  66. cv::GArray<cv::Point2f>,cv::GArray<cv::Point2f>,Size,GScalar,
  67. TermCriteria,int,double)>,
  68. "org.opencv.video.calcOpticalFlowPyrLKForPyr")
  69. {
  70. static std::tuple<GArrayDesc,GArrayDesc,GArrayDesc> outMeta(GArrayDesc,GArrayDesc,
  71. GArrayDesc,GArrayDesc,
  72. const Size&,GScalarDesc,
  73. const TermCriteria&,int,double)
  74. {
  75. return std::make_tuple(empty_array_desc(), empty_array_desc(), empty_array_desc());
  76. }
  77. };
  78. enum BackgroundSubtractorType
  79. {
  80. TYPE_BS_MOG2,
  81. TYPE_BS_KNN
  82. };
  83. /** @brief Structure for the Background Subtractor operation's initialization parameters.*/
  84. struct BackgroundSubtractorParams
  85. {
  86. //! Type of the Background Subtractor operation.
  87. BackgroundSubtractorType operation = TYPE_BS_MOG2;
  88. //! Length of the history.
  89. int history = 500;
  90. //! For MOG2: Threshold on the squared Mahalanobis distance between the pixel
  91. //! and the model to decide whether a pixel is well described by
  92. //! the background model.
  93. //! For KNN: Threshold on the squared distance between the pixel and the sample
  94. //! to decide whether a pixel is close to that sample.
  95. double threshold = 16;
  96. //! If true, the algorithm will detect shadows and mark them.
  97. bool detectShadows = true;
  98. //! The value between 0 and 1 that indicates how fast
  99. //! the background model is learnt.
  100. //! Negative parameter value makes the algorithm use some automatically
  101. //! chosen learning rate.
  102. double learningRate = -1;
  103. //! default constructor
  104. BackgroundSubtractorParams() {}
  105. /** Full constructor
  106. @param op MOG2/KNN Background Subtractor type.
  107. @param histLength Length of the history.
  108. @param thrshld For MOG2: Threshold on the squared Mahalanobis distance between
  109. the pixel and the model to decide whether a pixel is well described by the background model.
  110. For KNN: Threshold on the squared distance between the pixel and the sample to decide
  111. whether a pixel is close to that sample.
  112. @param detect If true, the algorithm will detect shadows and mark them. It decreases the
  113. speed a bit, so if you do not need this feature, set the parameter to false.
  114. @param lRate The value between 0 and 1 that indicates how fast the background model is learnt.
  115. Negative parameter value makes the algorithm to use some automatically chosen learning rate.
  116. */
  117. BackgroundSubtractorParams(BackgroundSubtractorType op, int histLength,
  118. double thrshld, bool detect, double lRate) : operation(op),
  119. history(histLength),
  120. threshold(thrshld),
  121. detectShadows(detect),
  122. learningRate(lRate){}
  123. };
  124. G_TYPED_KERNEL(GBackgroundSubtractor, <GMat(GMat, BackgroundSubtractorParams)>,
  125. "org.opencv.video.BackgroundSubtractor")
  126. {
  127. static GMatDesc outMeta(const GMatDesc& in, const BackgroundSubtractorParams& bsParams)
  128. {
  129. GAPI_Assert(bsParams.history >= 0);
  130. GAPI_Assert(bsParams.learningRate <= 1);
  131. return in.withType(CV_8U, 1);
  132. }
  133. };
  134. void checkParams(const cv::gapi::KalmanParams& kfParams,
  135. const cv::GMatDesc& measurement, const cv::GMatDesc& control = {});
  136. G_TYPED_KERNEL(GKalmanFilter, <GMat(GMat, GOpaque<bool>, GMat, KalmanParams)>,
  137. "org.opencv.video.KalmanFilter")
  138. {
  139. static GMatDesc outMeta(const GMatDesc& measurement, const GOpaqueDesc&,
  140. const GMatDesc& control, const KalmanParams& kfParams)
  141. {
  142. checkParams(kfParams, measurement, control);
  143. return measurement.withSize(Size(1, kfParams.transitionMatrix.rows));
  144. }
  145. };
  146. G_TYPED_KERNEL(GKalmanFilterNoControl, <GMat(GMat, GOpaque<bool>, KalmanParams)>, "org.opencv.video.KalmanFilterNoControl")
  147. {
  148. static GMatDesc outMeta(const GMatDesc& measurement, const GOpaqueDesc&, const KalmanParams& kfParams)
  149. {
  150. checkParams(kfParams, measurement);
  151. return measurement.withSize(Size(1, kfParams.transitionMatrix.rows));
  152. }
  153. };
  154. } //namespace video
  155. //! @addtogroup gapi_video
  156. //! @{
  157. /** @brief Constructs the image pyramid which can be passed to calcOpticalFlowPyrLK.
  158. @note Function textual ID is "org.opencv.video.buildOpticalFlowPyramid"
  159. @param img 8-bit input image.
  160. @param winSize window size of optical flow algorithm. Must be not less than winSize
  161. argument of calcOpticalFlowPyrLK. It is needed to calculate required
  162. padding for pyramid levels.
  163. @param maxLevel 0-based maximal pyramid level number.
  164. @param withDerivatives set to precompute gradients for the every pyramid level. If pyramid is
  165. constructed without the gradients then calcOpticalFlowPyrLK will calculate
  166. them internally.
  167. @param pyrBorder the border mode for pyramid layers.
  168. @param derivBorder the border mode for gradients.
  169. @param tryReuseInputImage put ROI of input image into the pyramid if possible. You can pass false
  170. to force data copying.
  171. @return
  172. - output pyramid.
  173. - number of levels in constructed pyramid. Can be less than maxLevel.
  174. */
  175. GAPI_EXPORTS std::tuple<GArray<GMat>, GScalar>
  176. buildOpticalFlowPyramid(const GMat &img,
  177. const Size &winSize,
  178. const GScalar &maxLevel,
  179. bool withDerivatives = true,
  180. int pyrBorder = BORDER_REFLECT_101,
  181. int derivBorder = BORDER_CONSTANT,
  182. bool tryReuseInputImage = true);
  183. /** @brief Calculates an optical flow for a sparse feature set using the iterative Lucas-Kanade
  184. method with pyramids.
  185. See @cite Bouguet00 .
  186. @note Function textual ID is "org.opencv.video.calcOpticalFlowPyrLK"
  187. @param prevImg first 8-bit input image (GMat) or pyramid (GArray<GMat>) constructed by
  188. buildOpticalFlowPyramid.
  189. @param nextImg second input image (GMat) or pyramid (GArray<GMat>) of the same size and the same
  190. type as prevImg.
  191. @param prevPts GArray of 2D points for which the flow needs to be found; point coordinates must be
  192. single-precision floating-point numbers.
  193. @param predPts GArray of 2D points initial for the flow search; make sense only when
  194. OPTFLOW_USE_INITIAL_FLOW flag is passed; in that case the vector must have the same size as in
  195. the input.
  196. @param winSize size of the search window at each pyramid level.
  197. @param maxLevel 0-based maximal pyramid level number; if set to 0, pyramids are not used (single
  198. level), if set to 1, two levels are used, and so on; if pyramids are passed to input then
  199. algorithm will use as many levels as pyramids have but no more than maxLevel.
  200. @param criteria parameter, specifying the termination criteria of the iterative search algorithm
  201. (after the specified maximum number of iterations criteria.maxCount or when the search window
  202. moves by less than criteria.epsilon).
  203. @param flags operation flags:
  204. - **OPTFLOW_USE_INITIAL_FLOW** uses initial estimations, stored in nextPts; if the flag is
  205. not set, then prevPts is copied to nextPts and is considered the initial estimate.
  206. - **OPTFLOW_LK_GET_MIN_EIGENVALS** use minimum eigen values as an error measure (see
  207. minEigThreshold description); if the flag is not set, then L1 distance between patches
  208. around the original and a moved point, divided by number of pixels in a window, is used as a
  209. error measure.
  210. @param minEigThresh the algorithm calculates the minimum eigen value of a 2x2 normal matrix of
  211. optical flow equations (this matrix is called a spatial gradient matrix in @cite Bouguet00), divided
  212. by number of pixels in a window; if this value is less than minEigThreshold, then a corresponding
  213. feature is filtered out and its flow is not processed, so it allows to remove bad points and get a
  214. performance boost.
  215. @return
  216. - GArray of 2D points (with single-precision floating-point coordinates)
  217. containing the calculated new positions of input features in the second image.
  218. - status GArray (of unsigned chars); each element of the vector is set to 1 if
  219. the flow for the corresponding features has been found, otherwise, it is set to 0.
  220. - GArray of errors (doubles); each element of the vector is set to an error for the
  221. corresponding feature, type of the error measure can be set in flags parameter; if the flow wasn't
  222. found then the error is not defined (use the status parameter to find such cases).
  223. */
  224. GAPI_EXPORTS std::tuple<GArray<Point2f>, GArray<uchar>, GArray<float>>
  225. calcOpticalFlowPyrLK(const GMat &prevImg,
  226. const GMat &nextImg,
  227. const GArray<Point2f> &prevPts,
  228. const GArray<Point2f> &predPts,
  229. const Size &winSize = Size(21, 21),
  230. const GScalar &maxLevel = 3,
  231. const TermCriteria &criteria = TermCriteria(TermCriteria::COUNT |
  232. TermCriteria::EPS,
  233. 30, 0.01),
  234. int flags = 0,
  235. double minEigThresh = 1e-4);
  236. /**
  237. @overload
  238. @note Function textual ID is "org.opencv.video.calcOpticalFlowPyrLKForPyr"
  239. */
  240. GAPI_EXPORTS std::tuple<GArray<Point2f>, GArray<uchar>, GArray<float>>
  241. calcOpticalFlowPyrLK(const GArray<GMat> &prevPyr,
  242. const GArray<GMat> &nextPyr,
  243. const GArray<Point2f> &prevPts,
  244. const GArray<Point2f> &predPts,
  245. const Size &winSize = Size(21, 21),
  246. const GScalar &maxLevel = 3,
  247. const TermCriteria &criteria = TermCriteria(TermCriteria::COUNT |
  248. TermCriteria::EPS,
  249. 30, 0.01),
  250. int flags = 0,
  251. double minEigThresh = 1e-4);
  252. /** @brief Gaussian Mixture-based or K-nearest neighbours-based Background/Foreground Segmentation Algorithm.
  253. The operation generates a foreground mask.
  254. @return Output image is foreground mask, i.e. 8-bit unsigned 1-channel (binary) matrix @ref CV_8UC1.
  255. @note Functional textual ID is "org.opencv.video.BackgroundSubtractor"
  256. @param src input image: Floating point frame is used without scaling and should be in range [0,255].
  257. @param bsParams Set of initialization parameters for Background Subtractor kernel.
  258. */
  259. GAPI_EXPORTS GMat BackgroundSubtractor(const GMat& src, const cv::gapi::video::BackgroundSubtractorParams& bsParams);
  260. /** @brief Standard Kalman filter algorithm <http://en.wikipedia.org/wiki/Kalman_filter>.
  261. @note Functional textual ID is "org.opencv.video.KalmanFilter"
  262. @param measurement input matrix: 32-bit or 64-bit float 1-channel matrix containing measurements.
  263. @param haveMeasurement dynamic input flag that indicates whether we get measurements
  264. at a particular iteration .
  265. @param control input matrix: 32-bit or 64-bit float 1-channel matrix contains control data
  266. for changing dynamic system.
  267. @param kfParams Set of initialization parameters for Kalman filter kernel.
  268. @return Output matrix is predicted or corrected state. They can be 32-bit or 64-bit float
  269. 1-channel matrix @ref CV_32FC1 or @ref CV_64FC1.
  270. @details If measurement matrix is given (haveMeasurements == true), corrected state will
  271. be returned which corresponds to the pipeline
  272. cv::KalmanFilter::predict(control) -> cv::KalmanFilter::correct(measurement).
  273. Otherwise, predicted state will be returned which corresponds to the call of
  274. cv::KalmanFilter::predict(control).
  275. @sa cv::KalmanFilter
  276. */
  277. GAPI_EXPORTS GMat KalmanFilter(const GMat& measurement, const GOpaque<bool>& haveMeasurement,
  278. const GMat& control, const cv::gapi::KalmanParams& kfParams);
  279. /** @overload
  280. The case of Standard Kalman filter algorithm when there is no control in a dynamic system.
  281. In this case the controlMatrix is empty and control vector is absent.
  282. @note Function textual ID is "org.opencv.video.KalmanFilterNoControl"
  283. @param measurement input matrix: 32-bit or 64-bit float 1-channel matrix containing measurements.
  284. @param haveMeasurement dynamic input flag that indicates whether we get measurements
  285. at a particular iteration.
  286. @param kfParams Set of initialization parameters for Kalman filter kernel.
  287. @return Output matrix is predicted or corrected state. They can be 32-bit or 64-bit float
  288. 1-channel matrix @ref CV_32FC1 or @ref CV_64FC1.
  289. @sa cv::KalmanFilter
  290. */
  291. GAPI_EXPORTS GMat KalmanFilter(const GMat& measurement, const GOpaque<bool>& haveMeasurement,
  292. const cv::gapi::KalmanParams& kfParams);
  293. //! @} gapi_video
  294. } //namespace gapi
  295. } //namespace cv
  296. namespace cv { namespace detail {
  297. template<> struct CompileArgTag<cv::gapi::video::BackgroundSubtractorParams>
  298. {
  299. static const char* tag()
  300. {
  301. return "org.opencv.video.background_substractor_params";
  302. }
  303. };
  304. } // namespace detail
  305. } // namespace cv
  306. #endif // OPENCV_GAPI_VIDEO_HPP