Merge pull request #1835 from SpecLad:windows-xadd
[platform/upstream/opencv.git] / modules / ocl / include / opencv2 / ocl.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) 2010-2012, Institute Of Software Chinese Academy Of Science, all rights reserved.
14 // Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
15 // Copyright (C) 2010-2012, Multicoreware, Inc., all rights reserved.
16 // Third party copyrights are property of their respective owners.
17 //
18 // Redistribution and use in source and binary forms, with or without modification,
19 // are permitted provided that the following conditions are met:
20 //
21 //   * Redistribution's of source code must retain the above copyright notice,
22 //     this list of conditions and the following disclaimer.
23 //
24 //   * Redistribution's in binary form must reproduce the above copyright notice,
25 //     this list of conditions and the following disclaimer in the documentation
26 //     and/or other materials provided with the distribution.
27 //
28 //   * The name of the copyright holders may not be used to endorse or promote products
29 //     derived from this software without specific prior written permission.
30 //
31 // This software is provided by the copyright holders and contributors "as is" and
32 // any express or implied warranties, including, but not limited to, the implied
33 // warranties of merchantability and fitness for a particular purpose are disclaimed.
34 // In no event shall the Intel Corporation or contributors be liable for any direct,
35 // indirect, incidental, special, exemplary, or consequential damages
36 // (including, but not limited to, procurement of substitute goods or services;
37 // loss of use, data, or profits; or business interruption) however caused
38 // and on any theory of liability, whether in contract, strict liability,
39 // or tort (including negligence or otherwise) arising in any way out of
40 // the use of this software, even if advised of the possibility of such damage.
41 //
42 //M*/
43
44 #ifndef __OPENCV_OCL_HPP__
45 #define __OPENCV_OCL_HPP__
46
47 #include <memory>
48 #include <vector>
49
50 #include "opencv2/core.hpp"
51 #include "opencv2/imgproc.hpp"
52 #include "opencv2/objdetect.hpp"
53 #include "opencv2/ml.hpp"
54
55 namespace cv
56 {
57     namespace ocl
58     {
59         enum DeviceType
60         {
61             CVCL_DEVICE_TYPE_DEFAULT     = (1 << 0),
62             CVCL_DEVICE_TYPE_CPU         = (1 << 1),
63             CVCL_DEVICE_TYPE_GPU         = (1 << 2),
64             CVCL_DEVICE_TYPE_ACCELERATOR = (1 << 3),
65             //CVCL_DEVICE_TYPE_CUSTOM      = (1 << 4)
66             CVCL_DEVICE_TYPE_ALL         = 0xFFFFFFFF
67         };
68
69         enum DevMemRW
70         {
71             DEVICE_MEM_R_W = 0,
72             DEVICE_MEM_R_ONLY,
73             DEVICE_MEM_W_ONLY
74         };
75
76         enum DevMemType
77         {
78             DEVICE_MEM_DEFAULT = 0,
79             DEVICE_MEM_AHP,         //alloc host pointer
80             DEVICE_MEM_UHP,         //use host pointer
81             DEVICE_MEM_CHP,         //copy host pointer
82             DEVICE_MEM_PM           //persistent memory
83         };
84
85         // these classes contain OpenCL runtime information
86
87         struct PlatformInfo;
88
89         struct DeviceInfo
90         {
91         public:
92             int _id; // reserved, don't use it
93
94             DeviceType deviceType;
95             std::string deviceProfile;
96             std::string deviceVersion;
97             std::string deviceName;
98             std::string deviceVendor;
99             int deviceVendorId;
100             std::string deviceDriverVersion;
101             std::string deviceExtensions;
102
103             size_t maxWorkGroupSize;
104             std::vector<size_t> maxWorkItemSizes;
105             int maxComputeUnits;
106             size_t localMemorySize;
107             size_t maxMemAllocSize;
108
109             int deviceVersionMajor;
110             int deviceVersionMinor;
111
112             bool haveDoubleSupport;
113             bool isUnifiedMemory; // 1 means integrated GPU, otherwise this value is 0
114             bool isIntelDevice;
115
116             std::string compilationExtraOptions;
117
118             const PlatformInfo* platform;
119
120             DeviceInfo();
121         };
122
123         struct PlatformInfo
124         {
125             int _id; // reserved, don't use it
126
127             std::string platformProfile;
128             std::string platformVersion;
129             std::string platformName;
130             std::string platformVendor;
131             std::string platformExtensons;
132
133             int platformVersionMajor;
134             int platformVersionMinor;
135
136             std::vector<const DeviceInfo*> devices;
137
138             PlatformInfo();
139         };
140
141         //////////////////////////////// Initialization & Info ////////////////////////
142         typedef std::vector<const PlatformInfo*> PlatformsInfo;
143
144         CV_EXPORTS int getOpenCLPlatforms(PlatformsInfo& platforms);
145
146         typedef std::vector<const DeviceInfo*> DevicesInfo;
147
148         CV_EXPORTS int getOpenCLDevices(DevicesInfo& devices, int deviceType = CVCL_DEVICE_TYPE_GPU,
149                 const PlatformInfo* platform = NULL);
150
151         // set device you want to use
152         CV_EXPORTS void setDevice(const DeviceInfo* info);
153
154         enum FEATURE_TYPE
155         {
156             FEATURE_CL_DOUBLE = 1,
157             FEATURE_CL_UNIFIED_MEM,
158             FEATURE_CL_VER_1_2,
159             FEATURE_CL_INTEL_DEVICE
160         };
161
162         // Represents OpenCL context, interface
163         class CV_EXPORTS Context
164         {
165         protected:
166             Context() { }
167             ~Context() { }
168         public:
169             static Context *getContext();
170
171             bool supportsFeature(FEATURE_TYPE featureType) const;
172             const DeviceInfo& getDeviceInfo() const;
173
174             const void* getOpenCLContextPtr() const;
175             const void* getOpenCLCommandQueuePtr() const;
176             const void* getOpenCLDeviceIDPtr() const;
177         };
178
179         inline const void *getClContextPtr()
180         {
181             return Context::getContext()->getOpenCLContextPtr();
182         }
183
184         inline const void *getClCommandQueuePtr()
185         {
186             return Context::getContext()->getOpenCLCommandQueuePtr();
187         }
188
189         CV_EXPORTS bool supportsFeature(FEATURE_TYPE featureType);
190
191         CV_EXPORTS void finish();
192
193         enum BINARY_CACHE_MODE
194         {
195             CACHE_NONE    = 0,        // do not cache OpenCL binary
196             CACHE_DEBUG   = 0x1 << 0, // cache OpenCL binary when built in debug mode
197             CACHE_RELEASE = 0x1 << 1, // default behavior, only cache when built in release mode
198             CACHE_ALL     = CACHE_DEBUG | CACHE_RELEASE, // cache opencl binary
199         };
200         //! Enable or disable OpenCL program binary caching onto local disk
201         // After a program (*.cl files in opencl/ folder) is built at runtime, we allow the
202         // compiled OpenCL program to be cached to the path automatically as "path/*.clb"
203         // binary file, which will be reused when the OpenCV executable is started again.
204         //
205         // This feature is enabled by default.
206         CV_EXPORTS void setBinaryDiskCache(int mode = CACHE_RELEASE, cv::String path = "./");
207
208         //! set where binary cache to be saved to
209         CV_EXPORTS void setBinaryPath(const char *path);
210
211         struct ProgramSource
212         {
213             const char* name;
214             const char* programStr;
215             const char* programHash;
216
217             // Cache in memory by name (should be unique). Caching on disk disabled.
218             inline ProgramSource(const char* _name, const char* _programStr)
219                 : name(_name), programStr(_programStr), programHash(NULL)
220             {
221             }
222
223             // Cache in memory by name (should be unique). Caching on disk uses programHash mark.
224             inline ProgramSource(const char* _name, const char* _programStr, const char* _programHash)
225                 : name(_name), programStr(_programStr), programHash(_programHash)
226             {
227             }
228         };
229
230         //! Calls OpenCL kernel. Pass globalThreads = NULL, and cleanUp = true, to finally clean-up without executing.
231         //! Deprecated, will be replaced
232         CV_EXPORTS void openCLExecuteKernelInterop(Context *clCxt,
233                 const cv::ocl::ProgramSource& source, String kernelName,
234                 size_t globalThreads[3], size_t localThreads[3],
235                 std::vector< std::pair<size_t, const void *> > &args,
236                 int channels, int depth, const char *build_options);
237
238         class CV_EXPORTS oclMatExpr;
239         //////////////////////////////// oclMat ////////////////////////////////
240         class CV_EXPORTS oclMat
241         {
242         public:
243             //! default constructor
244             oclMat();
245             //! constructs oclMatrix of the specified size and type (_type is CV_8UC1, CV_64FC3, CV_32SC(12) etc.)
246             oclMat(int rows, int cols, int type);
247             oclMat(Size size, int type);
248             //! constucts oclMatrix and fills it with the specified value _s.
249             oclMat(int rows, int cols, int type, const Scalar &s);
250             oclMat(Size size, int type, const Scalar &s);
251             //! copy constructor
252             oclMat(const oclMat &m);
253
254             //! constructor for oclMatrix headers pointing to user-allocated data
255             oclMat(int rows, int cols, int type, void *data, size_t step = Mat::AUTO_STEP);
256             oclMat(Size size, int type, void *data, size_t step = Mat::AUTO_STEP);
257
258             //! creates a matrix header for a part of the bigger matrix
259             oclMat(const oclMat &m, const Range &rowRange, const Range &colRange);
260             oclMat(const oclMat &m, const Rect &roi);
261
262             //! builds oclMat from Mat. Perfom blocking upload to device.
263             explicit oclMat (const Mat &m);
264
265             //! destructor - calls release()
266             ~oclMat();
267
268             //! assignment operators
269             oclMat &operator = (const oclMat &m);
270             //! assignment operator. Perfom blocking upload to device.
271             oclMat &operator = (const Mat &m);
272             oclMat &operator = (const oclMatExpr& expr);
273
274             //! pefroms blocking upload data to oclMat.
275             void upload(const cv::Mat &m);
276
277
278             //! downloads data from device to host memory. Blocking calls.
279             operator Mat() const;
280             void download(cv::Mat &m) const;
281
282             //! convert to _InputArray
283             operator _InputArray();
284
285             //! convert to _OutputArray
286             operator _OutputArray();
287
288             //! returns a new oclMatrix header for the specified row
289             oclMat row(int y) const;
290             //! returns a new oclMatrix header for the specified column
291             oclMat col(int x) const;
292             //! ... for the specified row span
293             oclMat rowRange(int startrow, int endrow) const;
294             oclMat rowRange(const Range &r) const;
295             //! ... for the specified column span
296             oclMat colRange(int startcol, int endcol) const;
297             oclMat colRange(const Range &r) const;
298
299             //! returns deep copy of the oclMatrix, i.e. the data is copied
300             oclMat clone() const;
301
302             //! copies those oclMatrix elements to "m" that are marked with non-zero mask elements.
303             // It calls m.create(this->size(), this->type()).
304             // It supports any data type
305             void copyTo( oclMat &m, const oclMat &mask = oclMat()) const;
306
307             //! converts oclMatrix to another datatype with optional scalng. See cvConvertScale.
308             void convertTo( oclMat &m, int rtype, double alpha = 1, double beta = 0 ) const;
309
310             void assignTo( oclMat &m, int type = -1 ) const;
311
312             //! sets every oclMatrix element to s
313             oclMat& operator = (const Scalar &s);
314             //! sets some of the oclMatrix elements to s, according to the mask
315             oclMat& setTo(const Scalar &s, const oclMat &mask = oclMat());
316             //! creates alternative oclMatrix header for the same data, with different
317             // number of channels and/or different number of rows. see cvReshape.
318             oclMat reshape(int cn, int rows = 0) const;
319
320             //! allocates new oclMatrix data unless the oclMatrix already has specified size and type.
321             // previous data is unreferenced if needed.
322             void create(int rows, int cols, int type);
323             void create(Size size, int type);
324
325             //! allocates new oclMatrix with specified device memory type.
326             void createEx(int rows, int cols, int type,
327                           DevMemRW rw_type, DevMemType mem_type);
328             void createEx(Size size, int type, DevMemRW rw_type,
329                           DevMemType mem_type);
330
331             //! decreases reference counter;
332             // deallocate the data when reference counter reaches 0.
333             void release();
334
335             //! swaps with other smart pointer
336             void swap(oclMat &mat);
337
338             //! locates oclMatrix header within a parent oclMatrix. See below
339             void locateROI( Size &wholeSize, Point &ofs ) const;
340             //! moves/resizes the current oclMatrix ROI inside the parent oclMatrix.
341             oclMat& adjustROI( int dtop, int dbottom, int dleft, int dright );
342             //! extracts a rectangular sub-oclMatrix
343             // (this is a generalized form of row, rowRange etc.)
344             oclMat operator()( Range rowRange, Range colRange ) const;
345             oclMat operator()( const Rect &roi ) const;
346
347             oclMat& operator+=( const oclMat& m );
348             oclMat& operator-=( const oclMat& m );
349             oclMat& operator*=( const oclMat& m );
350             oclMat& operator/=( const oclMat& m );
351
352             //! returns true if the oclMatrix data is continuous
353             // (i.e. when there are no gaps between successive rows).
354             // similar to CV_IS_oclMat_CONT(cvoclMat->type)
355             bool isContinuous() const;
356             //! returns element size in bytes,
357             // similar to CV_ELEM_SIZE(cvMat->type)
358             size_t elemSize() const;
359             //! returns the size of element channel in bytes.
360             size_t elemSize1() const;
361             //! returns element type, similar to CV_MAT_TYPE(cvMat->type)
362             int type() const;
363             //! returns element type, i.e. 8UC3 returns 8UC4 because in ocl
364             //! 3 channels element actually use 4 channel space
365             int ocltype() const;
366             //! returns element type, similar to CV_MAT_DEPTH(cvMat->type)
367             int depth() const;
368             //! returns element type, similar to CV_MAT_CN(cvMat->type)
369             int channels() const;
370             //! returns element type, return 4 for 3 channels element,
371             //!becuase 3 channels element actually use 4 channel space
372             int oclchannels() const;
373             //! returns step/elemSize1()
374             size_t step1() const;
375             //! returns oclMatrix size:
376             // width == number of columns, height == number of rows
377             Size size() const;
378             //! returns true if oclMatrix data is NULL
379             bool empty() const;
380
381             //! returns pointer to y-th row
382             uchar* ptr(int y = 0);
383             const uchar *ptr(int y = 0) const;
384
385             //! template version of the above method
386             template<typename _Tp> _Tp *ptr(int y = 0);
387             template<typename _Tp> const _Tp *ptr(int y = 0) const;
388
389             //! matrix transposition
390             oclMat t() const;
391
392             /*! includes several bit-fields:
393               - the magic signature
394               - continuity flag
395               - depth
396               - number of channels
397               */
398             int flags;
399             //! the number of rows and columns
400             int rows, cols;
401             //! a distance between successive rows in bytes; includes the gap if any
402             size_t step;
403             //! pointer to the data(OCL memory object)
404             uchar *data;
405
406             //! pointer to the reference counter;
407             // when oclMatrix points to user-allocated data, the pointer is NULL
408             int *refcount;
409
410             //! helper fields used in locateROI and adjustROI
411             //datastart and dataend are not used in current version
412             uchar *datastart;
413             uchar *dataend;
414
415             //! OpenCL context associated with the oclMat object.
416             Context *clCxt; // TODO clCtx
417             //add offset for handle ROI, calculated in byte
418             int offset;
419             //add wholerows and wholecols for the whole matrix, datastart and dataend are no longer used
420             int wholerows;
421             int wholecols;
422         };
423
424         // convert InputArray/OutputArray to oclMat references
425         CV_EXPORTS oclMat& getOclMatRef(InputArray src);
426         CV_EXPORTS oclMat& getOclMatRef(OutputArray src);
427
428         ///////////////////// mat split and merge /////////////////////////////////
429         //! Compose a multi-channel array from several single-channel arrays
430         // Support all types
431         CV_EXPORTS void merge(const oclMat *src, size_t n, oclMat &dst);
432         CV_EXPORTS void merge(const std::vector<oclMat> &src, oclMat &dst);
433
434         //! Divides multi-channel array into several single-channel arrays
435         // Support all types
436         CV_EXPORTS void split(const oclMat &src, oclMat *dst);
437         CV_EXPORTS void split(const oclMat &src, std::vector<oclMat> &dst);
438
439         ////////////////////////////// Arithmetics ///////////////////////////////////
440
441         //! adds one matrix to another with scale (dst = src1 * alpha + src2 * beta + gama)
442         // supports all data types
443         CV_EXPORTS void addWeighted(const oclMat &src1, double  alpha, const oclMat &src2, double beta, double gama, oclMat &dst);
444
445         //! adds one matrix to another (dst = src1 + src2)
446         // supports all data types
447         CV_EXPORTS void add(const oclMat &src1, const oclMat &src2, oclMat &dst, const oclMat &mask = oclMat());
448         //! adds scalar to a matrix (dst = src1 + s)
449         // supports all data types
450         CV_EXPORTS void add(const oclMat &src1, const Scalar &s, oclMat &dst, const oclMat &mask = oclMat());
451
452         //! subtracts one matrix from another (dst = src1 - src2)
453         // supports all data types
454         CV_EXPORTS void subtract(const oclMat &src1, const oclMat &src2, oclMat &dst, const oclMat &mask = oclMat());
455         //! subtracts scalar from a matrix (dst = src1 - s)
456         // supports all data types
457         CV_EXPORTS void subtract(const oclMat &src1, const Scalar &s, oclMat &dst, const oclMat &mask = oclMat());
458
459         //! computes element-wise product of the two arrays (dst = src1 * scale * src2)
460         // supports all data types
461         CV_EXPORTS void multiply(const oclMat &src1, const oclMat &src2, oclMat &dst, double scale = 1);
462         //! multiplies matrix to a number (dst = scalar * src)
463         // supports all data types
464         CV_EXPORTS void multiply(double scalar, const oclMat &src, oclMat &dst);
465
466         //! computes element-wise quotient of the two arrays (dst = src1 * scale / src2)
467         // supports all data types
468         CV_EXPORTS void divide(const oclMat &src1, const oclMat &src2, oclMat &dst, double scale = 1);
469         //! computes element-wise quotient of the two arrays (dst = scale / src)
470         // supports all data types
471         CV_EXPORTS void divide(double scale, const oclMat &src1, oclMat &dst);
472
473         //! computes element-wise minimum of the two arrays (dst = min(src1, src2))
474         // supports all data types
475         CV_EXPORTS void min(const oclMat &src1, const oclMat &src2, oclMat &dst);
476
477         //! computes element-wise maximum of the two arrays (dst = max(src1, src2))
478         // supports all data types
479         CV_EXPORTS void max(const oclMat &src1, const oclMat &src2, oclMat &dst);
480
481         //! compares elements of two arrays (dst = src1 <cmpop> src2)
482         // supports all data types
483         CV_EXPORTS void compare(const oclMat &src1, const oclMat &src2, oclMat &dst, int cmpop);
484
485         //! transposes the matrix
486         // supports all data types
487         CV_EXPORTS void transpose(const oclMat &src, oclMat &dst);
488
489         //! computes element-wise absolute values of an array (dst = abs(src))
490         // supports all data types
491         CV_EXPORTS void abs(const oclMat &src, oclMat &dst);
492
493         //! computes element-wise absolute difference of two arrays (dst = abs(src1 - src2))
494         // supports all data types
495         CV_EXPORTS void absdiff(const oclMat &src1, const oclMat &src2, oclMat &dst);
496         //! computes element-wise absolute difference of array and scalar (dst = abs(src1 - s))
497         // supports all data types
498         CV_EXPORTS void absdiff(const oclMat &src1, const Scalar &s, oclMat &dst);
499
500         //! computes mean value and standard deviation of all or selected array elements
501         // supports all data types
502         CV_EXPORTS void meanStdDev(const oclMat &mtx, Scalar &mean, Scalar &stddev);
503
504         //! computes norm of array
505         // supports NORM_INF, NORM_L1, NORM_L2
506         // supports all data types
507         CV_EXPORTS double norm(const oclMat &src1, int normType = NORM_L2);
508
509         //! computes norm of the difference between two arrays
510         // supports NORM_INF, NORM_L1, NORM_L2
511         // supports all data types
512         CV_EXPORTS double norm(const oclMat &src1, const oclMat &src2, int normType = NORM_L2);
513
514         //! reverses the order of the rows, columns or both in a matrix
515         // supports all types
516         CV_EXPORTS void flip(const oclMat &src, oclMat &dst, int flipCode);
517
518         //! computes sum of array elements
519         // support all types
520         CV_EXPORTS Scalar sum(const oclMat &m);
521         CV_EXPORTS Scalar absSum(const oclMat &m);
522         CV_EXPORTS Scalar sqrSum(const oclMat &m);
523
524         //! finds global minimum and maximum array elements and returns their values
525         // support all C1 types
526         CV_EXPORTS void minMax(const oclMat &src, double *minVal, double *maxVal = 0, const oclMat &mask = oclMat());
527
528         //! finds global minimum and maximum array elements and returns their values with locations
529         // support all C1 types
530         CV_EXPORTS void minMaxLoc(const oclMat &src, double *minVal, double *maxVal = 0, Point *minLoc = 0, Point *maxLoc = 0,
531                                   const oclMat &mask = oclMat());
532
533         //! counts non-zero array elements
534         // support all types
535         CV_EXPORTS int countNonZero(const oclMat &src);
536
537         //! transforms 8-bit unsigned integers using lookup table: dst(i)=lut(src(i))
538         // destination array will have the depth type as lut and the same channels number as source
539         //It supports 8UC1 8UC4 only
540         CV_EXPORTS void LUT(const oclMat &src, const oclMat &lut, oclMat &dst);
541
542         //! only 8UC1 and 256 bins is supported now
543         CV_EXPORTS void calcHist(const oclMat &mat_src, oclMat &mat_hist);
544         //! only 8UC1 and 256 bins is supported now
545         CV_EXPORTS void equalizeHist(const oclMat &mat_src, oclMat &mat_dst);
546
547         //! only 8UC1 is supported now
548         CV_EXPORTS Ptr<cv::CLAHE> createCLAHE(double clipLimit = 40.0, Size tileGridSize = Size(8, 8));
549
550         //! bilateralFilter
551         // supports 8UC1 8UC4
552         CV_EXPORTS void bilateralFilter(const oclMat& src, oclMat& dst, int d, double sigmaColor, double sigmaSpace, int borderType=BORDER_DEFAULT);
553
554         //! Applies an adaptive bilateral filter to the input image
555         //  Unlike the usual bilateral filter that uses fixed value for sigmaColor,
556         //  the adaptive version calculates the local variance in he ksize neighborhood
557         //  and use this as sigmaColor, for the value filtering. However, the local standard deviation is
558         //  clamped to the maxSigmaColor.
559         //  supports 8UC1, 8UC3
560         CV_EXPORTS void adaptiveBilateralFilter(const oclMat& src, oclMat& dst, Size ksize, double sigmaSpace, double maxSigmaColor=20.0, Point anchor = Point(-1, -1), int borderType=BORDER_DEFAULT);
561
562         //! computes exponent of each matrix element (dst = e**src)
563         // supports only CV_32FC1, CV_64FC1 type
564         CV_EXPORTS void exp(const oclMat &src, oclMat &dst);
565
566         //! computes natural logarithm of absolute value of each matrix element: dst = log(abs(src))
567         // supports only CV_32FC1, CV_64FC1 type
568         CV_EXPORTS void log(const oclMat &src, oclMat &dst);
569
570         //! computes magnitude of each (x(i), y(i)) vector
571         // supports only CV_32F, CV_64F type
572         CV_EXPORTS void magnitude(const oclMat &x, const oclMat &y, oclMat &magnitude);
573
574         //! computes angle (angle(i)) of each (x(i), y(i)) vector
575         // supports only CV_32F, CV_64F type
576         CV_EXPORTS void phase(const oclMat &x, const oclMat &y, oclMat &angle, bool angleInDegrees = false);
577
578         //! the function raises every element of tne input array to p
579         // support only CV_32F, CV_64F type
580         CV_EXPORTS void pow(const oclMat &x, double p, oclMat &y);
581
582         //! converts Cartesian coordinates to polar
583         // supports only CV_32F CV_64F type
584         CV_EXPORTS void cartToPolar(const oclMat &x, const oclMat &y, oclMat &magnitude, oclMat &angle, bool angleInDegrees = false);
585
586         //! converts polar coordinates to Cartesian
587         // supports only CV_32F CV_64F type
588         CV_EXPORTS void polarToCart(const oclMat &magnitude, const oclMat &angle, oclMat &x, oclMat &y, bool angleInDegrees = false);
589
590         //! perfroms per-elements bit-wise inversion
591         // supports all types
592         CV_EXPORTS void bitwise_not(const oclMat &src, oclMat &dst);
593
594         //! calculates per-element bit-wise disjunction of two arrays
595         // supports all types
596         CV_EXPORTS void bitwise_or(const oclMat &src1, const oclMat &src2, oclMat &dst, const oclMat &mask = oclMat());
597         CV_EXPORTS void bitwise_or(const oclMat &src1, const Scalar &s, oclMat &dst, const oclMat &mask = oclMat());
598
599         //! calculates per-element bit-wise conjunction of two arrays
600         // supports all types
601         CV_EXPORTS void bitwise_and(const oclMat &src1, const oclMat &src2, oclMat &dst, const oclMat &mask = oclMat());
602         CV_EXPORTS void bitwise_and(const oclMat &src1, const Scalar &s, oclMat &dst, const oclMat &mask = oclMat());
603
604         //! calculates per-element bit-wise "exclusive or" operation
605         // supports all types
606         CV_EXPORTS void bitwise_xor(const oclMat &src1, const oclMat &src2, oclMat &dst, const oclMat &mask = oclMat());
607         CV_EXPORTS void bitwise_xor(const oclMat &src1, const Scalar &s, oclMat &dst, const oclMat &mask = oclMat());
608
609         //! Logical operators
610         CV_EXPORTS oclMat operator ~ (const oclMat &);
611         CV_EXPORTS oclMat operator | (const oclMat &, const oclMat &);
612         CV_EXPORTS oclMat operator & (const oclMat &, const oclMat &);
613         CV_EXPORTS oclMat operator ^ (const oclMat &, const oclMat &);
614
615
616         //! Mathematics operators
617         CV_EXPORTS oclMatExpr operator + (const oclMat &src1, const oclMat &src2);
618         CV_EXPORTS oclMatExpr operator - (const oclMat &src1, const oclMat &src2);
619         CV_EXPORTS oclMatExpr operator * (const oclMat &src1, const oclMat &src2);
620         CV_EXPORTS oclMatExpr operator / (const oclMat &src1, const oclMat &src2);
621
622         struct CV_EXPORTS ConvolveBuf
623         {
624             Size result_size;
625             Size block_size;
626             Size user_block_size;
627             Size dft_size;
628
629             oclMat image_spect, templ_spect, result_spect;
630             oclMat image_block, templ_block, result_data;
631
632             void create(Size image_size, Size templ_size);
633             static Size estimateBlockSize(Size result_size, Size templ_size);
634         };
635
636         //! computes convolution of two images, may use discrete Fourier transform
637         // support only CV_32FC1 type
638         CV_EXPORTS void convolve(const oclMat &image, const oclMat &temp1, oclMat &result, bool ccorr = false);
639         CV_EXPORTS void convolve(const oclMat &image, const oclMat &temp1, oclMat &result, bool ccorr, ConvolveBuf& buf);
640
641         //! Performs a per-element multiplication of two Fourier spectrums.
642         //! Only full (not packed) CV_32FC2 complex spectrums in the interleaved format are supported for now.
643         //! support only CV_32FC2 type
644         CV_EXPORTS void mulSpectrums(const oclMat &a, const oclMat &b, oclMat &c, int flags, float scale, bool conjB = false);
645
646         CV_EXPORTS void cvtColor(const oclMat &src, oclMat &dst, int code, int dcn = 0);
647
648         //! initializes a scaled identity matrix
649         CV_EXPORTS void setIdentity(oclMat& src, const Scalar & val = Scalar(1));
650
651         //! fills the output array with repeated copies of the input array
652         CV_EXPORTS void repeat(const oclMat & src, int ny, int nx, oclMat & dst);
653
654         //////////////////////////////// Filter Engine ////////////////////////////////
655
656         /*!
657           The Base Class for 1D or Row-wise Filters
658
659           This is the base class for linear or non-linear filters that process 1D data.
660           In particular, such filters are used for the "horizontal" filtering parts in separable filters.
661           */
662         class CV_EXPORTS BaseRowFilter_GPU
663         {
664         public:
665             BaseRowFilter_GPU(int ksize_, int anchor_, int bordertype_) : ksize(ksize_), anchor(anchor_), bordertype(bordertype_) {}
666             virtual ~BaseRowFilter_GPU() {}
667             virtual void operator()(const oclMat &src, oclMat &dst) = 0;
668             int ksize, anchor, bordertype;
669         };
670
671         /*!
672           The Base Class for Column-wise Filters
673
674           This is the base class for linear or non-linear filters that process columns of 2D arrays.
675           Such filters are used for the "vertical" filtering parts in separable filters.
676           */
677         class CV_EXPORTS BaseColumnFilter_GPU
678         {
679         public:
680             BaseColumnFilter_GPU(int ksize_, int anchor_, int bordertype_) : ksize(ksize_), anchor(anchor_), bordertype(bordertype_) {}
681             virtual ~BaseColumnFilter_GPU() {}
682             virtual void operator()(const oclMat &src, oclMat &dst) = 0;
683             int ksize, anchor, bordertype;
684         };
685
686         /*!
687           The Base Class for Non-Separable 2D Filters.
688
689           This is the base class for linear or non-linear 2D filters.
690           */
691         class CV_EXPORTS BaseFilter_GPU
692         {
693         public:
694             BaseFilter_GPU(const Size &ksize_, const Point &anchor_, const int &borderType_)
695                 : ksize(ksize_), anchor(anchor_), borderType(borderType_) {}
696             virtual ~BaseFilter_GPU() {}
697             virtual void operator()(const oclMat &src, oclMat &dst) = 0;
698             Size ksize;
699             Point anchor;
700             int borderType;
701         };
702
703         /*!
704           The Base Class for Filter Engine.
705
706           The class can be used to apply an arbitrary filtering operation to an image.
707           It contains all the necessary intermediate buffers.
708           */
709         class CV_EXPORTS FilterEngine_GPU
710         {
711         public:
712             virtual ~FilterEngine_GPU() {}
713
714             virtual void apply(const oclMat &src, oclMat &dst, Rect roi = Rect(0, 0, -1, -1)) = 0;
715         };
716
717         //! returns the non-separable filter engine with the specified filter
718         CV_EXPORTS Ptr<FilterEngine_GPU> createFilter2D_GPU(const Ptr<BaseFilter_GPU> filter2D);
719
720         //! returns the primitive row filter with the specified kernel
721         CV_EXPORTS Ptr<BaseRowFilter_GPU> getLinearRowFilter_GPU(int srcType, int bufType, const Mat &rowKernel,
722                 int anchor = -1, int bordertype = BORDER_DEFAULT);
723
724         //! returns the primitive column filter with the specified kernel
725         CV_EXPORTS Ptr<BaseColumnFilter_GPU> getLinearColumnFilter_GPU(int bufType, int dstType, const Mat &columnKernel,
726                 int anchor = -1, int bordertype = BORDER_DEFAULT, double delta = 0.0);
727
728         //! returns the separable linear filter engine
729         CV_EXPORTS Ptr<FilterEngine_GPU> createSeparableLinearFilter_GPU(int srcType, int dstType, const Mat &rowKernel,
730                 const Mat &columnKernel, const Point &anchor = Point(-1, -1), double delta = 0.0, int bordertype = BORDER_DEFAULT);
731
732         //! returns the separable filter engine with the specified filters
733         CV_EXPORTS Ptr<FilterEngine_GPU> createSeparableFilter_GPU(const Ptr<BaseRowFilter_GPU> &rowFilter,
734                 const Ptr<BaseColumnFilter_GPU> &columnFilter);
735
736         //! returns the Gaussian filter engine
737         CV_EXPORTS Ptr<FilterEngine_GPU> createGaussianFilter_GPU(int type, Size ksize, double sigma1, double sigma2 = 0, int bordertype = BORDER_DEFAULT);
738
739         //! returns filter engine for the generalized Sobel operator
740         CV_EXPORTS Ptr<FilterEngine_GPU> createDerivFilter_GPU( int srcType, int dstType, int dx, int dy, int ksize, int borderType = BORDER_DEFAULT );
741
742         //! applies Laplacian operator to the image
743         // supports only ksize = 1 and ksize = 3
744         CV_EXPORTS void Laplacian(const oclMat &src, oclMat &dst, int ddepth, int ksize = 1, double scale = 1,
745                 double delta=0, int borderType=BORDER_DEFAULT);
746
747         //! returns 2D box filter
748         // dst type must be the same as source type
749         CV_EXPORTS Ptr<BaseFilter_GPU> getBoxFilter_GPU(int srcType, int dstType,
750                 const Size &ksize, Point anchor = Point(-1, -1), int borderType = BORDER_DEFAULT);
751
752         //! returns box filter engine
753         CV_EXPORTS Ptr<FilterEngine_GPU> createBoxFilter_GPU(int srcType, int dstType, const Size &ksize,
754                 const Point &anchor = Point(-1, -1), int borderType = BORDER_DEFAULT);
755
756         //! returns 2D filter with the specified kernel
757         // supports: dst type must be the same as source type
758         CV_EXPORTS Ptr<BaseFilter_GPU> getLinearFilter_GPU(int srcType, int dstType, const Mat &kernel, const Size &ksize,
759                 const Point &anchor = Point(-1, -1), int borderType = BORDER_DEFAULT);
760
761         //! returns the non-separable linear filter engine
762         // supports: dst type must be the same as source type
763         CV_EXPORTS Ptr<FilterEngine_GPU> createLinearFilter_GPU(int srcType, int dstType, const Mat &kernel,
764                 const Point &anchor = Point(-1, -1), int borderType = BORDER_DEFAULT);
765
766         //! smooths the image using the normalized box filter
767         CV_EXPORTS void boxFilter(const oclMat &src, oclMat &dst, int ddepth, Size ksize,
768                                   Point anchor = Point(-1, -1), int borderType = BORDER_DEFAULT);
769
770         //! returns 2D morphological filter
771         //! only MORPH_ERODE and MORPH_DILATE are supported
772         // supports CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4 types
773         // kernel must have CV_8UC1 type, one rows and cols == ksize.width * ksize.height
774         CV_EXPORTS Ptr<BaseFilter_GPU> getMorphologyFilter_GPU(int op, int type, const Mat &kernel, const Size &ksize,
775                 Point anchor = Point(-1, -1));
776
777         //! returns morphological filter engine. Only MORPH_ERODE and MORPH_DILATE are supported.
778         CV_EXPORTS Ptr<FilterEngine_GPU> createMorphologyFilter_GPU(int op, int type, const Mat &kernel,
779                 const Point &anchor = Point(-1, -1), int iterations = 1);
780
781         //! a synonym for normalized box filter
782         static inline void blur(const oclMat &src, oclMat &dst, Size ksize, Point anchor = Point(-1, -1),
783                                 int borderType = BORDER_CONSTANT)
784         {
785             boxFilter(src, dst, -1, ksize, anchor, borderType);
786         }
787
788         //! applies non-separable 2D linear filter to the image
789         CV_EXPORTS void filter2D(const oclMat &src, oclMat &dst, int ddepth, const Mat &kernel,
790                                  Point anchor = Point(-1, -1), double delta = 0.0, int borderType = BORDER_DEFAULT);
791
792         //! applies separable 2D linear filter to the image
793         CV_EXPORTS void sepFilter2D(const oclMat &src, oclMat &dst, int ddepth, const Mat &kernelX, const Mat &kernelY,
794                                     Point anchor = Point(-1, -1), double delta = 0.0, int bordertype = BORDER_DEFAULT);
795
796         //! applies generalized Sobel operator to the image
797         // dst.type must equalize src.type
798         // supports data type: CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4
799         // supports border type: BORDER_CONSTANT, BORDER_REPLICATE, BORDER_REFLECT,BORDER_REFLECT_101
800         CV_EXPORTS void Sobel(const oclMat &src, oclMat &dst, int ddepth, int dx, int dy, int ksize = 3, double scale = 1, double delta = 0.0, int bordertype = BORDER_DEFAULT);
801
802         //! applies the vertical or horizontal Scharr operator to the image
803         // dst.type must equalize src.type
804         // supports data type: CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4
805         // supports border type: BORDER_CONSTANT, BORDER_REPLICATE, BORDER_REFLECT,BORDER_REFLECT_101
806         CV_EXPORTS void Scharr(const oclMat &src, oclMat &dst, int ddepth, int dx, int dy, double scale = 1, double delta = 0.0, int bordertype = BORDER_DEFAULT);
807
808         //! smooths the image using Gaussian filter.
809         // dst.type must equalize src.type
810         // supports data type: CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4
811         // supports border type: BORDER_CONSTANT, BORDER_REPLICATE, BORDER_REFLECT,BORDER_REFLECT_101
812         CV_EXPORTS void GaussianBlur(const oclMat &src, oclMat &dst, Size ksize, double sigma1, double sigma2 = 0, int bordertype = BORDER_DEFAULT);
813
814         //! erodes the image (applies the local minimum operator)
815         // supports data type: CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4
816         CV_EXPORTS void erode( const oclMat &src, oclMat &dst, const Mat &kernel, Point anchor = Point(-1, -1), int iterations = 1,
817
818                                int borderType = BORDER_CONSTANT, const Scalar &borderValue = morphologyDefaultBorderValue());
819
820
821         //! dilates the image (applies the local maximum operator)
822         // supports data type: CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4
823         CV_EXPORTS void dilate( const oclMat &src, oclMat &dst, const Mat &kernel, Point anchor = Point(-1, -1), int iterations = 1,
824
825                                 int borderType = BORDER_CONSTANT, const Scalar &borderValue = morphologyDefaultBorderValue());
826
827
828         //! applies an advanced morphological operation to the image
829         CV_EXPORTS void morphologyEx( const oclMat &src, oclMat &dst, int op, const Mat &kernel, Point anchor = Point(-1, -1), int iterations = 1,
830
831                                       int borderType = BORDER_CONSTANT, const Scalar &borderValue = morphologyDefaultBorderValue());
832
833
834         ////////////////////////////// Image processing //////////////////////////////
835         //! Does mean shift filtering on GPU.
836         CV_EXPORTS void meanShiftFiltering(const oclMat &src, oclMat &dst, int sp, int sr,
837                                            TermCriteria criteria = TermCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 5, 1));
838
839         //! Does mean shift procedure on GPU.
840         CV_EXPORTS void meanShiftProc(const oclMat &src, oclMat &dstr, oclMat &dstsp, int sp, int sr,
841                                       TermCriteria criteria = TermCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 5, 1));
842
843         //! Does mean shift segmentation with elimiation of small regions.
844         CV_EXPORTS void meanShiftSegmentation(const oclMat &src, Mat &dst, int sp, int sr, int minsize,
845                                               TermCriteria criteria = TermCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 5, 1));
846
847         //! applies fixed threshold to the image.
848         // supports CV_8UC1 and CV_32FC1 data type
849         // supports threshold type: THRESH_BINARY, THRESH_BINARY_INV, THRESH_TRUNC, THRESH_TOZERO, THRESH_TOZERO_INV
850         CV_EXPORTS double threshold(const oclMat &src, oclMat &dst, double thresh, double maxVal, int type = THRESH_TRUNC);
851
852         //! resizes the image
853         // Supports INTER_NEAREST, INTER_LINEAR
854         // supports CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4 types
855         CV_EXPORTS void resize(const oclMat &src, oclMat &dst, Size dsize, double fx = 0, double fy = 0, int interpolation = INTER_LINEAR);
856
857         //! Applies a generic geometrical transformation to an image.
858
859         // Supports INTER_NEAREST, INTER_LINEAR.
860         // Map1 supports CV_16SC2, CV_32FC2  types.
861         // Src supports CV_8UC1, CV_8UC2, CV_8UC4.
862         CV_EXPORTS void remap(const oclMat &src, oclMat &dst, oclMat &map1, oclMat &map2, int interpolation, int bordertype, const Scalar &value = Scalar());
863
864         //! copies 2D array to a larger destination array and pads borders with user-specifiable constant
865         // supports CV_8UC1, CV_8UC4, CV_32SC1 types
866         CV_EXPORTS void copyMakeBorder(const oclMat &src, oclMat &dst, int top, int bottom, int left, int right, int boardtype, const Scalar &value = Scalar());
867
868         //! Smoothes image using median filter
869         // The source 1- or 4-channel image. m should be 3 or 5, the image depth should be CV_8U or CV_32F.
870         CV_EXPORTS void medianFilter(const oclMat &src, oclMat &dst, int m);
871
872         //! warps the image using affine transformation
873         // Supports INTER_NEAREST, INTER_LINEAR, INTER_CUBIC
874         // supports CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4 types
875         CV_EXPORTS void warpAffine(const oclMat &src, oclMat &dst, const Mat &M, Size dsize, int flags = INTER_LINEAR);
876
877         //! warps the image using perspective transformation
878         // Supports INTER_NEAREST, INTER_LINEAR, INTER_CUBIC
879         // supports CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4 types
880         CV_EXPORTS void warpPerspective(const oclMat &src, oclMat &dst, const Mat &M, Size dsize, int flags = INTER_LINEAR);
881
882         //! computes the integral image and integral for the squared image
883         // sum will support CV_32S, CV_32F, sqsum - support CV32F, CV_64F
884         // supports only CV_8UC1 source type
885         CV_EXPORTS void integral(const oclMat &src, oclMat &sum, oclMat &sqsum, int sdepth=-1 );
886         CV_EXPORTS void integral(const oclMat &src, oclMat &sum, int sdepth=-1 );
887         CV_EXPORTS void cornerHarris(const oclMat &src, oclMat &dst, int blockSize, int ksize, double k, int bordertype = cv::BORDER_DEFAULT);
888         CV_EXPORTS void cornerHarris_dxdy(const oclMat &src, oclMat &dst, oclMat &Dx, oclMat &Dy,
889             int blockSize, int ksize, double k, int bordertype = cv::BORDER_DEFAULT);
890         CV_EXPORTS void cornerMinEigenVal(const oclMat &src, oclMat &dst, int blockSize, int ksize, int bordertype = cv::BORDER_DEFAULT);
891         CV_EXPORTS void cornerMinEigenVal_dxdy(const oclMat &src, oclMat &dst, oclMat &Dx, oclMat &Dy,
892             int blockSize, int ksize, int bordertype = cv::BORDER_DEFAULT);
893
894
895         /////////////////////////////////// ML ///////////////////////////////////////////
896
897         //! Compute closest centers for each lines in source and lable it after center's index
898         // supports CV_32FC1/CV_32FC2/CV_32FC4 data type
899         // supports NORM_L1 and NORM_L2 distType
900         // if indices is provided, only the indexed rows will be calculated and their results are in the same
901         // order of indices
902         CV_EXPORTS void distanceToCenters(const oclMat &src, const oclMat &centers, Mat &dists, Mat &labels, int distType = NORM_L2SQR);
903
904         //!Does k-means procedure on GPU
905         // supports CV_32FC1/CV_32FC2/CV_32FC4 data type
906         CV_EXPORTS double kmeans(const oclMat &src, int K, oclMat &bestLabels,
907                                      TermCriteria criteria, int attemps, int flags, oclMat &centers);
908
909
910         ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
911         ///////////////////////////////////////////CascadeClassifier//////////////////////////////////////////////////////////////////
912         ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
913         class CV_EXPORTS OclCascadeClassifier : public  cv::CascadeClassifier
914         {
915         public:
916             void detectMultiScale(oclMat &image, CV_OUT std::vector<cv::Rect>& faces,
917                 double scaleFactor = 1.1, int minNeighbors = 3, int flags = 0,
918                 Size minSize = Size(), Size maxSize = Size());
919         };
920
921         /////////////////////////////// Pyramid /////////////////////////////////////
922         CV_EXPORTS void pyrDown(const oclMat &src, oclMat &dst);
923
924         //! upsamples the source image and then smoothes it
925         CV_EXPORTS void pyrUp(const oclMat &src, oclMat &dst);
926
927         //! performs linear blending of two images
928         //! to avoid accuracy errors sum of weigths shouldn't be very close to zero
929         // supports only CV_8UC1 source type
930         CV_EXPORTS void blendLinear(const oclMat &img1, const oclMat &img2, const oclMat &weights1, const oclMat &weights2, oclMat &result);
931
932         //! computes vertical sum, supports only CV_32FC1 images
933         CV_EXPORTS void columnSum(const oclMat &src, oclMat &sum);
934
935         ///////////////////////////////////////// match_template /////////////////////////////////////////////////////////////
936         struct CV_EXPORTS MatchTemplateBuf
937         {
938             Size user_block_size;
939             oclMat imagef, templf;
940             std::vector<oclMat> images;
941             std::vector<oclMat> image_sums;
942             std::vector<oclMat> image_sqsums;
943         };
944
945         //! computes the proximity map for the raster template and the image where the template is searched for
946         // Supports TM_SQDIFF, TM_SQDIFF_NORMED, TM_CCORR, TM_CCORR_NORMED, TM_CCOEFF, TM_CCOEFF_NORMED for type 8UC1 and 8UC4
947         // Supports TM_SQDIFF, TM_CCORR for type 32FC1 and 32FC4
948         CV_EXPORTS void matchTemplate(const oclMat &image, const oclMat &templ, oclMat &result, int method);
949
950         //! computes the proximity map for the raster template and the image where the template is searched for
951         // Supports TM_SQDIFF, TM_SQDIFF_NORMED, TM_CCORR, TM_CCORR_NORMED, TM_CCOEFF, TM_CCOEFF_NORMED for type 8UC1 and 8UC4
952         // Supports TM_SQDIFF, TM_CCORR for type 32FC1 and 32FC4
953         CV_EXPORTS void matchTemplate(const oclMat &image, const oclMat &templ, oclMat &result, int method, MatchTemplateBuf &buf);
954
955
956
957         ///////////////////////////////////////////// Canny /////////////////////////////////////////////
958         struct CV_EXPORTS CannyBuf;
959
960         //! compute edges of the input image using Canny operator
961         // Support CV_8UC1 only
962         CV_EXPORTS void Canny(const oclMat &image, oclMat &edges, double low_thresh, double high_thresh, int apperture_size = 3, bool L2gradient = false);
963         CV_EXPORTS void Canny(const oclMat &image, CannyBuf &buf, oclMat &edges, double low_thresh, double high_thresh, int apperture_size = 3, bool L2gradient = false);
964         CV_EXPORTS void Canny(const oclMat &dx, const oclMat &dy, oclMat &edges, double low_thresh, double high_thresh, bool L2gradient = false);
965         CV_EXPORTS void Canny(const oclMat &dx, const oclMat &dy, CannyBuf &buf, oclMat &edges, double low_thresh, double high_thresh, bool L2gradient = false);
966
967         struct CV_EXPORTS CannyBuf
968         {
969             CannyBuf() : counter(1, 1, CV_32S) { }
970             ~CannyBuf()
971             {
972                 release();
973             }
974             explicit CannyBuf(const Size &image_size, int apperture_size = 3) : counter(1, 1, CV_32S)
975             {
976                 create(image_size, apperture_size);
977             }
978             CannyBuf(const oclMat &dx_, const oclMat &dy_);
979             void create(const Size &image_size, int apperture_size = 3);
980             void release();
981
982             oclMat dx, dy;
983             oclMat dx_buf, dy_buf;
984             oclMat magBuf, mapBuf;
985             oclMat trackBuf1, trackBuf2;
986             oclMat counter;
987             Ptr<FilterEngine_GPU> filterDX, filterDY;
988         };
989
990         ///////////////////////////////////////// Hough Transform /////////////////////////////////////////
991         //! HoughCircles
992         struct HoughCirclesBuf
993         {
994             oclMat edges;
995             oclMat accum;
996             oclMat srcPoints;
997             oclMat centers;
998             CannyBuf cannyBuf;
999         };
1000
1001         CV_EXPORTS void HoughCircles(const oclMat& src, oclMat& circles, int method, float dp, float minDist, int cannyThreshold, int votesThreshold, int minRadius, int maxRadius, int maxCircles = 4096);
1002         CV_EXPORTS void HoughCircles(const oclMat& src, oclMat& circles, HoughCirclesBuf& buf, int method, float dp, float minDist, int cannyThreshold, int votesThreshold, int minRadius, int maxRadius, int maxCircles = 4096);
1003         CV_EXPORTS void HoughCirclesDownload(const oclMat& d_circles, OutputArray h_circles);
1004
1005
1006         ///////////////////////////////////////// clAmdFft related /////////////////////////////////////////
1007         //! Performs a forward or inverse discrete Fourier transform (1D or 2D) of floating point matrix.
1008         //! Param dft_size is the size of DFT transform.
1009         //!
1010         //! For complex-to-real transform it is assumed that the source matrix is packed in CLFFT's format.
1011         // support src type of CV32FC1, CV32FC2
1012         // support flags: DFT_INVERSE, DFT_REAL_OUTPUT, DFT_COMPLEX_OUTPUT, DFT_ROWS
1013         // dft_size is the size of original input, which is used for transformation from complex to real.
1014         // dft_size must be powers of 2, 3 and 5
1015         // real to complex dft requires at least v1.8 clAmdFft
1016         // real to complex dft output is not the same with cpu version
1017         // real to complex and complex to real does not support DFT_ROWS
1018         CV_EXPORTS void dft(const oclMat &src, oclMat &dst, Size dft_size = Size(), int flags = 0);
1019
1020         //! implements generalized matrix product algorithm GEMM from BLAS
1021         // The functionality requires clAmdBlas library
1022         // only support type CV_32FC1
1023         // flag GEMM_3_T is not supported
1024         CV_EXPORTS void gemm(const oclMat &src1, const oclMat &src2, double alpha,
1025                              const oclMat &src3, double beta, oclMat &dst, int flags = 0);
1026
1027         //////////////// HOG (Histogram-of-Oriented-Gradients) Descriptor and Object Detector //////////////
1028
1029         struct CV_EXPORTS HOGDescriptor
1030
1031         {
1032
1033             enum { DEFAULT_WIN_SIGMA = -1 };
1034
1035             enum { DEFAULT_NLEVELS = 64 };
1036
1037             enum { DESCR_FORMAT_ROW_BY_ROW, DESCR_FORMAT_COL_BY_COL };
1038
1039
1040
1041             HOGDescriptor(Size win_size = Size(64, 128), Size block_size = Size(16, 16),
1042
1043                           Size block_stride = Size(8, 8), Size cell_size = Size(8, 8),
1044
1045                           int nbins = 9, double win_sigma = DEFAULT_WIN_SIGMA,
1046
1047                           double threshold_L2hys = 0.2, bool gamma_correction = true,
1048
1049                           int nlevels = DEFAULT_NLEVELS);
1050
1051
1052
1053             size_t getDescriptorSize() const;
1054
1055             size_t getBlockHistogramSize() const;
1056
1057
1058
1059             void setSVMDetector(const std::vector<float> &detector);
1060
1061
1062
1063             static std::vector<float> getDefaultPeopleDetector();
1064
1065             static std::vector<float> getPeopleDetector48x96();
1066
1067             static std::vector<float> getPeopleDetector64x128();
1068
1069
1070
1071             void detect(const oclMat &img, std::vector<Point> &found_locations,
1072
1073                         double hit_threshold = 0, Size win_stride = Size(),
1074
1075                         Size padding = Size());
1076
1077
1078
1079             void detectMultiScale(const oclMat &img, std::vector<Rect> &found_locations,
1080
1081                                   double hit_threshold = 0, Size win_stride = Size(),
1082
1083                                   Size padding = Size(), double scale0 = 1.05,
1084
1085                                   int group_threshold = 2);
1086
1087
1088
1089             void getDescriptors(const oclMat &img, Size win_stride,
1090
1091                                 oclMat &descriptors,
1092
1093                                 int descr_format = DESCR_FORMAT_COL_BY_COL);
1094
1095
1096
1097             Size win_size;
1098
1099             Size block_size;
1100
1101             Size block_stride;
1102
1103             Size cell_size;
1104
1105             int nbins;
1106
1107             double win_sigma;
1108
1109             double threshold_L2hys;
1110
1111             bool gamma_correction;
1112
1113             int nlevels;
1114
1115
1116
1117         protected:
1118
1119             // initialize buffers; only need to do once in case of multiscale detection
1120
1121             void init_buffer(const oclMat &img, Size win_stride);
1122
1123
1124
1125             void computeBlockHistograms(const oclMat &img);
1126
1127             void computeGradient(const oclMat &img, oclMat &grad, oclMat &qangle);
1128
1129
1130
1131             double getWinSigma() const;
1132
1133             bool checkDetectorSize() const;
1134
1135
1136
1137             static int numPartsWithin(int size, int part_size, int stride);
1138
1139             static Size numPartsWithin(Size size, Size part_size, Size stride);
1140
1141
1142
1143             // Coefficients of the separating plane
1144
1145             float free_coef;
1146
1147             oclMat detector;
1148
1149
1150
1151             // Results of the last classification step
1152
1153             oclMat labels;
1154
1155             Mat labels_host;
1156
1157
1158
1159             // Results of the last histogram evaluation step
1160
1161             oclMat block_hists;
1162
1163
1164
1165             // Gradients conputation results
1166
1167             oclMat grad, qangle;
1168
1169
1170
1171             // scaled image
1172
1173             oclMat image_scale;
1174
1175
1176
1177             // effect size of input image (might be different from original size after scaling)
1178
1179             Size effect_size;
1180
1181         };
1182
1183
1184         ////////////////////////feature2d_ocl/////////////////
1185         /****************************************************************************************\
1186         *                                      Distance                                          *
1187         \****************************************************************************************/
1188         template<typename T>
1189         struct CV_EXPORTS Accumulator
1190         {
1191             typedef T Type;
1192         };
1193         template<> struct Accumulator<unsigned char>
1194         {
1195             typedef float Type;
1196         };
1197         template<> struct Accumulator<unsigned short>
1198         {
1199             typedef float Type;
1200         };
1201         template<> struct Accumulator<char>
1202         {
1203             typedef float Type;
1204         };
1205         template<> struct Accumulator<short>
1206         {
1207             typedef float Type;
1208         };
1209
1210         /*
1211          * Manhattan distance (city block distance) functor
1212          */
1213         template<class T>
1214         struct CV_EXPORTS L1
1215         {
1216             enum { normType = NORM_L1 };
1217             typedef T ValueType;
1218             typedef typename Accumulator<T>::Type ResultType;
1219
1220             ResultType operator()( const T *a, const T *b, int size ) const
1221             {
1222                 return normL1<ValueType, ResultType>(a, b, size);
1223             }
1224         };
1225
1226         /*
1227          * Euclidean distance functor
1228          */
1229         template<class T>
1230         struct CV_EXPORTS L2
1231         {
1232             enum { normType = NORM_L2 };
1233             typedef T ValueType;
1234             typedef typename Accumulator<T>::Type ResultType;
1235
1236             ResultType operator()( const T *a, const T *b, int size ) const
1237             {
1238                 return (ResultType)std::sqrt((double)normL2Sqr<ValueType, ResultType>(a, b, size));
1239             }
1240         };
1241
1242         /*
1243          * Hamming distance functor - counts the bit differences between two strings - useful for the Brief descriptor
1244          * bit count of A exclusive XOR'ed with B
1245          */
1246         struct CV_EXPORTS Hamming
1247         {
1248             enum { normType = NORM_HAMMING };
1249             typedef unsigned char ValueType;
1250             typedef int ResultType;
1251
1252             /** this will count the bits in a ^ b
1253              */
1254             ResultType operator()( const unsigned char *a, const unsigned char *b, int size ) const
1255             {
1256                 return normHamming(a, b, size);
1257             }
1258         };
1259
1260         ////////////////////////////////// BruteForceMatcher //////////////////////////////////
1261
1262         class CV_EXPORTS BruteForceMatcher_OCL_base
1263         {
1264         public:
1265             enum DistType {L1Dist = 0, L2Dist, HammingDist};
1266             explicit BruteForceMatcher_OCL_base(DistType distType = L2Dist);
1267
1268             // Add descriptors to train descriptor collection
1269             void add(const std::vector<oclMat> &descCollection);
1270
1271             // Get train descriptors collection
1272             const std::vector<oclMat> &getTrainDescriptors() const;
1273
1274             // Clear train descriptors collection
1275             void clear();
1276
1277             // Return true if there are not train descriptors in collection
1278             bool empty() const;
1279
1280             // Return true if the matcher supports mask in match methods
1281             bool isMaskSupported() const;
1282
1283             // Find one best match for each query descriptor
1284             void matchSingle(const oclMat &query, const oclMat &train,
1285                              oclMat &trainIdx, oclMat &distance,
1286                              const oclMat &mask = oclMat());
1287
1288             // Download trainIdx and distance and convert it to CPU vector with DMatch
1289             static void matchDownload(const oclMat &trainIdx, const oclMat &distance, std::vector<DMatch> &matches);
1290             // Convert trainIdx and distance to vector with DMatch
1291             static void matchConvert(const Mat &trainIdx, const Mat &distance, std::vector<DMatch> &matches);
1292
1293             // Find one best match for each query descriptor
1294             void match(const oclMat &query, const oclMat &train, std::vector<DMatch> &matches, const oclMat &mask = oclMat());
1295
1296             // Make gpu collection of trains and masks in suitable format for matchCollection function
1297             void makeGpuCollection(oclMat &trainCollection, oclMat &maskCollection, const std::vector<oclMat> &masks = std::vector<oclMat>());
1298
1299             // Find one best match from train collection for each query descriptor
1300             void matchCollection(const oclMat &query, const oclMat &trainCollection,
1301                                  oclMat &trainIdx, oclMat &imgIdx, oclMat &distance,
1302                                  const oclMat &masks = oclMat());
1303
1304             // Download trainIdx, imgIdx and distance and convert it to vector with DMatch
1305             static void matchDownload(const oclMat &trainIdx, const oclMat &imgIdx, const oclMat &distance, std::vector<DMatch> &matches);
1306             // Convert trainIdx, imgIdx and distance to vector with DMatch
1307             static void matchConvert(const Mat &trainIdx, const Mat &imgIdx, const Mat &distance, std::vector<DMatch> &matches);
1308
1309             // Find one best match from train collection for each query descriptor.
1310             void match(const oclMat &query, std::vector<DMatch> &matches, const std::vector<oclMat> &masks = std::vector<oclMat>());
1311
1312             // Find k best matches for each query descriptor (in increasing order of distances)
1313             void knnMatchSingle(const oclMat &query, const oclMat &train,
1314                                 oclMat &trainIdx, oclMat &distance, oclMat &allDist, int k,
1315                                 const oclMat &mask = oclMat());
1316
1317             // Download trainIdx and distance and convert it to vector with DMatch
1318             // compactResult is used when mask is not empty. If compactResult is false matches
1319             // vector will have the same size as queryDescriptors rows. If compactResult is true
1320             // matches vector will not contain matches for fully masked out query descriptors.
1321             static void knnMatchDownload(const oclMat &trainIdx, const oclMat &distance,
1322                                          std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
1323             // Convert trainIdx and distance to vector with DMatch
1324             static void knnMatchConvert(const Mat &trainIdx, const Mat &distance,
1325                                         std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
1326
1327             // Find k best matches for each query descriptor (in increasing order of distances).
1328             // compactResult is used when mask is not empty. If compactResult is false matches
1329             // vector will have the same size as queryDescriptors rows. If compactResult is true
1330             // matches vector will not contain matches for fully masked out query descriptors.
1331             void knnMatch(const oclMat &query, const oclMat &train,
1332                           std::vector< std::vector<DMatch> > &matches, int k, const oclMat &mask = oclMat(),
1333                           bool compactResult = false);
1334
1335             // Find k best matches from train collection for each query descriptor (in increasing order of distances)
1336             void knnMatch2Collection(const oclMat &query, const oclMat &trainCollection,
1337                                      oclMat &trainIdx, oclMat &imgIdx, oclMat &distance,
1338                                      const oclMat &maskCollection = oclMat());
1339
1340             // Download trainIdx and distance and convert it to vector with DMatch
1341             // compactResult is used when mask is not empty. If compactResult is false matches
1342             // vector will have the same size as queryDescriptors rows. If compactResult is true
1343             // matches vector will not contain matches for fully masked out query descriptors.
1344             static void knnMatch2Download(const oclMat &trainIdx, const oclMat &imgIdx, const oclMat &distance,
1345                                           std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
1346             // Convert trainIdx and distance to vector with DMatch
1347             static void knnMatch2Convert(const Mat &trainIdx, const Mat &imgIdx, const Mat &distance,
1348                                          std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
1349
1350             // Find k best matches  for each query descriptor (in increasing order of distances).
1351             // compactResult is used when mask is not empty. If compactResult is false matches
1352             // vector will have the same size as queryDescriptors rows. If compactResult is true
1353             // matches vector will not contain matches for fully masked out query descriptors.
1354             void knnMatch(const oclMat &query, std::vector< std::vector<DMatch> > &matches, int k,
1355                           const std::vector<oclMat> &masks = std::vector<oclMat>(), bool compactResult = false);
1356
1357             // Find best matches for each query descriptor which have distance less than maxDistance.
1358             // nMatches.at<int>(0, queryIdx) will contain matches count for queryIdx.
1359             // carefully nMatches can be greater than trainIdx.cols - it means that matcher didn't find all matches,
1360             // because it didn't have enough memory.
1361             // If trainIdx is empty, then trainIdx and distance will be created with size nQuery x max((nTrain / 100), 10),
1362             // otherwize user can pass own allocated trainIdx and distance with size nQuery x nMaxMatches
1363             // Matches doesn't sorted.
1364             void radiusMatchSingle(const oclMat &query, const oclMat &train,
1365                                    oclMat &trainIdx, oclMat &distance, oclMat &nMatches, float maxDistance,
1366                                    const oclMat &mask = oclMat());
1367
1368             // Download trainIdx, nMatches and distance and convert it to vector with DMatch.
1369             // matches will be sorted in increasing order of distances.
1370             // compactResult is used when mask is not empty. If compactResult is false matches
1371             // vector will have the same size as queryDescriptors rows. If compactResult is true
1372             // matches vector will not contain matches for fully masked out query descriptors.
1373             static void radiusMatchDownload(const oclMat &trainIdx, const oclMat &distance, const oclMat &nMatches,
1374                                             std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
1375             // Convert trainIdx, nMatches and distance to vector with DMatch.
1376             static void radiusMatchConvert(const Mat &trainIdx, const Mat &distance, const Mat &nMatches,
1377                                            std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
1378
1379             // Find best matches for each query descriptor which have distance less than maxDistance
1380             // in increasing order of distances).
1381             void radiusMatch(const oclMat &query, const oclMat &train,
1382                              std::vector< std::vector<DMatch> > &matches, float maxDistance,
1383                              const oclMat &mask = oclMat(), bool compactResult = false);
1384
1385             // Find best matches for each query descriptor which have distance less than maxDistance.
1386             // If trainIdx is empty, then trainIdx and distance will be created with size nQuery x max((nQuery / 100), 10),
1387             // otherwize user can pass own allocated trainIdx and distance with size nQuery x nMaxMatches
1388             // Matches doesn't sorted.
1389             void radiusMatchCollection(const oclMat &query, oclMat &trainIdx, oclMat &imgIdx, oclMat &distance, oclMat &nMatches, float maxDistance,
1390                                        const std::vector<oclMat> &masks = std::vector<oclMat>());
1391
1392             // Download trainIdx, imgIdx, nMatches and distance and convert it to vector with DMatch.
1393             // matches will be sorted 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             static void radiusMatchDownload(const oclMat &trainIdx, const oclMat &imgIdx, const oclMat &distance, const oclMat &nMatches,
1398                                             std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
1399             // Convert trainIdx, nMatches and distance to vector with DMatch.
1400             static void radiusMatchConvert(const Mat &trainIdx, const Mat &imgIdx, const Mat &distance, const Mat &nMatches,
1401                                            std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
1402
1403             // Find best matches from train collection for each query descriptor which have distance less than
1404             // maxDistance (in increasing order of distances).
1405             void radiusMatch(const oclMat &query, std::vector< std::vector<DMatch> > &matches, float maxDistance,
1406                              const std::vector<oclMat> &masks = std::vector<oclMat>(), bool compactResult = false);
1407
1408             DistType distType;
1409
1410         private:
1411             std::vector<oclMat> trainDescCollection;
1412         };
1413
1414         template <class Distance>
1415         class CV_EXPORTS BruteForceMatcher_OCL;
1416
1417         template <typename T>
1418         class CV_EXPORTS BruteForceMatcher_OCL< L1<T> > : public BruteForceMatcher_OCL_base
1419         {
1420         public:
1421             explicit BruteForceMatcher_OCL() : BruteForceMatcher_OCL_base(L1Dist) {}
1422             explicit BruteForceMatcher_OCL(L1<T> /*d*/) : BruteForceMatcher_OCL_base(L1Dist) {}
1423         };
1424         template <typename T>
1425         class CV_EXPORTS BruteForceMatcher_OCL< L2<T> > : public BruteForceMatcher_OCL_base
1426         {
1427         public:
1428             explicit BruteForceMatcher_OCL() : BruteForceMatcher_OCL_base(L2Dist) {}
1429             explicit BruteForceMatcher_OCL(L2<T> /*d*/) : BruteForceMatcher_OCL_base(L2Dist) {}
1430         };
1431         template <> class CV_EXPORTS BruteForceMatcher_OCL< Hamming > : public BruteForceMatcher_OCL_base
1432         {
1433         public:
1434             explicit BruteForceMatcher_OCL() : BruteForceMatcher_OCL_base(HammingDist) {}
1435             explicit BruteForceMatcher_OCL(Hamming /*d*/) : BruteForceMatcher_OCL_base(HammingDist) {}
1436         };
1437
1438         class CV_EXPORTS BFMatcher_OCL : public BruteForceMatcher_OCL_base
1439         {
1440         public:
1441             explicit BFMatcher_OCL(int norm = NORM_L2) : BruteForceMatcher_OCL_base(norm == NORM_L1 ? L1Dist : norm == NORM_L2 ? L2Dist : HammingDist) {}
1442         };
1443
1444         class CV_EXPORTS GoodFeaturesToTrackDetector_OCL
1445         {
1446         public:
1447             explicit GoodFeaturesToTrackDetector_OCL(int maxCorners = 1000, double qualityLevel = 0.01, double minDistance = 0.0,
1448                 int blockSize = 3, bool useHarrisDetector = false, double harrisK = 0.04);
1449
1450             //! return 1 rows matrix with CV_32FC2 type
1451             void operator ()(const oclMat& image, oclMat& corners, const oclMat& mask = oclMat());
1452             //! download points of type Point2f to a vector. the vector's content will be erased
1453             void downloadPoints(const oclMat &points, std::vector<Point2f> &points_v);
1454
1455             int maxCorners;
1456             double qualityLevel;
1457             double minDistance;
1458
1459             int blockSize;
1460             bool useHarrisDetector;
1461             double harrisK;
1462             void releaseMemory()
1463             {
1464                 Dx_.release();
1465                 Dy_.release();
1466                 eig_.release();
1467                 minMaxbuf_.release();
1468                 tmpCorners_.release();
1469             }
1470         private:
1471             oclMat Dx_;
1472             oclMat Dy_;
1473             oclMat eig_;
1474             oclMat minMaxbuf_;
1475             oclMat tmpCorners_;
1476         };
1477
1478         inline GoodFeaturesToTrackDetector_OCL::GoodFeaturesToTrackDetector_OCL(int maxCorners_, double qualityLevel_, double minDistance_,
1479             int blockSize_, bool useHarrisDetector_, double harrisK_)
1480         {
1481             maxCorners = maxCorners_;
1482             qualityLevel = qualityLevel_;
1483             minDistance = minDistance_;
1484             blockSize = blockSize_;
1485             useHarrisDetector = useHarrisDetector_;
1486             harrisK = harrisK_;
1487         }
1488
1489         /////////////////////////////// PyrLKOpticalFlow /////////////////////////////////////
1490
1491         class CV_EXPORTS PyrLKOpticalFlow
1492         {
1493         public:
1494             PyrLKOpticalFlow()
1495             {
1496                 winSize = Size(21, 21);
1497                 maxLevel = 3;
1498                 iters = 30;
1499                 derivLambda = 0.5;
1500                 useInitialFlow = false;
1501                 minEigThreshold = 1e-4f;
1502                 getMinEigenVals = false;
1503                 isDeviceArch11_ = false;
1504             }
1505
1506             void sparse(const oclMat &prevImg, const oclMat &nextImg, const oclMat &prevPts, oclMat &nextPts,
1507                         oclMat &status, oclMat *err = 0);
1508
1509             void dense(const oclMat &prevImg, const oclMat &nextImg, oclMat &u, oclMat &v, oclMat *err = 0);
1510
1511             Size winSize;
1512             int maxLevel;
1513             int iters;
1514             double derivLambda;
1515             bool useInitialFlow;
1516             float minEigThreshold;
1517             bool getMinEigenVals;
1518
1519             void releaseMemory()
1520             {
1521                 dx_calcBuf_.release();
1522                 dy_calcBuf_.release();
1523
1524                 prevPyr_.clear();
1525                 nextPyr_.clear();
1526
1527                 dx_buf_.release();
1528                 dy_buf_.release();
1529             }
1530
1531         private:
1532             void calcSharrDeriv(const oclMat &src, oclMat &dx, oclMat &dy);
1533
1534             void buildImagePyramid(const oclMat &img0, std::vector<oclMat> &pyr, bool withBorder);
1535
1536             oclMat dx_calcBuf_;
1537             oclMat dy_calcBuf_;
1538
1539             std::vector<oclMat> prevPyr_;
1540             std::vector<oclMat> nextPyr_;
1541
1542             oclMat dx_buf_;
1543             oclMat dy_buf_;
1544
1545             oclMat uPyr_[2];
1546             oclMat vPyr_[2];
1547
1548             bool isDeviceArch11_;
1549         };
1550
1551         class CV_EXPORTS FarnebackOpticalFlow
1552         {
1553         public:
1554             FarnebackOpticalFlow();
1555
1556             int numLevels;
1557             double pyrScale;
1558             bool fastPyramids;
1559             int winSize;
1560             int numIters;
1561             int polyN;
1562             double polySigma;
1563             int flags;
1564
1565             void operator ()(const oclMat &frame0, const oclMat &frame1, oclMat &flowx, oclMat &flowy);
1566
1567             void releaseMemory();
1568
1569         private:
1570             void prepareGaussian(
1571                 int n, double sigma, float *g, float *xg, float *xxg,
1572                 double &ig11, double &ig03, double &ig33, double &ig55);
1573
1574             void setPolynomialExpansionConsts(int n, double sigma);
1575
1576             void updateFlow_boxFilter(
1577                 const oclMat& R0, const oclMat& R1, oclMat& flowx, oclMat &flowy,
1578                 oclMat& M, oclMat &bufM, int blockSize, bool updateMatrices);
1579
1580             void updateFlow_gaussianBlur(
1581                 const oclMat& R0, const oclMat& R1, oclMat& flowx, oclMat& flowy,
1582                 oclMat& M, oclMat &bufM, int blockSize, bool updateMatrices);
1583
1584             oclMat frames_[2];
1585             oclMat pyrLevel_[2], M_, bufM_, R_[2], blurredFrame_[2];
1586             std::vector<oclMat> pyramid0_, pyramid1_;
1587         };
1588
1589         //////////////// build warping maps ////////////////////
1590         //! builds plane warping maps
1591         CV_EXPORTS void buildWarpPlaneMaps(Size src_size, Rect dst_roi, const Mat &K, const Mat &R, const Mat &T, float scale, oclMat &map_x, oclMat &map_y);
1592         //! builds cylindrical warping maps
1593         CV_EXPORTS void buildWarpCylindricalMaps(Size src_size, Rect dst_roi, const Mat &K, const Mat &R, float scale, oclMat &map_x, oclMat &map_y);
1594         //! builds spherical warping maps
1595         CV_EXPORTS void buildWarpSphericalMaps(Size src_size, Rect dst_roi, const Mat &K, const Mat &R, float scale, oclMat &map_x, oclMat &map_y);
1596         //! builds Affine warping maps
1597         CV_EXPORTS void buildWarpAffineMaps(const Mat &M, bool inverse, Size dsize, oclMat &xmap, oclMat &ymap);
1598
1599         //! builds Perspective warping maps
1600         CV_EXPORTS void buildWarpPerspectiveMaps(const Mat &M, bool inverse, Size dsize, oclMat &xmap, oclMat &ymap);
1601
1602         ///////////////////////////////////// interpolate frames //////////////////////////////////////////////
1603         //! Interpolate frames (images) using provided optical flow (displacement field).
1604         //! frame0   - frame 0 (32-bit floating point images, single channel)
1605         //! frame1   - frame 1 (the same type and size)
1606         //! fu       - forward horizontal displacement
1607         //! fv       - forward vertical displacement
1608         //! bu       - backward horizontal displacement
1609         //! bv       - backward vertical displacement
1610         //! pos      - new frame position
1611         //! newFrame - new frame
1612         //! buf      - temporary buffer, will have width x 6*height size, CV_32FC1 type and contain 6 oclMat;
1613         //!            occlusion masks            0, occlusion masks            1,
1614         //!            interpolated forward flow  0, interpolated forward flow  1,
1615         //!            interpolated backward flow 0, interpolated backward flow 1
1616         //!
1617         CV_EXPORTS void interpolateFrames(const oclMat &frame0, const oclMat &frame1,
1618                                           const oclMat &fu, const oclMat &fv,
1619                                           const oclMat &bu, const oclMat &bv,
1620                                           float pos, oclMat &newFrame, oclMat &buf);
1621
1622         //! computes moments of the rasterized shape or a vector of points
1623         //! _array should be a vector a points standing for the contour
1624         CV_EXPORTS Moments ocl_moments(InputArray contour);
1625         //! src should be a general image uploaded to the GPU.
1626         //! the supported oclMat type are CV_8UC1, CV_16UC1, CV_16SC1, CV_32FC1 and CV_64FC1
1627         //! to use type of CV_64FC1, the GPU should support CV_64FC1
1628         CV_EXPORTS Moments ocl_moments(oclMat& src, bool binary);
1629
1630         class CV_EXPORTS StereoBM_OCL
1631         {
1632         public:
1633             enum { BASIC_PRESET = 0, PREFILTER_XSOBEL = 1 };
1634
1635             enum { DEFAULT_NDISP = 64, DEFAULT_WINSZ = 19 };
1636
1637             //! the default constructor
1638             StereoBM_OCL();
1639             //! the full constructor taking the camera-specific preset, number of disparities and the SAD window size. ndisparities must be multiple of 8.
1640             StereoBM_OCL(int preset, int ndisparities = DEFAULT_NDISP, int winSize = DEFAULT_WINSZ);
1641
1642             //! the stereo correspondence operator. Finds the disparity for the specified rectified stereo pair
1643             //! Output disparity has CV_8U type.
1644             void operator() ( const oclMat &left, const oclMat &right, oclMat &disparity);
1645
1646             //! Some heuristics that tries to estmate
1647             // if current GPU will be faster then CPU in this algorithm.
1648             // It queries current active device.
1649             static bool checkIfGpuCallReasonable();
1650
1651             int preset;
1652             int ndisp;
1653             int winSize;
1654
1655             // If avergeTexThreshold  == 0 => post procesing is disabled
1656             // If avergeTexThreshold != 0 then disparity is set 0 in each point (x,y) where for left image
1657             // SumOfHorizontalGradiensInWindow(x, y, winSize) < (winSize * winSize) * avergeTexThreshold
1658             // i.e. input left image is low textured.
1659             float avergeTexThreshold;
1660         private:
1661             oclMat minSSD, leBuf, riBuf;
1662         };
1663
1664         class CV_EXPORTS StereoBeliefPropagation
1665         {
1666         public:
1667             enum { DEFAULT_NDISP  = 64 };
1668             enum { DEFAULT_ITERS  = 5  };
1669             enum { DEFAULT_LEVELS = 5  };
1670             static void estimateRecommendedParams(int width, int height, int &ndisp, int &iters, int &levels);
1671             explicit StereoBeliefPropagation(int ndisp  = DEFAULT_NDISP,
1672                                              int iters  = DEFAULT_ITERS,
1673                                              int levels = DEFAULT_LEVELS,
1674                                              int msg_type = CV_16S);
1675             StereoBeliefPropagation(int ndisp, int iters, int levels,
1676                                     float max_data_term, float data_weight,
1677                                     float max_disc_term, float disc_single_jump,
1678                                     int msg_type = CV_32F);
1679             void operator()(const oclMat &left, const oclMat &right, oclMat &disparity);
1680             void operator()(const oclMat &data, oclMat &disparity);
1681             int ndisp;
1682             int iters;
1683             int levels;
1684             float max_data_term;
1685             float data_weight;
1686             float max_disc_term;
1687             float disc_single_jump;
1688             int msg_type;
1689         private:
1690             oclMat u, d, l, r, u2, d2, l2, r2;
1691             std::vector<oclMat> datas;
1692             oclMat out;
1693         };
1694
1695         class CV_EXPORTS StereoConstantSpaceBP
1696         {
1697         public:
1698             enum { DEFAULT_NDISP    = 128 };
1699             enum { DEFAULT_ITERS    = 8   };
1700             enum { DEFAULT_LEVELS   = 4   };
1701             enum { DEFAULT_NR_PLANE = 4   };
1702             static void estimateRecommendedParams(int width, int height, int &ndisp, int &iters, int &levels, int &nr_plane);
1703             explicit StereoConstantSpaceBP(
1704                 int ndisp    = DEFAULT_NDISP,
1705                 int iters    = DEFAULT_ITERS,
1706                 int levels   = DEFAULT_LEVELS,
1707                 int nr_plane = DEFAULT_NR_PLANE,
1708                 int msg_type = CV_32F);
1709             StereoConstantSpaceBP(int ndisp, int iters, int levels, int nr_plane,
1710                 float max_data_term, float data_weight, float max_disc_term, float disc_single_jump,
1711                 int min_disp_th = 0,
1712                 int msg_type = CV_32F);
1713             void operator()(const oclMat &left, const oclMat &right, oclMat &disparity);
1714             int ndisp;
1715             int iters;
1716             int levels;
1717             int nr_plane;
1718             float max_data_term;
1719             float data_weight;
1720             float max_disc_term;
1721             float disc_single_jump;
1722             int min_disp_th;
1723             int msg_type;
1724             bool use_local_init_data_cost;
1725         private:
1726             oclMat u[2], d[2], l[2], r[2];
1727             oclMat disp_selected_pyr[2];
1728             oclMat data_cost;
1729             oclMat data_cost_selected;
1730             oclMat temp;
1731             oclMat out;
1732         };
1733
1734         // Implementation of the Zach, Pock and Bischof Dual TV-L1 Optical Flow method
1735         //
1736         // see reference:
1737         //   [1] C. Zach, T. Pock and H. Bischof, "A Duality Based Approach for Realtime TV-L1 Optical Flow".
1738         //   [2] Javier Sanchez, Enric Meinhardt-Llopis and Gabriele Facciolo. "TV-L1 Optical Flow Estimation".
1739         class CV_EXPORTS OpticalFlowDual_TVL1_OCL
1740         {
1741         public:
1742             OpticalFlowDual_TVL1_OCL();
1743
1744             void operator ()(const oclMat& I0, const oclMat& I1, oclMat& flowx, oclMat& flowy);
1745
1746             void collectGarbage();
1747
1748             /**
1749             * Time step of the numerical scheme.
1750             */
1751             double tau;
1752
1753             /**
1754             * Weight parameter for the data term, attachment parameter.
1755             * This is the most relevant parameter, which determines the smoothness of the output.
1756             * The smaller this parameter is, the smoother the solutions we obtain.
1757             * It depends on the range of motions of the images, so its value should be adapted to each image sequence.
1758             */
1759             double lambda;
1760
1761             /**
1762             * Weight parameter for (u - v)^2, tightness parameter.
1763             * It serves as a link between the attachment and the regularization terms.
1764             * In theory, it should have a small value in order to maintain both parts in correspondence.
1765             * The method is stable for a large range of values of this parameter.
1766             */
1767             double theta;
1768
1769             /**
1770             * Number of scales used to create the pyramid of images.
1771             */
1772             int nscales;
1773
1774             /**
1775             * Number of warpings per scale.
1776             * Represents the number of times that I1(x+u0) and grad( I1(x+u0) ) are computed per scale.
1777             * This is a parameter that assures the stability of the method.
1778             * It also affects the running time, so it is a compromise between speed and accuracy.
1779             */
1780             int warps;
1781
1782             /**
1783             * Stopping criterion threshold used in the numerical scheme, which is a trade-off between precision and running time.
1784             * A small value will yield more accurate solutions at the expense of a slower convergence.
1785             */
1786             double epsilon;
1787
1788             /**
1789             * Stopping criterion iterations number used in the numerical scheme.
1790             */
1791             int iterations;
1792
1793             bool useInitialFlow;
1794
1795         private:
1796             void procOneScale(const oclMat& I0, const oclMat& I1, oclMat& u1, oclMat& u2);
1797
1798             std::vector<oclMat> I0s;
1799             std::vector<oclMat> I1s;
1800             std::vector<oclMat> u1s;
1801             std::vector<oclMat> u2s;
1802
1803             oclMat I1x_buf;
1804             oclMat I1y_buf;
1805
1806             oclMat I1w_buf;
1807             oclMat I1wx_buf;
1808             oclMat I1wy_buf;
1809
1810             oclMat grad_buf;
1811             oclMat rho_c_buf;
1812
1813             oclMat p11_buf;
1814             oclMat p12_buf;
1815             oclMat p21_buf;
1816             oclMat p22_buf;
1817
1818             oclMat diff_buf;
1819             oclMat norm_buf;
1820         };
1821         // current supported sorting methods
1822         enum
1823         {
1824             SORT_BITONIC,   // only support power-of-2 buffer size
1825             SORT_SELECTION, // cannot sort duplicate keys
1826             SORT_MERGE,
1827             SORT_RADIX      // only support signed int/float keys(CV_32S/CV_32F)
1828         };
1829         //! Returns the sorted result of all the elements in input based on equivalent keys.
1830         //
1831         //  The element unit in the values to be sorted is determined from the data type,
1832         //  i.e., a CV_32FC2 input {a1a2, b1b2} will be considered as two elements, regardless its
1833         //  matrix dimension.
1834         //  both keys and values will be sorted inplace
1835         //  Key needs to be single channel oclMat.
1836         //
1837         //  Example:
1838         //  input -
1839         //    keys   = {2,    3,   1}   (CV_8UC1)
1840         //    values = {10,5, 4,3, 6,2} (CV_8UC2)
1841         //  sortByKey(keys, values, SORT_SELECTION, false);
1842         //  output -
1843         //    keys   = {1,    2,   3}   (CV_8UC1)
1844         //    values = {6,2, 10,5, 4,3} (CV_8UC2)
1845         CV_EXPORTS void sortByKey(oclMat& keys, oclMat& values, int method, bool isGreaterThan = false);
1846         /*!Base class for MOG and MOG2!*/
1847         class CV_EXPORTS BackgroundSubtractor
1848         {
1849         public:
1850             //! the virtual destructor
1851             virtual ~BackgroundSubtractor();
1852             //! the update operator that takes the next video frame and returns the current foreground mask as 8-bit binary image.
1853             virtual void operator()(const oclMat& image, oclMat& fgmask, float learningRate);
1854
1855             //! computes a background image
1856             virtual void getBackgroundImage(oclMat& backgroundImage) const = 0;
1857         };
1858                 /*!
1859         Gaussian Mixture-based Backbround/Foreground Segmentation Algorithm
1860
1861         The class implements the following algorithm:
1862         "An improved adaptive background mixture model for real-time tracking with shadow detection"
1863         P. KadewTraKuPong and R. Bowden,
1864         Proc. 2nd European Workshp on Advanced Video-Based Surveillance Systems, 2001."
1865         http://personal.ee.surrey.ac.uk/Personal/R.Bowden/publications/avbs01/avbs01.pdf
1866         */
1867         class CV_EXPORTS MOG: public cv::ocl::BackgroundSubtractor
1868         {
1869         public:
1870             //! the default constructor
1871             MOG(int nmixtures = -1);
1872
1873             //! re-initiaization method
1874             void initialize(Size frameSize, int frameType);
1875
1876             //! the update operator
1877             void operator()(const oclMat& frame, oclMat& fgmask, float learningRate = 0.f);
1878
1879             //! computes a background image which are the mean of all background gaussians
1880             void getBackgroundImage(oclMat& backgroundImage) const;
1881
1882             //! releases all inner buffers
1883             void release();
1884
1885             int history;
1886             float varThreshold;
1887             float backgroundRatio;
1888             float noiseSigma;
1889
1890         private:
1891             int nmixtures_;
1892
1893             Size frameSize_;
1894             int frameType_;
1895             int nframes_;
1896
1897             oclMat weight_;
1898             oclMat sortKey_;
1899             oclMat mean_;
1900             oclMat var_;
1901         };
1902
1903         /*!
1904         The class implements the following algorithm:
1905         "Improved adaptive Gausian mixture model for background subtraction"
1906         Z.Zivkovic
1907         International Conference Pattern Recognition, UK, August, 2004.
1908         http://www.zoranz.net/Publications/zivkovic2004ICPR.pdf
1909         */
1910         class CV_EXPORTS MOG2: public cv::ocl::BackgroundSubtractor
1911         {
1912         public:
1913             //! the default constructor
1914             MOG2(int nmixtures = -1);
1915
1916             //! re-initiaization method
1917             void initialize(Size frameSize, int frameType);
1918
1919             //! the update operator
1920             void operator()(const oclMat& frame, oclMat& fgmask, float learningRate = -1.0f);
1921
1922             //! computes a background image which are the mean of all background gaussians
1923             void getBackgroundImage(oclMat& backgroundImage) const;
1924
1925             //! releases all inner buffers
1926             void release();
1927
1928             // parameters
1929             // you should call initialize after parameters changes
1930
1931             int history;
1932
1933             //! here it is the maximum allowed number of mixture components.
1934             //! Actual number is determined dynamically per pixel
1935             float varThreshold;
1936             // threshold on the squared Mahalanobis distance to decide if it is well described
1937             // by the background model or not. Related to Cthr from the paper.
1938             // This does not influence the update of the background. A typical value could be 4 sigma
1939             // and that is varThreshold=4*4=16; Corresponds to Tb in the paper.
1940
1941             /////////////////////////
1942             // less important parameters - things you might change but be carefull
1943             ////////////////////////
1944
1945             float backgroundRatio;
1946             // corresponds to fTB=1-cf from the paper
1947             // TB - threshold when the component becomes significant enough to be included into
1948             // the background model. It is the TB=1-cf from the paper. So I use cf=0.1 => TB=0.
1949             // For alpha=0.001 it means that the mode should exist for approximately 105 frames before
1950             // it is considered foreground
1951             // float noiseSigma;
1952             float varThresholdGen;
1953
1954             //correspondts to Tg - threshold on the squared Mahalan. dist. to decide
1955             //when a sample is close to the existing components. If it is not close
1956             //to any a new component will be generated. I use 3 sigma => Tg=3*3=9.
1957             //Smaller Tg leads to more generated components and higher Tg might make
1958             //lead to small number of components but they can grow too large
1959             float fVarInit;
1960             float fVarMin;
1961             float fVarMax;
1962
1963             //initial variance  for the newly generated components.
1964             //It will will influence the speed of adaptation. A good guess should be made.
1965             //A simple way is to estimate the typical standard deviation from the images.
1966             //I used here 10 as a reasonable value
1967             // min and max can be used to further control the variance
1968             float fCT; //CT - complexity reduction prior
1969             //this is related to the number of samples needed to accept that a component
1970             //actually exists. We use CT=0.05 of all the samples. By setting CT=0 you get
1971             //the standard Stauffer&Grimson algorithm (maybe not exact but very similar)
1972
1973             //shadow detection parameters
1974             bool bShadowDetection; //default 1 - do shadow detection
1975             unsigned char nShadowDetection; //do shadow detection - insert this value as the detection result - 127 default value
1976             float fTau;
1977             // Tau - shadow threshold. The shadow is detected if the pixel is darker
1978             //version of the background. Tau is a threshold on how much darker the shadow can be.
1979             //Tau= 0.5 means that if pixel is more than 2 times darker then it is not shadow
1980             //See: Prati,Mikic,Trivedi,Cucchiarra,"Detecting Moving Shadows...",IEEE PAMI,2003.
1981
1982         private:
1983             int nmixtures_;
1984
1985             Size frameSize_;
1986             int frameType_;
1987             int nframes_;
1988
1989             oclMat weight_;
1990             oclMat variance_;
1991             oclMat mean_;
1992
1993             oclMat bgmodelUsedModes_; //keep track of number of modes per pixel
1994         };
1995
1996         /*!***************Kalman Filter*************!*/
1997         class CV_EXPORTS KalmanFilter
1998         {
1999         public:
2000             KalmanFilter();
2001             //! the full constructor taking the dimensionality of the state, of the measurement and of the control vector
2002             KalmanFilter(int dynamParams, int measureParams, int controlParams=0, int type=CV_32F);
2003             //! re-initializes Kalman filter. The previous content is destroyed.
2004             void init(int dynamParams, int measureParams, int controlParams=0, int type=CV_32F);
2005
2006             const oclMat& predict(const oclMat& control=oclMat());
2007             const oclMat& correct(const oclMat& measurement);
2008
2009             oclMat statePre;           //!< predicted state (x'(k)): x(k)=A*x(k-1)+B*u(k)
2010             oclMat statePost;          //!< corrected state (x(k)): x(k)=x'(k)+K(k)*(z(k)-H*x'(k))
2011             oclMat transitionMatrix;   //!< state transition matrix (A)
2012             oclMat controlMatrix;      //!< control matrix (B) (not used if there is no control)
2013             oclMat measurementMatrix;  //!< measurement matrix (H)
2014             oclMat processNoiseCov;    //!< process noise covariance matrix (Q)
2015             oclMat measurementNoiseCov;//!< measurement noise covariance matrix (R)
2016             oclMat errorCovPre;        //!< priori error estimate covariance matrix (P'(k)): P'(k)=A*P(k-1)*At + Q)*/
2017             oclMat gain;               //!< Kalman gain matrix (K(k)): K(k)=P'(k)*Ht*inv(H*P'(k)*Ht+R)
2018             oclMat errorCovPost;       //!< posteriori error estimate covariance matrix (P(k)): P(k)=(I-K(k)*H)*P'(k)
2019         private:
2020             oclMat temp1;
2021             oclMat temp2;
2022             oclMat temp3;
2023             oclMat temp4;
2024             oclMat temp5;
2025         };
2026
2027         /*!***************K Nearest Neighbour*************!*/
2028         class CV_EXPORTS KNearestNeighbour: public CvKNearest
2029         {
2030         public:
2031             KNearestNeighbour();
2032             ~KNearestNeighbour();
2033
2034             bool train(const Mat& trainData, Mat& labels, Mat& sampleIdx = Mat().setTo(Scalar::all(0)),
2035                 bool isRegression = false, int max_k = 32, bool updateBase = false);
2036
2037             void clear();
2038
2039             void find_nearest(const oclMat& samples, int k, oclMat& lables);
2040
2041         private:
2042             oclMat samples_ocl;
2043         };
2044
2045         /*!***************  SVM  *************!*/
2046         class CV_EXPORTS CvSVM_OCL : public CvSVM
2047         {
2048         public:
2049             CvSVM_OCL();
2050
2051             CvSVM_OCL(const cv::Mat& trainData, const cv::Mat& responses,
2052                       const cv::Mat& varIdx=cv::Mat(), const cv::Mat& sampleIdx=cv::Mat(),
2053                       CvSVMParams params=CvSVMParams());
2054             CV_WRAP float predict( const int row_index, Mat& src, bool returnDFVal=false ) const;
2055             CV_WRAP void predict( cv::InputArray samples, cv::OutputArray results ) const;
2056             CV_WRAP float predict( const cv::Mat& sample, bool returnDFVal=false ) const;
2057             float predict( const CvMat* samples, CV_OUT CvMat* results ) const;
2058
2059         protected:
2060             float predict( const int row_index, int row_len, Mat& src, bool returnDFVal=false ) const;
2061             void create_kernel();
2062             void create_solver();
2063         };
2064
2065         /*!***************  END  *************!*/
2066     }
2067 }
2068 #if defined _MSC_VER && _MSC_VER >= 1200
2069 #  pragma warning( push)
2070 #  pragma warning( disable: 4267)
2071 #endif
2072 #include "opencv2/ocl/matrix_operations.hpp"
2073 #if defined _MSC_VER && _MSC_VER >= 1200
2074 #  pragma warning( pop)
2075 #endif
2076
2077 #endif /* __OPENCV_OCL_HPP__ */