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