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