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