1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
11 // For Open Source Computer Vision Library
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
23 // * Redistribution's in binary form must reproduce the above copyright notice,
24 // this list of conditions and the following disclaimer in the documentation
25 // and/or other GpuMaterials provided with the distribution.
27 // * The name of the copyright holders may not be used to endorse or promote products
28 // derived from this software without specific prior written permission.
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
43 #ifndef __OPENCV_GPU_HPP__
44 #define __OPENCV_GPU_HPP__
52 #include "opencv2/core/gpumat.hpp"
53 #include "opencv2/imgproc/imgproc.hpp"
54 #include "opencv2/objdetect/objdetect.hpp"
55 #include "opencv2/features2d/features2d.hpp"
57 namespace cv { namespace gpu {
59 //////////////////////////////// CudaMem ////////////////////////////////
60 // CudaMem is limited cv::Mat with page locked memory allocation.
61 // Page locked memory is only needed for async and faster coping to GPU.
62 // It is convertable to cv::Mat header without reference counting
63 // so you can use it with other opencv functions.
65 // Page-locks the matrix m memory and maps it for the device(s)
66 CV_EXPORTS void registerPageLocked(Mat& m);
67 // Unmaps the memory of matrix m, and makes it pageable again.
68 CV_EXPORTS void unregisterPageLocked(Mat& m);
70 class CV_EXPORTS CudaMem
73 enum { ALLOC_PAGE_LOCKED = 1, ALLOC_ZEROCOPY = 2, ALLOC_WRITE_COMBINED = 4 };
76 CudaMem(const CudaMem& m);
78 CudaMem(int rows, int cols, int type, int _alloc_type = ALLOC_PAGE_LOCKED);
79 CudaMem(Size size, int type, int alloc_type = ALLOC_PAGE_LOCKED);
82 //! creates from cv::Mat with coping data
83 explicit CudaMem(const Mat& m, int alloc_type = ALLOC_PAGE_LOCKED);
87 CudaMem& operator = (const CudaMem& m);
89 //! returns deep copy of the matrix, i.e. the data is copied
90 CudaMem clone() const;
92 //! allocates new matrix data unless the matrix already has specified size and type.
93 void create(int rows, int cols, int type, int alloc_type = ALLOC_PAGE_LOCKED);
94 void create(Size size, int type, int alloc_type = ALLOC_PAGE_LOCKED);
96 //! decrements reference counter and released memory if needed.
99 //! returns matrix header with disabled reference counting for CudaMem data.
100 Mat createMatHeader() const;
101 operator Mat() const;
103 //! maps host memory into device address space and returns GpuMat header for it. Throws exception if not supported by hardware.
104 GpuMat createGpuMatHeader() const;
105 operator GpuMat() const;
107 //returns if host memory can be mapperd to gpu address space;
108 static bool canMapHostMemory();
110 // Please see cv::Mat for descriptions
111 bool isContinuous() const;
112 size_t elemSize() const;
113 size_t elemSize1() const;
116 int channels() const;
117 size_t step1() const;
122 // Please see cv::Mat for descriptions
136 //////////////////////////////// CudaStream ////////////////////////////////
137 // Encapculates Cuda Stream. Provides interface for async coping.
138 // Passed to each function that supports async kernel execution.
139 // Reference counting is enabled
141 class CV_EXPORTS Stream
147 Stream(const Stream&);
148 Stream& operator=(const Stream&);
150 bool queryIfComplete();
151 void waitForCompletion();
153 //! downloads asynchronously.
154 // Warning! cv::Mat must point to page locked memory (i.e. to CudaMem data or to its subMat)
155 void enqueueDownload(const GpuMat& src, CudaMem& dst);
156 void enqueueDownload(const GpuMat& src, Mat& dst);
158 //! uploads asynchronously.
159 // Warning! cv::Mat must point to page locked memory (i.e. to CudaMem data or to its ROI)
160 void enqueueUpload(const CudaMem& src, GpuMat& dst);
161 void enqueueUpload(const Mat& src, GpuMat& dst);
163 void enqueueCopy(const GpuMat& src, GpuMat& dst);
165 void enqueueMemSet(GpuMat& src, Scalar val);
166 void enqueueMemSet(GpuMat& src, Scalar val, const GpuMat& mask);
168 // converts matrix type, ex from float to uchar depending on type
169 void enqueueConvert(const GpuMat& src, GpuMat& dst, int type, double a = 1, double b = 0);
171 static Stream& Null();
173 operator bool() const;
182 friend struct StreamAccessor;
184 explicit Stream(Impl* impl);
188 //////////////////////////////// Filter Engine ////////////////////////////////
191 The Base Class for 1D or Row-wise Filters
193 This is the base class for linear or non-linear filters that process 1D data.
194 In particular, such filters are used for the "horizontal" filtering parts in separable filters.
196 class CV_EXPORTS BaseRowFilter_GPU
199 BaseRowFilter_GPU(int ksize_, int anchor_) : ksize(ksize_), anchor(anchor_) {}
200 virtual ~BaseRowFilter_GPU() {}
201 virtual void operator()(const GpuMat& src, GpuMat& dst, Stream& stream = Stream::Null()) = 0;
206 The Base Class for Column-wise Filters
208 This is the base class for linear or non-linear filters that process columns of 2D arrays.
209 Such filters are used for the "vertical" filtering parts in separable filters.
211 class CV_EXPORTS BaseColumnFilter_GPU
214 BaseColumnFilter_GPU(int ksize_, int anchor_) : ksize(ksize_), anchor(anchor_) {}
215 virtual ~BaseColumnFilter_GPU() {}
216 virtual void operator()(const GpuMat& src, GpuMat& dst, Stream& stream = Stream::Null()) = 0;
221 The Base Class for Non-Separable 2D Filters.
223 This is the base class for linear or non-linear 2D filters.
225 class CV_EXPORTS BaseFilter_GPU
228 BaseFilter_GPU(const Size& ksize_, const Point& anchor_) : ksize(ksize_), anchor(anchor_) {}
229 virtual ~BaseFilter_GPU() {}
230 virtual void operator()(const GpuMat& src, GpuMat& dst, Stream& stream = Stream::Null()) = 0;
236 The Base Class for Filter Engine.
238 The class can be used to apply an arbitrary filtering operation to an image.
239 It contains all the necessary intermediate buffers.
241 class CV_EXPORTS FilterEngine_GPU
244 virtual ~FilterEngine_GPU() {}
246 virtual void apply(const GpuMat& src, GpuMat& dst, Rect roi = Rect(0,0,-1,-1), Stream& stream = Stream::Null()) = 0;
249 //! returns the non-separable filter engine with the specified filter
250 CV_EXPORTS Ptr<FilterEngine_GPU> createFilter2D_GPU(const Ptr<BaseFilter_GPU>& filter2D, int srcType, int dstType);
252 //! returns the separable filter engine with the specified filters
253 CV_EXPORTS Ptr<FilterEngine_GPU> createSeparableFilter_GPU(const Ptr<BaseRowFilter_GPU>& rowFilter,
254 const Ptr<BaseColumnFilter_GPU>& columnFilter, int srcType, int bufType, int dstType);
255 CV_EXPORTS Ptr<FilterEngine_GPU> createSeparableFilter_GPU(const Ptr<BaseRowFilter_GPU>& rowFilter,
256 const Ptr<BaseColumnFilter_GPU>& columnFilter, int srcType, int bufType, int dstType, GpuMat& buf);
258 //! returns horizontal 1D box filter
259 //! supports only CV_8UC1 source type and CV_32FC1 sum type
260 CV_EXPORTS Ptr<BaseRowFilter_GPU> getRowSumFilter_GPU(int srcType, int sumType, int ksize, int anchor = -1);
262 //! returns vertical 1D box filter
263 //! supports only CV_8UC1 sum type and CV_32FC1 dst type
264 CV_EXPORTS Ptr<BaseColumnFilter_GPU> getColumnSumFilter_GPU(int sumType, int dstType, int ksize, int anchor = -1);
266 //! returns 2D box filter
267 //! supports CV_8UC1 and CV_8UC4 source type, dst type must be the same as source type
268 CV_EXPORTS Ptr<BaseFilter_GPU> getBoxFilter_GPU(int srcType, int dstType, const Size& ksize, Point anchor = Point(-1, -1));
270 //! returns box filter engine
271 CV_EXPORTS Ptr<FilterEngine_GPU> createBoxFilter_GPU(int srcType, int dstType, const Size& ksize,
272 const Point& anchor = Point(-1,-1));
274 //! returns 2D morphological filter
275 //! only MORPH_ERODE and MORPH_DILATE are supported
276 //! supports CV_8UC1 and CV_8UC4 types
277 //! kernel must have CV_8UC1 type, one rows and cols == ksize.width * ksize.height
278 CV_EXPORTS Ptr<BaseFilter_GPU> getMorphologyFilter_GPU(int op, int type, const Mat& kernel, const Size& ksize,
279 Point anchor=Point(-1,-1));
281 //! returns morphological filter engine. Only MORPH_ERODE and MORPH_DILATE are supported.
282 CV_EXPORTS Ptr<FilterEngine_GPU> createMorphologyFilter_GPU(int op, int type, const Mat& kernel,
283 const Point& anchor = Point(-1,-1), int iterations = 1);
284 CV_EXPORTS Ptr<FilterEngine_GPU> createMorphologyFilter_GPU(int op, int type, const Mat& kernel, GpuMat& buf,
285 const Point& anchor = Point(-1,-1), int iterations = 1);
287 //! returns 2D filter with the specified kernel
288 //! supports CV_8U, CV_16U and CV_32F one and four channel image
289 CV_EXPORTS Ptr<BaseFilter_GPU> getLinearFilter_GPU(int srcType, int dstType, const Mat& kernel, Point anchor = Point(-1, -1), int borderType = BORDER_DEFAULT);
291 //! returns the non-separable linear filter engine
292 CV_EXPORTS Ptr<FilterEngine_GPU> createLinearFilter_GPU(int srcType, int dstType, const Mat& kernel,
293 Point anchor = Point(-1,-1), int borderType = BORDER_DEFAULT);
295 //! returns the primitive row filter with the specified kernel.
296 //! supports only CV_8UC1, CV_8UC4, CV_16SC1, CV_16SC2, CV_32SC1, CV_32FC1 source type.
297 //! there are two version of algorithm: NPP and OpenCV.
298 //! NPP calls when srcType == CV_8UC1 or srcType == CV_8UC4 and bufType == srcType,
299 //! otherwise calls OpenCV version.
300 //! NPP supports only BORDER_CONSTANT border type.
301 //! OpenCV version supports only CV_32F as buffer depth and
302 //! BORDER_REFLECT101, BORDER_REPLICATE and BORDER_CONSTANT border types.
303 CV_EXPORTS Ptr<BaseRowFilter_GPU> getLinearRowFilter_GPU(int srcType, int bufType, const Mat& rowKernel,
304 int anchor = -1, int borderType = BORDER_DEFAULT);
306 //! returns the primitive column filter with the specified kernel.
307 //! supports only CV_8UC1, CV_8UC4, CV_16SC1, CV_16SC2, CV_32SC1, CV_32FC1 dst type.
308 //! there are two version of algorithm: NPP and OpenCV.
309 //! NPP calls when dstType == CV_8UC1 or dstType == CV_8UC4 and bufType == dstType,
310 //! otherwise calls OpenCV version.
311 //! NPP supports only BORDER_CONSTANT border type.
312 //! OpenCV version supports only CV_32F as buffer depth and
313 //! BORDER_REFLECT101, BORDER_REPLICATE and BORDER_CONSTANT border types.
314 CV_EXPORTS Ptr<BaseColumnFilter_GPU> getLinearColumnFilter_GPU(int bufType, int dstType, const Mat& columnKernel,
315 int anchor = -1, int borderType = BORDER_DEFAULT);
317 //! returns the separable linear filter engine
318 CV_EXPORTS Ptr<FilterEngine_GPU> createSeparableLinearFilter_GPU(int srcType, int dstType, const Mat& rowKernel,
319 const Mat& columnKernel, const Point& anchor = Point(-1,-1), int rowBorderType = BORDER_DEFAULT,
320 int columnBorderType = -1);
321 CV_EXPORTS Ptr<FilterEngine_GPU> createSeparableLinearFilter_GPU(int srcType, int dstType, const Mat& rowKernel,
322 const Mat& columnKernel, GpuMat& buf, const Point& anchor = Point(-1,-1), int rowBorderType = BORDER_DEFAULT,
323 int columnBorderType = -1);
325 //! returns filter engine for the generalized Sobel operator
326 CV_EXPORTS Ptr<FilterEngine_GPU> createDerivFilter_GPU(int srcType, int dstType, int dx, int dy, int ksize,
327 int rowBorderType = BORDER_DEFAULT, int columnBorderType = -1);
328 CV_EXPORTS Ptr<FilterEngine_GPU> createDerivFilter_GPU(int srcType, int dstType, int dx, int dy, int ksize, GpuMat& buf,
329 int rowBorderType = BORDER_DEFAULT, int columnBorderType = -1);
331 //! returns the Gaussian filter engine
332 CV_EXPORTS Ptr<FilterEngine_GPU> createGaussianFilter_GPU(int type, Size ksize, double sigma1, double sigma2 = 0,
333 int rowBorderType = BORDER_DEFAULT, int columnBorderType = -1);
334 CV_EXPORTS Ptr<FilterEngine_GPU> createGaussianFilter_GPU(int type, Size ksize, GpuMat& buf, double sigma1, double sigma2 = 0,
335 int rowBorderType = BORDER_DEFAULT, int columnBorderType = -1);
337 //! returns maximum filter
338 CV_EXPORTS Ptr<BaseFilter_GPU> getMaxFilter_GPU(int srcType, int dstType, const Size& ksize, Point anchor = Point(-1,-1));
340 //! returns minimum filter
341 CV_EXPORTS Ptr<BaseFilter_GPU> getMinFilter_GPU(int srcType, int dstType, const Size& ksize, Point anchor = Point(-1,-1));
343 //! smooths the image using the normalized box filter
344 //! supports CV_8UC1, CV_8UC4 types
345 CV_EXPORTS void boxFilter(const GpuMat& src, GpuMat& dst, int ddepth, Size ksize, Point anchor = Point(-1,-1), Stream& stream = Stream::Null());
347 //! a synonym for normalized box filter
348 static inline void blur(const GpuMat& src, GpuMat& dst, Size ksize, Point anchor = Point(-1,-1), Stream& stream = Stream::Null())
350 boxFilter(src, dst, -1, ksize, anchor, stream);
353 //! erodes the image (applies the local minimum operator)
354 CV_EXPORTS void erode(const GpuMat& src, GpuMat& dst, const Mat& kernel, Point anchor = Point(-1, -1), int iterations = 1);
355 CV_EXPORTS void erode(const GpuMat& src, GpuMat& dst, const Mat& kernel, GpuMat& buf,
356 Point anchor = Point(-1, -1), int iterations = 1,
357 Stream& stream = Stream::Null());
359 //! dilates the image (applies the local maximum operator)
360 CV_EXPORTS void dilate(const GpuMat& src, GpuMat& dst, const Mat& kernel, Point anchor = Point(-1, -1), int iterations = 1);
361 CV_EXPORTS void dilate(const GpuMat& src, GpuMat& dst, const Mat& kernel, GpuMat& buf,
362 Point anchor = Point(-1, -1), int iterations = 1,
363 Stream& stream = Stream::Null());
365 //! applies an advanced morphological operation to the image
366 CV_EXPORTS void morphologyEx(const GpuMat& src, GpuMat& dst, int op, const Mat& kernel, Point anchor = Point(-1, -1), int iterations = 1);
367 CV_EXPORTS void morphologyEx(const GpuMat& src, GpuMat& dst, int op, const Mat& kernel, GpuMat& buf1, GpuMat& buf2,
368 Point anchor = Point(-1, -1), int iterations = 1, Stream& stream = Stream::Null());
370 //! applies non-separable 2D linear filter to the image
371 CV_EXPORTS void filter2D(const GpuMat& src, GpuMat& dst, int ddepth, const Mat& kernel, Point anchor=Point(-1,-1), int borderType = BORDER_DEFAULT, Stream& stream = Stream::Null());
373 //! applies separable 2D linear filter to the image
374 CV_EXPORTS void sepFilter2D(const GpuMat& src, GpuMat& dst, int ddepth, const Mat& kernelX, const Mat& kernelY,
375 Point anchor = Point(-1,-1), int rowBorderType = BORDER_DEFAULT, int columnBorderType = -1);
376 CV_EXPORTS void sepFilter2D(const GpuMat& src, GpuMat& dst, int ddepth, const Mat& kernelX, const Mat& kernelY, GpuMat& buf,
377 Point anchor = Point(-1,-1), int rowBorderType = BORDER_DEFAULT, int columnBorderType = -1,
378 Stream& stream = Stream::Null());
380 //! applies generalized Sobel operator to the image
381 CV_EXPORTS void Sobel(const GpuMat& src, GpuMat& dst, int ddepth, int dx, int dy, int ksize = 3, double scale = 1,
382 int rowBorderType = BORDER_DEFAULT, int columnBorderType = -1);
383 CV_EXPORTS void Sobel(const GpuMat& src, GpuMat& dst, int ddepth, int dx, int dy, GpuMat& buf, int ksize = 3, double scale = 1,
384 int rowBorderType = BORDER_DEFAULT, int columnBorderType = -1, Stream& stream = Stream::Null());
386 //! applies the vertical or horizontal Scharr operator to the image
387 CV_EXPORTS void Scharr(const GpuMat& src, GpuMat& dst, int ddepth, int dx, int dy, double scale = 1,
388 int rowBorderType = BORDER_DEFAULT, int columnBorderType = -1);
389 CV_EXPORTS void Scharr(const GpuMat& src, GpuMat& dst, int ddepth, int dx, int dy, GpuMat& buf, double scale = 1,
390 int rowBorderType = BORDER_DEFAULT, int columnBorderType = -1, Stream& stream = Stream::Null());
392 //! smooths the image using Gaussian filter.
393 CV_EXPORTS void GaussianBlur(const GpuMat& src, GpuMat& dst, Size ksize, double sigma1, double sigma2 = 0,
394 int rowBorderType = BORDER_DEFAULT, int columnBorderType = -1);
395 CV_EXPORTS void GaussianBlur(const GpuMat& src, GpuMat& dst, Size ksize, GpuMat& buf, double sigma1, double sigma2 = 0,
396 int rowBorderType = BORDER_DEFAULT, int columnBorderType = -1, Stream& stream = Stream::Null());
398 //! applies Laplacian operator to the image
399 //! supports only ksize = 1 and ksize = 3
400 CV_EXPORTS void Laplacian(const GpuMat& src, GpuMat& dst, int ddepth, int ksize = 1, double scale = 1, int borderType = BORDER_DEFAULT, Stream& stream = Stream::Null());
403 ////////////////////////////// Arithmetics ///////////////////////////////////
405 //! implements generalized matrix product algorithm GEMM from BLAS
406 CV_EXPORTS void gemm(const GpuMat& src1, const GpuMat& src2, double alpha,
407 const GpuMat& src3, double beta, GpuMat& dst, int flags = 0, Stream& stream = Stream::Null());
409 //! transposes the matrix
410 //! supports matrix with element size = 1, 4 and 8 bytes (CV_8UC1, CV_8UC4, CV_16UC2, CV_32FC1, etc)
411 CV_EXPORTS void transpose(const GpuMat& src1, GpuMat& dst, Stream& stream = Stream::Null());
413 //! reverses the order of the rows, columns or both in a matrix
414 //! supports 1, 3 and 4 channels images with CV_8U, CV_16U, CV_32S or CV_32F depth
415 CV_EXPORTS void flip(const GpuMat& a, GpuMat& b, int flipCode, Stream& stream = Stream::Null());
417 //! transforms 8-bit unsigned integers using lookup table: dst(i)=lut(src(i))
418 //! destination array will have the depth type as lut and the same channels number as source
419 //! supports CV_8UC1, CV_8UC3 types
420 CV_EXPORTS void LUT(const GpuMat& src, const Mat& lut, GpuMat& dst, Stream& stream = Stream::Null());
422 //! makes multi-channel array out of several single-channel arrays
423 CV_EXPORTS void merge(const GpuMat* src, size_t n, GpuMat& dst, Stream& stream = Stream::Null());
425 //! makes multi-channel array out of several single-channel arrays
426 CV_EXPORTS void merge(const vector<GpuMat>& src, GpuMat& dst, Stream& stream = Stream::Null());
428 //! copies each plane of a multi-channel array to a dedicated array
429 CV_EXPORTS void split(const GpuMat& src, GpuMat* dst, Stream& stream = Stream::Null());
431 //! copies each plane of a multi-channel array to a dedicated array
432 CV_EXPORTS void split(const GpuMat& src, vector<GpuMat>& dst, Stream& stream = Stream::Null());
434 //! computes magnitude of complex (x(i).re, x(i).im) vector
435 //! supports only CV_32FC2 type
436 CV_EXPORTS void magnitude(const GpuMat& xy, GpuMat& magnitude, Stream& stream = Stream::Null());
438 //! computes squared magnitude of complex (x(i).re, x(i).im) vector
439 //! supports only CV_32FC2 type
440 CV_EXPORTS void magnitudeSqr(const GpuMat& xy, GpuMat& magnitude, Stream& stream = Stream::Null());
442 //! computes magnitude of each (x(i), y(i)) vector
443 //! supports only floating-point source
444 CV_EXPORTS void magnitude(const GpuMat& x, const GpuMat& y, GpuMat& magnitude, Stream& stream = Stream::Null());
446 //! computes squared magnitude of each (x(i), y(i)) vector
447 //! supports only floating-point source
448 CV_EXPORTS void magnitudeSqr(const GpuMat& x, const GpuMat& y, GpuMat& magnitude, Stream& stream = Stream::Null());
450 //! computes angle (angle(i)) of each (x(i), y(i)) vector
451 //! supports only floating-point source
452 CV_EXPORTS void phase(const GpuMat& x, const GpuMat& y, GpuMat& angle, bool angleInDegrees = false, Stream& stream = Stream::Null());
454 //! converts Cartesian coordinates to polar
455 //! supports only floating-point source
456 CV_EXPORTS void cartToPolar(const GpuMat& x, const GpuMat& y, GpuMat& magnitude, GpuMat& angle, bool angleInDegrees = false, Stream& stream = Stream::Null());
458 //! converts polar coordinates to Cartesian
459 //! supports only floating-point source
460 CV_EXPORTS void polarToCart(const GpuMat& magnitude, const GpuMat& angle, GpuMat& x, GpuMat& y, bool angleInDegrees = false, Stream& stream = Stream::Null());
463 //////////////////////////// Per-element operations ////////////////////////////////////
465 //! adds one matrix to another (c = a + b)
466 CV_EXPORTS void add(const GpuMat& a, const GpuMat& b, GpuMat& c, const GpuMat& mask = GpuMat(), int dtype = -1, Stream& stream = Stream::Null());
467 //! adds scalar to a matrix (c = a + s)
468 CV_EXPORTS void add(const GpuMat& a, const Scalar& sc, GpuMat& c, const GpuMat& mask = GpuMat(), int dtype = -1, Stream& stream = Stream::Null());
470 //! subtracts one matrix from another (c = a - b)
471 CV_EXPORTS void subtract(const GpuMat& a, const GpuMat& b, GpuMat& c, const GpuMat& mask = GpuMat(), int dtype = -1, Stream& stream = Stream::Null());
472 //! subtracts scalar from a matrix (c = a - s)
473 CV_EXPORTS void subtract(const GpuMat& a, const Scalar& sc, GpuMat& c, const GpuMat& mask = GpuMat(), int dtype = -1, Stream& stream = Stream::Null());
475 //! computes element-wise weighted product of the two arrays (c = scale * a * b)
476 CV_EXPORTS void multiply(const GpuMat& a, const GpuMat& b, GpuMat& c, double scale = 1, int dtype = -1, Stream& stream = Stream::Null());
477 //! weighted multiplies matrix to a scalar (c = scale * a * s)
478 CV_EXPORTS void multiply(const GpuMat& a, const Scalar& sc, GpuMat& c, double scale = 1, int dtype = -1, Stream& stream = Stream::Null());
480 //! computes element-wise weighted quotient of the two arrays (c = a / b)
481 CV_EXPORTS void divide(const GpuMat& a, const GpuMat& b, GpuMat& c, double scale = 1, int dtype = -1, Stream& stream = Stream::Null());
482 //! computes element-wise weighted quotient of matrix and scalar (c = a / s)
483 CV_EXPORTS void divide(const GpuMat& a, const Scalar& sc, GpuMat& c, double scale = 1, int dtype = -1, Stream& stream = Stream::Null());
484 //! computes element-wise weighted reciprocal of an array (dst = scale/src2)
485 CV_EXPORTS void divide(double scale, const GpuMat& b, GpuMat& c, int dtype = -1, Stream& stream = Stream::Null());
487 //! computes the weighted sum of two arrays (dst = alpha*src1 + beta*src2 + gamma)
488 CV_EXPORTS void addWeighted(const GpuMat& src1, double alpha, const GpuMat& src2, double beta, double gamma, GpuMat& dst,
489 int dtype = -1, Stream& stream = Stream::Null());
491 //! adds scaled array to another one (dst = alpha*src1 + src2)
492 static inline void scaleAdd(const GpuMat& src1, double alpha, const GpuMat& src2, GpuMat& dst, Stream& stream = Stream::Null())
494 addWeighted(src1, alpha, src2, 1.0, 0.0, dst, -1, stream);
497 //! computes element-wise absolute difference of two arrays (c = abs(a - b))
498 CV_EXPORTS void absdiff(const GpuMat& a, const GpuMat& b, GpuMat& c, Stream& stream = Stream::Null());
499 //! computes element-wise absolute difference of array and scalar (c = abs(a - s))
500 CV_EXPORTS void absdiff(const GpuMat& a, const Scalar& s, GpuMat& c, Stream& stream = Stream::Null());
502 //! computes absolute value of each matrix element
503 //! supports CV_16S and CV_32F depth
504 CV_EXPORTS void abs(const GpuMat& src, GpuMat& dst, Stream& stream = Stream::Null());
506 //! computes square of each pixel in an image
507 //! supports CV_8U, CV_16U, CV_16S and CV_32F depth
508 CV_EXPORTS void sqr(const GpuMat& src, GpuMat& dst, Stream& stream = Stream::Null());
510 //! computes square root of each pixel in an image
511 //! supports CV_8U, CV_16U, CV_16S and CV_32F depth
512 CV_EXPORTS void sqrt(const GpuMat& src, GpuMat& dst, Stream& stream = Stream::Null());
514 //! computes exponent of each matrix element (b = e**a)
515 //! supports CV_8U, CV_16U, CV_16S and CV_32F depth
516 CV_EXPORTS void exp(const GpuMat& a, GpuMat& b, Stream& stream = Stream::Null());
518 //! computes natural logarithm of absolute value of each matrix element: b = log(abs(a))
519 //! supports CV_8U, CV_16U, CV_16S and CV_32F depth
520 CV_EXPORTS void log(const GpuMat& a, GpuMat& b, Stream& stream = Stream::Null());
522 //! computes power of each matrix element:
523 // (dst(i,j) = pow( src(i,j) , power), if src.type() is integer
524 // (dst(i,j) = pow(fabs(src(i,j)), power), otherwise
525 //! supports all, except depth == CV_64F
526 CV_EXPORTS void pow(const GpuMat& src, double power, GpuMat& dst, Stream& stream = Stream::Null());
528 //! compares elements of two arrays (c = a <cmpop> b)
529 CV_EXPORTS void compare(const GpuMat& a, const GpuMat& b, GpuMat& c, int cmpop, Stream& stream = Stream::Null());
531 //! performs per-elements bit-wise inversion
532 CV_EXPORTS void bitwise_not(const GpuMat& src, GpuMat& dst, const GpuMat& mask=GpuMat(), Stream& stream = Stream::Null());
534 //! calculates per-element bit-wise disjunction of two arrays
535 CV_EXPORTS void bitwise_or(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat& mask=GpuMat(), Stream& stream = Stream::Null());
536 //! calculates per-element bit-wise disjunction of array and scalar
537 //! supports 1, 3 and 4 channels images with CV_8U, CV_16U or CV_32S depth
538 CV_EXPORTS void bitwise_or(const GpuMat& src1, const Scalar& sc, GpuMat& dst, Stream& stream = Stream::Null());
540 //! calculates per-element bit-wise conjunction of two arrays
541 CV_EXPORTS void bitwise_and(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat& mask=GpuMat(), Stream& stream = Stream::Null());
542 //! calculates per-element bit-wise conjunction of array and scalar
543 //! supports 1, 3 and 4 channels images with CV_8U, CV_16U or CV_32S depth
544 CV_EXPORTS void bitwise_and(const GpuMat& src1, const Scalar& sc, GpuMat& dst, Stream& stream = Stream::Null());
546 //! calculates per-element bit-wise "exclusive or" operation
547 CV_EXPORTS void bitwise_xor(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat& mask=GpuMat(), Stream& stream = Stream::Null());
548 //! calculates per-element bit-wise "exclusive or" of array and scalar
549 //! supports 1, 3 and 4 channels images with CV_8U, CV_16U or CV_32S depth
550 CV_EXPORTS void bitwise_xor(const GpuMat& src1, const Scalar& sc, GpuMat& dst, Stream& stream = Stream::Null());
552 //! pixel by pixel right shift of an image by a constant value
553 //! supports 1, 3 and 4 channels images with integers elements
554 CV_EXPORTS void rshift(const GpuMat& src, Scalar_<int> sc, GpuMat& dst, Stream& stream = Stream::Null());
556 //! pixel by pixel left shift of an image by a constant value
557 //! supports 1, 3 and 4 channels images with CV_8U, CV_16U or CV_32S depth
558 CV_EXPORTS void lshift(const GpuMat& src, Scalar_<int> sc, GpuMat& dst, Stream& stream = Stream::Null());
560 //! computes per-element minimum of two arrays (dst = min(src1, src2))
561 CV_EXPORTS void min(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, Stream& stream = Stream::Null());
563 //! computes per-element minimum of array and scalar (dst = min(src1, src2))
564 CV_EXPORTS void min(const GpuMat& src1, double src2, GpuMat& dst, Stream& stream = Stream::Null());
566 //! computes per-element maximum of two arrays (dst = max(src1, src2))
567 CV_EXPORTS void max(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, Stream& stream = Stream::Null());
569 //! computes per-element maximum of array and scalar (dst = max(src1, src2))
570 CV_EXPORTS void max(const GpuMat& src1, double src2, GpuMat& dst, Stream& stream = Stream::Null());
572 enum { ALPHA_OVER, ALPHA_IN, ALPHA_OUT, ALPHA_ATOP, ALPHA_XOR, ALPHA_PLUS, ALPHA_OVER_PREMUL, ALPHA_IN_PREMUL, ALPHA_OUT_PREMUL,
573 ALPHA_ATOP_PREMUL, ALPHA_XOR_PREMUL, ALPHA_PLUS_PREMUL, ALPHA_PREMUL};
575 //! Composite two images using alpha opacity values contained in each image
576 //! Supports CV_8UC4, CV_16UC4, CV_32SC4 and CV_32FC4 types
577 CV_EXPORTS void alphaComp(const GpuMat& img1, const GpuMat& img2, GpuMat& dst, int alpha_op, Stream& stream = Stream::Null());
580 ////////////////////////////// Image processing //////////////////////////////
582 //! DST[x,y] = SRC[xmap[x,y],ymap[x,y]]
583 //! supports only CV_32FC1 map type
584 CV_EXPORTS void remap(const GpuMat& src, GpuMat& dst, const GpuMat& xmap, const GpuMat& ymap,
585 int interpolation, int borderMode = BORDER_CONSTANT, Scalar borderValue = Scalar(),
586 Stream& stream = Stream::Null());
588 //! Does mean shift filtering on GPU.
589 CV_EXPORTS void meanShiftFiltering(const GpuMat& src, GpuMat& dst, int sp, int sr,
590 TermCriteria criteria = TermCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 5, 1),
591 Stream& stream = Stream::Null());
593 //! Does mean shift procedure on GPU.
594 CV_EXPORTS void meanShiftProc(const GpuMat& src, GpuMat& dstr, GpuMat& dstsp, int sp, int sr,
595 TermCriteria criteria = TermCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 5, 1),
596 Stream& stream = Stream::Null());
598 //! Does mean shift segmentation with elimination of small regions.
599 CV_EXPORTS void meanShiftSegmentation(const GpuMat& src, Mat& dst, int sp, int sr, int minsize,
600 TermCriteria criteria = TermCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 5, 1));
602 //! Does coloring of disparity image: [0..ndisp) -> [0..240, 1, 1] in HSV.
603 //! Supported types of input disparity: CV_8U, CV_16S.
604 //! Output disparity has CV_8UC4 type in BGRA format (alpha = 255).
605 CV_EXPORTS void drawColorDisp(const GpuMat& src_disp, GpuMat& dst_disp, int ndisp, Stream& stream = Stream::Null());
607 //! Reprojects disparity image to 3D space.
608 //! Supports CV_8U and CV_16S types of input disparity.
609 //! The output is a 3- or 4-channel floating-point matrix.
610 //! Each element of this matrix will contain the 3D coordinates of the point (x,y,z,1), computed from the disparity map.
611 //! Q is the 4x4 perspective transformation matrix that can be obtained with cvStereoRectify.
612 CV_EXPORTS void reprojectImageTo3D(const GpuMat& disp, GpuMat& xyzw, const Mat& Q, int dst_cn = 4, Stream& stream = Stream::Null());
614 //! converts image from one color space to another
615 CV_EXPORTS void cvtColor(const GpuMat& src, GpuMat& dst, int code, int dcn = 0, Stream& stream = Stream::Null());
618 //! dstOrder - Integer array describing how channel values are permutated. The n-th entry
619 //! of the array contains the number of the channel that is stored in the n-th channel of
620 //! the output image. E.g. Given an RGBA image, aDstOrder = [3,2,1,0] converts this to ABGR
622 CV_EXPORTS void swapChannels(GpuMat& image, const int dstOrder[4], Stream& stream = Stream::Null());
624 //! Routines for correcting image color gamma
625 CV_EXPORTS void gammaCorrection(const GpuMat& src, GpuMat& dst, bool forward = true, Stream& stream = Stream::Null());
627 //! applies fixed threshold to the image
628 CV_EXPORTS double threshold(const GpuMat& src, GpuMat& dst, double thresh, double maxval, int type, Stream& stream = Stream::Null());
630 //! resizes the image
631 //! Supports INTER_NEAREST, INTER_LINEAR, INTER_CUBIC, INTER_AREA
632 CV_EXPORTS void resize(const GpuMat& src, GpuMat& dst, Size dsize, double fx=0, double fy=0, int interpolation = INTER_LINEAR, Stream& stream = Stream::Null());
634 //! warps the image using affine transformation
635 //! Supports INTER_NEAREST, INTER_LINEAR, INTER_CUBIC
636 CV_EXPORTS void warpAffine(const GpuMat& src, GpuMat& dst, const Mat& M, Size dsize, int flags = INTER_LINEAR,
637 int borderMode = BORDER_CONSTANT, Scalar borderValue = Scalar(), Stream& stream = Stream::Null());
639 CV_EXPORTS void buildWarpAffineMaps(const Mat& M, bool inverse, Size dsize, GpuMat& xmap, GpuMat& ymap, Stream& stream = Stream::Null());
641 //! warps the image using perspective transformation
642 //! Supports INTER_NEAREST, INTER_LINEAR, INTER_CUBIC
643 CV_EXPORTS void warpPerspective(const GpuMat& src, GpuMat& dst, const Mat& M, Size dsize, int flags = INTER_LINEAR,
644 int borderMode = BORDER_CONSTANT, Scalar borderValue = Scalar(), Stream& stream = Stream::Null());
646 CV_EXPORTS void buildWarpPerspectiveMaps(const Mat& M, bool inverse, Size dsize, GpuMat& xmap, GpuMat& ymap, Stream& stream = Stream::Null());
648 //! builds plane warping maps
649 CV_EXPORTS void buildWarpPlaneMaps(Size src_size, Rect dst_roi, const Mat &K, const Mat& R, const Mat &T, float scale,
650 GpuMat& map_x, GpuMat& map_y, Stream& stream = Stream::Null());
652 //! builds cylindrical warping maps
653 CV_EXPORTS void buildWarpCylindricalMaps(Size src_size, Rect dst_roi, const Mat &K, const Mat& R, float scale,
654 GpuMat& map_x, GpuMat& map_y, Stream& stream = Stream::Null());
656 //! builds spherical warping maps
657 CV_EXPORTS void buildWarpSphericalMaps(Size src_size, Rect dst_roi, const Mat &K, const Mat& R, float scale,
658 GpuMat& map_x, GpuMat& map_y, Stream& stream = Stream::Null());
660 //! rotates an image around the origin (0,0) and then shifts it
661 //! supports INTER_NEAREST, INTER_LINEAR, INTER_CUBIC
662 //! supports 1, 3 or 4 channels images with CV_8U, CV_16U or CV_32F depth
663 CV_EXPORTS void rotate(const GpuMat& src, GpuMat& dst, Size dsize, double angle, double xShift = 0, double yShift = 0,
664 int interpolation = INTER_LINEAR, Stream& stream = Stream::Null());
666 //! copies 2D array to a larger destination array and pads borders with user-specifiable constant
667 CV_EXPORTS void copyMakeBorder(const GpuMat& src, GpuMat& dst, int top, int bottom, int left, int right, int borderType,
668 const Scalar& value = Scalar(), Stream& stream = Stream::Null());
670 //! computes the integral image
671 //! sum will have CV_32S type, but will contain unsigned int values
672 //! supports only CV_8UC1 source type
673 CV_EXPORTS void integral(const GpuMat& src, GpuMat& sum, Stream& stream = Stream::Null());
675 CV_EXPORTS void integralBuffered(const GpuMat& src, GpuMat& sum, GpuMat& buffer, Stream& stream = Stream::Null());
677 //! computes squared integral image
678 //! result matrix will have 64F type, but will contain 64U values
679 //! supports source images of 8UC1 type only
680 CV_EXPORTS void sqrIntegral(const GpuMat& src, GpuMat& sqsum, Stream& stream = Stream::Null());
682 //! computes vertical sum, supports only CV_32FC1 images
683 CV_EXPORTS void columnSum(const GpuMat& src, GpuMat& sum);
685 //! computes the standard deviation of integral images
686 //! supports only CV_32SC1 source type and CV_32FC1 sqr type
687 //! output will have CV_32FC1 type
688 CV_EXPORTS void rectStdDev(const GpuMat& src, const GpuMat& sqr, GpuMat& dst, const Rect& rect, Stream& stream = Stream::Null());
690 //! computes Harris cornerness criteria at each image pixel
691 CV_EXPORTS void cornerHarris(const GpuMat& src, GpuMat& dst, int blockSize, int ksize, double k, int borderType = BORDER_REFLECT101);
692 CV_EXPORTS void cornerHarris(const GpuMat& src, GpuMat& dst, GpuMat& Dx, GpuMat& Dy, int blockSize, int ksize, double k, int borderType = BORDER_REFLECT101);
693 CV_EXPORTS void cornerHarris(const GpuMat& src, GpuMat& dst, GpuMat& Dx, GpuMat& Dy, GpuMat& buf, int blockSize, int ksize, double k,
694 int borderType = BORDER_REFLECT101, Stream& stream = Stream::Null());
696 //! computes minimum eigen value of 2x2 derivative covariation matrix at each pixel - the cornerness criteria
697 CV_EXPORTS void cornerMinEigenVal(const GpuMat& src, GpuMat& dst, int blockSize, int ksize, int borderType=BORDER_REFLECT101);
698 CV_EXPORTS void cornerMinEigenVal(const GpuMat& src, GpuMat& dst, GpuMat& Dx, GpuMat& Dy, int blockSize, int ksize, int borderType=BORDER_REFLECT101);
699 CV_EXPORTS void cornerMinEigenVal(const GpuMat& src, GpuMat& dst, GpuMat& Dx, GpuMat& Dy, GpuMat& buf, int blockSize, int ksize,
700 int borderType=BORDER_REFLECT101, Stream& stream = Stream::Null());
702 //! performs per-element multiplication of two full (not packed) Fourier spectrums
703 //! supports 32FC2 matrixes only (interleaved format)
704 CV_EXPORTS void mulSpectrums(const GpuMat& a, const GpuMat& b, GpuMat& c, int flags, bool conjB=false, Stream& stream = Stream::Null());
706 //! performs per-element multiplication of two full (not packed) Fourier spectrums
707 //! supports 32FC2 matrixes only (interleaved format)
708 CV_EXPORTS void mulAndScaleSpectrums(const GpuMat& a, const GpuMat& b, GpuMat& c, int flags, float scale, bool conjB=false, Stream& stream = Stream::Null());
710 //! Performs a forward or inverse discrete Fourier transform (1D or 2D) of floating point matrix.
711 //! Param dft_size is the size of DFT transform.
713 //! If the source matrix is not continous, then additional copy will be done,
714 //! so to avoid copying ensure the source matrix is continous one. If you want to use
715 //! preallocated output ensure it is continuous too, otherwise it will be reallocated.
717 //! Being implemented via CUFFT real-to-complex transform result contains only non-redundant values
718 //! in CUFFT's format. Result as full complex matrix for such kind of transform cannot be retrieved.
720 //! For complex-to-real transform it is assumed that the source matrix is packed in CUFFT's format.
721 CV_EXPORTS void dft(const GpuMat& src, GpuMat& dst, Size dft_size, int flags=0, Stream& stream = Stream::Null());
723 struct CV_EXPORTS ConvolveBuf
727 Size user_block_size;
731 GpuMat image_spect, templ_spect, result_spect;
732 GpuMat image_block, templ_block, result_data;
734 void create(Size image_size, Size templ_size);
735 static Size estimateBlockSize(Size result_size, Size templ_size);
739 //! computes convolution (or cross-correlation) of two images using discrete Fourier transform
740 //! supports source images of 32FC1 type only
741 //! result matrix will have 32FC1 type
742 CV_EXPORTS void convolve(const GpuMat& image, const GpuMat& templ, GpuMat& result, bool ccorr = false);
743 CV_EXPORTS void convolve(const GpuMat& image, const GpuMat& templ, GpuMat& result, bool ccorr, ConvolveBuf& buf, Stream& stream = Stream::Null());
745 struct CV_EXPORTS MatchTemplateBuf
747 Size user_block_size;
748 GpuMat imagef, templf;
749 std::vector<GpuMat> images;
750 std::vector<GpuMat> image_sums;
751 std::vector<GpuMat> image_sqsums;
754 //! computes the proximity map for the raster template and the image where the template is searched for
755 CV_EXPORTS void matchTemplate(const GpuMat& image, const GpuMat& templ, GpuMat& result, int method, Stream &stream = Stream::Null());
757 //! computes the proximity map for the raster template and the image where the template is searched for
758 CV_EXPORTS void matchTemplate(const GpuMat& image, const GpuMat& templ, GpuMat& result, int method, MatchTemplateBuf &buf, Stream& stream = Stream::Null());
760 //! smoothes the source image and downsamples it
761 CV_EXPORTS void pyrDown(const GpuMat& src, GpuMat& dst, Stream& stream = Stream::Null());
763 //! upsamples the source image and then smoothes it
764 CV_EXPORTS void pyrUp(const GpuMat& src, GpuMat& dst, Stream& stream = Stream::Null());
766 //! performs linear blending of two images
767 //! to avoid accuracy errors sum of weigths shouldn't be very close to zero
768 CV_EXPORTS void blendLinear(const GpuMat& img1, const GpuMat& img2, const GpuMat& weights1, const GpuMat& weights2,
769 GpuMat& result, Stream& stream = Stream::Null());
771 //! Performa bilateral filtering of passsed image
772 CV_EXPORTS void bilateralFilter(const GpuMat& src, GpuMat& dst, int kernel_size, float sigma_color, float sigma_spatial,
773 int borderMode = BORDER_DEFAULT, Stream& stream = Stream::Null());
775 //! Brute force non-local means algorith (slow but universal)
776 CV_EXPORTS void nonLocalMeans(const GpuMat& src, GpuMat& dst, float h, int search_window = 21, int block_size = 7, int borderMode = BORDER_DEFAULT, Stream& s = Stream::Null());
778 //! Fast (but approximate)version of non-local means algorith similar to CPU function (running sums technique)
779 class CV_EXPORTS FastNonLocalMeansDenoising
782 //! Simple method, recommended for grayscale images (though it supports multichannel images)
783 void simpleMethod(const GpuMat& src, GpuMat& dst, float h, int search_window = 21, int block_size = 7, Stream& s = Stream::Null());
785 //! Processes luminance and color components separatelly
786 void labMethod(const GpuMat& src, GpuMat& dst, float h_luminance, float h_color, int search_window = 21, int block_size = 7, Stream& s = Stream::Null());
790 GpuMat buffer, extended_src_buffer;
795 struct CV_EXPORTS CannyBuf;
797 CV_EXPORTS void Canny(const GpuMat& image, GpuMat& edges, double low_thresh, double high_thresh, int apperture_size = 3, bool L2gradient = false);
798 CV_EXPORTS void Canny(const GpuMat& image, CannyBuf& buf, GpuMat& edges, double low_thresh, double high_thresh, int apperture_size = 3, bool L2gradient = false);
799 CV_EXPORTS void Canny(const GpuMat& dx, const GpuMat& dy, GpuMat& edges, double low_thresh, double high_thresh, bool L2gradient = false);
800 CV_EXPORTS void Canny(const GpuMat& dx, const GpuMat& dy, CannyBuf& buf, GpuMat& edges, double low_thresh, double high_thresh, bool L2gradient = false);
802 struct CV_EXPORTS CannyBuf
805 explicit CannyBuf(const Size& image_size, int apperture_size = 3) {create(image_size, apperture_size);}
806 CannyBuf(const GpuMat& dx_, const GpuMat& dy_);
808 void create(const Size& image_size, int apperture_size = 3);
813 GpuMat dx_buf, dy_buf;
815 GpuMat trackBuf1, trackBuf2;
816 Ptr<FilterEngine_GPU> filterDX, filterDY;
819 class CV_EXPORTS ImagePyramid
822 inline ImagePyramid() : nLayers_(0) {}
823 inline ImagePyramid(const GpuMat& img, int nLayers, Stream& stream = Stream::Null())
825 build(img, nLayers, stream);
828 void build(const GpuMat& img, int nLayers, Stream& stream = Stream::Null());
830 void getLayer(GpuMat& outImg, Size outRoi, Stream& stream = Stream::Null()) const;
832 inline void release()
841 std::vector<GpuMat> pyramid_;
853 CV_EXPORTS void HoughLines(const GpuMat& src, GpuMat& lines, float rho, float theta, int threshold, bool doSort = false, int maxLines = 4096);
854 CV_EXPORTS void HoughLines(const GpuMat& src, GpuMat& lines, HoughLinesBuf& buf, float rho, float theta, int threshold, bool doSort = false, int maxLines = 4096);
855 CV_EXPORTS void HoughLinesDownload(const GpuMat& d_lines, OutputArray h_lines, OutputArray h_votes = noArray());
859 struct HoughCirclesBuf
867 CV_EXPORTS void HoughCircles(const GpuMat& src, GpuMat& circles, int method, float dp, float minDist, int cannyThreshold, int votesThreshold, int minRadius, int maxRadius, int maxCircles = 4096);
868 CV_EXPORTS void HoughCircles(const GpuMat& src, GpuMat& circles, HoughCirclesBuf& buf, int method, float dp, float minDist, int cannyThreshold, int votesThreshold, int minRadius, int maxRadius, int maxCircles = 4096);
869 CV_EXPORTS void HoughCirclesDownload(const GpuMat& d_circles, OutputArray h_circles);
871 //! finds arbitrary template in the grayscale image using Generalized Hough Transform
872 //! Ballard, D.H. (1981). Generalizing the Hough transform to detect arbitrary shapes. Pattern Recognition 13 (2): 111-122.
873 //! Guil, N., González-Linares, J.M. and Zapata, E.L. (1999). Bidimensional shape detection using an invariant approach. Pattern Recognition 32 (6): 1025-1038.
874 class CV_EXPORTS GeneralizedHough_GPU : public Algorithm
877 static Ptr<GeneralizedHough_GPU> create(int method);
879 virtual ~GeneralizedHough_GPU();
881 //! set template to search
882 void setTemplate(const GpuMat& templ, int cannyThreshold = 100, Point templCenter = Point(-1, -1));
883 void setTemplate(const GpuMat& edges, const GpuMat& dx, const GpuMat& dy, Point templCenter = Point(-1, -1));
885 //! find template on image
886 void detect(const GpuMat& image, GpuMat& positions, int cannyThreshold = 100);
887 void detect(const GpuMat& edges, const GpuMat& dx, const GpuMat& dy, GpuMat& positions);
889 void download(const GpuMat& d_positions, OutputArray h_positions, OutputArray h_votes = noArray());
894 virtual void setTemplateImpl(const GpuMat& edges, const GpuMat& dx, const GpuMat& dy, Point templCenter) = 0;
895 virtual void detectImpl(const GpuMat& edges, const GpuMat& dx, const GpuMat& dy, GpuMat& positions) = 0;
896 virtual void releaseImpl() = 0;
903 ////////////////////////////// Matrix reductions //////////////////////////////
905 //! computes mean value and standard deviation of all or selected array elements
906 //! supports only CV_8UC1 type
907 CV_EXPORTS void meanStdDev(const GpuMat& mtx, Scalar& mean, Scalar& stddev);
909 CV_EXPORTS void meanStdDev(const GpuMat& mtx, Scalar& mean, Scalar& stddev, GpuMat& buf);
911 //! computes norm of array
912 //! supports NORM_INF, NORM_L1, NORM_L2
913 //! supports all matrices except 64F
914 CV_EXPORTS double norm(const GpuMat& src1, int normType=NORM_L2);
916 //! computes norm of array
917 //! supports NORM_INF, NORM_L1, NORM_L2
918 //! supports all matrices except 64F
919 CV_EXPORTS double norm(const GpuMat& src1, int normType, GpuMat& buf);
921 //! computes norm of the difference between two arrays
922 //! supports NORM_INF, NORM_L1, NORM_L2
923 //! supports only CV_8UC1 type
924 CV_EXPORTS double norm(const GpuMat& src1, const GpuMat& src2, int normType=NORM_L2);
926 //! computes sum of array elements
927 //! supports only single channel images
928 CV_EXPORTS Scalar sum(const GpuMat& src);
930 //! computes sum of array elements
931 //! supports only single channel images
932 CV_EXPORTS Scalar sum(const GpuMat& src, GpuMat& buf);
934 //! computes sum of array elements absolute values
935 //! supports only single channel images
936 CV_EXPORTS Scalar absSum(const GpuMat& src);
938 //! computes sum of array elements absolute values
939 //! supports only single channel images
940 CV_EXPORTS Scalar absSum(const GpuMat& src, GpuMat& buf);
942 //! computes squared sum of array elements
943 //! supports only single channel images
944 CV_EXPORTS Scalar sqrSum(const GpuMat& src);
946 //! computes squared sum of array elements
947 //! supports only single channel images
948 CV_EXPORTS Scalar sqrSum(const GpuMat& src, GpuMat& buf);
950 //! finds global minimum and maximum array elements and returns their values
951 CV_EXPORTS void minMax(const GpuMat& src, double* minVal, double* maxVal=0, const GpuMat& mask=GpuMat());
953 //! finds global minimum and maximum array elements and returns their values
954 CV_EXPORTS void minMax(const GpuMat& src, double* minVal, double* maxVal, const GpuMat& mask, GpuMat& buf);
956 //! finds global minimum and maximum array elements and returns their values with locations
957 CV_EXPORTS void minMaxLoc(const GpuMat& src, double* minVal, double* maxVal=0, Point* minLoc=0, Point* maxLoc=0,
958 const GpuMat& mask=GpuMat());
960 //! finds global minimum and maximum array elements and returns their values with locations
961 CV_EXPORTS void minMaxLoc(const GpuMat& src, double* minVal, double* maxVal, Point* minLoc, Point* maxLoc,
962 const GpuMat& mask, GpuMat& valbuf, GpuMat& locbuf);
964 //! counts non-zero array elements
965 CV_EXPORTS int countNonZero(const GpuMat& src);
967 //! counts non-zero array elements
968 CV_EXPORTS int countNonZero(const GpuMat& src, GpuMat& buf);
970 //! reduces a matrix to a vector
971 CV_EXPORTS void reduce(const GpuMat& mtx, GpuMat& vec, int dim, int reduceOp, int dtype = -1, Stream& stream = Stream::Null());
974 ///////////////////////////// Calibration 3D //////////////////////////////////
976 CV_EXPORTS void transformPoints(const GpuMat& src, const Mat& rvec, const Mat& tvec,
977 GpuMat& dst, Stream& stream = Stream::Null());
979 CV_EXPORTS void projectPoints(const GpuMat& src, const Mat& rvec, const Mat& tvec,
980 const Mat& camera_mat, const Mat& dist_coef, GpuMat& dst,
981 Stream& stream = Stream::Null());
983 CV_EXPORTS void solvePnPRansac(const Mat& object, const Mat& image, const Mat& camera_mat,
984 const Mat& dist_coef, Mat& rvec, Mat& tvec, bool use_extrinsic_guess=false,
985 int num_iters=100, float max_dist=8.0, int min_inlier_count=100,
986 std::vector<int>* inliers=NULL);
988 //////////////////////////////// Image Labeling ////////////////////////////////
990 //!performs labeling via graph cuts of a 2D regular 4-connected graph.
991 CV_EXPORTS void graphcut(GpuMat& terminals, GpuMat& leftTransp, GpuMat& rightTransp, GpuMat& top, GpuMat& bottom, GpuMat& labels,
992 GpuMat& buf, Stream& stream = Stream::Null());
994 //!performs labeling via graph cuts of a 2D regular 8-connected graph.
995 CV_EXPORTS void graphcut(GpuMat& terminals, GpuMat& leftTransp, GpuMat& rightTransp, GpuMat& top, GpuMat& topLeft, GpuMat& topRight,
996 GpuMat& bottom, GpuMat& bottomLeft, GpuMat& bottomRight,
998 GpuMat& buf, Stream& stream = Stream::Null());
1000 //! compute mask for Generalized Flood fill componetns labeling.
1001 CV_EXPORTS void connectivityMask(const GpuMat& image, GpuMat& mask, const cv::Scalar& lo, const cv::Scalar& hi, Stream& stream = Stream::Null());
1003 //! performs connected componnents labeling.
1004 CV_EXPORTS void labelComponents(const GpuMat& mask, GpuMat& components, int flags = 0, Stream& stream = Stream::Null());
1006 ////////////////////////////////// Histograms //////////////////////////////////
1008 //! Compute levels with even distribution. levels will have 1 row and nLevels cols and CV_32SC1 type.
1009 CV_EXPORTS void evenLevels(GpuMat& levels, int nLevels, int lowerLevel, int upperLevel);
1010 //! Calculates histogram with evenly distributed bins for signle channel source.
1011 //! Supports CV_8UC1, CV_16UC1 and CV_16SC1 source types.
1012 //! Output hist will have one row and histSize cols and CV_32SC1 type.
1013 CV_EXPORTS void histEven(const GpuMat& src, GpuMat& hist, int histSize, int lowerLevel, int upperLevel, Stream& stream = Stream::Null());
1014 CV_EXPORTS void histEven(const GpuMat& src, GpuMat& hist, GpuMat& buf, int histSize, int lowerLevel, int upperLevel, Stream& stream = Stream::Null());
1015 //! Calculates histogram with evenly distributed bins for four-channel source.
1016 //! All channels of source are processed separately.
1017 //! Supports CV_8UC4, CV_16UC4 and CV_16SC4 source types.
1018 //! Output hist[i] will have one row and histSize[i] cols and CV_32SC1 type.
1019 CV_EXPORTS void histEven(const GpuMat& src, GpuMat hist[4], int histSize[4], int lowerLevel[4], int upperLevel[4], Stream& stream = Stream::Null());
1020 CV_EXPORTS void histEven(const GpuMat& src, GpuMat hist[4], GpuMat& buf, int histSize[4], int lowerLevel[4], int upperLevel[4], Stream& stream = Stream::Null());
1021 //! Calculates histogram with bins determined by levels array.
1022 //! levels must have one row and CV_32SC1 type if source has integer type or CV_32FC1 otherwise.
1023 //! Supports CV_8UC1, CV_16UC1, CV_16SC1 and CV_32FC1 source types.
1024 //! Output hist will have one row and (levels.cols-1) cols and CV_32SC1 type.
1025 CV_EXPORTS void histRange(const GpuMat& src, GpuMat& hist, const GpuMat& levels, Stream& stream = Stream::Null());
1026 CV_EXPORTS void histRange(const GpuMat& src, GpuMat& hist, const GpuMat& levels, GpuMat& buf, Stream& stream = Stream::Null());
1027 //! Calculates histogram with bins determined by levels array.
1028 //! All levels must have one row and CV_32SC1 type if source has integer type or CV_32FC1 otherwise.
1029 //! All channels of source are processed separately.
1030 //! Supports CV_8UC4, CV_16UC4, CV_16SC4 and CV_32FC4 source types.
1031 //! Output hist[i] will have one row and (levels[i].cols-1) cols and CV_32SC1 type.
1032 CV_EXPORTS void histRange(const GpuMat& src, GpuMat hist[4], const GpuMat levels[4], Stream& stream = Stream::Null());
1033 CV_EXPORTS void histRange(const GpuMat& src, GpuMat hist[4], const GpuMat levels[4], GpuMat& buf, Stream& stream = Stream::Null());
1035 //! Calculates histogram for 8u one channel image
1036 //! Output hist will have one row, 256 cols and CV32SC1 type.
1037 CV_EXPORTS void calcHist(const GpuMat& src, GpuMat& hist, Stream& stream = Stream::Null());
1038 CV_EXPORTS void calcHist(const GpuMat& src, GpuMat& hist, GpuMat& buf, Stream& stream = Stream::Null());
1040 //! normalizes the grayscale image brightness and contrast by normalizing its histogram
1041 CV_EXPORTS void equalizeHist(const GpuMat& src, GpuMat& dst, Stream& stream = Stream::Null());
1042 CV_EXPORTS void equalizeHist(const GpuMat& src, GpuMat& dst, GpuMat& hist, Stream& stream = Stream::Null());
1043 CV_EXPORTS void equalizeHist(const GpuMat& src, GpuMat& dst, GpuMat& hist, GpuMat& buf, Stream& stream = Stream::Null());
1045 //////////////////////////////// StereoBM_GPU ////////////////////////////////
1047 class CV_EXPORTS StereoBM_GPU
1050 enum { BASIC_PRESET = 0, PREFILTER_XSOBEL = 1 };
1052 enum { DEFAULT_NDISP = 64, DEFAULT_WINSZ = 19 };
1054 //! the default constructor
1056 //! the full constructor taking the camera-specific preset, number of disparities and the SAD window size. ndisparities must be multiple of 8.
1057 StereoBM_GPU(int preset, int ndisparities = DEFAULT_NDISP, int winSize = DEFAULT_WINSZ);
1059 //! the stereo correspondence operator. Finds the disparity for the specified rectified stereo pair
1060 //! Output disparity has CV_8U type.
1061 void operator()(const GpuMat& left, const GpuMat& right, GpuMat& disparity, Stream& stream = Stream::Null());
1063 //! Some heuristics that tries to estmate
1064 // if current GPU will be faster than CPU in this algorithm.
1065 // It queries current active device.
1066 static bool checkIfGpuCallReasonable();
1072 // If avergeTexThreshold == 0 => post procesing is disabled
1073 // If avergeTexThreshold != 0 then disparity is set 0 in each point (x,y) where for left image
1074 // SumOfHorizontalGradiensInWindow(x, y, winSize) < (winSize * winSize) * avergeTexThreshold
1075 // i.e. input left image is low textured.
1076 float avergeTexThreshold;
1079 GpuMat minSSD, leBuf, riBuf;
1082 ////////////////////////// StereoBeliefPropagation ///////////////////////////
1083 // "Efficient Belief Propagation for Early Vision"
1086 class CV_EXPORTS StereoBeliefPropagation
1089 enum { DEFAULT_NDISP = 64 };
1090 enum { DEFAULT_ITERS = 5 };
1091 enum { DEFAULT_LEVELS = 5 };
1093 static void estimateRecommendedParams(int width, int height, int& ndisp, int& iters, int& levels);
1095 //! the default constructor
1096 explicit StereoBeliefPropagation(int ndisp = DEFAULT_NDISP,
1097 int iters = DEFAULT_ITERS,
1098 int levels = DEFAULT_LEVELS,
1099 int msg_type = CV_32F);
1101 //! the full constructor taking the number of disparities, number of BP iterations on each level,
1102 //! number of levels, truncation of data cost, data weight,
1103 //! truncation of discontinuity cost and discontinuity single jump
1104 //! DataTerm = data_weight * min(fabs(I2-I1), max_data_term)
1105 //! DiscTerm = min(disc_single_jump * fabs(f1-f2), max_disc_term)
1106 //! please see paper for more details
1107 StereoBeliefPropagation(int ndisp, int iters, int levels,
1108 float max_data_term, float data_weight,
1109 float max_disc_term, float disc_single_jump,
1110 int msg_type = CV_32F);
1112 //! the stereo correspondence operator. Finds the disparity for the specified rectified stereo pair,
1113 //! if disparity is empty output type will be CV_16S else output type will be disparity.type().
1114 void operator()(const GpuMat& left, const GpuMat& right, GpuMat& disparity, Stream& stream = Stream::Null());
1117 //! version for user specified data term
1118 void operator()(const GpuMat& data, GpuMat& disparity, Stream& stream = Stream::Null());
1125 float max_data_term;
1127 float max_disc_term;
1128 float disc_single_jump;
1132 GpuMat u, d, l, r, u2, d2, l2, r2;
1133 std::vector<GpuMat> datas;
1137 /////////////////////////// StereoConstantSpaceBP ///////////////////////////
1138 // "A Constant-Space Belief Propagation Algorithm for Stereo Matching"
1139 // Qingxiong Yang, Liang Wang, Narendra Ahuja
1140 // http://vision.ai.uiuc.edu/~qyang6/
1142 class CV_EXPORTS StereoConstantSpaceBP
1145 enum { DEFAULT_NDISP = 128 };
1146 enum { DEFAULT_ITERS = 8 };
1147 enum { DEFAULT_LEVELS = 4 };
1148 enum { DEFAULT_NR_PLANE = 4 };
1150 static void estimateRecommendedParams(int width, int height, int& ndisp, int& iters, int& levels, int& nr_plane);
1152 //! the default constructor
1153 explicit StereoConstantSpaceBP(int ndisp = DEFAULT_NDISP,
1154 int iters = DEFAULT_ITERS,
1155 int levels = DEFAULT_LEVELS,
1156 int nr_plane = DEFAULT_NR_PLANE,
1157 int msg_type = CV_32F);
1159 //! the full constructor taking the number of disparities, number of BP iterations on each level,
1160 //! number of levels, number of active disparity on the first level, truncation of data cost, data weight,
1161 //! truncation of discontinuity cost, discontinuity single jump and minimum disparity threshold
1162 StereoConstantSpaceBP(int ndisp, int iters, int levels, int nr_plane,
1163 float max_data_term, float data_weight, float max_disc_term, float disc_single_jump,
1164 int min_disp_th = 0,
1165 int msg_type = CV_32F);
1167 //! the stereo correspondence operator. Finds the disparity for the specified rectified stereo pair,
1168 //! if disparity is empty output type will be CV_16S else output type will be disparity.type().
1169 void operator()(const GpuMat& left, const GpuMat& right, GpuMat& disparity, Stream& stream = Stream::Null());
1178 float max_data_term;
1180 float max_disc_term;
1181 float disc_single_jump;
1187 bool use_local_init_data_cost;
1189 GpuMat messages_buffers;
1195 /////////////////////////// DisparityBilateralFilter ///////////////////////////
1196 // Disparity map refinement using joint bilateral filtering given a single color image.
1197 // Qingxiong Yang, Liang Wang, Narendra Ahuja
1198 // http://vision.ai.uiuc.edu/~qyang6/
1200 class CV_EXPORTS DisparityBilateralFilter
1203 enum { DEFAULT_NDISP = 64 };
1204 enum { DEFAULT_RADIUS = 3 };
1205 enum { DEFAULT_ITERS = 1 };
1207 //! the default constructor
1208 explicit DisparityBilateralFilter(int ndisp = DEFAULT_NDISP, int radius = DEFAULT_RADIUS, int iters = DEFAULT_ITERS);
1210 //! the full constructor taking the number of disparities, filter radius,
1211 //! number of iterations, truncation of data continuity, truncation of disparity continuity
1212 //! and filter range sigma
1213 DisparityBilateralFilter(int ndisp, int radius, int iters, float edge_threshold, float max_disc_threshold, float sigma_range);
1215 //! the disparity map refinement operator. Refine disparity map using joint bilateral filtering given a single color image.
1216 //! disparity must have CV_8U or CV_16S type, image must have CV_8UC1 or CV_8UC3 type.
1217 void operator()(const GpuMat& disparity, const GpuMat& image, GpuMat& dst, Stream& stream = Stream::Null());
1224 float edge_threshold;
1225 float max_disc_threshold;
1233 //////////////// HOG (Histogram-of-Oriented-Gradients) Descriptor and Object Detector //////////////
1234 struct CV_EXPORTS HOGConfidence
1237 vector<Point> locations;
1238 vector<double> confidences;
1239 vector<double> part_scores[4];
1242 struct CV_EXPORTS HOGDescriptor
1244 enum { DEFAULT_WIN_SIGMA = -1 };
1245 enum { DEFAULT_NLEVELS = 64 };
1246 enum { DESCR_FORMAT_ROW_BY_ROW, DESCR_FORMAT_COL_BY_COL };
1248 HOGDescriptor(Size win_size=Size(64, 128), Size block_size=Size(16, 16),
1249 Size block_stride=Size(8, 8), Size cell_size=Size(8, 8),
1250 int nbins=9, double win_sigma=DEFAULT_WIN_SIGMA,
1251 double threshold_L2hys=0.2, bool gamma_correction=true,
1252 int nlevels=DEFAULT_NLEVELS);
1254 size_t getDescriptorSize() const;
1255 size_t getBlockHistogramSize() const;
1257 void setSVMDetector(const vector<float>& detector);
1259 static vector<float> getDefaultPeopleDetector();
1260 static vector<float> getPeopleDetector48x96();
1261 static vector<float> getPeopleDetector64x128();
1263 void detect(const GpuMat& img, vector<Point>& found_locations,
1264 double hit_threshold=0, Size win_stride=Size(),
1265 Size padding=Size());
1267 void detectMultiScale(const GpuMat& img, vector<Rect>& found_locations,
1268 double hit_threshold=0, Size win_stride=Size(),
1269 Size padding=Size(), double scale0=1.05,
1270 int group_threshold=2);
1272 void computeConfidence(const GpuMat& img, vector<Point>& hits, double hit_threshold,
1273 Size win_stride, Size padding, vector<Point>& locations, vector<double>& confidences);
1275 void computeConfidenceMultiScale(const GpuMat& img, vector<Rect>& found_locations,
1276 double hit_threshold, Size win_stride, Size padding,
1277 vector<HOGConfidence> &conf_out, int group_threshold);
1279 void getDescriptors(const GpuMat& img, Size win_stride,
1280 GpuMat& descriptors,
1281 int descr_format=DESCR_FORMAT_COL_BY_COL);
1289 double threshold_L2hys;
1290 bool gamma_correction;
1294 void computeBlockHistograms(const GpuMat& img);
1295 void computeGradient(const GpuMat& img, GpuMat& grad, GpuMat& qangle);
1297 double getWinSigma() const;
1298 bool checkDetectorSize() const;
1300 static int numPartsWithin(int size, int part_size, int stride);
1301 static Size numPartsWithin(Size size, Size part_size, Size stride);
1303 // Coefficients of the separating plane
1307 // Results of the last classification step
1308 GpuMat labels, labels_buf;
1311 // Results of the last histogram evaluation step
1312 GpuMat block_hists, block_hists_buf;
1314 // Gradients conputation results
1315 GpuMat grad, qangle, grad_buf, qangle_buf;
1317 // returns subbuffer with required size, reallocates buffer if nessesary.
1318 static GpuMat getBuffer(const Size& sz, int type, GpuMat& buf);
1319 static GpuMat getBuffer(int rows, int cols, int type, GpuMat& buf);
1321 std::vector<GpuMat> image_scales;
1325 ////////////////////////////////// BruteForceMatcher //////////////////////////////////
1327 class CV_EXPORTS BruteForceMatcher_GPU_base
1330 enum DistType {L1Dist = 0, L2Dist, HammingDist};
1332 explicit BruteForceMatcher_GPU_base(DistType distType = L2Dist);
1334 // Add descriptors to train descriptor collection
1335 void add(const std::vector<GpuMat>& descCollection);
1337 // Get train descriptors collection
1338 const std::vector<GpuMat>& getTrainDescriptors() const;
1340 // Clear train descriptors collection
1343 // Return true if there are not train descriptors in collection
1346 // Return true if the matcher supports mask in match methods
1347 bool isMaskSupported() const;
1349 // Find one best match for each query descriptor
1350 void matchSingle(const GpuMat& query, const GpuMat& train,
1351 GpuMat& trainIdx, GpuMat& distance,
1352 const GpuMat& mask = GpuMat(), Stream& stream = Stream::Null());
1354 // Download trainIdx and distance and convert it to CPU vector with DMatch
1355 static void matchDownload(const GpuMat& trainIdx, const GpuMat& distance, std::vector<DMatch>& matches);
1356 // Convert trainIdx and distance to vector with DMatch
1357 static void matchConvert(const Mat& trainIdx, const Mat& distance, std::vector<DMatch>& matches);
1359 // Find one best match for each query descriptor
1360 void match(const GpuMat& query, const GpuMat& train, std::vector<DMatch>& matches, const GpuMat& mask = GpuMat());
1362 // Make gpu collection of trains and masks in suitable format for matchCollection function
1363 void makeGpuCollection(GpuMat& trainCollection, GpuMat& maskCollection, const std::vector<GpuMat>& masks = std::vector<GpuMat>());
1365 // Find one best match from train collection for each query descriptor
1366 void matchCollection(const GpuMat& query, const GpuMat& trainCollection,
1367 GpuMat& trainIdx, GpuMat& imgIdx, GpuMat& distance,
1368 const GpuMat& masks = GpuMat(), Stream& stream = Stream::Null());
1370 // Download trainIdx, imgIdx and distance and convert it to vector with DMatch
1371 static void matchDownload(const GpuMat& trainIdx, const GpuMat& imgIdx, const GpuMat& distance, std::vector<DMatch>& matches);
1372 // Convert trainIdx, imgIdx and distance to vector with DMatch
1373 static void matchConvert(const Mat& trainIdx, const Mat& imgIdx, const Mat& distance, std::vector<DMatch>& matches);
1375 // Find one best match from train collection for each query descriptor.
1376 void match(const GpuMat& query, std::vector<DMatch>& matches, const std::vector<GpuMat>& masks = std::vector<GpuMat>());
1378 // Find k best matches for each query descriptor (in increasing order of distances)
1379 void knnMatchSingle(const GpuMat& query, const GpuMat& train,
1380 GpuMat& trainIdx, GpuMat& distance, GpuMat& allDist, int k,
1381 const GpuMat& mask = GpuMat(), Stream& stream = Stream::Null());
1383 // Download trainIdx and distance and convert it to vector with DMatch
1384 // compactResult is used when mask is not empty. If compactResult is false matches
1385 // vector will have the same size as queryDescriptors rows. If compactResult is true
1386 // matches vector will not contain matches for fully masked out query descriptors.
1387 static void knnMatchDownload(const GpuMat& trainIdx, const GpuMat& distance,
1388 std::vector< std::vector<DMatch> >& matches, bool compactResult = false);
1389 // Convert trainIdx and distance to vector with DMatch
1390 static void knnMatchConvert(const Mat& trainIdx, const Mat& distance,
1391 std::vector< std::vector<DMatch> >& matches, bool compactResult = false);
1393 // Find k best matches for each query descriptor (in increasing order of distances).
1394 // compactResult is used when mask is not empty. If compactResult is false matches
1395 // vector will have the same size as queryDescriptors rows. If compactResult is true
1396 // matches vector will not contain matches for fully masked out query descriptors.
1397 void knnMatch(const GpuMat& query, const GpuMat& train,
1398 std::vector< std::vector<DMatch> >& matches, int k, const GpuMat& mask = GpuMat(),
1399 bool compactResult = false);
1401 // Find k best matches from train collection for each query descriptor (in increasing order of distances)
1402 void knnMatch2Collection(const GpuMat& query, const GpuMat& trainCollection,
1403 GpuMat& trainIdx, GpuMat& imgIdx, GpuMat& distance,
1404 const GpuMat& maskCollection = GpuMat(), Stream& stream = Stream::Null());
1406 // Download trainIdx and distance and convert it to vector with DMatch
1407 // compactResult is used when mask is not empty. If compactResult is false matches
1408 // vector will have the same size as queryDescriptors rows. If compactResult is true
1409 // matches vector will not contain matches for fully masked out query descriptors.
1410 static void knnMatch2Download(const GpuMat& trainIdx, const GpuMat& imgIdx, const GpuMat& distance,
1411 std::vector< std::vector<DMatch> >& matches, bool compactResult = false);
1412 // Convert trainIdx and distance to vector with DMatch
1413 static void knnMatch2Convert(const Mat& trainIdx, const Mat& imgIdx, const Mat& distance,
1414 std::vector< std::vector<DMatch> >& matches, bool compactResult = false);
1416 // Find k best matches for each query descriptor (in increasing order of distances).
1417 // compactResult is used when mask is not empty. If compactResult is false matches
1418 // vector will have the same size as queryDescriptors rows. If compactResult is true
1419 // matches vector will not contain matches for fully masked out query descriptors.
1420 void knnMatch(const GpuMat& query, std::vector< std::vector<DMatch> >& matches, int k,
1421 const std::vector<GpuMat>& masks = std::vector<GpuMat>(), bool compactResult = false);
1423 // Find best matches for each query descriptor which have distance less than maxDistance.
1424 // nMatches.at<int>(0, queryIdx) will contain matches count for queryIdx.
1425 // carefully nMatches can be greater than trainIdx.cols - it means that matcher didn't find all matches,
1426 // because it didn't have enough memory.
1427 // If trainIdx is empty, then trainIdx and distance will be created with size nQuery x max((nTrain / 100), 10),
1428 // otherwize user can pass own allocated trainIdx and distance with size nQuery x nMaxMatches
1429 // Matches doesn't sorted.
1430 void radiusMatchSingle(const GpuMat& query, const GpuMat& train,
1431 GpuMat& trainIdx, GpuMat& distance, GpuMat& nMatches, float maxDistance,
1432 const GpuMat& mask = GpuMat(), Stream& stream = Stream::Null());
1434 // Download trainIdx, nMatches and distance and convert it to vector with DMatch.
1435 // matches will be sorted in increasing order of distances.
1436 // compactResult is used when mask is not empty. If compactResult is false matches
1437 // vector will have the same size as queryDescriptors rows. If compactResult is true
1438 // matches vector will not contain matches for fully masked out query descriptors.
1439 static void radiusMatchDownload(const GpuMat& trainIdx, const GpuMat& distance, const GpuMat& nMatches,
1440 std::vector< std::vector<DMatch> >& matches, bool compactResult = false);
1441 // Convert trainIdx, nMatches and distance to vector with DMatch.
1442 static void radiusMatchConvert(const Mat& trainIdx, const Mat& distance, const Mat& nMatches,
1443 std::vector< std::vector<DMatch> >& matches, bool compactResult = false);
1445 // Find best matches for each query descriptor which have distance less than maxDistance
1446 // in increasing order of distances).
1447 void radiusMatch(const GpuMat& query, const GpuMat& train,
1448 std::vector< std::vector<DMatch> >& matches, float maxDistance,
1449 const GpuMat& mask = GpuMat(), bool compactResult = false);
1451 // Find best matches for each query descriptor which have distance less than maxDistance.
1452 // If trainIdx is empty, then trainIdx and distance will be created with size nQuery x max((nQuery / 100), 10),
1453 // otherwize user can pass own allocated trainIdx and distance with size nQuery x nMaxMatches
1454 // Matches doesn't sorted.
1455 void radiusMatchCollection(const GpuMat& query, GpuMat& trainIdx, GpuMat& imgIdx, GpuMat& distance, GpuMat& nMatches, float maxDistance,
1456 const std::vector<GpuMat>& masks = std::vector<GpuMat>(), Stream& stream = Stream::Null());
1458 // Download trainIdx, imgIdx, nMatches and distance and convert it to vector with DMatch.
1459 // matches will be sorted in increasing order of distances.
1460 // compactResult is used when mask is not empty. If compactResult is false matches
1461 // vector will have the same size as queryDescriptors rows. If compactResult is true
1462 // matches vector will not contain matches for fully masked out query descriptors.
1463 static void radiusMatchDownload(const GpuMat& trainIdx, const GpuMat& imgIdx, const GpuMat& distance, const GpuMat& nMatches,
1464 std::vector< std::vector<DMatch> >& matches, bool compactResult = false);
1465 // Convert trainIdx, nMatches and distance to vector with DMatch.
1466 static void radiusMatchConvert(const Mat& trainIdx, const Mat& imgIdx, const Mat& distance, const Mat& nMatches,
1467 std::vector< std::vector<DMatch> >& matches, bool compactResult = false);
1469 // Find best matches from train collection for each query descriptor which have distance less than
1470 // maxDistance (in increasing order of distances).
1471 void radiusMatch(const GpuMat& query, std::vector< std::vector<DMatch> >& matches, float maxDistance,
1472 const std::vector<GpuMat>& masks = std::vector<GpuMat>(), bool compactResult = false);
1477 std::vector<GpuMat> trainDescCollection;
1480 template <class Distance>
1481 class CV_EXPORTS BruteForceMatcher_GPU;
1483 template <typename T>
1484 class CV_EXPORTS BruteForceMatcher_GPU< L1<T> > : public BruteForceMatcher_GPU_base
1487 explicit BruteForceMatcher_GPU() : BruteForceMatcher_GPU_base(L1Dist) {}
1488 explicit BruteForceMatcher_GPU(L1<T> /*d*/) : BruteForceMatcher_GPU_base(L1Dist) {}
1490 template <typename T>
1491 class CV_EXPORTS BruteForceMatcher_GPU< L2<T> > : public BruteForceMatcher_GPU_base
1494 explicit BruteForceMatcher_GPU() : BruteForceMatcher_GPU_base(L2Dist) {}
1495 explicit BruteForceMatcher_GPU(L2<T> /*d*/) : BruteForceMatcher_GPU_base(L2Dist) {}
1497 template <> class CV_EXPORTS BruteForceMatcher_GPU< Hamming > : public BruteForceMatcher_GPU_base
1500 explicit BruteForceMatcher_GPU() : BruteForceMatcher_GPU_base(HammingDist) {}
1501 explicit BruteForceMatcher_GPU(Hamming /*d*/) : BruteForceMatcher_GPU_base(HammingDist) {}
1504 ////////////////////////////////// CascadeClassifier_GPU //////////////////////////////////////////
1505 // The cascade classifier class for object detection: supports old haar and new lbp xlm formats and nvbin for haar cascades olny.
1506 class CV_EXPORTS CascadeClassifier_GPU
1509 CascadeClassifier_GPU();
1510 CascadeClassifier_GPU(const std::string& filename);
1511 ~CascadeClassifier_GPU();
1514 bool load(const std::string& filename);
1517 /* returns number of detected objects */
1518 int detectMultiScale(const GpuMat& image, GpuMat& objectsBuf, double scaleFactor = 1.1, int minNeighbors = 4, Size minSize = Size());
1520 bool findLargestObject;
1521 bool visualizeInPlace;
1523 Size getClassifierSize() const;
1527 struct CascadeClassifierImpl;
1528 CascadeClassifierImpl* impl;
1531 friend class CascadeClassifier_GPU_LBP;
1534 ////////////////////////////////// SURF //////////////////////////////////////////
1536 class CV_EXPORTS SURF_GPU
1551 //! the default constructor
1553 //! the full constructor taking all the necessary parameters
1554 explicit SURF_GPU(double _hessianThreshold, int _nOctaves=4,
1555 int _nOctaveLayers=2, bool _extended=false, float _keypointsRatio=0.01f, bool _upright = false);
1557 //! returns the descriptor size in float's (64 or 128)
1558 int descriptorSize() const;
1560 //! upload host keypoints to device memory
1561 void uploadKeypoints(const vector<KeyPoint>& keypoints, GpuMat& keypointsGPU);
1562 //! download keypoints from device to host memory
1563 void downloadKeypoints(const GpuMat& keypointsGPU, vector<KeyPoint>& keypoints);
1565 //! download descriptors from device to host memory
1566 void downloadDescriptors(const GpuMat& descriptorsGPU, vector<float>& descriptors);
1568 //! finds the keypoints using fast hessian detector used in SURF
1569 //! supports CV_8UC1 images
1570 //! keypoints will have nFeature cols and 6 rows
1571 //! keypoints.ptr<float>(X_ROW)[i] will contain x coordinate of i'th feature
1572 //! keypoints.ptr<float>(Y_ROW)[i] will contain y coordinate of i'th feature
1573 //! keypoints.ptr<float>(LAPLACIAN_ROW)[i] will contain laplacian sign of i'th feature
1574 //! keypoints.ptr<float>(OCTAVE_ROW)[i] will contain octave of i'th feature
1575 //! keypoints.ptr<float>(SIZE_ROW)[i] will contain size of i'th feature
1576 //! keypoints.ptr<float>(ANGLE_ROW)[i] will contain orientation of i'th feature
1577 //! keypoints.ptr<float>(HESSIAN_ROW)[i] will contain response of i'th feature
1578 void operator()(const GpuMat& img, const GpuMat& mask, GpuMat& keypoints);
1579 //! finds the keypoints and computes their descriptors.
1580 //! Optionally it can compute descriptors for the user-provided keypoints and recompute keypoints direction
1581 void operator()(const GpuMat& img, const GpuMat& mask, GpuMat& keypoints, GpuMat& descriptors,
1582 bool useProvidedKeypoints = false);
1584 void operator()(const GpuMat& img, const GpuMat& mask, std::vector<KeyPoint>& keypoints);
1585 void operator()(const GpuMat& img, const GpuMat& mask, std::vector<KeyPoint>& keypoints, GpuMat& descriptors,
1586 bool useProvidedKeypoints = false);
1588 void operator()(const GpuMat& img, const GpuMat& mask, std::vector<KeyPoint>& keypoints, std::vector<float>& descriptors,
1589 bool useProvidedKeypoints = false);
1591 void releaseMemory();
1594 double hessianThreshold;
1600 //! max keypoints = min(keypointsRatio * img.size().area(), 65535)
1601 float keypointsRatio;
1603 GpuMat sum, mask1, maskSum, intBuffer;
1607 GpuMat maxPosBuffer;
1610 ////////////////////////////////// FAST //////////////////////////////////////////
1612 class CV_EXPORTS FAST_GPU
1622 // all features have same size
1623 static const int FEATURE_SIZE = 7;
1625 explicit FAST_GPU(int threshold, bool nonmaxSupression = true, double keypointsRatio = 0.05);
1627 //! finds the keypoints using FAST detector
1628 //! supports only CV_8UC1 images
1629 void operator ()(const GpuMat& image, const GpuMat& mask, GpuMat& keypoints);
1630 void operator ()(const GpuMat& image, const GpuMat& mask, std::vector<KeyPoint>& keypoints);
1632 //! download keypoints from device to host memory
1633 void downloadKeypoints(const GpuMat& d_keypoints, std::vector<KeyPoint>& keypoints);
1635 //! convert keypoints to KeyPoint vector
1636 void convertKeypoints(const Mat& h_keypoints, std::vector<KeyPoint>& keypoints);
1638 //! release temporary buffer's memory
1641 bool nonmaxSupression;
1645 //! max keypoints = keypointsRatio * img.size().area()
1646 double keypointsRatio;
1648 //! find keypoints and compute it's response if nonmaxSupression is true
1649 //! return count of detected keypoints
1650 int calcKeyPointsLocation(const GpuMat& image, const GpuMat& mask);
1652 //! get final array of keypoints
1653 //! performs nonmax supression if needed
1654 //! return final count of keypoints
1655 int getKeyPoints(GpuMat& keypoints);
1663 GpuMat d_keypoints_;
1666 ////////////////////////////////// ORB //////////////////////////////////////////
1668 class CV_EXPORTS ORB_GPU
1684 DEFAULT_FAST_THRESHOLD = 20
1688 explicit ORB_GPU(int nFeatures = 500, float scaleFactor = 1.2f, int nLevels = 8, int edgeThreshold = 31,
1689 int firstLevel = 0, int WTA_K = 2, int scoreType = 0, int patchSize = 31);
1691 //! Compute the ORB features on an image
1692 //! image - the image to compute the features (supports only CV_8UC1 images)
1693 //! mask - the mask to apply
1694 //! keypoints - the resulting keypoints
1695 void operator()(const GpuMat& image, const GpuMat& mask, std::vector<KeyPoint>& keypoints);
1696 void operator()(const GpuMat& image, const GpuMat& mask, GpuMat& keypoints);
1698 //! Compute the ORB features and descriptors on an image
1699 //! image - the image to compute the features (supports only CV_8UC1 images)
1700 //! mask - the mask to apply
1701 //! keypoints - the resulting keypoints
1702 //! descriptors - descriptors array
1703 void operator()(const GpuMat& image, const GpuMat& mask, std::vector<KeyPoint>& keypoints, GpuMat& descriptors);
1704 void operator()(const GpuMat& image, const GpuMat& mask, GpuMat& keypoints, GpuMat& descriptors);
1706 //! download keypoints from device to host memory
1707 void downloadKeyPoints(GpuMat& d_keypoints, std::vector<KeyPoint>& keypoints);
1709 //! convert keypoints to KeyPoint vector
1710 void convertKeyPoints(Mat& d_keypoints, std::vector<KeyPoint>& keypoints);
1712 //! returns the descriptor size in bytes
1713 inline int descriptorSize() const { return kBytes; }
1715 inline void setFastParams(int threshold, bool nonmaxSupression = true)
1717 fastDetector_.threshold = threshold;
1718 fastDetector_.nonmaxSupression = nonmaxSupression;
1721 //! release temporary buffer's memory
1724 //! if true, image will be blurred before descriptors calculation
1725 bool blurForDescriptor;
1728 enum { kBytes = 32 };
1730 void buildScalePyramids(const GpuMat& image, const GpuMat& mask);
1732 void computeKeyPointsPyramid();
1734 void computeDescriptors(GpuMat& descriptors);
1736 void mergeKeyPoints(GpuMat& keypoints);
1747 // The number of desired features per scale
1748 std::vector<size_t> n_features_per_level_;
1750 // Points to compute BRIEF descriptors from
1753 std::vector<GpuMat> imagePyr_;
1754 std::vector<GpuMat> maskPyr_;
1758 std::vector<GpuMat> keyPointsPyr_;
1759 std::vector<int> keyPointsCount_;
1761 FAST_GPU fastDetector_;
1763 Ptr<FilterEngine_GPU> blurFilter;
1765 GpuMat d_keypoints_;
1768 ////////////////////////////////// Optical Flow //////////////////////////////////////////
1770 class CV_EXPORTS BroxOpticalFlow
1773 BroxOpticalFlow(float alpha_, float gamma_, float scale_factor_, int inner_iterations_, int outer_iterations_, int solver_iterations_) :
1774 alpha(alpha_), gamma(gamma_), scale_factor(scale_factor_),
1775 inner_iterations(inner_iterations_), outer_iterations(outer_iterations_), solver_iterations(solver_iterations_)
1779 //! Compute optical flow
1780 //! frame0 - source frame (supports only CV_32FC1 type)
1781 //! frame1 - frame to track (with the same size and type as frame0)
1782 //! u - flow horizontal component (along x axis)
1783 //! v - flow vertical component (along y axis)
1784 void operator ()(const GpuMat& frame0, const GpuMat& frame1, GpuMat& u, GpuMat& v, Stream& stream = Stream::Null());
1789 //! gradient constancy importance
1792 //! pyramid scale factor
1795 //! number of lagged non-linearity iterations (inner loop)
1796 int inner_iterations;
1798 //! number of warping iterations (number of pyramid levels)
1799 int outer_iterations;
1801 //! number of linear system solver iterations
1802 int solver_iterations;
1807 class CV_EXPORTS GoodFeaturesToTrackDetector_GPU
1810 explicit GoodFeaturesToTrackDetector_GPU(int maxCorners = 1000, double qualityLevel = 0.01, double minDistance = 0.0,
1811 int blockSize = 3, bool useHarrisDetector = false, double harrisK = 0.04);
1813 //! return 1 rows matrix with CV_32FC2 type
1814 void operator ()(const GpuMat& image, GpuMat& corners, const GpuMat& mask = GpuMat());
1817 double qualityLevel;
1821 bool useHarrisDetector;
1824 void releaseMemory()
1830 minMaxbuf_.release();
1831 tmpCorners_.release();
1843 inline GoodFeaturesToTrackDetector_GPU::GoodFeaturesToTrackDetector_GPU(int maxCorners_, double qualityLevel_, double minDistance_,
1844 int blockSize_, bool useHarrisDetector_, double harrisK_)
1846 maxCorners = maxCorners_;
1847 qualityLevel = qualityLevel_;
1848 minDistance = minDistance_;
1849 blockSize = blockSize_;
1850 useHarrisDetector = useHarrisDetector_;
1855 class CV_EXPORTS PyrLKOpticalFlow
1860 winSize = Size(21, 21);
1864 useInitialFlow = false;
1865 minEigThreshold = 1e-4f;
1866 getMinEigenVals = false;
1867 isDeviceArch11_ = !DeviceInfo().supports(FEATURE_SET_COMPUTE_12);
1870 void sparse(const GpuMat& prevImg, const GpuMat& nextImg, const GpuMat& prevPts, GpuMat& nextPts,
1871 GpuMat& status, GpuMat* err = 0);
1873 void dense(const GpuMat& prevImg, const GpuMat& nextImg, GpuMat& u, GpuMat& v, GpuMat* err = 0);
1879 bool useInitialFlow;
1880 float minEigThreshold;
1881 bool getMinEigenVals;
1883 void releaseMemory()
1885 dx_calcBuf_.release();
1886 dy_calcBuf_.release();
1899 void calcSharrDeriv(const GpuMat& src, GpuMat& dx, GpuMat& dy);
1901 void buildImagePyramid(const GpuMat& img0, vector<GpuMat>& pyr, bool withBorder);
1906 vector<GpuMat> prevPyr_;
1907 vector<GpuMat> nextPyr_;
1912 vector<GpuMat> uPyr_;
1913 vector<GpuMat> vPyr_;
1915 bool isDeviceArch11_;
1919 class CV_EXPORTS FarnebackOpticalFlow
1922 FarnebackOpticalFlow()
1926 fastPyramids = false;
1932 isDeviceArch11_ = !DeviceInfo().supports(FEATURE_SET_COMPUTE_12);
1944 void operator ()(const GpuMat &frame0, const GpuMat &frame1, GpuMat &flowx, GpuMat &flowy, Stream &s = Stream::Null());
1946 void releaseMemory()
1948 frames_[0].release();
1949 frames_[1].release();
1950 pyrLevel_[0].release();
1951 pyrLevel_[1].release();
1956 blurredFrame_[0].release();
1957 blurredFrame_[1].release();
1963 void prepareGaussian(
1964 int n, double sigma, float *g, float *xg, float *xxg,
1965 double &ig11, double &ig03, double &ig33, double &ig55);
1967 void setPolynomialExpansionConsts(int n, double sigma);
1969 void updateFlow_boxFilter(
1970 const GpuMat& R0, const GpuMat& R1, GpuMat& flowx, GpuMat &flowy,
1971 GpuMat& M, GpuMat &bufM, int blockSize, bool updateMatrices, Stream streams[]);
1973 void updateFlow_gaussianBlur(
1974 const GpuMat& R0, const GpuMat& R1, GpuMat& flowx, GpuMat& flowy,
1975 GpuMat& M, GpuMat &bufM, int blockSize, bool updateMatrices, Stream streams[]);
1978 GpuMat pyrLevel_[2], M_, bufM_, R_[2], blurredFrame_[2];
1979 std::vector<GpuMat> pyramid0_, pyramid1_;
1981 bool isDeviceArch11_;
1985 // Implementation of the Zach, Pock and Bischof Dual TV-L1 Optical Flow method
1988 // [1] C. Zach, T. Pock and H. Bischof, "A Duality Based Approach for Realtime TV-L1 Optical Flow".
1989 // [2] Javier Sanchez, Enric Meinhardt-Llopis and Gabriele Facciolo. "TV-L1 Optical Flow Estimation".
1990 class CV_EXPORTS OpticalFlowDual_TVL1_GPU
1993 OpticalFlowDual_TVL1_GPU();
1995 void operator ()(const GpuMat& I0, const GpuMat& I1, GpuMat& flowx, GpuMat& flowy);
1997 void collectGarbage();
2000 * Time step of the numerical scheme.
2005 * Weight parameter for the data term, attachment parameter.
2006 * This is the most relevant parameter, which determines the smoothness of the output.
2007 * The smaller this parameter is, the smoother the solutions we obtain.
2008 * It depends on the range of motions of the images, so its value should be adapted to each image sequence.
2013 * Weight parameter for (u - v)^2, tightness parameter.
2014 * It serves as a link between the attachment and the regularization terms.
2015 * In theory, it should have a small value in order to maintain both parts in correspondence.
2016 * The method is stable for a large range of values of this parameter.
2021 * Number of scales used to create the pyramid of images.
2026 * Number of warpings per scale.
2027 * Represents the number of times that I1(x+u0) and grad( I1(x+u0) ) are computed per scale.
2028 * This is a parameter that assures the stability of the method.
2029 * It also affects the running time, so it is a compromise between speed and accuracy.
2034 * Stopping criterion threshold used in the numerical scheme, which is a trade-off between precision and running time.
2035 * A small value will yield more accurate solutions at the expense of a slower convergence.
2040 * Stopping criterion iterations number used in the numerical scheme.
2044 bool useInitialFlow;
2047 void procOneScale(const GpuMat& I0, const GpuMat& I1, GpuMat& u1, GpuMat& u2);
2049 std::vector<GpuMat> I0s;
2050 std::vector<GpuMat> I1s;
2051 std::vector<GpuMat> u1s;
2052 std::vector<GpuMat> u2s;
2074 //! Interpolate frames (images) using provided optical flow (displacement field).
2075 //! frame0 - frame 0 (32-bit floating point images, single channel)
2076 //! frame1 - frame 1 (the same type and size)
2077 //! fu - forward horizontal displacement
2078 //! fv - forward vertical displacement
2079 //! bu - backward horizontal displacement
2080 //! bv - backward vertical displacement
2081 //! pos - new frame position
2082 //! newFrame - new frame
2083 //! buf - temporary buffer, will have width x 6*height size, CV_32FC1 type and contain 6 GpuMat;
2084 //! occlusion masks 0, occlusion masks 1,
2085 //! interpolated forward flow 0, interpolated forward flow 1,
2086 //! interpolated backward flow 0, interpolated backward flow 1
2088 CV_EXPORTS void interpolateFrames(const GpuMat& frame0, const GpuMat& frame1,
2089 const GpuMat& fu, const GpuMat& fv,
2090 const GpuMat& bu, const GpuMat& bv,
2091 float pos, GpuMat& newFrame, GpuMat& buf,
2092 Stream& stream = Stream::Null());
2094 CV_EXPORTS void createOpticalFlowNeedleMap(const GpuMat& u, const GpuMat& v, GpuMat& vertex, GpuMat& colors);
2097 //////////////////////// Background/foreground segmentation ////////////////////////
2099 // Foreground Object Detection from Videos Containing Complex Background.
2100 // Liyuan Li, Weimin Huang, Irene Y.H. Gu, and Qi Tian.
2102 class CV_EXPORTS FGDStatModel
2105 struct CV_EXPORTS Params
2107 int Lc; // Quantized levels per 'color' component. Power of two, typically 32, 64 or 128.
2108 int N1c; // Number of color vectors used to model normal background color variation at a given pixel.
2109 int N2c; // Number of color vectors retained at given pixel. Must be > N1c, typically ~ 5/3 of N1c.
2110 // Used to allow the first N1c vectors to adapt over time to changing background.
2112 int Lcc; // Quantized levels per 'color co-occurrence' component. Power of two, typically 16, 32 or 64.
2113 int N1cc; // Number of color co-occurrence vectors used to model normal background color variation at a given pixel.
2114 int N2cc; // Number of color co-occurrence vectors retained at given pixel. Must be > N1cc, typically ~ 5/3 of N1cc.
2115 // Used to allow the first N1cc vectors to adapt over time to changing background.
2117 bool is_obj_without_holes; // If TRUE we ignore holes within foreground blobs. Defaults to TRUE.
2118 int perform_morphing; // Number of erode-dilate-erode foreground-blob cleanup iterations.
2119 // These erase one-pixel junk blobs and merge almost-touching blobs. Default value is 1.
2121 float alpha1; // How quickly we forget old background pixel values seen. Typically set to 0.1.
2122 float alpha2; // "Controls speed of feature learning". Depends on T. Typical value circa 0.005.
2123 float alpha3; // Alternate to alpha2, used (e.g.) for quicker initial convergence. Typical value 0.1.
2125 float delta; // Affects color and color co-occurrence quantization, typically set to 2.
2126 float T; // A percentage value which determines when new features can be recognized as new background. (Typically 0.9).
2127 float minArea; // Discard foreground blobs whose bounding box is smaller than this threshold.
2133 // out_cn - channels count in output result (can be 3 or 4)
2134 // 4-channels require more memory, but a bit faster
2135 explicit FGDStatModel(int out_cn = 3);
2136 explicit FGDStatModel(const cv::gpu::GpuMat& firstFrame, const Params& params = Params(), int out_cn = 3);
2140 void create(const cv::gpu::GpuMat& firstFrame, const Params& params = Params());
2143 int update(const cv::gpu::GpuMat& curFrame);
2145 //8UC3 or 8UC4 reference background image
2146 cv::gpu::GpuMat background;
2148 //8UC1 foreground image
2149 cv::gpu::GpuMat foreground;
2151 std::vector< std::vector<cv::Point> > foreground_regions;
2154 FGDStatModel(const FGDStatModel&);
2155 FGDStatModel& operator=(const FGDStatModel&);
2158 std::auto_ptr<Impl> impl_;
2162 Gaussian Mixture-based Backbround/Foreground Segmentation Algorithm
2164 The class implements the following algorithm:
2165 "An improved adaptive background mixture model for real-time tracking with shadow detection"
2166 P. KadewTraKuPong and R. Bowden,
2167 Proc. 2nd European Workshp on Advanced Video-Based Surveillance Systems, 2001."
2168 http://personal.ee.surrey.ac.uk/Personal/R.Bowden/publications/avbs01/avbs01.pdf
2170 class CV_EXPORTS MOG_GPU
2173 //! the default constructor
2174 MOG_GPU(int nmixtures = -1);
2176 //! re-initiaization method
2177 void initialize(Size frameSize, int frameType);
2179 //! the update operator
2180 void operator()(const GpuMat& frame, GpuMat& fgmask, float learningRate = 0.0f, Stream& stream = Stream::Null());
2182 //! computes a background image which are the mean of all background gaussians
2183 void getBackgroundImage(GpuMat& backgroundImage, Stream& stream = Stream::Null()) const;
2185 //! releases all inner buffers
2190 float backgroundRatio;
2207 The class implements the following algorithm:
2208 "Improved adaptive Gausian mixture model for background subtraction"
2210 International Conference Pattern Recognition, UK, August, 2004.
2211 http://www.zoranz.net/Publications/zivkovic2004ICPR.pdf
2213 class CV_EXPORTS MOG2_GPU
2216 //! the default constructor
2217 MOG2_GPU(int nmixtures = -1);
2219 //! re-initiaization method
2220 void initialize(Size frameSize, int frameType);
2222 //! the update operator
2223 void operator()(const GpuMat& frame, GpuMat& fgmask, float learningRate = -1.0f, Stream& stream = Stream::Null());
2225 //! computes a background image which are the mean of all background gaussians
2226 void getBackgroundImage(GpuMat& backgroundImage, Stream& stream = Stream::Null()) const;
2228 //! releases all inner buffers
2232 // you should call initialize after parameters changes
2236 //! here it is the maximum allowed number of mixture components.
2237 //! Actual number is determined dynamically per pixel
2239 // threshold on the squared Mahalanobis distance to decide if it is well described
2240 // by the background model or not. Related to Cthr from the paper.
2241 // This does not influence the update of the background. A typical value could be 4 sigma
2242 // and that is varThreshold=4*4=16; Corresponds to Tb in the paper.
2244 /////////////////////////
2245 // less important parameters - things you might change but be carefull
2246 ////////////////////////
2248 float backgroundRatio;
2249 // corresponds to fTB=1-cf from the paper
2250 // TB - threshold when the component becomes significant enough to be included into
2251 // the background model. It is the TB=1-cf from the paper. So I use cf=0.1 => TB=0.
2252 // For alpha=0.001 it means that the mode should exist for approximately 105 frames before
2253 // it is considered foreground
2254 // float noiseSigma;
2255 float varThresholdGen;
2257 //correspondts to Tg - threshold on the squared Mahalan. dist. to decide
2258 //when a sample is close to the existing components. If it is not close
2259 //to any a new component will be generated. I use 3 sigma => Tg=3*3=9.
2260 //Smaller Tg leads to more generated components and higher Tg might make
2261 //lead to small number of components but they can grow too large
2266 //initial variance for the newly generated components.
2267 //It will will influence the speed of adaptation. A good guess should be made.
2268 //A simple way is to estimate the typical standard deviation from the images.
2269 //I used here 10 as a reasonable value
2270 // min and max can be used to further control the variance
2271 float fCT; //CT - complexity reduction prior
2272 //this is related to the number of samples needed to accept that a component
2273 //actually exists. We use CT=0.05 of all the samples. By setting CT=0 you get
2274 //the standard Stauffer&Grimson algorithm (maybe not exact but very similar)
2276 //shadow detection parameters
2277 bool bShadowDetection; //default 1 - do shadow detection
2278 unsigned char nShadowDetection; //do shadow detection - insert this value as the detection result - 127 default value
2280 // Tau - shadow threshold. The shadow is detected if the pixel is darker
2281 //version of the background. Tau is a threshold on how much darker the shadow can be.
2282 //Tau= 0.5 means that if pixel is more than 2 times darker then it is not shadow
2283 //See: Prati,Mikic,Trivedi,Cucchiarra,"Detecting Moving Shadows...",IEEE PAMI,2003.
2296 GpuMat bgmodelUsedModes_; //keep track of number of modes per pixel
2300 * The class implements the following algorithm:
2301 * "ViBe: A universal background subtraction algorithm for video sequences"
2302 * O. Barnich and M. Van D Roogenbroeck
2303 * IEEE Transactions on Image Processing, 20(6) :1709-1724, June 2011
2305 class CV_EXPORTS VIBE_GPU
2308 //! the default constructor
2309 explicit VIBE_GPU(unsigned long rngSeed = 1234567);
2311 //! re-initiaization method
2312 void initialize(const GpuMat& firstFrame, Stream& stream = Stream::Null());
2314 //! the update operator
2315 void operator()(const GpuMat& frame, GpuMat& fgmask, Stream& stream = Stream::Null());
2317 //! releases all inner buffers
2320 int nbSamples; // number of samples per pixel
2321 int reqMatches; // #_min
2323 int subsamplingFactor; // amount of random subsampling
2328 unsigned long rngSeed_;
2335 * Background Subtractor module. Takes a series of images and returns a sequence of mask (8UC1)
2336 * images of the same size, where 255 indicates Foreground and 0 represents Background.
2337 * This class implements an algorithm described in "Visual Tracking of Human Visitors under
2338 * Variable-Lighting Conditions for a Responsive Audio Art Installation," A. Godbehere,
2339 * A. Matsukawa, K. Goldberg, American Control Conference, Montreal, June 2012.
2341 class CV_EXPORTS GMG_GPU
2347 * Validate parameters and set up data structures for appropriate frame size.
2348 * @param frameSize Input frame size
2349 * @param min Minimum value taken on by pixels in image sequence. Usually 0
2350 * @param max Maximum value taken on by pixels in image sequence. e.g. 1.0 or 255
2352 void initialize(Size frameSize, float min = 0.0f, float max = 255.0f);
2355 * Performs single-frame background subtraction and builds up a statistical background image
2357 * @param frame Input frame
2358 * @param fgmask Output mask image representing foreground and background pixels
2359 * @param stream Stream for the asynchronous version
2361 void operator ()(const GpuMat& frame, GpuMat& fgmask, float learningRate = -1.0f, Stream& stream = Stream::Null());
2363 //! Releases all inner buffers
2366 //! Total number of distinct colors to maintain in histogram.
2369 //! Set between 0.0 and 1.0, determines how quickly features are "forgotten" from histograms.
2372 //! Number of frames of video to use to initialize histograms.
2373 int numInitializationFrames;
2375 //! Number of discrete levels in each channel to be used in histograms.
2376 int quantizationLevels;
2378 //! Prior probability that any given pixel is a background pixel. A sensitivity parameter.
2379 float backgroundPrior;
2381 //! Value above which pixel is determined to be FG.
2382 float decisionThreshold;
2384 //! Smoothing radius, in pixels, for cleaning up FG image.
2385 int smoothingRadius;
2387 //! Perform background model update.
2388 bool updateBackgroundModel;
2391 float maxVal_, minVal_;
2401 Ptr<FilterEngine_GPU> boxFilter_;
2405 ////////////////////////////////// Video Encoding //////////////////////////////////
2407 // Works only under Windows
2408 // Supports olny H264 video codec and AVI files
2409 class CV_EXPORTS VideoWriter_GPU
2412 struct EncoderParams;
2414 // Callbacks for video encoder, use it if you want to work with raw video stream
2415 class EncoderCallBack;
2429 VideoWriter_GPU(const std::string& fileName, cv::Size frameSize, double fps, SurfaceFormat format = SF_BGR);
2430 VideoWriter_GPU(const std::string& fileName, cv::Size frameSize, double fps, const EncoderParams& params, SurfaceFormat format = SF_BGR);
2431 VideoWriter_GPU(const cv::Ptr<EncoderCallBack>& encoderCallback, cv::Size frameSize, double fps, SurfaceFormat format = SF_BGR);
2432 VideoWriter_GPU(const cv::Ptr<EncoderCallBack>& encoderCallback, cv::Size frameSize, double fps, const EncoderParams& params, SurfaceFormat format = SF_BGR);
2435 // all methods throws cv::Exception if error occurs
2436 void open(const std::string& fileName, cv::Size frameSize, double fps, SurfaceFormat format = SF_BGR);
2437 void open(const std::string& fileName, cv::Size frameSize, double fps, const EncoderParams& params, SurfaceFormat format = SF_BGR);
2438 void open(const cv::Ptr<EncoderCallBack>& encoderCallback, cv::Size frameSize, double fps, SurfaceFormat format = SF_BGR);
2439 void open(const cv::Ptr<EncoderCallBack>& encoderCallback, cv::Size frameSize, double fps, const EncoderParams& params, SurfaceFormat format = SF_BGR);
2441 bool isOpened() const;
2444 void write(const cv::gpu::GpuMat& image, bool lastFrame = false);
2446 struct CV_EXPORTS EncoderParams
2448 int P_Interval; // NVVE_P_INTERVAL,
2449 int IDR_Period; // NVVE_IDR_PERIOD,
2450 int DynamicGOP; // NVVE_DYNAMIC_GOP,
2451 int RCType; // NVVE_RC_TYPE,
2452 int AvgBitrate; // NVVE_AVG_BITRATE,
2453 int PeakBitrate; // NVVE_PEAK_BITRATE,
2454 int QP_Level_Intra; // NVVE_QP_LEVEL_INTRA,
2455 int QP_Level_InterP; // NVVE_QP_LEVEL_INTER_P,
2456 int QP_Level_InterB; // NVVE_QP_LEVEL_INTER_B,
2457 int DeblockMode; // NVVE_DEBLOCK_MODE,
2458 int ProfileLevel; // NVVE_PROFILE_LEVEL,
2459 int ForceIntra; // NVVE_FORCE_INTRA,
2460 int ForceIDR; // NVVE_FORCE_IDR,
2461 int ClearStat; // NVVE_CLEAR_STAT,
2462 int DIMode; // NVVE_SET_DEINTERLACE,
2463 int Presets; // NVVE_PRESETS,
2464 int DisableCabac; // NVVE_DISABLE_CABAC,
2465 int NaluFramingType; // NVVE_CONFIGURE_NALU_FRAMING_TYPE
2466 int DisableSPSPPS; // NVVE_DISABLE_SPS_PPS
2469 explicit EncoderParams(const std::string& configFile);
2471 void load(const std::string& configFile);
2472 void save(const std::string& configFile) const;
2475 EncoderParams getParams() const;
2477 class CV_EXPORTS EncoderCallBack
2487 virtual ~EncoderCallBack() {}
2489 // callback function to signal the start of bitstream that is to be encoded
2490 // must return pointer to buffer
2491 virtual uchar* acquireBitStream(int* bufferSize) = 0;
2493 // callback function to signal that the encoded bitstream is ready to be written to file
2494 virtual void releaseBitStream(unsigned char* data, int size) = 0;
2496 // callback function to signal that the encoding operation on the frame has started
2497 virtual void onBeginFrame(int frameNumber, PicType picType) = 0;
2499 // callback function signals that the encoding operation on the frame has finished
2500 virtual void onEndFrame(int frameNumber, PicType picType) = 0;
2504 VideoWriter_GPU(const VideoWriter_GPU&);
2505 VideoWriter_GPU& operator=(const VideoWriter_GPU&);
2508 std::auto_ptr<Impl> impl_;
2512 ////////////////////////////////// Video Decoding //////////////////////////////////////////
2520 class CV_EXPORTS VideoReader_GPU
2534 Uncompressed_YUV420 = (('I'<<24)|('Y'<<16)|('U'<<8)|('V')), // Y,U,V (4:2:0)
2535 Uncompressed_YV12 = (('Y'<<24)|('V'<<16)|('1'<<8)|('2')), // Y,V,U (4:2:0)
2536 Uncompressed_NV12 = (('N'<<24)|('V'<<16)|('1'<<8)|('2')), // Y,UV (4:2:0)
2537 Uncompressed_YUYV = (('Y'<<24)|('U'<<16)|('Y'<<8)|('V')), // YUYV/YUY2 (4:2:2)
2538 Uncompressed_UYVY = (('U'<<24)|('Y'<<16)|('V'<<8)|('Y')), // UYVY (4:2:2)
2552 ChromaFormat chromaFormat;
2560 explicit VideoReader_GPU(const std::string& filename);
2561 explicit VideoReader_GPU(const cv::Ptr<VideoSource>& source);
2565 void open(const std::string& filename);
2566 void open(const cv::Ptr<VideoSource>& source);
2567 bool isOpened() const;
2571 bool read(GpuMat& image);
2573 FormatInfo format() const;
2574 void dumpFormat(std::ostream& st);
2576 class CV_EXPORTS VideoSource
2579 VideoSource() : frameQueue_(0), videoParser_(0) {}
2580 virtual ~VideoSource() {}
2582 virtual FormatInfo format() const = 0;
2583 virtual void start() = 0;
2584 virtual void stop() = 0;
2585 virtual bool isStarted() const = 0;
2586 virtual bool hasError() const = 0;
2588 void setFrameQueue(detail::FrameQueue* frameQueue) { frameQueue_ = frameQueue; }
2589 void setVideoParser(detail::VideoParser* videoParser) { videoParser_ = videoParser; }
2592 bool parseVideoData(const uchar* data, size_t size, bool endOfStream = false);
2595 VideoSource(const VideoSource&);
2596 VideoSource& operator =(const VideoSource&);
2598 detail::FrameQueue* frameQueue_;
2599 detail::VideoParser* videoParser_;
2603 VideoReader_GPU(const VideoReader_GPU&);
2604 VideoReader_GPU& operator =(const VideoReader_GPU&);
2607 std::auto_ptr<Impl> impl_;
2610 //! removes points (CV_32FC2, single row matrix) with zero mask value
2611 CV_EXPORTS void compactPoints(GpuMat &points0, GpuMat &points1, const GpuMat &mask);
2613 CV_EXPORTS void calcWobbleSuppressionMaps(
2614 int left, int idx, int right, Size size, const Mat &ml, const Mat &mr,
2615 GpuMat &mapx, GpuMat &mapy);
2621 #endif /* __OPENCV_GPU_HPP__ */