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