fixed extra memory allocations.
[profile/ivi/opencv.git] / modules / gpu / include / opencv2 / gpu / gpu.hpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////\r
2 //\r
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.\r
4 //\r
5 //  By downloading, copying, installing or using the software you agree to this license.\r
6 //  If you do not agree to this license, do not download, install,\r
7 //  copy or use the software.\r
8 //\r
9 //\r
10 //                           License Agreement\r
11 //                For Open Source Computer Vision Library\r
12 //\r
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.\r
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.\r
15 // Third party copyrights are property of their respective owners.\r
16 //\r
17 // Redistribution and use in source and binary forms, with or without modification,\r
18 // are permitted provided that the following conditions are met:\r
19 //\r
20 //   * Redistribution's of source code must retain the above copyright notice,\r
21 //     this list of conditions and the following disclaimer.\r
22 //\r
23 //   * Redistribution's in binary form must reproduce the above copyright notice,\r
24 //     this list of conditions and the following disclaimer in the documentation\r
25 //     and/or other GpuMaterials provided with the distribution.\r
26 //\r
27 //   * The name of the copyright holders may not be used to endorse or promote products\r
28 //     derived from this software without specific prior written permission.\r
29 //\r
30 // This software is provided by the copyright holders and contributors "as is" and\r
31 // any express or implied warranties, including, but not limited to, the implied\r
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.\r
33 // In no event shall the Intel Corporation or contributors be liable for any direct,\r
34 // indirect, incidental, special, exemplary, or consequential damages\r
35 // (including, but not limited to, procurement of substitute goods or services;\r
36 // loss of use, data, or profits; or business interruption) however caused\r
37 // and on any theory of liability, whether in contract, strict liability,\r
38 // or tort (including negligence or otherwise) arising in any way out of\r
39 // the use of this software, even if advised of the possibility of such damage.\r
40 //\r
41 //M*/\r
42 \r
43 #ifndef __OPENCV_GPU_HPP__\r
44 #define __OPENCV_GPU_HPP__\r
45 \r
46 #include <vector>\r
47 #include "opencv2/core/core.hpp"\r
48 #include "opencv2/imgproc/imgproc.hpp"\r
49 #include "opencv2/objdetect/objdetect.hpp"\r
50 #include "opencv2/gpu/devmem2d.hpp"\r
51 #include "opencv2/features2d/features2d.hpp"\r
52 \r
53 namespace cv\r
54 {\r
55     namespace gpu\r
56     {\r
57         //////////////////////////////// Initialization & Info ////////////////////////\r
58 \r
59         //! This is the only function that do not throw exceptions if the library is compiled without Cuda.\r
60         CV_EXPORTS int getCudaEnabledDeviceCount();\r
61 \r
62         //! Functions below throw cv::Expception if the library is compiled without Cuda.\r
63 \r
64         CV_EXPORTS void setDevice(int device);\r
65         CV_EXPORTS int getDevice();\r
66 \r
67         enum FeatureSet\r
68         {\r
69             FEATURE_SET_COMPUTE_10 = 10,\r
70             FEATURE_SET_COMPUTE_11 = 11,\r
71             FEATURE_SET_COMPUTE_12 = 12,\r
72             FEATURE_SET_COMPUTE_13 = 13,\r
73             FEATURE_SET_COMPUTE_20 = 20,\r
74             FEATURE_SET_COMPUTE_21 = 21,\r
75             GLOBAL_ATOMICS = FEATURE_SET_COMPUTE_11,\r
76             NATIVE_DOUBLE = FEATURE_SET_COMPUTE_13\r
77         };\r
78 \r
79         // Gives information about what GPU archs this OpenCV GPU module was \r
80         // compiled for\r
81         class CV_EXPORTS TargetArchs\r
82         {\r
83         public:\r
84             static bool builtWith(FeatureSet feature_set);\r
85             static bool has(int major, int minor);\r
86             static bool hasPtx(int major, int minor);\r
87             static bool hasBin(int major, int minor);\r
88             static bool hasEqualOrLessPtx(int major, int minor);\r
89             static bool hasEqualOrGreater(int major, int minor);\r
90             static bool hasEqualOrGreaterPtx(int major, int minor);\r
91             static bool hasEqualOrGreaterBin(int major, int minor);\r
92         private:\r
93             TargetArchs();\r
94         };\r
95 \r
96         // Gives information about the given GPU\r
97         class CV_EXPORTS DeviceInfo\r
98         {\r
99         public:\r
100             // Creates DeviceInfo object for the current GPU\r
101             DeviceInfo() : device_id_(getDevice()) { query(); }\r
102 \r
103             // Creates DeviceInfo object for the given GPU\r
104             DeviceInfo(int device_id) : device_id_(device_id) { query(); }\r
105 \r
106             string name() const { return name_; }\r
107 \r
108             // Return compute capability versions\r
109             int majorVersion() const { return majorVersion_; }\r
110             int minorVersion() const { return minorVersion_; }\r
111 \r
112             int multiProcessorCount() const { return multi_processor_count_; }\r
113 \r
114             size_t freeMemory() const;\r
115             size_t totalMemory() const;\r
116 \r
117             // Checks whether device supports the given feature\r
118             bool supports(FeatureSet feature_set) const;\r
119 \r
120             // Checks whether the GPU module can be run on the given device\r
121             bool isCompatible() const;\r
122 \r
123         private:\r
124             void query();\r
125             void queryMemory(size_t& free_memory, size_t& total_memory) const;\r
126 \r
127             int device_id_;\r
128 \r
129             string name_;\r
130             int multi_processor_count_;\r
131             int majorVersion_;\r
132             int minorVersion_;\r
133         };\r
134 \r
135         /////////////////////////// Multi GPU Manager //////////////////////////////\r
136 \r
137         // Provides functionality for working with many GPUs\r
138         class CV_EXPORTS MultiGpuManager\r
139         {\r
140         public:\r
141             MultiGpuManager();\r
142             ~MultiGpuManager();\r
143 \r
144             // Must be called before any other GPU calls\r
145             void init();\r
146 \r
147             // Makes the given GPU active\r
148             void gpuOn(int gpu_id);\r
149 \r
150             // Finishes the piece of work on the current GPU\r
151             void gpuOff();\r
152 \r
153             static const int BAD_GPU_ID = -1;\r
154 \r
155         private:\r
156             void operator=(const MultiGpuManager&);\r
157             MultiGpuManager(const MultiGpuManager&);\r
158 \r
159             class Impl;\r
160             Ptr<Impl> impl_;\r
161         };\r
162 \r
163         //////////////////////////////// Error handling ////////////////////////\r
164 \r
165         CV_EXPORTS void error(const char *error_string, const char *file, const int line, const char *func);\r
166         CV_EXPORTS void nppError( int err, const char *file, const int line, const char *func);\r
167 \r
168         //////////////////////////////// GpuMat ////////////////////////////////\r
169         class Stream;\r
170         class CudaMem;\r
171 \r
172         //! Smart pointer for GPU memory with reference counting. Its interface is mostly similar with cv::Mat.\r
173         class CV_EXPORTS GpuMat\r
174         {\r
175         public:\r
176             //! default constructor\r
177             GpuMat();\r
178             //! constructs GpuMatrix of the specified size and type (_type is CV_8UC1, CV_64FC3, CV_32SC(12) etc.)\r
179             GpuMat(int rows, int cols, int type);\r
180             GpuMat(Size size, int type);\r
181             //! constucts GpuMatrix and fills it with the specified value _s.\r
182             GpuMat(int rows, int cols, int type, const Scalar& s);\r
183             GpuMat(Size size, int type, const Scalar& s);\r
184             //! copy constructor\r
185             GpuMat(const GpuMat& m);\r
186 \r
187             //! constructor for GpuMatrix headers pointing to user-allocated data\r
188             GpuMat(int rows, int cols, int type, void* data, size_t step = Mat::AUTO_STEP);\r
189             GpuMat(Size size, int type, void* data, size_t step = Mat::AUTO_STEP);\r
190 \r
191             //! creates a matrix header for a part of the bigger matrix\r
192             GpuMat(const GpuMat& m, const Range& rowRange, const Range& colRange);\r
193             GpuMat(const GpuMat& m, const Rect& roi);\r
194 \r
195             //! builds GpuMat from Mat. Perfom blocking upload to device.\r
196             explicit GpuMat (const Mat& m);\r
197 \r
198             //! destructor - calls release()\r
199             ~GpuMat();\r
200 \r
201             //! assignment operators\r
202             GpuMat& operator = (const GpuMat& m);\r
203             //! assignment operator. Perfom blocking upload to device.\r
204             GpuMat& operator = (const Mat& m);\r
205 \r
206             //! returns lightweight DevMem2D_ structure for passing to nvcc-compiled code.\r
207             // Contains just image size, data ptr and step.\r
208             template <class T> operator DevMem2D_<T>() const;\r
209             template <class T> operator PtrStep_<T>() const;\r
210 \r
211             //! pefroms blocking upload data to GpuMat.\r
212             void upload(const cv::Mat& m);\r
213 \r
214             //! upload async\r
215             void upload(const CudaMem& m, Stream& stream);\r
216 \r
217             //! downloads data from device to host memory. Blocking calls.\r
218             operator Mat() const;\r
219             void download(cv::Mat& m) const;\r
220 \r
221             //! download async\r
222             void download(CudaMem& m, Stream& stream) const;\r
223 \r
224             //! returns a new GpuMatrix header for the specified row\r
225             GpuMat row(int y) const;\r
226             //! returns a new GpuMatrix header for the specified column\r
227             GpuMat col(int x) const;\r
228             //! ... for the specified row span\r
229             GpuMat rowRange(int startrow, int endrow) const;\r
230             GpuMat rowRange(const Range& r) const;\r
231             //! ... for the specified column span\r
232             GpuMat colRange(int startcol, int endcol) const;\r
233             GpuMat colRange(const Range& r) const;\r
234 \r
235             //! returns deep copy of the GpuMatrix, i.e. the data is copied\r
236             GpuMat clone() const;\r
237             //! copies the GpuMatrix content to "m".\r
238             // It calls m.create(this->size(), this->type()).\r
239             void copyTo( GpuMat& m ) const;\r
240             //! copies those GpuMatrix elements to "m" that are marked with non-zero mask elements.\r
241             void copyTo( GpuMat& m, const GpuMat& mask ) const;\r
242             //! converts GpuMatrix to another datatype with optional scalng. See cvConvertScale.\r
243             void convertTo( GpuMat& m, int rtype, double alpha=1, double beta=0 ) const;\r
244 \r
245             void assignTo( GpuMat& m, int type=-1 ) const;\r
246 \r
247             //! sets every GpuMatrix element to s\r
248             GpuMat& operator = (const Scalar& s);\r
249             //! sets some of the GpuMatrix elements to s, according to the mask\r
250             GpuMat& setTo(const Scalar& s, const GpuMat& mask = GpuMat());\r
251             //! creates alternative GpuMatrix header for the same data, with different\r
252             // number of channels and/or different number of rows. see cvReshape.\r
253             GpuMat reshape(int cn, int rows = 0) const;\r
254 \r
255             //! allocates new GpuMatrix data unless the GpuMatrix already has specified size and type.\r
256             // previous data is unreferenced if needed.\r
257             void create(int rows, int cols, int type);\r
258             void create(Size size, int type);\r
259             //! decreases reference counter;\r
260             // deallocate the data when reference counter reaches 0.\r
261             void release();\r
262 \r
263             //! swaps with other smart pointer\r
264             void swap(GpuMat& mat);\r
265 \r
266             //! locates GpuMatrix header within a parent GpuMatrix. See below\r
267             void locateROI( Size& wholeSize, Point& ofs ) const;\r
268             //! moves/resizes the current GpuMatrix ROI inside the parent GpuMatrix.\r
269             GpuMat& adjustROI( int dtop, int dbottom, int dleft, int dright );\r
270             //! extracts a rectangular sub-GpuMatrix\r
271             // (this is a generalized form of row, rowRange etc.)\r
272             GpuMat operator()( Range rowRange, Range colRange ) const;\r
273             GpuMat operator()( const Rect& roi ) const;\r
274 \r
275             //! returns true iff the GpuMatrix data is continuous\r
276             // (i.e. when there are no gaps between successive rows).\r
277             // similar to CV_IS_GpuMat_CONT(cvGpuMat->type)\r
278             bool isContinuous() const;\r
279             //! returns element size in bytes,\r
280             // similar to CV_ELEM_SIZE(cvMat->type)\r
281             size_t elemSize() const;\r
282             //! returns the size of element channel in bytes.\r
283             size_t elemSize1() const;\r
284             //! returns element type, similar to CV_MAT_TYPE(cvMat->type)\r
285             int type() const;\r
286             //! returns element type, similar to CV_MAT_DEPTH(cvMat->type)\r
287             int depth() const;\r
288             //! returns element type, similar to CV_MAT_CN(cvMat->type)\r
289             int channels() const;\r
290             //! returns step/elemSize1()\r
291             size_t step1() const;\r
292             //! returns GpuMatrix size:\r
293             // width == number of columns, height == number of rows\r
294             Size size() const;\r
295             //! returns true if GpuMatrix data is NULL\r
296             bool empty() const;\r
297 \r
298             //! returns pointer to y-th row\r
299             uchar* ptr(int y = 0);\r
300             const uchar* ptr(int y = 0) const;\r
301 \r
302             //! template version of the above method\r
303             template<typename _Tp> _Tp* ptr(int y = 0);\r
304             template<typename _Tp> const _Tp* ptr(int y = 0) const;\r
305 \r
306             //! matrix transposition\r
307             GpuMat t() const;\r
308 \r
309             /*! includes several bit-fields:\r
310             - the magic signature\r
311             - continuity flag\r
312             - depth\r
313             - number of channels\r
314             */\r
315             int flags;\r
316             //! the number of rows and columns\r
317             int rows, cols;\r
318             //! a distance between successive rows in bytes; includes the gap if any\r
319             size_t step;\r
320             //! pointer to the data\r
321             uchar* data;\r
322 \r
323             //! pointer to the reference counter;\r
324             // when GpuMatrix points to user-allocated data, the pointer is NULL\r
325             int* refcount;\r
326 \r
327             //! helper fields used in locateROI and adjustROI\r
328             uchar* datastart;\r
329             uchar* dataend;\r
330         };\r
331 \r
332 //#define TemplatedGpuMat // experimental now, deprecated to use\r
333 #ifdef TemplatedGpuMat\r
334     #include "GpuMat_BetaDeprecated.hpp"\r
335 #endif\r
336 \r
337         //! Creates continuous GPU matrix\r
338         CV_EXPORTS void createContinuous(int rows, int cols, int type, GpuMat& m);\r
339 \r
340         //! Ensures that size of the given matrix is not less than (rows, cols) size\r
341         //! and matrix type is match specified one too\r
342         CV_EXPORTS void ensureSizeIsEnough(int rows, int cols, int type, GpuMat& m);\r
343 \r
344         //////////////////////////////// CudaMem ////////////////////////////////\r
345         // CudaMem is limited cv::Mat with page locked memory allocation.\r
346         // Page locked memory is only needed for async and faster coping to GPU.\r
347         // It is convertable to cv::Mat header without reference counting\r
348         // so you can use it with other opencv functions.\r
349 \r
350         class CV_EXPORTS CudaMem\r
351         {\r
352         public:\r
353             enum  { ALLOC_PAGE_LOCKED = 1, ALLOC_ZEROCOPY = 2, ALLOC_WRITE_COMBINED = 4 };\r
354 \r
355             CudaMem();\r
356             CudaMem(const CudaMem& m);\r
357 \r
358             CudaMem(int rows, int cols, int type, int _alloc_type = ALLOC_PAGE_LOCKED);\r
359             CudaMem(Size size, int type, int alloc_type = ALLOC_PAGE_LOCKED);\r
360 \r
361 \r
362             //! creates from cv::Mat with coping data\r
363             explicit CudaMem(const Mat& m, int alloc_type = ALLOC_PAGE_LOCKED);\r
364 \r
365             ~CudaMem();\r
366 \r
367             CudaMem& operator = (const CudaMem& m);\r
368 \r
369             //! returns deep copy of the matrix, i.e. the data is copied\r
370             CudaMem clone() const;\r
371 \r
372             //! allocates new matrix data unless the matrix already has specified size and type.\r
373             void create(int rows, int cols, int type, int alloc_type = ALLOC_PAGE_LOCKED);\r
374             void create(Size size, int type, int alloc_type = ALLOC_PAGE_LOCKED);\r
375 \r
376             //! decrements reference counter and released memory if needed.\r
377             void release();\r
378 \r
379             //! returns matrix header with disabled reference counting for CudaMem data.\r
380             Mat createMatHeader() const;\r
381             operator Mat() const;\r
382 \r
383             //! maps host memory into device address space and returns GpuMat header for it. Throws exception if not supported by hardware.\r
384             GpuMat createGpuMatHeader() const;\r
385             operator GpuMat() const;\r
386 \r
387             //returns if host memory can be mapperd to gpu address space;\r
388             static bool canMapHostMemory();\r
389 \r
390             // Please see cv::Mat for descriptions\r
391             bool isContinuous() const;\r
392             size_t elemSize() const;\r
393             size_t elemSize1() const;\r
394             int type() const;\r
395             int depth() const;\r
396             int channels() const;\r
397             size_t step1() const;\r
398             Size size() const;\r
399             bool empty() const;\r
400 \r
401 \r
402             // Please see cv::Mat for descriptions\r
403             int flags;\r
404             int rows, cols;\r
405             size_t step;\r
406 \r
407             uchar* data;\r
408             int* refcount;\r
409 \r
410             uchar* datastart;\r
411             uchar* dataend;\r
412 \r
413             int alloc_type;\r
414         };\r
415 \r
416         //////////////////////////////// CudaStream ////////////////////////////////\r
417         // Encapculates Cuda Stream. Provides interface for async coping.\r
418         // Passed to each function that supports async kernel execution.\r
419         // Reference counting is enabled\r
420 \r
421         class CV_EXPORTS Stream\r
422         {\r
423         public:\r
424             Stream();\r
425             ~Stream();\r
426 \r
427             Stream(const Stream&);\r
428             Stream& operator=(const Stream&);\r
429 \r
430             bool queryIfComplete();\r
431             void waitForCompletion();\r
432 \r
433             //! downloads asynchronously.\r
434             // Warning! cv::Mat must point to page locked memory (i.e. to CudaMem data or to its subMat)\r
435             void enqueueDownload(const GpuMat& src, CudaMem& dst);\r
436             void enqueueDownload(const GpuMat& src, Mat& dst);\r
437 \r
438             //! uploads asynchronously.\r
439             // Warning! cv::Mat must point to page locked memory (i.e. to CudaMem data or to its ROI)\r
440             void enqueueUpload(const CudaMem& src, GpuMat& dst);\r
441             void enqueueUpload(const Mat& src, GpuMat& dst);\r
442 \r
443             void enqueueCopy(const GpuMat& src, GpuMat& dst);\r
444 \r
445             void enqueueMemSet(GpuMat& src, Scalar val);\r
446             void enqueueMemSet(GpuMat& src, Scalar val, const GpuMat& mask);\r
447 \r
448             // converts matrix type, ex from float to uchar depending on type\r
449             void enqueueConvert(const GpuMat& src, GpuMat& dst, int type, double a = 1, double b = 0);\r
450         private:\r
451             void create();\r
452             void release();\r
453             struct Impl;\r
454             Impl *impl;\r
455             friend struct StreamAccessor;\r
456         };\r
457 \r
458 \r
459         ////////////////////////////// Arithmetics ///////////////////////////////////\r
460 \r
461         //! transposes the matrix\r
462         //! supports matrix with element size = 1, 4 and 8 bytes (CV_8UC1, CV_8UC4, CV_16UC2, CV_32FC1, etc)\r
463         CV_EXPORTS void transpose(const GpuMat& src1, GpuMat& dst);\r
464 \r
465         //! reverses the order of the rows, columns or both in a matrix\r
466         //! supports CV_8UC1, CV_8UC4 types\r
467         CV_EXPORTS void flip(const GpuMat& a, GpuMat& b, int flipCode);\r
468 \r
469         //! transforms 8-bit unsigned integers using lookup table: dst(i)=lut(src(i))\r
470         //! destination array will have the depth type as lut and the same channels number as source\r
471         //! supports CV_8UC1, CV_8UC3 types\r
472         CV_EXPORTS void LUT(const GpuMat& src, const Mat& lut, GpuMat& dst);\r
473 \r
474         //! makes multi-channel array out of several single-channel arrays\r
475         CV_EXPORTS void merge(const GpuMat* src, size_t n, GpuMat& dst);\r
476 \r
477         //! makes multi-channel array out of several single-channel arrays\r
478         CV_EXPORTS void merge(const vector<GpuMat>& src, GpuMat& dst);\r
479 \r
480         //! makes multi-channel array out of several single-channel arrays (async version)\r
481         CV_EXPORTS void merge(const GpuMat* src, size_t n, GpuMat& dst, const Stream& stream);\r
482 \r
483         //! makes multi-channel array out of several single-channel arrays (async version)\r
484         CV_EXPORTS void merge(const vector<GpuMat>& src, GpuMat& dst, const Stream& stream);\r
485 \r
486         //! copies each plane of a multi-channel array to a dedicated array\r
487         CV_EXPORTS void split(const GpuMat& src, GpuMat* dst);\r
488 \r
489         //! copies each plane of a multi-channel array to a dedicated array\r
490         CV_EXPORTS void split(const GpuMat& src, vector<GpuMat>& dst);\r
491 \r
492         //! copies each plane of a multi-channel array to a dedicated array (async version)\r
493         CV_EXPORTS void split(const GpuMat& src, GpuMat* dst, const Stream& stream);\r
494 \r
495         //! copies each plane of a multi-channel array to a dedicated array (async version)\r
496         CV_EXPORTS void split(const GpuMat& src, vector<GpuMat>& dst, const Stream& stream);\r
497 \r
498         //! computes magnitude of complex (x(i).re, x(i).im) vector\r
499         //! supports only CV_32FC2 type\r
500         CV_EXPORTS void magnitude(const GpuMat& x, GpuMat& magnitude);\r
501 \r
502         //! computes squared magnitude of complex (x(i).re, x(i).im) vector\r
503         //! supports only CV_32FC2 type\r
504         CV_EXPORTS void magnitudeSqr(const GpuMat& x, GpuMat& magnitude);\r
505 \r
506         //! computes magnitude of each (x(i), y(i)) vector\r
507         //! supports only floating-point source\r
508         CV_EXPORTS void magnitude(const GpuMat& x, const GpuMat& y, GpuMat& magnitude);\r
509         //! async version\r
510         CV_EXPORTS void magnitude(const GpuMat& x, const GpuMat& y, GpuMat& magnitude, const Stream& stream);\r
511 \r
512         //! computes squared magnitude of each (x(i), y(i)) vector\r
513         //! supports only floating-point source\r
514         CV_EXPORTS void magnitudeSqr(const GpuMat& x, const GpuMat& y, GpuMat& magnitude);\r
515         //! async version\r
516         CV_EXPORTS void magnitudeSqr(const GpuMat& x, const GpuMat& y, GpuMat& magnitude, const Stream& stream);\r
517 \r
518         //! computes angle (angle(i)) of each (x(i), y(i)) vector\r
519         //! supports only floating-point source\r
520         CV_EXPORTS void phase(const GpuMat& x, const GpuMat& y, GpuMat& angle, bool angleInDegrees = false);\r
521         //! async version\r
522         CV_EXPORTS void phase(const GpuMat& x, const GpuMat& y, GpuMat& angle, bool angleInDegrees, const Stream& stream);\r
523 \r
524         //! converts Cartesian coordinates to polar\r
525         //! supports only floating-point source\r
526         CV_EXPORTS void cartToPolar(const GpuMat& x, const GpuMat& y, GpuMat& magnitude, GpuMat& angle, bool angleInDegrees = false);\r
527         //! async version\r
528         CV_EXPORTS void cartToPolar(const GpuMat& x, const GpuMat& y, GpuMat& magnitude, GpuMat& angle, bool angleInDegrees, const Stream& stream);\r
529 \r
530         //! converts polar coordinates to Cartesian\r
531         //! supports only floating-point source\r
532         CV_EXPORTS void polarToCart(const GpuMat& magnitude, const GpuMat& angle, GpuMat& x, GpuMat& y, bool angleInDegrees = false);\r
533         //! async version\r
534         CV_EXPORTS void polarToCart(const GpuMat& magnitude, const GpuMat& angle, GpuMat& x, GpuMat& y, bool angleInDegrees, const Stream& stream);\r
535 \r
536 \r
537         //////////////////////////// Per-element operations ////////////////////////////////////\r
538 \r
539         //! adds one matrix to another (c = a + b)\r
540         //! supports CV_8UC1, CV_8UC4, CV_32SC1, CV_32FC1 types\r
541         CV_EXPORTS void add(const GpuMat& a, const GpuMat& b, GpuMat& c);\r
542         //! adds scalar to a matrix (c = a + s)\r
543         //! supports CV_32FC1 and CV_32FC2 type\r
544         CV_EXPORTS void add(const GpuMat& a, const Scalar& sc, GpuMat& c);\r
545 \r
546         //! subtracts one matrix from another (c = a - b)\r
547         //! supports CV_8UC1, CV_8UC4, CV_32SC1, CV_32FC1 types\r
548         CV_EXPORTS void subtract(const GpuMat& a, const GpuMat& b, GpuMat& c);\r
549         //! subtracts scalar from a matrix (c = a - s)\r
550         //! supports CV_32FC1 and CV_32FC2 type\r
551         CV_EXPORTS void subtract(const GpuMat& a, const Scalar& sc, GpuMat& c);\r
552 \r
553         //! computes element-wise product of the two arrays (c = a * b)\r
554         //! supports CV_8UC1, CV_8UC4, CV_32SC1, CV_32FC1 types\r
555         CV_EXPORTS void multiply(const GpuMat& a, const GpuMat& b, GpuMat& c);\r
556         //! multiplies matrix to a scalar (c = a * s)\r
557         //! supports CV_32FC1 and CV_32FC2 type\r
558         CV_EXPORTS void multiply(const GpuMat& a, const Scalar& sc, GpuMat& c);\r
559 \r
560         //! computes element-wise quotient of the two arrays (c = a / b)\r
561         //! supports CV_8UC1, CV_8UC4, CV_32SC1, CV_32FC1 types\r
562         CV_EXPORTS void divide(const GpuMat& a, const GpuMat& b, GpuMat& c);\r
563         //! computes element-wise quotient of matrix and scalar (c = a / s)\r
564         //! supports CV_32FC1 and CV_32FC2 type\r
565         CV_EXPORTS void divide(const GpuMat& a, const Scalar& sc, GpuMat& c);\r
566 \r
567         //! computes exponent of each matrix element (b = e**a)\r
568         //! supports only CV_32FC1 type\r
569         CV_EXPORTS void exp(const GpuMat& a, GpuMat& b);\r
570 \r
571         //! computes natural logarithm of absolute value of each matrix element: b = log(abs(a))\r
572         //! supports only CV_32FC1 type\r
573         CV_EXPORTS void log(const GpuMat& a, GpuMat& b);\r
574 \r
575         //! computes element-wise absolute difference of two arrays (c = abs(a - b))\r
576         //! supports CV_8UC1, CV_8UC4, CV_32SC1, CV_32FC1 types\r
577         CV_EXPORTS void absdiff(const GpuMat& a, const GpuMat& b, GpuMat& c);\r
578         //! computes element-wise absolute difference of array and scalar (c = abs(a - s))\r
579         //! supports only CV_32FC1 type\r
580         CV_EXPORTS void absdiff(const GpuMat& a, const Scalar& s, GpuMat& c);\r
581 \r
582         //! compares elements of two arrays (c = a <cmpop> b)\r
583         //! supports CV_8UC4, CV_32FC1 types\r
584         CV_EXPORTS void compare(const GpuMat& a, const GpuMat& b, GpuMat& c, int cmpop);\r
585 \r
586         //! performs per-elements bit-wise inversion\r
587         CV_EXPORTS void bitwise_not(const GpuMat& src, GpuMat& dst, const GpuMat& mask=GpuMat());\r
588         //! async version\r
589         CV_EXPORTS void bitwise_not(const GpuMat& src, GpuMat& dst, const GpuMat& mask, const Stream& stream);\r
590 \r
591         //! calculates per-element bit-wise disjunction of two arrays\r
592         CV_EXPORTS void bitwise_or(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat& mask=GpuMat());\r
593         //! async version\r
594         CV_EXPORTS void bitwise_or(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat& mask, const Stream& stream);\r
595 \r
596         //! calculates per-element bit-wise conjunction of two arrays\r
597         CV_EXPORTS void bitwise_and(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat& mask=GpuMat());\r
598         //! async version\r
599         CV_EXPORTS void bitwise_and(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat& mask, const Stream& stream);\r
600 \r
601         //! calculates per-element bit-wise "exclusive or" operation\r
602         CV_EXPORTS void bitwise_xor(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat& mask=GpuMat());\r
603         //! async version\r
604         CV_EXPORTS void bitwise_xor(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const GpuMat& mask, const Stream& stream);\r
605 \r
606         //! computes per-element minimum of two arrays (dst = min(src1, src2))\r
607         CV_EXPORTS void min(const GpuMat& src1, const GpuMat& src2, GpuMat& dst);\r
608         //! Async version\r
609         CV_EXPORTS void min(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const Stream& stream);\r
610 \r
611         //! computes per-element minimum of array and scalar (dst = min(src1, src2))\r
612         CV_EXPORTS void min(const GpuMat& src1, double src2, GpuMat& dst);\r
613         //! Async version\r
614         CV_EXPORTS void min(const GpuMat& src1, double src2, GpuMat& dst, const Stream& stream);\r
615 \r
616         //! computes per-element maximum of two arrays (dst = max(src1, src2))\r
617         CV_EXPORTS void max(const GpuMat& src1, const GpuMat& src2, GpuMat& dst);\r
618         //! Async version\r
619         CV_EXPORTS void max(const GpuMat& src1, const GpuMat& src2, GpuMat& dst, const Stream& stream);\r
620 \r
621         //! computes per-element maximum of array and scalar (dst = max(src1, src2))\r
622         CV_EXPORTS void max(const GpuMat& src1, double src2, GpuMat& dst);\r
623         //! Async version\r
624         CV_EXPORTS void max(const GpuMat& src1, double src2, GpuMat& dst, const Stream& stream);\r
625 \r
626 \r
627         ////////////////////////////// Image processing //////////////////////////////\r
628 \r
629         //! DST[x,y] = SRC[xmap[x,y],ymap[x,y]] with bilinear interpolation.\r
630         //! supports CV_8UC1, CV_8UC3 source types and CV_32FC1 map type\r
631         CV_EXPORTS void remap(const GpuMat& src, GpuMat& dst, const GpuMat& xmap, const GpuMat& ymap);\r
632 \r
633         //! Does mean shift filtering on GPU.\r
634         CV_EXPORTS void meanShiftFiltering(const GpuMat& src, GpuMat& dst, int sp, int sr,\r
635             TermCriteria criteria = TermCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 5, 1));\r
636 \r
637         //! Does mean shift procedure on GPU.\r
638         CV_EXPORTS void meanShiftProc(const GpuMat& src, GpuMat& dstr, GpuMat& dstsp, int sp, int sr,\r
639             TermCriteria criteria = TermCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 5, 1));\r
640 \r
641         //! Does mean shift segmentation with elimination of small regions.\r
642         CV_EXPORTS void meanShiftSegmentation(const GpuMat& src, Mat& dst, int sp, int sr, int minsize,\r
643             TermCriteria criteria = TermCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 5, 1));\r
644 \r
645         //! Does coloring of disparity image: [0..ndisp) -> [0..240, 1, 1] in HSV.\r
646         //! Supported types of input disparity: CV_8U, CV_16S.\r
647         //! Output disparity has CV_8UC4 type in BGRA format (alpha = 255).\r
648         CV_EXPORTS void drawColorDisp(const GpuMat& src_disp, GpuMat& dst_disp, int ndisp);\r
649         //! async version\r
650         CV_EXPORTS void drawColorDisp(const GpuMat& src_disp, GpuMat& dst_disp, int ndisp, const Stream& stream);\r
651 \r
652         //! Reprojects disparity image to 3D space.\r
653         //! Supports CV_8U and CV_16S types of input disparity.\r
654         //! The output is a 4-channel floating-point (CV_32FC4) matrix.\r
655         //! Each element of this matrix will contain the 3D coordinates of the point (x,y,z,1), computed from the disparity map.\r
656         //! Q is the 4x4 perspective transformation matrix that can be obtained with cvStereoRectify.\r
657         CV_EXPORTS void reprojectImageTo3D(const GpuMat& disp, GpuMat& xyzw, const Mat& Q);\r
658         //! async version\r
659         CV_EXPORTS void reprojectImageTo3D(const GpuMat& disp, GpuMat& xyzw, const Mat& Q, const Stream& stream);\r
660 \r
661         //! converts image from one color space to another\r
662         CV_EXPORTS void cvtColor(const GpuMat& src, GpuMat& dst, int code, int dcn = 0);\r
663         //! async version\r
664         CV_EXPORTS void cvtColor(const GpuMat& src, GpuMat& dst, int code, int dcn, const Stream& stream);\r
665 \r
666         //! applies fixed threshold to the image\r
667         CV_EXPORTS double threshold(const GpuMat& src, GpuMat& dst, double thresh, double maxval, int type);\r
668         //! async version\r
669         CV_EXPORTS double threshold(const GpuMat& src, GpuMat& dst, double thresh, double maxval, int type, const Stream& stream);\r
670 \r
671         //! resizes the image\r
672         //! Supports INTER_NEAREST, INTER_LINEAR\r
673         //! supports CV_8UC1, CV_8UC4 types\r
674         CV_EXPORTS void resize(const GpuMat& src, GpuMat& dst, Size dsize, double fx=0, double fy=0, int interpolation = INTER_LINEAR);\r
675 \r
676         //! warps the image using affine transformation\r
677         //! Supports INTER_NEAREST, INTER_LINEAR, INTER_CUBIC\r
678         CV_EXPORTS void warpAffine(const GpuMat& src, GpuMat& dst, const Mat& M, Size dsize, int flags = INTER_LINEAR);\r
679 \r
680         //! warps the image using perspective transformation\r
681         //! Supports INTER_NEAREST, INTER_LINEAR, INTER_CUBIC\r
682         CV_EXPORTS void warpPerspective(const GpuMat& src, GpuMat& dst, const Mat& M, Size dsize, int flags = INTER_LINEAR);\r
683 \r
684         //! rotate 8bit single or four channel image\r
685         //! Supports INTER_NEAREST, INTER_LINEAR, INTER_CUBIC\r
686         //! supports CV_8UC1, CV_8UC4 types\r
687         CV_EXPORTS void rotate(const GpuMat& src, GpuMat& dst, Size dsize, double angle, double xShift = 0, double yShift = 0, int interpolation = INTER_LINEAR);\r
688 \r
689         //! copies 2D array to a larger destination array and pads borders with user-specifiable constant\r
690         //! supports CV_8UC1, CV_8UC4, CV_32SC1 and CV_32FC1 types\r
691         CV_EXPORTS void copyMakeBorder(const GpuMat& src, GpuMat& dst, int top, int bottom, int left, int right, const Scalar& value = Scalar());\r
692 \r
693         //! computes the integral image\r
694         //! sum will have CV_32S type, but will contain unsigned int values\r
695         //! supports only CV_8UC1 source type\r
696         CV_EXPORTS void integral(const GpuMat& src, GpuMat& sum);\r
697 \r
698         //! buffered version\r
699         CV_EXPORTS void integralBuffered(const GpuMat& src, GpuMat& sum, GpuMat& buffer);\r
700 \r
701         //! computes the integral image and integral for the squared image\r
702         //! sum will have CV_32S type, sqsum - CV32F type\r
703         //! supports only CV_8UC1 source type\r
704         CV_EXPORTS void integral(const GpuMat& src, GpuMat& sum, GpuMat& sqsum);\r
705 \r
706         //! computes squared integral image\r
707         //! result matrix will have 64F type, but will contain 64U values\r
708         //! supports source images of 8UC1 type only\r
709         CV_EXPORTS void sqrIntegral(const GpuMat& src, GpuMat& sqsum);\r
710 \r
711         //! computes vertical sum, supports only CV_32FC1 images\r
712         CV_EXPORTS void columnSum(const GpuMat& src, GpuMat& sum);\r
713 \r
714         //! computes the standard deviation of integral images\r
715         //! supports only CV_32SC1 source type and CV_32FC1 sqr type\r
716         //! output will have CV_32FC1 type\r
717         CV_EXPORTS void rectStdDev(const GpuMat& src, const GpuMat& sqr, GpuMat& dst, const Rect& rect);\r
718 \r
719         // applies Canny edge detector and produces the edge map\r
720         // disabled until fix crash\r
721         //CV_EXPORTS void Canny(const GpuMat& image, GpuMat& edges, double threshold1, double threshold2, int apertureSize = 3);\r
722         //CV_EXPORTS void Canny(const GpuMat& image, GpuMat& edges, GpuMat& buffer, double threshold1, double threshold2, int apertureSize = 3);\r
723         //CV_EXPORTS void Canny(const GpuMat& srcDx, const GpuMat& srcDy, GpuMat& edges, double threshold1, double threshold2, int apertureSize = 3);\r
724         //CV_EXPORTS void Canny(const GpuMat& srcDx, const GpuMat& srcDy, GpuMat& edges, GpuMat& buffer, double threshold1, double threshold2, int apertureSize = 3);\r
725 \r
726         //! computes Harris cornerness criteria at each image pixel\r
727         CV_EXPORTS void cornerHarris(const GpuMat& src, GpuMat& dst, int blockSize, int ksize, double k, int borderType=BORDER_REFLECT101);\r
728 \r
729         //! computes minimum eigen value of 2x2 derivative covariation matrix at each pixel - the cornerness criteria\r
730         CV_EXPORTS void cornerMinEigenVal(const GpuMat& src, GpuMat& dst, int blockSize, int ksize, int borderType=BORDER_REFLECT101);\r
731 \r
732         //! performs per-element multiplication of two full (not packed) Fourier spectrums\r
733         //! supports 32FC2 matrixes only (interleaved format)\r
734         CV_EXPORTS void mulSpectrums(const GpuMat& a, const GpuMat& b, GpuMat& c, int flags, bool conjB=false);\r
735 \r
736         //! performs per-element multiplication of two full (not packed) Fourier spectrums\r
737         //! supports 32FC2 matrixes only (interleaved format)\r
738         CV_EXPORTS void mulAndScaleSpectrums(const GpuMat& a, const GpuMat& b, GpuMat& c, int flags, \r
739                                              float scale, bool conjB=false);\r
740 \r
741         //! Performs a forward or inverse discrete Fourier transform (1D or 2D) of floating point matrix.\r
742         //! Param dft_size is the size of DFT transform.\r
743         //! \r
744         //! If the source matrix is not continous, then additional copy will be done,\r
745         //! so to avoid copying ensure the source matrix is continous one. If you want to use\r
746         //! preallocated output ensure it is continuous too, otherwise it will be reallocated.\r
747         //!\r
748         //! Being implemented via CUFFT real-to-complex transform result contains only non-redundant values\r
749         //! in CUFFT's format. Result as full complex matrix for such kind of transform cannot be retrieved.\r
750         //!\r
751         //! For complex-to-real transform it is assumed that the source matrix is packed in CUFFT's format.\r
752         CV_EXPORTS void dft(const GpuMat& src, GpuMat& dst, Size dft_size, int flags=0);\r
753 \r
754         //! computes convolution (or cross-correlation) of two images using discrete Fourier transform\r
755         //! supports source images of 32FC1 type only\r
756         //! result matrix will have 32FC1 type\r
757         CV_EXPORTS void convolve(const GpuMat& image, const GpuMat& templ, GpuMat& result, \r
758                                  bool ccorr=false);\r
759 \r
760         struct CV_EXPORTS ConvolveBuf;\r
761 \r
762         //! buffered version\r
763         CV_EXPORTS void convolve(const GpuMat& image, const GpuMat& templ, GpuMat& result, \r
764                                  bool ccorr, ConvolveBuf& buf);\r
765 \r
766         struct CV_EXPORTS ConvolveBuf\r
767         {\r
768             ConvolveBuf() {}\r
769             ConvolveBuf(Size image_size, Size templ_size) \r
770                 { create(image_size, templ_size); }\r
771             void create(Size image_size, Size templ_size);\r
772 \r
773         private:\r
774             static Size estimateBlockSize(Size result_size, Size templ_size);\r
775             friend void convolve(const GpuMat&, const GpuMat&, GpuMat&, bool, ConvolveBuf&);\r
776 \r
777             Size result_size;\r
778             Size block_size;\r
779             Size dft_size;\r
780             int spect_len;\r
781 \r
782             GpuMat image_spect, templ_spect, result_spect;\r
783             GpuMat image_block, templ_block, result_data;\r
784         };\r
785 \r
786         //! computes the proximity map for the raster template and the image where the template is searched for\r
787         CV_EXPORTS void matchTemplate(const GpuMat& image, const GpuMat& templ, GpuMat& result, int method);\r
788 \r
789 \r
790         ////////////////////////////// Matrix reductions //////////////////////////////\r
791 \r
792         //! computes mean value and standard deviation of all or selected array elements\r
793         //! supports only CV_8UC1 type\r
794         CV_EXPORTS void meanStdDev(const GpuMat& mtx, Scalar& mean, Scalar& stddev);\r
795 \r
796         //! computes norm of array\r
797         //! supports NORM_INF, NORM_L1, NORM_L2\r
798         //! supports all matrices except 64F\r
799         CV_EXPORTS double norm(const GpuMat& src1, int normType=NORM_L2);\r
800 \r
801         //! computes norm of array\r
802         //! supports NORM_INF, NORM_L1, NORM_L2\r
803         //! supports all matrices except 64F\r
804         CV_EXPORTS double norm(const GpuMat& src1, int normType, GpuMat& buf);\r
805 \r
806         //! computes norm of the difference between two arrays\r
807         //! supports NORM_INF, NORM_L1, NORM_L2\r
808         //! supports only CV_8UC1 type\r
809         CV_EXPORTS double norm(const GpuMat& src1, const GpuMat& src2, int normType=NORM_L2);\r
810 \r
811         //! computes sum of array elements\r
812         //! supports only single channel images\r
813         CV_EXPORTS Scalar sum(const GpuMat& src);\r
814 \r
815         //! computes sum of array elements\r
816         //! supports only single channel images\r
817         CV_EXPORTS Scalar sum(const GpuMat& src, GpuMat& buf);\r
818 \r
819         //! computes sum of array elements absolute values\r
820         //! supports only single channel images\r
821         CV_EXPORTS Scalar absSum(const GpuMat& src);\r
822 \r
823         //! computes sum of array elements absolute values\r
824         //! supports only single channel images\r
825         CV_EXPORTS Scalar absSum(const GpuMat& src, GpuMat& buf);\r
826 \r
827         //! computes squared sum of array elements\r
828         //! supports only single channel images\r
829         CV_EXPORTS Scalar sqrSum(const GpuMat& src);\r
830 \r
831         //! computes squared sum of array elements\r
832         //! supports only single channel images\r
833         CV_EXPORTS Scalar sqrSum(const GpuMat& src, GpuMat& buf);\r
834 \r
835         //! finds global minimum and maximum array elements and returns their values\r
836         CV_EXPORTS void minMax(const GpuMat& src, double* minVal, double* maxVal=0, const GpuMat& mask=GpuMat());\r
837 \r
838         //! finds global minimum and maximum array elements and returns their values\r
839         CV_EXPORTS void minMax(const GpuMat& src, double* minVal, double* maxVal, const GpuMat& mask, GpuMat& buf);\r
840 \r
841         //! finds global minimum and maximum array elements and returns their values with locations\r
842         CV_EXPORTS void minMaxLoc(const GpuMat& src, double* minVal, double* maxVal=0, Point* minLoc=0, Point* maxLoc=0,\r
843                                   const GpuMat& mask=GpuMat());\r
844 \r
845         //! finds global minimum and maximum array elements and returns their values with locations\r
846         CV_EXPORTS void minMaxLoc(const GpuMat& src, double* minVal, double* maxVal, Point* minLoc, Point* maxLoc,\r
847                                   const GpuMat& mask, GpuMat& valbuf, GpuMat& locbuf);\r
848 \r
849         //! counts non-zero array elements\r
850         CV_EXPORTS int countNonZero(const GpuMat& src);\r
851 \r
852         //! counts non-zero array elements\r
853         CV_EXPORTS int countNonZero(const GpuMat& src, GpuMat& buf);\r
854 \r
855 \r
856         ///////////////////////////// Calibration 3D //////////////////////////////////\r
857 \r
858         CV_EXPORTS void transformPoints(const GpuMat& src, const Mat& rvec, const Mat& tvec,\r
859                                         GpuMat& dst);\r
860 \r
861         CV_EXPORTS void transformPoints(const GpuMat& src, const Mat& rvec, const Mat& tvec,\r
862                                         GpuMat& dst, const Stream& stream);\r
863 \r
864         CV_EXPORTS void projectPoints(const GpuMat& src, const Mat& rvec, const Mat& tvec,\r
865                                       const Mat& camera_mat, const Mat& dist_coef, GpuMat& dst);\r
866 \r
867         CV_EXPORTS void projectPoints(const GpuMat& src, const Mat& rvec, const Mat& tvec,\r
868                                       const Mat& camera_mat, const Mat& dist_coef, GpuMat& dst,\r
869                                       const Stream& stream);\r
870 \r
871         CV_EXPORTS void solvePnPRansac(const Mat& object, const Mat& image, const Mat& camera_mat,\r
872                                        const Mat& dist_coef, Mat& rvec, Mat& tvec, bool use_extrinsic_guess=false,\r
873                                        int num_iters=100, float max_dist=8.0, int min_inlier_count=100, \r
874                                        vector<int>* inliers=NULL);\r
875 \r
876         //////////////////////////////// Filter Engine ////////////////////////////////\r
877 \r
878         /*!\r
879         The Base Class for 1D or Row-wise Filters\r
880 \r
881         This is the base class for linear or non-linear filters that process 1D data.\r
882         In particular, such filters are used for the "horizontal" filtering parts in separable filters.\r
883         */\r
884         class CV_EXPORTS BaseRowFilter_GPU\r
885         {\r
886         public:\r
887             BaseRowFilter_GPU(int ksize_, int anchor_) : ksize(ksize_), anchor(anchor_) {}\r
888             virtual ~BaseRowFilter_GPU() {}\r
889             virtual void operator()(const GpuMat& src, GpuMat& dst) = 0;\r
890             int ksize, anchor;\r
891         };\r
892 \r
893         /*!\r
894         The Base Class for Column-wise Filters\r
895 \r
896         This is the base class for linear or non-linear filters that process columns of 2D arrays.\r
897         Such filters are used for the "vertical" filtering parts in separable filters.\r
898         */\r
899         class CV_EXPORTS BaseColumnFilter_GPU\r
900         {\r
901         public:\r
902             BaseColumnFilter_GPU(int ksize_, int anchor_) : ksize(ksize_), anchor(anchor_) {}\r
903             virtual ~BaseColumnFilter_GPU() {}\r
904             virtual void operator()(const GpuMat& src, GpuMat& dst) = 0;\r
905             int ksize, anchor;\r
906         };\r
907 \r
908         /*!\r
909         The Base Class for Non-Separable 2D Filters.\r
910 \r
911         This is the base class for linear or non-linear 2D filters.\r
912         */\r
913         class CV_EXPORTS BaseFilter_GPU\r
914         {\r
915         public:\r
916             BaseFilter_GPU(const Size& ksize_, const Point& anchor_) : ksize(ksize_), anchor(anchor_) {}\r
917             virtual ~BaseFilter_GPU() {}\r
918             virtual void operator()(const GpuMat& src, GpuMat& dst) = 0;\r
919             Size ksize;\r
920             Point anchor;\r
921         };\r
922 \r
923         /*!\r
924         The Base Class for Filter Engine.\r
925 \r
926         The class can be used to apply an arbitrary filtering operation to an image.\r
927         It contains all the necessary intermediate buffers.\r
928         */\r
929         class CV_EXPORTS FilterEngine_GPU\r
930         {\r
931         public:\r
932             virtual ~FilterEngine_GPU() {}\r
933 \r
934             virtual void apply(const GpuMat& src, GpuMat& dst, Rect roi = Rect(0,0,-1,-1)) = 0;\r
935         };\r
936 \r
937         //! returns the non-separable filter engine with the specified filter\r
938         CV_EXPORTS Ptr<FilterEngine_GPU> createFilter2D_GPU(const Ptr<BaseFilter_GPU>& filter2D, int srcType, int dstType);\r
939 \r
940         //! returns the separable filter engine with the specified filters\r
941         CV_EXPORTS Ptr<FilterEngine_GPU> createSeparableFilter_GPU(const Ptr<BaseRowFilter_GPU>& rowFilter,\r
942             const Ptr<BaseColumnFilter_GPU>& columnFilter, int srcType, int bufType, int dstType);\r
943 \r
944         //! returns horizontal 1D box filter\r
945         //! supports only CV_8UC1 source type and CV_32FC1 sum type\r
946         CV_EXPORTS Ptr<BaseRowFilter_GPU> getRowSumFilter_GPU(int srcType, int sumType, int ksize, int anchor = -1);\r
947 \r
948         //! returns vertical 1D box filter\r
949         //! supports only CV_8UC1 sum type and CV_32FC1 dst type\r
950         CV_EXPORTS Ptr<BaseColumnFilter_GPU> getColumnSumFilter_GPU(int sumType, int dstType, int ksize, int anchor = -1);\r
951 \r
952         //! returns 2D box filter\r
953         //! supports CV_8UC1 and CV_8UC4 source type, dst type must be the same as source type\r
954         CV_EXPORTS Ptr<BaseFilter_GPU> getBoxFilter_GPU(int srcType, int dstType, const Size& ksize, Point anchor = Point(-1, -1));\r
955 \r
956         //! returns box filter engine\r
957         CV_EXPORTS Ptr<FilterEngine_GPU> createBoxFilter_GPU(int srcType, int dstType, const Size& ksize,\r
958             const Point& anchor = Point(-1,-1));\r
959 \r
960         //! returns 2D morphological filter\r
961         //! only MORPH_ERODE and MORPH_DILATE are supported\r
962         //! supports CV_8UC1 and CV_8UC4 types\r
963         //! kernel must have CV_8UC1 type, one rows and cols == ksize.width * ksize.height\r
964         CV_EXPORTS Ptr<BaseFilter_GPU> getMorphologyFilter_GPU(int op, int type, const Mat& kernel, const Size& ksize,\r
965             Point anchor=Point(-1,-1));\r
966 \r
967         //! returns morphological filter engine. Only MORPH_ERODE and MORPH_DILATE are supported.\r
968         CV_EXPORTS Ptr<FilterEngine_GPU> createMorphologyFilter_GPU(int op, int type, const Mat& kernel,\r
969             const Point& anchor = Point(-1,-1), int iterations = 1);\r
970 \r
971         //! returns 2D filter with the specified kernel\r
972         //! supports CV_8UC1 and CV_8UC4 types\r
973         CV_EXPORTS Ptr<BaseFilter_GPU> getLinearFilter_GPU(int srcType, int dstType, const Mat& kernel, const Size& ksize,\r
974             Point anchor = Point(-1, -1));\r
975 \r
976         //! returns the non-separable linear filter engine\r
977         CV_EXPORTS Ptr<FilterEngine_GPU> createLinearFilter_GPU(int srcType, int dstType, const Mat& kernel,\r
978             const Point& anchor = Point(-1,-1));\r
979 \r
980         //! returns the primitive row filter with the specified kernel.\r
981         //! supports only CV_8UC1, CV_8UC4, CV_16SC1, CV_16SC2, CV_32SC1, CV_32FC1 source type.\r
982         //! there are two version of algorithm: NPP and OpenCV.\r
983         //! NPP calls when srcType == CV_8UC1 or srcType == CV_8UC4 and bufType == srcType,\r
984         //! otherwise calls OpenCV version.\r
985         //! NPP supports only BORDER_CONSTANT border type.\r
986         //! OpenCV version supports only CV_32F as buffer depth and\r
987         //! BORDER_REFLECT101, BORDER_REPLICATE and BORDER_CONSTANT border types.\r
988         CV_EXPORTS Ptr<BaseRowFilter_GPU> getLinearRowFilter_GPU(int srcType, int bufType, const Mat& rowKernel,\r
989             int anchor = -1, int borderType = BORDER_CONSTANT);\r
990 \r
991         //! returns the primitive column filter with the specified kernel.\r
992         //! supports only CV_8UC1, CV_8UC4, CV_16SC1, CV_16SC2, CV_32SC1, CV_32FC1 dst type.\r
993         //! there are two version of algorithm: NPP and OpenCV.\r
994         //! NPP calls when dstType == CV_8UC1 or dstType == CV_8UC4 and bufType == dstType,\r
995         //! otherwise calls OpenCV version.\r
996         //! NPP supports only BORDER_CONSTANT border type.\r
997         //! OpenCV version supports only CV_32F as buffer depth and\r
998         //! BORDER_REFLECT101, BORDER_REPLICATE and BORDER_CONSTANT border types.\r
999         CV_EXPORTS Ptr<BaseColumnFilter_GPU> getLinearColumnFilter_GPU(int bufType, int dstType, const Mat& columnKernel,\r
1000             int anchor = -1, int borderType = BORDER_CONSTANT);\r
1001 \r
1002         //! returns the separable linear filter engine\r
1003         CV_EXPORTS Ptr<FilterEngine_GPU> createSeparableLinearFilter_GPU(int srcType, int dstType, const Mat& rowKernel,\r
1004             const Mat& columnKernel, const Point& anchor = Point(-1,-1), int rowBorderType = BORDER_DEFAULT,\r
1005             int columnBorderType = -1);\r
1006 \r
1007         //! returns filter engine for the generalized Sobel operator\r
1008         CV_EXPORTS Ptr<FilterEngine_GPU> createDerivFilter_GPU(int srcType, int dstType, int dx, int dy, int ksize,\r
1009             int rowBorderType = BORDER_DEFAULT, int columnBorderType = -1);\r
1010 \r
1011         //! returns the Gaussian filter engine\r
1012         CV_EXPORTS Ptr<FilterEngine_GPU> createGaussianFilter_GPU(int type, Size ksize, double sigma1, double sigma2 = 0,\r
1013             int rowBorderType = BORDER_DEFAULT, int columnBorderType = -1);\r
1014 \r
1015         //! returns maximum filter\r
1016         CV_EXPORTS Ptr<BaseFilter_GPU> getMaxFilter_GPU(int srcType, int dstType, const Size& ksize, Point anchor = Point(-1,-1));\r
1017 \r
1018         //! returns minimum filter\r
1019         CV_EXPORTS Ptr<BaseFilter_GPU> getMinFilter_GPU(int srcType, int dstType, const Size& ksize, Point anchor = Point(-1,-1));\r
1020 \r
1021         //! smooths the image using the normalized box filter\r
1022         //! supports CV_8UC1, CV_8UC4 types\r
1023         CV_EXPORTS void boxFilter(const GpuMat& src, GpuMat& dst, int ddepth, Size ksize, Point anchor = Point(-1,-1));\r
1024 \r
1025         //! a synonym for normalized box filter\r
1026         static inline void blur(const GpuMat& src, GpuMat& dst, Size ksize, Point anchor = Point(-1,-1)) { boxFilter(src, dst, -1, ksize, anchor); }\r
1027 \r
1028         //! erodes the image (applies the local minimum operator)\r
1029         CV_EXPORTS void erode( const GpuMat& src, GpuMat& dst, const Mat& kernel, Point anchor = Point(-1, -1), int iterations = 1);\r
1030 \r
1031         //! dilates the image (applies the local maximum operator)\r
1032         CV_EXPORTS void dilate( const GpuMat& src, GpuMat& dst, const Mat& kernel, Point anchor = Point(-1, -1), int iterations = 1);\r
1033 \r
1034         //! applies an advanced morphological operation to the image\r
1035         CV_EXPORTS void morphologyEx( const GpuMat& src, GpuMat& dst, int op, const Mat& kernel, Point anchor = Point(-1, -1), int iterations = 1);\r
1036 \r
1037         //! applies non-separable 2D linear filter to the image\r
1038         CV_EXPORTS void filter2D(const GpuMat& src, GpuMat& dst, int ddepth, const Mat& kernel, Point anchor=Point(-1,-1));\r
1039 \r
1040         //! applies separable 2D linear filter to the image\r
1041         CV_EXPORTS void sepFilter2D(const GpuMat& src, GpuMat& dst, int ddepth, const Mat& kernelX, const Mat& kernelY,\r
1042             Point anchor = Point(-1,-1), int rowBorderType = BORDER_DEFAULT, int columnBorderType = -1);\r
1043 \r
1044         //! applies generalized Sobel operator to the image\r
1045         CV_EXPORTS void Sobel(const GpuMat& src, GpuMat& dst, int ddepth, int dx, int dy, int ksize = 3, double scale = 1,\r
1046             int rowBorderType = BORDER_DEFAULT, int columnBorderType = -1);\r
1047 \r
1048         //! applies the vertical or horizontal Scharr operator to the image\r
1049         CV_EXPORTS void Scharr(const GpuMat& src, GpuMat& dst, int ddepth, int dx, int dy, double scale = 1,\r
1050             int rowBorderType = BORDER_DEFAULT, int columnBorderType = -1);\r
1051 \r
1052         //! smooths the image using Gaussian filter.\r
1053         CV_EXPORTS void GaussianBlur(const GpuMat& src, GpuMat& dst, Size ksize, double sigma1, double sigma2 = 0,\r
1054             int rowBorderType = BORDER_DEFAULT, int columnBorderType = -1);\r
1055 \r
1056         //! applies Laplacian operator to the image\r
1057         //! supports only ksize = 1 and ksize = 3\r
1058         CV_EXPORTS void Laplacian(const GpuMat& src, GpuMat& dst, int ddepth, int ksize = 1, double scale = 1);\r
1059 \r
1060         //////////////////////////////// Image Labeling ////////////////////////////////\r
1061 \r
1062         //!performs labeling via graph cuts\r
1063         CV_EXPORTS void graphcut(GpuMat& terminals, GpuMat& leftTransp, GpuMat& rightTransp, GpuMat& top, GpuMat& bottom, GpuMat& labels, GpuMat& buf);\r
1064 \r
1065         ////////////////////////////////// Histograms //////////////////////////////////\r
1066 \r
1067         //! Compute levels with even distribution. levels will have 1 row and nLevels cols and CV_32SC1 type.\r
1068         CV_EXPORTS void evenLevels(GpuMat& levels, int nLevels, int lowerLevel, int upperLevel);\r
1069         //! Calculates histogram with evenly distributed bins for signle channel source.\r
1070         //! Supports CV_8UC1, CV_16UC1 and CV_16SC1 source types.\r
1071         //! Output hist will have one row and histSize cols and CV_32SC1 type.\r
1072         CV_EXPORTS void histEven(const GpuMat& src, GpuMat& hist, int histSize, int lowerLevel, int upperLevel);\r
1073         //! Calculates histogram with evenly distributed bins for four-channel source.\r
1074         //! All channels of source are processed separately.\r
1075         //! Supports CV_8UC4, CV_16UC4 and CV_16SC4 source types.\r
1076         //! Output hist[i] will have one row and histSize[i] cols and CV_32SC1 type.\r
1077         CV_EXPORTS void histEven(const GpuMat& src, GpuMat hist[4], int histSize[4], int lowerLevel[4], int upperLevel[4]);\r
1078         //! Calculates histogram with bins determined by levels array.\r
1079         //! levels must have one row and CV_32SC1 type if source has integer type or CV_32FC1 otherwise.\r
1080         //! Supports CV_8UC1, CV_16UC1, CV_16SC1 and CV_32FC1 source types.\r
1081         //! Output hist will have one row and (levels.cols-1) cols and CV_32SC1 type.\r
1082         CV_EXPORTS void histRange(const GpuMat& src, GpuMat& hist, const GpuMat& levels);\r
1083         //! Calculates histogram with bins determined by levels array.\r
1084         //! All levels must have one row and CV_32SC1 type if source has integer type or CV_32FC1 otherwise.\r
1085         //! All channels of source are processed separately.\r
1086         //! Supports CV_8UC4, CV_16UC4, CV_16SC4 and CV_32FC4 source types.\r
1087         //! Output hist[i] will have one row and (levels[i].cols-1) cols and CV_32SC1 type.\r
1088         CV_EXPORTS void histRange(const GpuMat& src, GpuMat hist[4], const GpuMat levels[4]);\r
1089 \r
1090         //////////////////////////////// StereoBM_GPU ////////////////////////////////\r
1091 \r
1092         class CV_EXPORTS StereoBM_GPU\r
1093         {\r
1094         public:\r
1095             enum { BASIC_PRESET = 0, PREFILTER_XSOBEL = 1 };\r
1096 \r
1097             enum { DEFAULT_NDISP = 64, DEFAULT_WINSZ = 19 };\r
1098 \r
1099             //! the default constructor\r
1100             StereoBM_GPU();\r
1101             //! the full constructor taking the camera-specific preset, number of disparities and the SAD window size. ndisparities must be multiple of 8.\r
1102             StereoBM_GPU(int preset, int ndisparities = DEFAULT_NDISP, int winSize = DEFAULT_WINSZ);\r
1103 \r
1104             //! the stereo correspondence operator. Finds the disparity for the specified rectified stereo pair\r
1105             //! Output disparity has CV_8U type.\r
1106             void operator() ( const GpuMat& left, const GpuMat& right, GpuMat& disparity);\r
1107 \r
1108             //! async version\r
1109             void operator() ( const GpuMat& left, const GpuMat& right, GpuMat& disparity, const Stream & stream);\r
1110 \r
1111             //! Some heuristics that tries to estmate\r
1112             // if current GPU will be faster then CPU in this algorithm.\r
1113             // It queries current active device.\r
1114             static bool checkIfGpuCallReasonable();\r
1115 \r
1116             int preset;\r
1117             int ndisp;\r
1118             int winSize;\r
1119 \r
1120             // If avergeTexThreshold  == 0 => post procesing is disabled\r
1121             // If avergeTexThreshold != 0 then disparity is set 0 in each point (x,y) where for left image\r
1122             // SumOfHorizontalGradiensInWindow(x, y, winSize) < (winSize * winSize) * avergeTexThreshold\r
1123             // i.e. input left image is low textured.\r
1124             float avergeTexThreshold;\r
1125         private:\r
1126             GpuMat minSSD, leBuf, riBuf;\r
1127         };\r
1128 \r
1129         ////////////////////////// StereoBeliefPropagation ///////////////////////////\r
1130         // "Efficient Belief Propagation for Early Vision"\r
1131         // P.Felzenszwalb\r
1132 \r
1133         class CV_EXPORTS StereoBeliefPropagation\r
1134         {\r
1135         public:\r
1136             enum { DEFAULT_NDISP  = 64 };\r
1137             enum { DEFAULT_ITERS  = 5  };\r
1138             enum { DEFAULT_LEVELS = 5  };\r
1139 \r
1140             static void estimateRecommendedParams(int width, int height, int& ndisp, int& iters, int& levels);\r
1141 \r
1142             //! the default constructor\r
1143             explicit StereoBeliefPropagation(int ndisp  = DEFAULT_NDISP,\r
1144                 int iters  = DEFAULT_ITERS,\r
1145                 int levels = DEFAULT_LEVELS,\r
1146                 int msg_type = CV_32F);\r
1147 \r
1148             //! the full constructor taking the number of disparities, number of BP iterations on each level,\r
1149             //! number of levels, truncation of data cost, data weight,\r
1150             //! truncation of discontinuity cost and discontinuity single jump\r
1151             //! DataTerm = data_weight * min(fabs(I2-I1), max_data_term)\r
1152             //! DiscTerm = min(disc_single_jump * fabs(f1-f2), max_disc_term)\r
1153             //! please see paper for more details\r
1154             StereoBeliefPropagation(int ndisp, int iters, int levels,\r
1155                 float max_data_term, float data_weight,\r
1156                 float max_disc_term, float disc_single_jump,\r
1157                 int msg_type = CV_32F);\r
1158 \r
1159             //! the stereo correspondence operator. Finds the disparity for the specified rectified stereo pair,\r
1160             //! if disparity is empty output type will be CV_16S else output type will be disparity.type().\r
1161             void operator()(const GpuMat& left, const GpuMat& right, GpuMat& disparity);\r
1162 \r
1163             //! async version\r
1164             void operator()(const GpuMat& left, const GpuMat& right, GpuMat& disparity, Stream& stream);\r
1165 \r
1166 \r
1167             //! version for user specified data term\r
1168             void operator()(const GpuMat& data, GpuMat& disparity);\r
1169             void operator()(const GpuMat& data, GpuMat& disparity, Stream& stream);\r
1170 \r
1171             int ndisp;\r
1172 \r
1173             int iters;\r
1174             int levels;\r
1175 \r
1176             float max_data_term;\r
1177             float data_weight;\r
1178             float max_disc_term;\r
1179             float disc_single_jump;\r
1180 \r
1181             int msg_type;\r
1182         private:\r
1183             GpuMat u, d, l, r, u2, d2, l2, r2;\r
1184             std::vector<GpuMat> datas;\r
1185             GpuMat out;\r
1186         };\r
1187 \r
1188         /////////////////////////// StereoConstantSpaceBP ///////////////////////////\r
1189         // "A Constant-Space Belief Propagation Algorithm for Stereo Matching"\r
1190         // Qingxiong Yang, Liang Wang�, Narendra Ahuja\r
1191         // http://vision.ai.uiuc.edu/~qyang6/\r
1192 \r
1193         class CV_EXPORTS StereoConstantSpaceBP\r
1194         {\r
1195         public:\r
1196             enum { DEFAULT_NDISP    = 128 };\r
1197             enum { DEFAULT_ITERS    = 8   };\r
1198             enum { DEFAULT_LEVELS   = 4   };\r
1199             enum { DEFAULT_NR_PLANE = 4   };\r
1200 \r
1201             static void estimateRecommendedParams(int width, int height, int& ndisp, int& iters, int& levels, int& nr_plane);\r
1202 \r
1203             //! the default constructor\r
1204             explicit StereoConstantSpaceBP(int ndisp    = DEFAULT_NDISP,\r
1205                 int iters    = DEFAULT_ITERS,\r
1206                 int levels   = DEFAULT_LEVELS,\r
1207                 int nr_plane = DEFAULT_NR_PLANE,\r
1208                 int msg_type = CV_32F);\r
1209 \r
1210             //! the full constructor taking the number of disparities, number of BP iterations on each level,\r
1211             //! number of levels, number of active disparity on the first level, truncation of data cost, data weight,\r
1212             //! truncation of discontinuity cost, discontinuity single jump and minimum disparity threshold\r
1213             StereoConstantSpaceBP(int ndisp, int iters, int levels, int nr_plane,\r
1214                 float max_data_term, float data_weight, float max_disc_term, float disc_single_jump,\r
1215                 int min_disp_th = 0,\r
1216                 int msg_type = CV_32F);\r
1217 \r
1218             //! the stereo correspondence operator. Finds the disparity for the specified rectified stereo pair,\r
1219             //! if disparity is empty output type will be CV_16S else output type will be disparity.type().\r
1220             void operator()(const GpuMat& left, const GpuMat& right, GpuMat& disparity);\r
1221 \r
1222             //! async version\r
1223             void operator()(const GpuMat& left, const GpuMat& right, GpuMat& disparity, Stream& stream);\r
1224 \r
1225             int ndisp;\r
1226 \r
1227             int iters;\r
1228             int levels;\r
1229 \r
1230             int nr_plane;\r
1231 \r
1232             float max_data_term;\r
1233             float data_weight;\r
1234             float max_disc_term;\r
1235             float disc_single_jump;\r
1236 \r
1237             int min_disp_th;\r
1238 \r
1239             int msg_type;\r
1240 \r
1241             bool use_local_init_data_cost;\r
1242         private:\r
1243             GpuMat u[2], d[2], l[2], r[2];\r
1244             GpuMat disp_selected_pyr[2];\r
1245 \r
1246             GpuMat data_cost;\r
1247             GpuMat data_cost_selected;\r
1248 \r
1249             GpuMat temp;\r
1250 \r
1251             GpuMat out;\r
1252         };\r
1253 \r
1254         /////////////////////////// DisparityBilateralFilter ///////////////////////////\r
1255         // Disparity map refinement using joint bilateral filtering given a single color image.\r
1256         // Qingxiong Yang, Liang Wang�, Narendra Ahuja\r
1257         // http://vision.ai.uiuc.edu/~qyang6/\r
1258 \r
1259         class CV_EXPORTS DisparityBilateralFilter\r
1260         {\r
1261         public:\r
1262             enum { DEFAULT_NDISP  = 64 };\r
1263             enum { DEFAULT_RADIUS = 3 };\r
1264             enum { DEFAULT_ITERS  = 1 };\r
1265 \r
1266             //! the default constructor\r
1267             explicit DisparityBilateralFilter(int ndisp = DEFAULT_NDISP, int radius = DEFAULT_RADIUS, int iters = DEFAULT_ITERS);\r
1268 \r
1269             //! the full constructor taking the number of disparities, filter radius,\r
1270             //! number of iterations, truncation of data continuity, truncation of disparity continuity\r
1271             //! and filter range sigma\r
1272             DisparityBilateralFilter(int ndisp, int radius, int iters, float edge_threshold, float max_disc_threshold, float sigma_range);\r
1273 \r
1274             //! the disparity map refinement operator. Refine disparity map using joint bilateral filtering given a single color image.\r
1275             //! disparity must have CV_8U or CV_16S type, image must have CV_8UC1 or CV_8UC3 type.\r
1276             void operator()(const GpuMat& disparity, const GpuMat& image, GpuMat& dst);\r
1277 \r
1278             //! async version\r
1279             void operator()(const GpuMat& disparity, const GpuMat& image, GpuMat& dst, Stream& stream);\r
1280 \r
1281         private:\r
1282             int ndisp;\r
1283             int radius;\r
1284             int iters;\r
1285 \r
1286             float edge_threshold;\r
1287             float max_disc_threshold;\r
1288             float sigma_range;\r
1289 \r
1290             GpuMat table_color;\r
1291             GpuMat table_space;\r
1292         };\r
1293 \r
1294 \r
1295         //////////////// HOG (Histogram-of-Oriented-Gradients) Descriptor and Object Detector //////////////\r
1296 \r
1297         struct CV_EXPORTS HOGDescriptor\r
1298         {\r
1299             enum { DEFAULT_WIN_SIGMA = -1 };\r
1300             enum { DEFAULT_NLEVELS = 64 };\r
1301             enum { DESCR_FORMAT_ROW_BY_ROW, DESCR_FORMAT_COL_BY_COL };\r
1302 \r
1303             HOGDescriptor(Size win_size=Size(64, 128), Size block_size=Size(16, 16),\r
1304                           Size block_stride=Size(8, 8), Size cell_size=Size(8, 8),\r
1305                           int nbins=9, double win_sigma=DEFAULT_WIN_SIGMA,\r
1306                           double threshold_L2hys=0.2, bool gamma_correction=true,\r
1307                           int nlevels=DEFAULT_NLEVELS);\r
1308 \r
1309             size_t getDescriptorSize() const;\r
1310             size_t getBlockHistogramSize() const;\r
1311 \r
1312             void setSVMDetector(const vector<float>& detector);\r
1313 \r
1314             static vector<float> getDefaultPeopleDetector();\r
1315             static vector<float> getPeopleDetector48x96();\r
1316             static vector<float> getPeopleDetector64x128();\r
1317 \r
1318             void detect(const GpuMat& img, vector<Point>& found_locations, \r
1319                         double hit_threshold=0, Size win_stride=Size(), \r
1320                         Size padding=Size());\r
1321 \r
1322             void detectMultiScale(const GpuMat& img, vector<Rect>& found_locations,\r
1323                                   double hit_threshold=0, Size win_stride=Size(), \r
1324                                   Size padding=Size(), double scale0=1.05, \r
1325                                   int group_threshold=2);\r
1326 \r
1327             void getDescriptors(const GpuMat& img, Size win_stride, \r
1328                                 GpuMat& descriptors,\r
1329                                 int descr_format=DESCR_FORMAT_COL_BY_COL);\r
1330 \r
1331             Size win_size;\r
1332             Size block_size;\r
1333             Size block_stride;\r
1334             Size cell_size;\r
1335             int nbins;\r
1336             double win_sigma;\r
1337             double threshold_L2hys;\r
1338             bool gamma_correction;\r
1339             int nlevels;\r
1340 \r
1341         protected:\r
1342             void computeBlockHistograms(const GpuMat& img);\r
1343             void computeGradient(const GpuMat& img, GpuMat& grad, GpuMat& qangle);\r
1344 \r
1345             double getWinSigma() const;\r
1346             bool checkDetectorSize() const;\r
1347 \r
1348             static int numPartsWithin(int size, int part_size, int stride);\r
1349             static Size numPartsWithin(Size size, Size part_size, Size stride);\r
1350 \r
1351             // Coefficients of the separating plane\r
1352             float free_coef;\r
1353             GpuMat detector;\r
1354 \r
1355             // Results of the last classification step\r
1356             GpuMat labels, labels_buf;\r
1357             Mat labels_host;\r
1358 \r
1359             // Results of the last histogram evaluation step\r
1360             GpuMat block_hists, block_hists_buf;\r
1361 \r
1362             // Gradients conputation results\r
1363             GpuMat grad, qangle, grad_buf, qangle_buf;\r
1364 \r
1365                         // returns subbuffer with required size, reallocates buffer if nessesary.\r
1366                         static GpuMat getBuffer(const Size& sz, int type, GpuMat& buf);\r
1367                         static GpuMat getBuffer(int rows, int cols, int type, GpuMat& buf);\r
1368 \r
1369                         std::vector<GpuMat> image_scales;\r
1370         };\r
1371 \r
1372 \r
1373         ////////////////////////////////// BruteForceMatcher //////////////////////////////////\r
1374 \r
1375         class CV_EXPORTS BruteForceMatcher_GPU_base\r
1376         {\r
1377         public:\r
1378             enum DistType {L1Dist = 0, L2Dist};\r
1379 \r
1380             explicit BruteForceMatcher_GPU_base(DistType distType = L2Dist);\r
1381 \r
1382             // Add descriptors to train descriptor collection.\r
1383             void add(const std::vector<GpuMat>& descCollection);\r
1384 \r
1385             // Get train descriptors collection.\r
1386             const std::vector<GpuMat>& getTrainDescriptors() const;\r
1387 \r
1388             // Clear train descriptors collection.\r
1389             void clear();\r
1390 \r
1391             // Return true if there are not train descriptors in collection.\r
1392             bool empty() const;\r
1393 \r
1394             // Return true if the matcher supports mask in match methods.\r
1395             bool isMaskSupported() const;\r
1396 \r
1397             // Find one best match for each query descriptor.\r
1398             // trainIdx.at<int>(0, queryIdx) will contain best train index for queryIdx\r
1399             // distance.at<float>(0, queryIdx) will contain distance\r
1400             void matchSingle(const GpuMat& queryDescs, const GpuMat& trainDescs,\r
1401                 GpuMat& trainIdx, GpuMat& distance,\r
1402                 const GpuMat& mask = GpuMat());\r
1403 \r
1404             // Download trainIdx and distance to CPU vector with DMatch\r
1405             static void matchDownload(const GpuMat& trainIdx, const GpuMat& distance, std::vector<DMatch>& matches);\r
1406 \r
1407             // Find one best match for each query descriptor.\r
1408             void match(const GpuMat& queryDescs, const GpuMat& trainDescs, std::vector<DMatch>& matches,\r
1409                 const GpuMat& mask = GpuMat());\r
1410 \r
1411             // Make gpu collection of trains and masks in suitable format for matchCollection function\r
1412             void makeGpuCollection(GpuMat& trainCollection, GpuMat& maskCollection,\r
1413                 const vector<GpuMat>& masks = std::vector<GpuMat>());\r
1414 \r
1415             // Find one best match from train collection for each query descriptor.\r
1416             // trainIdx.at<int>(0, queryIdx) will contain best train index for queryIdx\r
1417             // imgIdx.at<int>(0, queryIdx) will contain best image index for queryIdx\r
1418             // distance.at<float>(0, queryIdx) will contain distance\r
1419             void matchCollection(const GpuMat& queryDescs, const GpuMat& trainCollection,\r
1420                 GpuMat& trainIdx, GpuMat& imgIdx, GpuMat& distance,\r
1421                 const GpuMat& maskCollection);\r
1422 \r
1423             // Download trainIdx, imgIdx and distance to CPU vector with DMatch\r
1424             static void matchDownload(const GpuMat& trainIdx, const GpuMat& imgIdx, const GpuMat& distance,\r
1425                 std::vector<DMatch>& matches);\r
1426 \r
1427             // Find one best match from train collection for each query descriptor.\r
1428             void match(const GpuMat& queryDescs, std::vector<DMatch>& matches,\r
1429                 const std::vector<GpuMat>& masks = std::vector<GpuMat>());\r
1430 \r
1431             // Find k best matches for each query descriptor (in increasing order of distances).\r
1432             // trainIdx.at<int>(queryIdx, i) will contain index of i'th best trains (i < k).\r
1433             // distance.at<float>(queryIdx, i) will contain distance.\r
1434             // allDist is a buffer to store all distance between query descriptors and train descriptors\r
1435             // it have size (nQuery,nTrain) and CV_32F type\r
1436             // allDist.at<float>(queryIdx, trainIdx) will contain FLT_MAX, if trainIdx is one from k best,\r
1437             // otherwise it will contain distance between queryIdx and trainIdx descriptors\r
1438             void knnMatch(const GpuMat& queryDescs, const GpuMat& trainDescs,\r
1439                 GpuMat& trainIdx, GpuMat& distance, GpuMat& allDist, int k, const GpuMat& mask = GpuMat());\r
1440 \r
1441             // Download trainIdx and distance to CPU vector with DMatch\r
1442             // compactResult is used when mask is not empty. If compactResult is false matches\r
1443             // vector will have the same size as queryDescriptors rows. If compactResult is true\r
1444             // matches vector will not contain matches for fully masked out query descriptors.\r
1445             static void knnMatchDownload(const GpuMat& trainIdx, const GpuMat& distance,\r
1446                 std::vector< std::vector<DMatch> >& matches, bool compactResult = false);\r
1447 \r
1448             // Find k best matches for each query descriptor (in increasing order of distances).\r
1449             // compactResult is used when mask is not empty. If compactResult is false matches\r
1450             // vector will have the same size as queryDescriptors rows. If compactResult is true\r
1451             // matches vector will not contain matches for fully masked out query descriptors.\r
1452             void knnMatch(const GpuMat& queryDescs, const GpuMat& trainDescs,\r
1453                 std::vector< std::vector<DMatch> >& matches, int k, const GpuMat& mask = GpuMat(),\r
1454                 bool compactResult = false);\r
1455 \r
1456             // Find k best matches  for each query descriptor (in increasing order of distances).\r
1457             // compactResult is used when mask is not empty. If compactResult is false matches\r
1458             // vector will have the same size as queryDescriptors rows. If compactResult is true\r
1459             // matches vector will not contain matches for fully masked out query descriptors.\r
1460             void knnMatch(const GpuMat& queryDescs, std::vector< std::vector<DMatch> >& matches, int knn,\r
1461                 const std::vector<GpuMat>& masks = std::vector<GpuMat>(), bool compactResult = false );\r
1462 \r
1463             // Find best matches for each query descriptor which have distance less than maxDistance.\r
1464             // nMatches.at<unsigned int>(0, queruIdx) will contain matches count for queryIdx.\r
1465             // carefully nMatches can be greater than trainIdx.cols - it means that matcher didn't find all matches,\r
1466             // because it didn't have enough memory.\r
1467             // trainIdx.at<int>(queruIdx, i) will contain ith train index (i < min(nMatches.at<unsigned int>(0, queruIdx), trainIdx.cols))\r
1468             // distance.at<int>(queruIdx, i) will contain ith distance (i < min(nMatches.at<unsigned int>(0, queruIdx), trainIdx.cols))\r
1469             // If trainIdx is empty, then trainIdx and distance will be created with size nQuery x nTrain,\r
1470             // otherwize user can pass own allocated trainIdx and distance with size nQuery x nMaxMatches\r
1471             // Matches doesn't sorted.\r
1472             void radiusMatch(const GpuMat& queryDescs, const GpuMat& trainDescs,\r
1473                 GpuMat& trainIdx, GpuMat& nMatches, GpuMat& distance, float maxDistance,\r
1474                 const GpuMat& mask = GpuMat());\r
1475 \r
1476             // Download trainIdx, nMatches and distance to CPU vector with DMatch.\r
1477             // matches will be sorted in increasing order of distances.\r
1478             // compactResult is used when mask is not empty. If compactResult is false matches\r
1479             // vector will have the same size as queryDescriptors rows. If compactResult is true\r
1480             // matches vector will not contain matches for fully masked out query descriptors.\r
1481             static void radiusMatchDownload(const GpuMat& trainIdx, const GpuMat& nMatches, const GpuMat& distance,\r
1482                 std::vector< std::vector<DMatch> >& matches, bool compactResult = false);\r
1483 \r
1484             // Find best matches for each query descriptor which have distance less than maxDistance\r
1485             // in increasing order of distances).\r
1486             void radiusMatch(const GpuMat& queryDescs, const GpuMat& trainDescs,\r
1487                 std::vector< std::vector<DMatch> >& matches, float maxDistance,\r
1488                 const GpuMat& mask = GpuMat(), bool compactResult = false);\r
1489 \r
1490             // Find best matches from train collection for each query descriptor which have distance less than\r
1491             // maxDistance (in increasing order of distances).\r
1492             void radiusMatch(const GpuMat& queryDescs, std::vector< std::vector<DMatch> >& matches, float maxDistance,\r
1493                 const std::vector<GpuMat>& masks = std::vector<GpuMat>(), bool compactResult = false);\r
1494 \r
1495         private:\r
1496             DistType distType;\r
1497 \r
1498             std::vector<GpuMat> trainDescCollection;\r
1499         };\r
1500 \r
1501         template <class Distance>\r
1502         class CV_EXPORTS BruteForceMatcher_GPU;\r
1503 \r
1504         template <typename T>\r
1505         class CV_EXPORTS BruteForceMatcher_GPU< L1<T> > : public BruteForceMatcher_GPU_base\r
1506         {\r
1507         public:\r
1508             explicit BruteForceMatcher_GPU() : BruteForceMatcher_GPU_base(L1Dist) {}\r
1509             explicit BruteForceMatcher_GPU(L1<T> /*d*/) : BruteForceMatcher_GPU_base(L1Dist) {}\r
1510         };\r
1511         template <typename T>\r
1512         class CV_EXPORTS BruteForceMatcher_GPU< L2<T> > : public BruteForceMatcher_GPU_base\r
1513         {\r
1514         public:\r
1515             explicit BruteForceMatcher_GPU() : BruteForceMatcher_GPU_base(L2Dist) {}\r
1516             explicit BruteForceMatcher_GPU(L2<T> /*d*/) : BruteForceMatcher_GPU_base(L2Dist) {}\r
1517         };\r
1518 \r
1519         ////////////////////////////////// CascadeClassifier_GPU //////////////////////////////////////////\r
1520         // The cascade classifier class for object detection.\r
1521         class CV_EXPORTS CascadeClassifier_GPU\r
1522         {\r
1523         public:            \r
1524             CascadeClassifier_GPU();\r
1525             CascadeClassifier_GPU(const string& filename);\r
1526             ~CascadeClassifier_GPU();\r
1527 \r
1528             bool empty() const;\r
1529             bool load(const string& filename);\r
1530             void release();\r
1531             \r
1532             /* returns number of detected objects */\r
1533             int detectMultiScale( const GpuMat& image, GpuMat& objectsBuf, double scaleFactor=1.2, int minNeighbors=4, Size minSize=Size());\r
1534                                     \r
1535             bool findLargestObject;\r
1536             bool visualizeInPlace;\r
1537 \r
1538             Size getClassifierSize() const;\r
1539         private:\r
1540             \r
1541             struct CascadeClassifierImpl;                        \r
1542             CascadeClassifierImpl* impl;            \r
1543         };\r
1544         \r
1545         ////////////////////////////////// SURF //////////////////////////////////////////\r
1546 \r
1547         class CV_EXPORTS SURF_GPU : public CvSURFParams\r
1548         {\r
1549         public:\r
1550             //! the default constructor\r
1551             SURF_GPU();\r
1552             //! the full constructor taking all the necessary parameters\r
1553             explicit SURF_GPU(double _hessianThreshold, int _nOctaves=4,\r
1554                  int _nOctaveLayers=2, bool _extended=false, float _keypointsRatio=0.01f, bool _upright = false);\r
1555 \r
1556             //! returns the descriptor size in float's (64 or 128)\r
1557             int descriptorSize() const;\r
1558 \r
1559             //! upload host keypoints to device memory\r
1560             void uploadKeypoints(const vector<KeyPoint>& keypoints, GpuMat& keypointsGPU);\r
1561             //! download keypoints from device to host memory\r
1562             void downloadKeypoints(const GpuMat& keypointsGPU, vector<KeyPoint>& keypoints);\r
1563 \r
1564             //! download descriptors from device to host memory\r
1565             void downloadDescriptors(const GpuMat& descriptorsGPU, vector<float>& descriptors);\r
1566             \r
1567             //! finds the keypoints using fast hessian detector used in SURF\r
1568             //! supports CV_8UC1 images\r
1569             //! keypoints will have 1 row and type CV_32FC(6)\r
1570             //! keypoints.at<float[6]>(1, i) contains i'th keypoint\r
1571             //! format: (x, y, laplacian, size, dir, hessian)\r
1572             void operator()(const GpuMat& img, const GpuMat& mask, GpuMat& keypoints);\r
1573             //! finds the keypoints and computes their descriptors. \r
1574             //! Optionally it can compute descriptors for the user-provided keypoints and recompute keypoints direction\r
1575             void operator()(const GpuMat& img, const GpuMat& mask, GpuMat& keypoints, GpuMat& descriptors, \r
1576                 bool useProvidedKeypoints = false);\r
1577 \r
1578             void operator()(const GpuMat& img, const GpuMat& mask, std::vector<KeyPoint>& keypoints);\r
1579             void operator()(const GpuMat& img, const GpuMat& mask, std::vector<KeyPoint>& keypoints, GpuMat& descriptors, \r
1580                 bool useProvidedKeypoints = false);\r
1581 \r
1582             void operator()(const GpuMat& img, const GpuMat& mask, std::vector<KeyPoint>& keypoints, std::vector<float>& descriptors, \r
1583                 bool useProvidedKeypoints = false);\r
1584 \r
1585             //! max keypoints = min(keypointsRatio * img.size().area(), 65535)\r
1586             float keypointsRatio;\r
1587 \r
1588             bool upright;\r
1589 \r
1590             GpuMat sum, mask1, maskSum, intBuffer;\r
1591 \r
1592             GpuMat det, trace;\r
1593 \r
1594             GpuMat maxPosBuffer;\r
1595             GpuMat featuresBuffer;\r
1596             GpuMat keypointsBuffer;\r
1597         };\r
1598 \r
1599     }\r
1600 \r
1601     //! Speckle filtering - filters small connected components on diparity image.\r
1602     //! It sets pixel (x,y) to newVal if it coresponds to small CC with size < maxSpeckleSize.\r
1603     //! Threshold for border between CC is diffThreshold;\r
1604     CV_EXPORTS void filterSpeckles( Mat& img, uchar newVal, int maxSpeckleSize, uchar diffThreshold, Mat& buf);\r
1605 \r
1606 }\r
1607 #include "opencv2/gpu/matrix_operations.hpp"\r
1608 \r
1609 #endif /* __OPENCV_GPU_HPP__ */\r