added dual tvl1 optical flow gpu implementation
[profile/ivi/opencv.git] / modules / gpu / include / opencv2 / gpu / gpu.hpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                           License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
16 //
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
19 //
20 //   * Redistribution's of source code must retain the above copyright notice,
21 //     this list of conditions and the following disclaimer.
22 //
23 //   * Redistribution's in binary form must reproduce the above copyright notice,
24 //     this list of conditions and the following disclaimer in the documentation
25 //     and/or other GpuMaterials provided with the distribution.
26 //
27 //   * The name of the copyright holders may not be used to endorse or promote products
28 //     derived from this software without specific prior written permission.
29 //
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
40 //
41 //M*/
42
43 #ifndef __OPENCV_GPU_HPP__
44 #define __OPENCV_GPU_HPP__
45
46 #ifndef SKIP_INCLUDES
47 #include <vector>
48 #include <memory>
49 #include <iosfwd>
50 #endif
51
52 #include "opencv2/core/gpumat.hpp"
53 #include "opencv2/imgproc/imgproc.hpp"
54 #include "opencv2/objdetect/objdetect.hpp"
55 #include "opencv2/features2d/features2d.hpp"
56
57 namespace cv { namespace gpu {
58
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.
64
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);
69
70 class CV_EXPORTS CudaMem
71 {
72 public:
73     enum  { ALLOC_PAGE_LOCKED = 1, ALLOC_ZEROCOPY = 2, ALLOC_WRITE_COMBINED = 4 };
74
75     CudaMem();
76     CudaMem(const CudaMem& m);
77
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);
80
81
82     //! creates from cv::Mat with coping data
83     explicit CudaMem(const Mat& m, int alloc_type = ALLOC_PAGE_LOCKED);
84
85     ~CudaMem();
86
87     CudaMem& operator = (const CudaMem& m);
88
89     //! returns deep copy of the matrix, i.e. the data is copied
90     CudaMem clone() const;
91
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);
95
96     //! decrements reference counter and released memory if needed.
97     void release();
98
99     //! returns matrix header with disabled reference counting for CudaMem data.
100     Mat createMatHeader() const;
101     operator Mat() const;
102
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;
106
107     //returns if host memory can be mapperd to gpu address space;
108     static bool canMapHostMemory();
109
110     // Please see cv::Mat for descriptions
111     bool isContinuous() const;
112     size_t elemSize() const;
113     size_t elemSize1() const;
114     int type() const;
115     int depth() const;
116     int channels() const;
117     size_t step1() const;
118     Size size() const;
119     bool empty() const;
120
121
122     // Please see cv::Mat for descriptions
123     int flags;
124     int rows, cols;
125     size_t step;
126
127     uchar* data;
128     int* refcount;
129
130     uchar* datastart;
131     uchar* dataend;
132
133     int alloc_type;
134 };
135
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
140
141 class CV_EXPORTS Stream
142 {
143 public:
144     Stream();
145     ~Stream();
146
147     Stream(const Stream&);
148     Stream& operator=(const Stream&);
149
150     bool queryIfComplete();
151     void waitForCompletion();
152
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);
157
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);
162
163     void enqueueCopy(const GpuMat& src, GpuMat& dst);
164
165     void enqueueMemSet(GpuMat& src, Scalar val);
166     void enqueueMemSet(GpuMat& src, Scalar val, const GpuMat& mask);
167
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);
170
171     static Stream& Null();
172
173     operator bool() const;
174
175 private:
176     void create();
177     void release();
178
179     struct Impl;
180     Impl *impl;
181
182     friend struct StreamAccessor;
183
184     explicit Stream(Impl* impl);
185 };
186
187
188 //////////////////////////////// Filter Engine ////////////////////////////////
189
190 /*!
191 The Base Class for 1D or Row-wise Filters
192
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.
195 */
196 class CV_EXPORTS BaseRowFilter_GPU
197 {
198 public:
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;
202     int ksize, anchor;
203 };
204
205 /*!
206 The Base Class for Column-wise Filters
207
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.
210 */
211 class CV_EXPORTS BaseColumnFilter_GPU
212 {
213 public:
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;
217     int ksize, anchor;
218 };
219
220 /*!
221 The Base Class for Non-Separable 2D Filters.
222
223 This is the base class for linear or non-linear 2D filters.
224 */
225 class CV_EXPORTS BaseFilter_GPU
226 {
227 public:
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;
231     Size ksize;
232     Point anchor;
233 };
234
235 /*!
236 The Base Class for Filter Engine.
237
238 The class can be used to apply an arbitrary filtering operation to an image.
239 It contains all the necessary intermediate buffers.
240 */
241 class CV_EXPORTS FilterEngine_GPU
242 {
243 public:
244     virtual ~FilterEngine_GPU() {}
245
246     virtual void apply(const GpuMat& src, GpuMat& dst, Rect roi = Rect(0,0,-1,-1), Stream& stream = Stream::Null()) = 0;
247 };
248
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);
251
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);
257
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);
261
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);
265
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));
269
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));
273
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));
280
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);
286
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);
290
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);
294
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);
305
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);
316
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);
324
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);
330
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);
336
337 //! returns maximum filter
338 CV_EXPORTS Ptr<BaseFilter_GPU> getMaxFilter_GPU(int srcType, int dstType, const Size& ksize, Point anchor = Point(-1,-1));
339
340 //! returns minimum filter
341 CV_EXPORTS Ptr<BaseFilter_GPU> getMinFilter_GPU(int srcType, int dstType, const Size& ksize, Point anchor = Point(-1,-1));
342
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());
346
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())
349 {
350     boxFilter(src, dst, -1, ksize, anchor, stream);
351 }
352
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());
358
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());
364
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());
369
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());
372
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());
379
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());
385
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());
391
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());
397
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());
401
402
403 ////////////////////////////// Arithmetics ///////////////////////////////////
404
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());
408
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());
412
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());
416
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());
421
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());
424
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());
427
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());
430
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());
433
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());
437
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());
441
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());
445
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());
449
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());
453
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());
457
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());
461
462
463 //////////////////////////// Per-element operations ////////////////////////////////////
464
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());
469
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());
474
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());
479
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());
486
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());
490
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())
493 {
494     addWeighted(src1, alpha, src2, 1.0, 0.0, dst, -1, stream);
495 }
496
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());
501
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());
505
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());
509
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());
513
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());
517
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());
521
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());
527
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());
530
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());
533
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());
539
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());
545
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());
551
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());
555
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());
559
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());
562
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());
565
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());
568
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());
571
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};
574
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());
578
579
580 ////////////////////////////// Image processing //////////////////////////////
581
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());
587
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());
592
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());
597
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));
601
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());
606
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());
613
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());
616
617 //! swap channels
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
621 //!            channel order.
622 CV_EXPORTS void swapChannels(GpuMat& image, const int dstOrder[4], Stream& stream = Stream::Null());
623
624 //! Routines for correcting image color gamma
625 CV_EXPORTS void gammaCorrection(const GpuMat& src, GpuMat& dst, bool forward = true, Stream& stream = Stream::Null());
626
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());
629
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());
633
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());
638
639 CV_EXPORTS void buildWarpAffineMaps(const Mat& M, bool inverse, Size dsize, GpuMat& xmap, GpuMat& ymap, Stream& stream = Stream::Null());
640
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());
645
646 CV_EXPORTS void buildWarpPerspectiveMaps(const Mat& M, bool inverse, Size dsize, GpuMat& xmap, GpuMat& ymap, Stream& stream = Stream::Null());
647
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());
651
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());
655
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());
659
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());
665
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());
669
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());
674 //! buffered version
675 CV_EXPORTS void integralBuffered(const GpuMat& src, GpuMat& sum, GpuMat& buffer, Stream& stream = Stream::Null());
676
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());
681
682 //! computes vertical sum, supports only CV_32FC1 images
683 CV_EXPORTS void columnSum(const GpuMat& src, GpuMat& sum);
684
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());
689
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());
695
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());
701
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());
705
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());
709
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.
712 //!
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.
716 //!
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.
719 //!
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());
722
723 struct CV_EXPORTS ConvolveBuf
724 {
725     Size result_size;
726     Size block_size;
727     Size user_block_size;
728     Size dft_size;
729     int spect_len;
730
731     GpuMat image_spect, templ_spect, result_spect;
732     GpuMat image_block, templ_block, result_data;
733
734     void create(Size image_size, Size templ_size);
735     static Size estimateBlockSize(Size result_size, Size templ_size);
736 };
737
738
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());
744
745 struct CV_EXPORTS MatchTemplateBuf
746 {
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;
752 };
753
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());
756
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());
759
760 //! smoothes the source image and downsamples it
761 CV_EXPORTS void pyrDown(const GpuMat& src, GpuMat& dst, Stream& stream = Stream::Null());
762
763 //! upsamples the source image and then smoothes it
764 CV_EXPORTS void pyrUp(const GpuMat& src, GpuMat& dst, Stream& stream = Stream::Null());
765
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());
770
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());
774
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());
777
778 //! Fast (but approximate)version of non-local means algorith similar to CPU function (running sums technique)
779 class CV_EXPORTS FastNonLocalMeansDenoising
780 {
781 public:
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());
784
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());
787
788 private:
789
790     GpuMat buffer, extended_src_buffer;
791     GpuMat lab, l, ab;
792 };
793
794
795 struct CV_EXPORTS CannyBuf;
796
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);
801
802 struct CV_EXPORTS CannyBuf
803 {
804     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_);
807
808     void create(const Size& image_size, int apperture_size = 3);
809
810     void release();
811
812     GpuMat dx, dy;
813     GpuMat dx_buf, dy_buf;
814     GpuMat edgeBuf;
815     GpuMat trackBuf1, trackBuf2;
816     Ptr<FilterEngine_GPU> filterDX, filterDY;
817 };
818
819 class CV_EXPORTS ImagePyramid
820 {
821 public:
822     inline ImagePyramid() : nLayers_(0) {}
823     inline ImagePyramid(const GpuMat& img, int nLayers, Stream& stream = Stream::Null())
824     {
825         build(img, nLayers, stream);
826     }
827
828     void build(const GpuMat& img, int nLayers, Stream& stream = Stream::Null());
829
830     void getLayer(GpuMat& outImg, Size outRoi, Stream& stream = Stream::Null()) const;
831
832     inline void release()
833     {
834         layer0_.release();
835         pyramid_.clear();
836         nLayers_ = 0;
837     }
838
839 private:
840     GpuMat layer0_;
841     std::vector<GpuMat> pyramid_;
842     int nLayers_;
843 };
844
845 //! HoughLines
846
847 struct HoughLinesBuf
848 {
849     GpuMat accum;
850     GpuMat list;
851 };
852
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());
856
857 //! HoughCircles
858
859 struct HoughCirclesBuf
860 {
861     GpuMat edges;
862     GpuMat accum;
863     GpuMat list;
864     CannyBuf cannyBuf;
865 };
866
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);
870
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
875 {
876 public:
877     static Ptr<GeneralizedHough_GPU> create(int method);
878
879     virtual ~GeneralizedHough_GPU();
880
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));
884
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);
888
889     void download(const GpuMat& d_positions, OutputArray h_positions, OutputArray h_votes = noArray());
890
891     void release();
892
893 protected:
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;
897
898 private:
899     GpuMat edges_;
900     CannyBuf cannyBuf_;
901 };
902
903 ////////////////////////////// Matrix reductions //////////////////////////////
904
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);
908 //! buffered version
909 CV_EXPORTS void meanStdDev(const GpuMat& mtx, Scalar& mean, Scalar& stddev, GpuMat& buf);
910
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);
915
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);
920
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);
925
926 //! computes sum of array elements
927 //! supports only single channel images
928 CV_EXPORTS Scalar sum(const GpuMat& src);
929
930 //! computes sum of array elements
931 //! supports only single channel images
932 CV_EXPORTS Scalar sum(const GpuMat& src, GpuMat& buf);
933
934 //! computes sum of array elements absolute values
935 //! supports only single channel images
936 CV_EXPORTS Scalar absSum(const GpuMat& src);
937
938 //! computes sum of array elements absolute values
939 //! supports only single channel images
940 CV_EXPORTS Scalar absSum(const GpuMat& src, GpuMat& buf);
941
942 //! computes squared sum of array elements
943 //! supports only single channel images
944 CV_EXPORTS Scalar sqrSum(const GpuMat& src);
945
946 //! computes squared sum of array elements
947 //! supports only single channel images
948 CV_EXPORTS Scalar sqrSum(const GpuMat& src, GpuMat& buf);
949
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());
952
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);
955
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());
959
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);
963
964 //! counts non-zero array elements
965 CV_EXPORTS int countNonZero(const GpuMat& src);
966
967 //! counts non-zero array elements
968 CV_EXPORTS int countNonZero(const GpuMat& src, GpuMat& buf);
969
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());
972
973
974 ///////////////////////////// Calibration 3D //////////////////////////////////
975
976 CV_EXPORTS void transformPoints(const GpuMat& src, const Mat& rvec, const Mat& tvec,
977                                 GpuMat& dst, Stream& stream = Stream::Null());
978
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());
982
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);
987
988 //////////////////////////////// Image Labeling ////////////////////////////////
989
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());
993
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,
997                          GpuMat& labels,
998                          GpuMat& buf, Stream& stream = Stream::Null());
999
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());
1002
1003 //! performs connected componnents labeling.
1004 CV_EXPORTS void labelComponents(const GpuMat& mask, GpuMat& components, int flags = 0, Stream& stream = Stream::Null());
1005
1006 ////////////////////////////////// Histograms //////////////////////////////////
1007
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());
1034
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());
1039
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());
1044
1045 //////////////////////////////// StereoBM_GPU ////////////////////////////////
1046
1047 class CV_EXPORTS StereoBM_GPU
1048 {
1049 public:
1050     enum { BASIC_PRESET = 0, PREFILTER_XSOBEL = 1 };
1051
1052     enum { DEFAULT_NDISP = 64, DEFAULT_WINSZ = 19 };
1053
1054     //! the default constructor
1055     StereoBM_GPU();
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);
1058
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());
1062
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();
1067
1068     int preset;
1069     int ndisp;
1070     int winSize;
1071
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;
1077
1078 private:
1079     GpuMat minSSD, leBuf, riBuf;
1080 };
1081
1082 ////////////////////////// StereoBeliefPropagation ///////////////////////////
1083 // "Efficient Belief Propagation for Early Vision"
1084 // P.Felzenszwalb
1085
1086 class CV_EXPORTS StereoBeliefPropagation
1087 {
1088 public:
1089     enum { DEFAULT_NDISP  = 64 };
1090     enum { DEFAULT_ITERS  = 5  };
1091     enum { DEFAULT_LEVELS = 5  };
1092
1093     static void estimateRecommendedParams(int width, int height, int& ndisp, int& iters, int& levels);
1094
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);
1100
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);
1111
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());
1115
1116
1117     //! version for user specified data term
1118     void operator()(const GpuMat& data, GpuMat& disparity, Stream& stream = Stream::Null());
1119
1120     int ndisp;
1121
1122     int iters;
1123     int levels;
1124
1125     float max_data_term;
1126     float data_weight;
1127     float max_disc_term;
1128     float disc_single_jump;
1129
1130     int msg_type;
1131 private:
1132     GpuMat u, d, l, r, u2, d2, l2, r2;
1133     std::vector<GpuMat> datas;
1134     GpuMat out;
1135 };
1136
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/
1141
1142 class CV_EXPORTS StereoConstantSpaceBP
1143 {
1144 public:
1145     enum { DEFAULT_NDISP    = 128 };
1146     enum { DEFAULT_ITERS    = 8   };
1147     enum { DEFAULT_LEVELS   = 4   };
1148     enum { DEFAULT_NR_PLANE = 4   };
1149
1150     static void estimateRecommendedParams(int width, int height, int& ndisp, int& iters, int& levels, int& nr_plane);
1151
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);
1158
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);
1166
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());
1170
1171     int ndisp;
1172
1173     int iters;
1174     int levels;
1175
1176     int nr_plane;
1177
1178     float max_data_term;
1179     float data_weight;
1180     float max_disc_term;
1181     float disc_single_jump;
1182
1183     int min_disp_th;
1184
1185     int msg_type;
1186
1187     bool use_local_init_data_cost;
1188 private:
1189     GpuMat messages_buffers;
1190
1191     GpuMat temp;
1192     GpuMat out;
1193 };
1194
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/
1199
1200 class CV_EXPORTS DisparityBilateralFilter
1201 {
1202 public:
1203     enum { DEFAULT_NDISP  = 64 };
1204     enum { DEFAULT_RADIUS = 3 };
1205     enum { DEFAULT_ITERS  = 1 };
1206
1207     //! the default constructor
1208     explicit DisparityBilateralFilter(int ndisp = DEFAULT_NDISP, int radius = DEFAULT_RADIUS, int iters = DEFAULT_ITERS);
1209
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);
1214
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());
1218
1219 private:
1220     int ndisp;
1221     int radius;
1222     int iters;
1223
1224     float edge_threshold;
1225     float max_disc_threshold;
1226     float sigma_range;
1227
1228     GpuMat table_color;
1229     GpuMat table_space;
1230 };
1231
1232
1233 //////////////// HOG (Histogram-of-Oriented-Gradients) Descriptor and Object Detector //////////////
1234 struct CV_EXPORTS HOGConfidence
1235 {
1236    double scale;
1237    vector<Point> locations;
1238    vector<double> confidences;
1239    vector<double> part_scores[4];
1240 };
1241
1242 struct CV_EXPORTS HOGDescriptor
1243 {
1244     enum { DEFAULT_WIN_SIGMA = -1 };
1245     enum { DEFAULT_NLEVELS = 64 };
1246     enum { DESCR_FORMAT_ROW_BY_ROW, DESCR_FORMAT_COL_BY_COL };
1247
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);
1253
1254     size_t getDescriptorSize() const;
1255     size_t getBlockHistogramSize() const;
1256
1257     void setSVMDetector(const vector<float>& detector);
1258
1259     static vector<float> getDefaultPeopleDetector();
1260     static vector<float> getPeopleDetector48x96();
1261     static vector<float> getPeopleDetector64x128();
1262
1263     void detect(const GpuMat& img, vector<Point>& found_locations,
1264                 double hit_threshold=0, Size win_stride=Size(),
1265                 Size padding=Size());
1266
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);
1271
1272     void computeConfidence(const GpuMat& img, vector<Point>& hits, double hit_threshold,
1273                                                 Size win_stride, Size padding, vector<Point>& locations, vector<double>& confidences);
1274
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);
1278
1279     void getDescriptors(const GpuMat& img, Size win_stride,
1280                         GpuMat& descriptors,
1281                         int descr_format=DESCR_FORMAT_COL_BY_COL);
1282
1283     Size win_size;
1284     Size block_size;
1285     Size block_stride;
1286     Size cell_size;
1287     int nbins;
1288     double win_sigma;
1289     double threshold_L2hys;
1290     bool gamma_correction;
1291     int nlevels;
1292
1293 protected:
1294     void computeBlockHistograms(const GpuMat& img);
1295     void computeGradient(const GpuMat& img, GpuMat& grad, GpuMat& qangle);
1296
1297     double getWinSigma() const;
1298     bool checkDetectorSize() const;
1299
1300     static int numPartsWithin(int size, int part_size, int stride);
1301     static Size numPartsWithin(Size size, Size part_size, Size stride);
1302
1303     // Coefficients of the separating plane
1304     float free_coef;
1305     GpuMat detector;
1306
1307     // Results of the last classification step
1308     GpuMat labels, labels_buf;
1309     Mat labels_host;
1310
1311     // Results of the last histogram evaluation step
1312     GpuMat block_hists, block_hists_buf;
1313
1314     // Gradients conputation results
1315     GpuMat grad, qangle, grad_buf, qangle_buf;
1316
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);
1320
1321     std::vector<GpuMat> image_scales;
1322 };
1323
1324
1325 ////////////////////////////////// BruteForceMatcher //////////////////////////////////
1326
1327 class CV_EXPORTS BruteForceMatcher_GPU_base
1328 {
1329 public:
1330     enum DistType {L1Dist = 0, L2Dist, HammingDist};
1331
1332     explicit BruteForceMatcher_GPU_base(DistType distType = L2Dist);
1333
1334     // Add descriptors to train descriptor collection
1335     void add(const std::vector<GpuMat>& descCollection);
1336
1337     // Get train descriptors collection
1338     const std::vector<GpuMat>& getTrainDescriptors() const;
1339
1340     // Clear train descriptors collection
1341     void clear();
1342
1343     // Return true if there are not train descriptors in collection
1344     bool empty() const;
1345
1346     // Return true if the matcher supports mask in match methods
1347     bool isMaskSupported() const;
1348
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());
1353
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);
1358
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());
1361
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>());
1364
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());
1369
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);
1374
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>());
1377
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());
1382
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);
1392
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);
1400
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());
1405
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);
1415
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);
1422
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());
1433
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);
1444
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);
1450
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());
1457
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);
1468
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);
1473
1474     DistType distType;
1475
1476 private:
1477     std::vector<GpuMat> trainDescCollection;
1478 };
1479
1480 template <class Distance>
1481 class CV_EXPORTS BruteForceMatcher_GPU;
1482
1483 template <typename T>
1484 class CV_EXPORTS BruteForceMatcher_GPU< L1<T> > : public BruteForceMatcher_GPU_base
1485 {
1486 public:
1487     explicit BruteForceMatcher_GPU() : BruteForceMatcher_GPU_base(L1Dist) {}
1488     explicit BruteForceMatcher_GPU(L1<T> /*d*/) : BruteForceMatcher_GPU_base(L1Dist) {}
1489 };
1490 template <typename T>
1491 class CV_EXPORTS BruteForceMatcher_GPU< L2<T> > : public BruteForceMatcher_GPU_base
1492 {
1493 public:
1494     explicit BruteForceMatcher_GPU() : BruteForceMatcher_GPU_base(L2Dist) {}
1495     explicit BruteForceMatcher_GPU(L2<T> /*d*/) : BruteForceMatcher_GPU_base(L2Dist) {}
1496 };
1497 template <> class CV_EXPORTS BruteForceMatcher_GPU< Hamming > : public BruteForceMatcher_GPU_base
1498 {
1499 public:
1500     explicit BruteForceMatcher_GPU() : BruteForceMatcher_GPU_base(HammingDist) {}
1501     explicit BruteForceMatcher_GPU(Hamming /*d*/) : BruteForceMatcher_GPU_base(HammingDist) {}
1502 };
1503
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
1507 {
1508 public:
1509     CascadeClassifier_GPU();
1510     CascadeClassifier_GPU(const std::string& filename);
1511     ~CascadeClassifier_GPU();
1512
1513     bool empty() const;
1514     bool load(const std::string& filename);
1515     void release();
1516
1517     /* returns number of detected objects */
1518     int detectMultiScale(const GpuMat& image, GpuMat& objectsBuf, double scaleFactor = 1.1, int minNeighbors = 4, Size minSize = Size());
1519
1520     bool findLargestObject;
1521     bool visualizeInPlace;
1522
1523     Size getClassifierSize() const;
1524
1525 private:
1526
1527     struct CascadeClassifierImpl;
1528     CascadeClassifierImpl* impl;
1529     struct HaarCascade;
1530     struct LbpCascade;
1531     friend class CascadeClassifier_GPU_LBP;
1532 };
1533
1534 ////////////////////////////////// SURF //////////////////////////////////////////
1535
1536 class CV_EXPORTS SURF_GPU
1537 {
1538 public:
1539     enum KeypointLayout
1540     {
1541         X_ROW = 0,
1542         Y_ROW,
1543         LAPLACIAN_ROW,
1544         OCTAVE_ROW,
1545         SIZE_ROW,
1546         ANGLE_ROW,
1547         HESSIAN_ROW,
1548         ROWS_COUNT
1549     };
1550
1551     //! the default constructor
1552     SURF_GPU();
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);
1556
1557     //! returns the descriptor size in float's (64 or 128)
1558     int descriptorSize() const;
1559
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);
1564
1565     //! download descriptors from device to host memory
1566     void downloadDescriptors(const GpuMat& descriptorsGPU, vector<float>& descriptors);
1567
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);
1583
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);
1587
1588     void operator()(const GpuMat& img, const GpuMat& mask, std::vector<KeyPoint>& keypoints, std::vector<float>& descriptors,
1589         bool useProvidedKeypoints = false);
1590
1591     void releaseMemory();
1592
1593     // SURF parameters
1594     double hessianThreshold;
1595     int nOctaves;
1596     int nOctaveLayers;
1597     bool extended;
1598     bool upright;
1599
1600     //! max keypoints = min(keypointsRatio * img.size().area(), 65535)
1601     float keypointsRatio;
1602
1603     GpuMat sum, mask1, maskSum, intBuffer;
1604
1605     GpuMat det, trace;
1606
1607     GpuMat maxPosBuffer;
1608 };
1609
1610 ////////////////////////////////// FAST //////////////////////////////////////////
1611
1612 class CV_EXPORTS FAST_GPU
1613 {
1614 public:
1615     enum
1616     {
1617         LOCATION_ROW = 0,
1618         RESPONSE_ROW,
1619         ROWS_COUNT
1620     };
1621
1622     // all features have same size
1623     static const int FEATURE_SIZE = 7;
1624
1625     explicit FAST_GPU(int threshold, bool nonmaxSupression = true, double keypointsRatio = 0.05);
1626
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);
1631
1632     //! download keypoints from device to host memory
1633     void downloadKeypoints(const GpuMat& d_keypoints, std::vector<KeyPoint>& keypoints);
1634
1635     //! convert keypoints to KeyPoint vector
1636     void convertKeypoints(const Mat& h_keypoints, std::vector<KeyPoint>& keypoints);
1637
1638     //! release temporary buffer's memory
1639     void release();
1640
1641     bool nonmaxSupression;
1642
1643     int threshold;
1644
1645     //! max keypoints = keypointsRatio * img.size().area()
1646     double keypointsRatio;
1647
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);
1651
1652     //! get final array of keypoints
1653     //! performs nonmax supression if needed
1654     //! return final count of keypoints
1655     int getKeyPoints(GpuMat& keypoints);
1656
1657 private:
1658     GpuMat kpLoc_;
1659     int count_;
1660
1661     GpuMat score_;
1662
1663     GpuMat d_keypoints_;
1664 };
1665
1666 ////////////////////////////////// ORB //////////////////////////////////////////
1667
1668 class CV_EXPORTS ORB_GPU
1669 {
1670 public:
1671     enum
1672     {
1673         X_ROW = 0,
1674         Y_ROW,
1675         RESPONSE_ROW,
1676         ANGLE_ROW,
1677         OCTAVE_ROW,
1678         SIZE_ROW,
1679         ROWS_COUNT
1680     };
1681
1682     enum
1683     {
1684         DEFAULT_FAST_THRESHOLD = 20
1685     };
1686
1687     //! Constructor
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);
1690
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);
1697
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);
1705
1706     //! download keypoints from device to host memory
1707     void downloadKeyPoints(GpuMat& d_keypoints, std::vector<KeyPoint>& keypoints);
1708
1709     //! convert keypoints to KeyPoint vector
1710     void convertKeyPoints(Mat& d_keypoints, std::vector<KeyPoint>& keypoints);
1711
1712     //! returns the descriptor size in bytes
1713     inline int descriptorSize() const { return kBytes; }
1714
1715     inline void setFastParams(int threshold, bool nonmaxSupression = true)
1716     {
1717         fastDetector_.threshold = threshold;
1718         fastDetector_.nonmaxSupression = nonmaxSupression;
1719     }
1720
1721     //! release temporary buffer's memory
1722     void release();
1723
1724     //! if true, image will be blurred before descriptors calculation
1725     bool blurForDescriptor;
1726
1727 private:
1728     enum { kBytes = 32 };
1729
1730     void buildScalePyramids(const GpuMat& image, const GpuMat& mask);
1731
1732     void computeKeyPointsPyramid();
1733
1734     void computeDescriptors(GpuMat& descriptors);
1735
1736     void mergeKeyPoints(GpuMat& keypoints);
1737
1738     int nFeatures_;
1739     float scaleFactor_;
1740     int nLevels_;
1741     int edgeThreshold_;
1742     int firstLevel_;
1743     int WTA_K_;
1744     int scoreType_;
1745     int patchSize_;
1746
1747     // The number of desired features per scale
1748     std::vector<size_t> n_features_per_level_;
1749
1750     // Points to compute BRIEF descriptors from
1751     GpuMat pattern_;
1752
1753     std::vector<GpuMat> imagePyr_;
1754     std::vector<GpuMat> maskPyr_;
1755
1756     GpuMat buf_;
1757
1758     std::vector<GpuMat> keyPointsPyr_;
1759     std::vector<int> keyPointsCount_;
1760
1761     FAST_GPU fastDetector_;
1762
1763     Ptr<FilterEngine_GPU> blurFilter;
1764
1765     GpuMat d_keypoints_;
1766 };
1767
1768 ////////////////////////////////// Optical Flow //////////////////////////////////////////
1769
1770 class CV_EXPORTS BroxOpticalFlow
1771 {
1772 public:
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_)
1776     {
1777     }
1778
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());
1785
1786     //! flow smoothness
1787     float alpha;
1788
1789     //! gradient constancy importance
1790     float gamma;
1791
1792     //! pyramid scale factor
1793     float scale_factor;
1794
1795     //! number of lagged non-linearity iterations (inner loop)
1796     int inner_iterations;
1797
1798     //! number of warping iterations (number of pyramid levels)
1799     int outer_iterations;
1800
1801     //! number of linear system solver iterations
1802     int solver_iterations;
1803
1804     GpuMat buf;
1805 };
1806
1807 class CV_EXPORTS GoodFeaturesToTrackDetector_GPU
1808 {
1809 public:
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);
1812
1813     //! return 1 rows matrix with CV_32FC2 type
1814     void operator ()(const GpuMat& image, GpuMat& corners, const GpuMat& mask = GpuMat());
1815
1816     int maxCorners;
1817     double qualityLevel;
1818     double minDistance;
1819
1820     int blockSize;
1821     bool useHarrisDetector;
1822     double harrisK;
1823
1824     void releaseMemory()
1825     {
1826         Dx_.release();
1827         Dy_.release();
1828         buf_.release();
1829         eig_.release();
1830         minMaxbuf_.release();
1831         tmpCorners_.release();
1832     }
1833
1834 private:
1835     GpuMat Dx_;
1836     GpuMat Dy_;
1837     GpuMat buf_;
1838     GpuMat eig_;
1839     GpuMat minMaxbuf_;
1840     GpuMat tmpCorners_;
1841 };
1842
1843 inline GoodFeaturesToTrackDetector_GPU::GoodFeaturesToTrackDetector_GPU(int maxCorners_, double qualityLevel_, double minDistance_,
1844         int blockSize_, bool useHarrisDetector_, double harrisK_)
1845 {
1846     maxCorners = maxCorners_;
1847     qualityLevel = qualityLevel_;
1848     minDistance = minDistance_;
1849     blockSize = blockSize_;
1850     useHarrisDetector = useHarrisDetector_;
1851     harrisK = harrisK_;
1852 }
1853
1854
1855 class CV_EXPORTS PyrLKOpticalFlow
1856 {
1857 public:
1858     PyrLKOpticalFlow()
1859     {
1860         winSize = Size(21, 21);
1861         maxLevel = 3;
1862         iters = 30;
1863         derivLambda = 0.5;
1864         useInitialFlow = false;
1865         minEigThreshold = 1e-4f;
1866         getMinEigenVals = false;
1867         isDeviceArch11_ = !DeviceInfo().supports(FEATURE_SET_COMPUTE_12);
1868     }
1869
1870     void sparse(const GpuMat& prevImg, const GpuMat& nextImg, const GpuMat& prevPts, GpuMat& nextPts,
1871         GpuMat& status, GpuMat* err = 0);
1872
1873     void dense(const GpuMat& prevImg, const GpuMat& nextImg, GpuMat& u, GpuMat& v, GpuMat* err = 0);
1874
1875     Size winSize;
1876     int maxLevel;
1877     int iters;
1878     double derivLambda;
1879     bool useInitialFlow;
1880     float minEigThreshold;
1881     bool getMinEigenVals;
1882
1883     void releaseMemory()
1884     {
1885         dx_calcBuf_.release();
1886         dy_calcBuf_.release();
1887
1888         prevPyr_.clear();
1889         nextPyr_.clear();
1890
1891         dx_buf_.release();
1892         dy_buf_.release();
1893
1894         uPyr_.clear();
1895         vPyr_.clear();
1896     }
1897
1898 private:
1899     void calcSharrDeriv(const GpuMat& src, GpuMat& dx, GpuMat& dy);
1900
1901     void buildImagePyramid(const GpuMat& img0, vector<GpuMat>& pyr, bool withBorder);
1902
1903     GpuMat dx_calcBuf_;
1904     GpuMat dy_calcBuf_;
1905
1906     vector<GpuMat> prevPyr_;
1907     vector<GpuMat> nextPyr_;
1908
1909     GpuMat dx_buf_;
1910     GpuMat dy_buf_;
1911
1912     vector<GpuMat> uPyr_;
1913     vector<GpuMat> vPyr_;
1914
1915     bool isDeviceArch11_;
1916 };
1917
1918
1919 class CV_EXPORTS FarnebackOpticalFlow
1920 {
1921 public:
1922     FarnebackOpticalFlow()
1923     {
1924         numLevels = 5;
1925         pyrScale = 0.5;
1926         fastPyramids = false;
1927         winSize = 13;
1928         numIters = 10;
1929         polyN = 5;
1930         polySigma = 1.1;
1931         flags = 0;
1932         isDeviceArch11_ = !DeviceInfo().supports(FEATURE_SET_COMPUTE_12);
1933     }
1934
1935     int numLevels;
1936     double pyrScale;
1937     bool fastPyramids;
1938     int winSize;
1939     int numIters;
1940     int polyN;
1941     double polySigma;
1942     int flags;
1943
1944     void operator ()(const GpuMat &frame0, const GpuMat &frame1, GpuMat &flowx, GpuMat &flowy, Stream &s = Stream::Null());
1945
1946     void releaseMemory()
1947     {
1948         frames_[0].release();
1949         frames_[1].release();
1950         pyrLevel_[0].release();
1951         pyrLevel_[1].release();
1952         M_.release();
1953         bufM_.release();
1954         R_[0].release();
1955         R_[1].release();
1956         blurredFrame_[0].release();
1957         blurredFrame_[1].release();
1958         pyramid0_.clear();
1959         pyramid1_.clear();
1960     }
1961
1962 private:
1963     void prepareGaussian(
1964             int n, double sigma, float *g, float *xg, float *xxg,
1965             double &ig11, double &ig03, double &ig33, double &ig55);
1966
1967     void setPolynomialExpansionConsts(int n, double sigma);
1968
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[]);
1972
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[]);
1976
1977     GpuMat frames_[2];
1978     GpuMat pyrLevel_[2], M_, bufM_, R_[2], blurredFrame_[2];
1979     std::vector<GpuMat> pyramid0_, pyramid1_;
1980
1981     bool isDeviceArch11_;
1982 };
1983
1984
1985 // Implementation of the Zach, Pock and Bischof Dual TV-L1 Optical Flow method
1986 //
1987 // see reference:
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
1991 {
1992 public:
1993     OpticalFlowDual_TVL1_GPU();
1994
1995     void operator ()(const GpuMat& I0, const GpuMat& I1, GpuMat& flowx, GpuMat& flowy);
1996
1997     void collectGarbage();
1998
1999     /**
2000      * Time step of the numerical scheme.
2001      */
2002     double tau;
2003
2004     /**
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.
2009      */
2010     double lambda;
2011
2012     /**
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.
2017      */
2018     double theta;
2019
2020     /**
2021      * Number of scales used to create the pyramid of images.
2022      */
2023     int nscales;
2024
2025     /**
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.
2030      */
2031     int warps;
2032
2033     /**
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.
2036      */
2037     double epsilon;
2038
2039     /**
2040      * Stopping criterion iterations number used in the numerical scheme.
2041      */
2042     int iterations;
2043
2044     bool useInitialFlow;
2045
2046 private:
2047     void procOneScale(const GpuMat& I0, const GpuMat& I1, GpuMat& u1, GpuMat& u2);
2048
2049     std::vector<GpuMat> I0s;
2050     std::vector<GpuMat> I1s;
2051     std::vector<GpuMat> u1s;
2052     std::vector<GpuMat> u2s;
2053
2054     GpuMat I1x_buf;
2055     GpuMat I1y_buf;
2056
2057     GpuMat I1w_buf;
2058     GpuMat I1wx_buf;
2059     GpuMat I1wy_buf;
2060
2061     GpuMat grad_buf;
2062     GpuMat rho_c_buf;
2063
2064     GpuMat p11_buf;
2065     GpuMat p12_buf;
2066     GpuMat p21_buf;
2067     GpuMat p22_buf;
2068
2069     GpuMat diff_buf;
2070     GpuMat norm_buf;
2071 };
2072
2073
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
2087 //!
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());
2093
2094 CV_EXPORTS void createOpticalFlowNeedleMap(const GpuMat& u, const GpuMat& v, GpuMat& vertex, GpuMat& colors);
2095
2096
2097 //////////////////////// Background/foreground segmentation ////////////////////////
2098
2099 // Foreground Object Detection from Videos Containing Complex Background.
2100 // Liyuan Li, Weimin Huang, Irene Y.H. Gu, and Qi Tian.
2101 // ACM MM2003 9p
2102 class CV_EXPORTS FGDStatModel
2103 {
2104 public:
2105     struct CV_EXPORTS Params
2106     {
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.
2111
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.
2116
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.
2120
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.
2124
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.
2128
2129         // default Params
2130         Params();
2131     };
2132
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);
2137
2138     ~FGDStatModel();
2139
2140     void create(const cv::gpu::GpuMat& firstFrame, const Params& params = Params());
2141     void release();
2142
2143     int update(const cv::gpu::GpuMat& curFrame);
2144
2145     //8UC3 or 8UC4 reference background image
2146     cv::gpu::GpuMat background;
2147
2148     //8UC1 foreground image
2149     cv::gpu::GpuMat foreground;
2150
2151     std::vector< std::vector<cv::Point> > foreground_regions;
2152
2153 private:
2154     FGDStatModel(const FGDStatModel&);
2155     FGDStatModel& operator=(const FGDStatModel&);
2156
2157     class Impl;
2158     std::auto_ptr<Impl> impl_;
2159 };
2160
2161 /*!
2162  Gaussian Mixture-based Backbround/Foreground Segmentation Algorithm
2163
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
2169 */
2170 class CV_EXPORTS MOG_GPU
2171 {
2172 public:
2173     //! the default constructor
2174     MOG_GPU(int nmixtures = -1);
2175
2176     //! re-initiaization method
2177     void initialize(Size frameSize, int frameType);
2178
2179     //! the update operator
2180     void operator()(const GpuMat& frame, GpuMat& fgmask, float learningRate = 0.0f, Stream& stream = Stream::Null());
2181
2182     //! computes a background image which are the mean of all background gaussians
2183     void getBackgroundImage(GpuMat& backgroundImage, Stream& stream = Stream::Null()) const;
2184
2185     //! releases all inner buffers
2186     void release();
2187
2188     int history;
2189     float varThreshold;
2190     float backgroundRatio;
2191     float noiseSigma;
2192
2193 private:
2194     int nmixtures_;
2195
2196     Size frameSize_;
2197     int frameType_;
2198     int nframes_;
2199
2200     GpuMat weight_;
2201     GpuMat sortKey_;
2202     GpuMat mean_;
2203     GpuMat var_;
2204 };
2205
2206 /*!
2207  The class implements the following algorithm:
2208  "Improved adaptive Gausian mixture model for background subtraction"
2209  Z.Zivkovic
2210  International Conference Pattern Recognition, UK, August, 2004.
2211  http://www.zoranz.net/Publications/zivkovic2004ICPR.pdf
2212 */
2213 class CV_EXPORTS MOG2_GPU
2214 {
2215 public:
2216     //! the default constructor
2217     MOG2_GPU(int nmixtures = -1);
2218
2219     //! re-initiaization method
2220     void initialize(Size frameSize, int frameType);
2221
2222     //! the update operator
2223     void operator()(const GpuMat& frame, GpuMat& fgmask, float learningRate = -1.0f, Stream& stream = Stream::Null());
2224
2225     //! computes a background image which are the mean of all background gaussians
2226     void getBackgroundImage(GpuMat& backgroundImage, Stream& stream = Stream::Null()) const;
2227
2228     //! releases all inner buffers
2229     void release();
2230
2231     // parameters
2232     // you should call initialize after parameters changes
2233
2234     int history;
2235
2236     //! here it is the maximum allowed number of mixture components.
2237     //! Actual number is determined dynamically per pixel
2238     float varThreshold;
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.
2243
2244     /////////////////////////
2245     // less important parameters - things you might change but be carefull
2246     ////////////////////////
2247
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;
2256
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
2262     float fVarInit;
2263     float fVarMin;
2264     float fVarMax;
2265
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)
2275
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
2279     float fTau;
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.
2284
2285 private:
2286     int nmixtures_;
2287
2288     Size frameSize_;
2289     int frameType_;
2290     int nframes_;
2291
2292     GpuMat weight_;
2293     GpuMat variance_;
2294     GpuMat mean_;
2295
2296     GpuMat bgmodelUsedModes_; //keep track of number of modes per pixel
2297 };
2298
2299 /*!
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
2304  */
2305 class CV_EXPORTS VIBE_GPU
2306 {
2307 public:
2308     //! the default constructor
2309     explicit VIBE_GPU(unsigned long rngSeed = 1234567);
2310
2311     //! re-initiaization method
2312     void initialize(const GpuMat& firstFrame, Stream& stream = Stream::Null());
2313
2314     //! the update operator
2315     void operator()(const GpuMat& frame, GpuMat& fgmask, Stream& stream = Stream::Null());
2316
2317     //! releases all inner buffers
2318     void release();
2319
2320     int nbSamples;         // number of samples per pixel
2321     int reqMatches;        // #_min
2322     int radius;            // R
2323     int subsamplingFactor; // amount of random subsampling
2324
2325 private:
2326     Size frameSize_;
2327
2328     unsigned long rngSeed_;
2329     GpuMat randStates_;
2330
2331     GpuMat samples_;
2332 };
2333
2334 /**
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.
2340  */
2341 class CV_EXPORTS GMG_GPU
2342 {
2343 public:
2344     GMG_GPU();
2345
2346     /**
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
2351      */
2352     void initialize(Size frameSize, float min = 0.0f, float max = 255.0f);
2353
2354     /**
2355      * Performs single-frame background subtraction and builds up a statistical background image
2356      * model.
2357      * @param frame        Input frame
2358      * @param fgmask       Output mask image representing foreground and background pixels
2359      * @param stream       Stream for the asynchronous version
2360      */
2361     void operator ()(const GpuMat& frame, GpuMat& fgmask, float learningRate = -1.0f, Stream& stream = Stream::Null());
2362
2363     //! Releases all inner buffers
2364     void release();
2365
2366     //! Total number of distinct colors to maintain in histogram.
2367     int maxFeatures;
2368
2369     //! Set between 0.0 and 1.0, determines how quickly features are "forgotten" from histograms.
2370     float learningRate;
2371
2372     //! Number of frames of video to use to initialize histograms.
2373     int numInitializationFrames;
2374
2375     //! Number of discrete levels in each channel to be used in histograms.
2376     int quantizationLevels;
2377
2378     //! Prior probability that any given pixel is a background pixel. A sensitivity parameter.
2379     float backgroundPrior;
2380
2381     //! Value above which pixel is determined to be FG.
2382     float decisionThreshold;
2383
2384     //! Smoothing radius, in pixels, for cleaning up FG image.
2385     int smoothingRadius;
2386
2387     //! Perform background model update.
2388     bool updateBackgroundModel;
2389
2390 private:
2391     float maxVal_, minVal_;
2392
2393     Size frameSize_;
2394
2395     int frameNum_;
2396
2397     GpuMat nfeatures_;
2398     GpuMat colors_;
2399     GpuMat weights_;
2400
2401     Ptr<FilterEngine_GPU> boxFilter_;
2402     GpuMat buf_;
2403 };
2404
2405 ////////////////////////////////// Video Encoding //////////////////////////////////
2406
2407 // Works only under Windows
2408 // Supports olny H264 video codec and AVI files
2409 class CV_EXPORTS VideoWriter_GPU
2410 {
2411 public:
2412     struct EncoderParams;
2413
2414     // Callbacks for video encoder, use it if you want to work with raw video stream
2415     class EncoderCallBack;
2416
2417     enum SurfaceFormat
2418     {
2419         SF_UYVY = 0,
2420         SF_YUY2,
2421         SF_YV12,
2422         SF_NV12,
2423         SF_IYUV,
2424         SF_BGR,
2425         SF_GRAY = SF_BGR
2426     };
2427
2428     VideoWriter_GPU();
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);
2433     ~VideoWriter_GPU();
2434
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);
2440
2441     bool isOpened() const;
2442     void close();
2443
2444     void write(const cv::gpu::GpuMat& image, bool lastFrame = false);
2445
2446     struct CV_EXPORTS EncoderParams
2447     {
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
2467
2468         EncoderParams();
2469         explicit EncoderParams(const std::string& configFile);
2470
2471         void load(const std::string& configFile);
2472         void save(const std::string& configFile) const;
2473     };
2474
2475     EncoderParams getParams() const;
2476
2477     class CV_EXPORTS EncoderCallBack
2478     {
2479     public:
2480         enum PicType
2481         {
2482             IFRAME = 1,
2483             PFRAME = 2,
2484             BFRAME = 3
2485         };
2486
2487         virtual ~EncoderCallBack() {}
2488
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;
2492
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;
2495
2496         // callback function to signal that the encoding operation on the frame has started
2497         virtual void onBeginFrame(int frameNumber, PicType picType) = 0;
2498
2499         // callback function signals that the encoding operation on the frame has finished
2500         virtual void onEndFrame(int frameNumber, PicType picType) = 0;
2501     };
2502
2503 private:
2504     VideoWriter_GPU(const VideoWriter_GPU&);
2505     VideoWriter_GPU& operator=(const VideoWriter_GPU&);
2506
2507     class Impl;
2508     std::auto_ptr<Impl> impl_;
2509 };
2510
2511
2512 ////////////////////////////////// Video Decoding //////////////////////////////////////////
2513
2514 namespace detail
2515 {
2516     class FrameQueue;
2517     class VideoParser;
2518 }
2519
2520 class CV_EXPORTS VideoReader_GPU
2521 {
2522 public:
2523     enum Codec
2524     {
2525         MPEG1 = 0,
2526         MPEG2,
2527         MPEG4,
2528         VC1,
2529         H264,
2530         JPEG,
2531         H264_SVC,
2532         H264_MVC,
2533
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)
2539     };
2540
2541     enum ChromaFormat
2542     {
2543         Monochrome=0,
2544         YUV420,
2545         YUV422,
2546         YUV444,
2547     };
2548
2549     struct FormatInfo
2550     {
2551         Codec codec;
2552         ChromaFormat chromaFormat;
2553         int width;
2554         int height;
2555     };
2556
2557     class VideoSource;
2558
2559     VideoReader_GPU();
2560     explicit VideoReader_GPU(const std::string& filename);
2561     explicit VideoReader_GPU(const cv::Ptr<VideoSource>& source);
2562
2563     ~VideoReader_GPU();
2564
2565     void open(const std::string& filename);
2566     void open(const cv::Ptr<VideoSource>& source);
2567     bool isOpened() const;
2568
2569     void close();
2570
2571     bool read(GpuMat& image);
2572
2573     FormatInfo format() const;
2574     void dumpFormat(std::ostream& st);
2575
2576     class CV_EXPORTS VideoSource
2577     {
2578     public:
2579         VideoSource() : frameQueue_(0), videoParser_(0) {}
2580         virtual ~VideoSource() {}
2581
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;
2587
2588         void setFrameQueue(detail::FrameQueue* frameQueue) { frameQueue_ = frameQueue; }
2589         void setVideoParser(detail::VideoParser* videoParser) { videoParser_ = videoParser; }
2590
2591     protected:
2592         bool parseVideoData(const uchar* data, size_t size, bool endOfStream = false);
2593
2594     private:
2595         VideoSource(const VideoSource&);
2596         VideoSource& operator =(const VideoSource&);
2597
2598         detail::FrameQueue* frameQueue_;
2599         detail::VideoParser* videoParser_;
2600     };
2601
2602 private:
2603     VideoReader_GPU(const VideoReader_GPU&);
2604     VideoReader_GPU& operator =(const VideoReader_GPU&);
2605
2606     class Impl;
2607     std::auto_ptr<Impl> impl_;
2608 };
2609
2610 //! removes points (CV_32FC2, single row matrix) with zero mask value
2611 CV_EXPORTS void compactPoints(GpuMat &points0, GpuMat &points1, const GpuMat &mask);
2612
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);
2616
2617 } // namespace gpu
2618
2619 } // namespace cv
2620
2621 #endif /* __OPENCV_GPU_HPP__ */