Merge branch 'master' into gpu-cuda-rename
[platform/upstream/opencv.git] / modules / ocl / include / opencv2 / ocl.hpp
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 //  By downloading, copying, installing or using the software you agree to this license.
6 //  If you do not agree to this license, do not download, install,
7 //  copy or use the software.
8 //
9 //
10 //                           License Agreement
11 //                For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2010-2012, Institute Of Software Chinese Academy Of Science, all rights reserved.
14 // Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
15 // Copyright (C) 2010-2012, Multicoreware, Inc., all rights reserved.
16 // Third party copyrights are property of their respective owners.
17 //
18 // Redistribution and use in source and binary forms, with or without modification,
19 // are permitted provided that the following conditions are met:
20 //
21 //   * Redistribution's of source code must retain the above copyright notice,
22 //     this list of conditions and the following disclaimer.
23 //
24 //   * Redistribution's in binary form must reproduce the above copyright notice,
25 //     this list of conditions and the following disclaimer in the documentation
26 //     and/or other oclMaterials provided with the distribution.
27 //
28 //   * The name of the copyright holders may not be used to endorse or promote products
29 //     derived from this software without specific prior written permission.
30 //
31 // This software is provided by the copyright holders and contributors "as is" and
32 // any express or implied warranties, including, but not limited to, the implied
33 // warranties of merchantability and fitness for a particular purpose are disclaimed.
34 // In no event shall the Intel Corporation or contributors be liable for any direct,
35 // indirect, incidental, special, exemplary, or consequential damages
36 // (including, but not limited to, procurement of substitute goods or services;
37 // loss of use, data, or profits; or business interruption) however caused
38 // and on any theory of liability, whether in contract, strict liability,
39 // or tort (including negligence or otherwise) arising in any way out of
40 // the use of this software, even if advised of the possibility of such damage.
41 //
42 //M*/
43
44 #ifndef __OPENCV_OCL_HPP__
45 #define __OPENCV_OCL_HPP__
46
47 #include <memory>
48 #include <vector>
49
50 #include "opencv2/core.hpp"
51 #include "opencv2/imgproc.hpp"
52 #include "opencv2/objdetect.hpp"
53
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 sigmaSpace, int borderType=BORDER_DEFAULT);
523
524         //! Applies an adaptive bilateral filter to the input image
525         //  This is not truly a bilateral filter. Instead of using user provided fixed parameters,
526         //  the function calculates a constant at each window based on local standard deviation,
527         //  and use this constant to do filtering.
528         //  supports 8UC1 8UC3
529         CV_EXPORTS void adaptiveBilateralFilter(const oclMat& src, oclMat& dst, Size ksize, double sigmaSpace, Point anchor = Point(-1, -1), int borderType=BORDER_DEFAULT);
530
531         //! computes exponent of each matrix element (b = e**a)
532         // supports only CV_32FC1 type
533         CV_EXPORTS void exp(const oclMat &a, oclMat &b);
534
535         //! computes natural logarithm of absolute value of each matrix element: b = log(abs(a))
536         // supports only CV_32FC1 type
537         CV_EXPORTS void log(const oclMat &a, oclMat &b);
538
539         //! computes magnitude of each (x(i), y(i)) vector
540         // supports only CV_32F CV_64F type
541         CV_EXPORTS void magnitude(const oclMat &x, const oclMat &y, oclMat &magnitude);
542         CV_EXPORTS void magnitudeSqr(const oclMat &x, const oclMat &y, oclMat &magnitude);
543
544         CV_EXPORTS void magnitudeSqr(const oclMat &x, oclMat &magnitude);
545
546         //! computes angle (angle(i)) of each (x(i), y(i)) vector
547         // supports only CV_32F CV_64F type
548         CV_EXPORTS void phase(const oclMat &x, const oclMat &y, oclMat &angle, bool angleInDegrees = false);
549
550         //! the function raises every element of tne input array to p
551         //! support only CV_32F CV_64F type
552         CV_EXPORTS void pow(const oclMat &x, double p, oclMat &y);
553
554         //! converts Cartesian coordinates to polar
555         // supports only CV_32F CV_64F type
556         CV_EXPORTS void cartToPolar(const oclMat &x, const oclMat &y, oclMat &magnitude, oclMat &angle, bool angleInDegrees = false);
557
558         //! converts polar coordinates to Cartesian
559         // supports only CV_32F CV_64F type
560         CV_EXPORTS void polarToCart(const oclMat &magnitude, const oclMat &angle, oclMat &x, oclMat &y, bool angleInDegrees = false);
561
562         //! perfroms per-elements bit-wise inversion
563         // supports all types
564         CV_EXPORTS void bitwise_not(const oclMat &src, oclMat &dst);
565         //! calculates per-element bit-wise disjunction of two arrays
566         // supports all types
567         CV_EXPORTS void bitwise_or(const oclMat &src1, const oclMat &src2, oclMat &dst, const oclMat &mask = oclMat());
568         CV_EXPORTS void bitwise_or(const oclMat &src1, const Scalar &s, oclMat &dst, const oclMat &mask = oclMat());
569         //! calculates per-element bit-wise conjunction of two arrays
570         // supports all types
571         CV_EXPORTS void bitwise_and(const oclMat &src1, const oclMat &src2, oclMat &dst, const oclMat &mask = oclMat());
572         CV_EXPORTS void bitwise_and(const oclMat &src1, const Scalar &s, oclMat &dst, const oclMat &mask = oclMat());
573         //! calculates per-element bit-wise "exclusive or" operation
574         // supports all types
575         CV_EXPORTS void bitwise_xor(const oclMat &src1, const oclMat &src2, oclMat &dst, const oclMat &mask = oclMat());
576         CV_EXPORTS void bitwise_xor(const oclMat &src1, const Scalar &s, oclMat &dst, const oclMat &mask = oclMat());
577
578         //! Logical operators
579         CV_EXPORTS oclMat operator ~ (const oclMat &);
580         CV_EXPORTS oclMat operator | (const oclMat &, const oclMat &);
581         CV_EXPORTS oclMat operator & (const oclMat &, const oclMat &);
582         CV_EXPORTS oclMat operator ^ (const oclMat &, const oclMat &);
583
584
585         //! Mathematics operators
586         CV_EXPORTS oclMatExpr operator + (const oclMat &src1, const oclMat &src2);
587         CV_EXPORTS oclMatExpr operator - (const oclMat &src1, const oclMat &src2);
588         CV_EXPORTS oclMatExpr operator * (const oclMat &src1, const oclMat &src2);
589         CV_EXPORTS oclMatExpr operator / (const oclMat &src1, const oclMat &src2);
590
591         struct CV_EXPORTS ConvolveBuf
592         {
593             Size result_size;
594             Size block_size;
595             Size user_block_size;
596             Size dft_size;
597
598             oclMat image_spect, templ_spect, result_spect;
599             oclMat image_block, templ_block, result_data;
600
601             void create(Size image_size, Size templ_size);
602             static Size estimateBlockSize(Size result_size, Size templ_size);
603         };
604
605         //! computes convolution of two images, may use discrete Fourier transform
606         //! support only CV_32FC1 type
607         CV_EXPORTS void convolve(const oclMat &image, const oclMat &temp1, oclMat &result, bool ccorr = false);
608         CV_EXPORTS void convolve(const oclMat &image, const oclMat &temp1, oclMat &result, bool ccorr, ConvolveBuf& buf);
609
610         //! Performs a per-element multiplication of two Fourier spectrums.
611         //! Only full (not packed) CV_32FC2 complex spectrums in the interleaved format are supported for now.
612         //! support only CV_32FC2 type
613         CV_EXPORTS void mulSpectrums(const oclMat &a, const oclMat &b, oclMat &c, int flags, float scale, bool conjB = false);
614
615         CV_EXPORTS void cvtColor(const oclMat &src, oclMat &dst, int code , int dcn = 0);
616
617         //////////////////////////////// Filter Engine ////////////////////////////////
618
619         /*!
620           The Base Class for 1D or Row-wise Filters
621
622           This is the base class for linear or non-linear filters that process 1D data.
623           In particular, such filters are used for the "horizontal" filtering parts in separable filters.
624           */
625         class CV_EXPORTS BaseRowFilter_GPU
626         {
627         public:
628             BaseRowFilter_GPU(int ksize_, int anchor_, int bordertype_) : ksize(ksize_), anchor(anchor_), bordertype(bordertype_) {}
629             virtual ~BaseRowFilter_GPU() {}
630             virtual void operator()(const oclMat &src, oclMat &dst) = 0;
631             int ksize, anchor, bordertype;
632         };
633
634         /*!
635           The Base Class for Column-wise Filters
636
637           This is the base class for linear or non-linear filters that process columns of 2D arrays.
638           Such filters are used for the "vertical" filtering parts in separable filters.
639           */
640         class CV_EXPORTS BaseColumnFilter_GPU
641         {
642         public:
643             BaseColumnFilter_GPU(int ksize_, int anchor_, int bordertype_) : ksize(ksize_), anchor(anchor_), bordertype(bordertype_) {}
644             virtual ~BaseColumnFilter_GPU() {}
645             virtual void operator()(const oclMat &src, oclMat &dst) = 0;
646             int ksize, anchor, bordertype;
647         };
648
649         /*!
650           The Base Class for Non-Separable 2D Filters.
651
652           This is the base class for linear or non-linear 2D filters.
653           */
654         class CV_EXPORTS BaseFilter_GPU
655         {
656         public:
657             BaseFilter_GPU(const Size &ksize_, const Point &anchor_, const int &borderType_)
658                 : ksize(ksize_), anchor(anchor_), borderType(borderType_) {}
659             virtual ~BaseFilter_GPU() {}
660             virtual void operator()(const oclMat &src, oclMat &dst) = 0;
661             Size ksize;
662             Point anchor;
663             int borderType;
664         };
665
666         /*!
667           The Base Class for Filter Engine.
668
669           The class can be used to apply an arbitrary filtering operation to an image.
670           It contains all the necessary intermediate buffers.
671           */
672         class CV_EXPORTS FilterEngine_GPU
673         {
674         public:
675             virtual ~FilterEngine_GPU() {}
676
677             virtual void apply(const oclMat &src, oclMat &dst, Rect roi = Rect(0, 0, -1, -1)) = 0;
678         };
679
680         //! returns the non-separable filter engine with the specified filter
681         CV_EXPORTS Ptr<FilterEngine_GPU> createFilter2D_GPU(const Ptr<BaseFilter_GPU> filter2D);
682
683         //! returns the primitive row filter with the specified kernel
684         CV_EXPORTS Ptr<BaseRowFilter_GPU> getLinearRowFilter_GPU(int srcType, int bufType, const Mat &rowKernel,
685                 int anchor = -1, int bordertype = BORDER_DEFAULT);
686
687         //! returns the primitive column filter with the specified kernel
688         CV_EXPORTS Ptr<BaseColumnFilter_GPU> getLinearColumnFilter_GPU(int bufType, int dstType, const Mat &columnKernel,
689                 int anchor = -1, int bordertype = BORDER_DEFAULT, double delta = 0.0);
690
691         //! returns the separable linear filter engine
692         CV_EXPORTS Ptr<FilterEngine_GPU> createSeparableLinearFilter_GPU(int srcType, int dstType, const Mat &rowKernel,
693                 const Mat &columnKernel, const Point &anchor = Point(-1, -1), double delta = 0.0, int bordertype = BORDER_DEFAULT);
694
695         //! returns the separable filter engine with the specified filters
696         CV_EXPORTS Ptr<FilterEngine_GPU> createSeparableFilter_GPU(const Ptr<BaseRowFilter_GPU> &rowFilter,
697                 const Ptr<BaseColumnFilter_GPU> &columnFilter);
698
699         //! returns the Gaussian filter engine
700         CV_EXPORTS Ptr<FilterEngine_GPU> createGaussianFilter_GPU(int type, Size ksize, double sigma1, double sigma2 = 0, int bordertype = BORDER_DEFAULT);
701
702         //! returns filter engine for the generalized Sobel operator
703         CV_EXPORTS Ptr<FilterEngine_GPU> createDerivFilter_GPU( int srcType, int dstType, int dx, int dy, int ksize, int borderType = BORDER_DEFAULT );
704
705         //! applies Laplacian operator to the image
706         // supports only ksize = 1 and ksize = 3 8UC1 8UC4 32FC1 32FC4 data type
707         CV_EXPORTS void Laplacian(const oclMat &src, oclMat &dst, int ddepth, int ksize = 1, double scale = 1);
708
709         //! returns 2D box filter
710         // supports CV_8UC1 and CV_8UC4 source type, dst type must be the same as source type
711         CV_EXPORTS Ptr<BaseFilter_GPU> getBoxFilter_GPU(int srcType, int dstType,
712                 const Size &ksize, Point anchor = Point(-1, -1), int borderType = BORDER_DEFAULT);
713
714         //! returns box filter engine
715         CV_EXPORTS Ptr<FilterEngine_GPU> createBoxFilter_GPU(int srcType, int dstType, const Size &ksize,
716                 const Point &anchor = Point(-1, -1), int borderType = BORDER_DEFAULT);
717
718         //! returns 2D filter with the specified kernel
719         // supports CV_8UC1 and CV_8UC4 types
720         CV_EXPORTS Ptr<BaseFilter_GPU> getLinearFilter_GPU(int srcType, int dstType, const Mat &kernel, const Size &ksize,
721                 const Point &anchor = Point(-1, -1), int borderType = BORDER_DEFAULT);
722
723         //! returns the non-separable linear filter engine
724         CV_EXPORTS Ptr<FilterEngine_GPU> createLinearFilter_GPU(int srcType, int dstType, const Mat &kernel,
725                 const Point &anchor = Point(-1, -1), int borderType = BORDER_DEFAULT);
726
727         //! smooths the image using the normalized box filter
728         // supports data type: CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4
729         // supports border type: BORDER_CONSTANT, BORDER_REPLICATE, BORDER_REFLECT,BORDER_REFLECT_101,BORDER_WRAP
730         CV_EXPORTS void boxFilter(const oclMat &src, oclMat &dst, int ddepth, Size ksize,
731                                   Point anchor = Point(-1, -1), int borderType = BORDER_DEFAULT);
732
733         //! returns 2D morphological filter
734         //! only MORPH_ERODE and MORPH_DILATE are supported
735         // supports CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4 types
736         // kernel must have CV_8UC1 type, one rows and cols == ksize.width * ksize.height
737         CV_EXPORTS Ptr<BaseFilter_GPU> getMorphologyFilter_GPU(int op, int type, const Mat &kernel, const Size &ksize,
738                 Point anchor = Point(-1, -1));
739
740         //! returns morphological filter engine. Only MORPH_ERODE and MORPH_DILATE are supported.
741         CV_EXPORTS Ptr<FilterEngine_GPU> createMorphologyFilter_GPU(int op, int type, const Mat &kernel,
742                 const Point &anchor = Point(-1, -1), int iterations = 1);
743
744         //! a synonym for normalized box filter
745         // supports data type: CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4
746         // supports border type: BORDER_CONSTANT, BORDER_REPLICATE, BORDER_REFLECT,BORDER_REFLECT_101
747         static inline void blur(const oclMat &src, oclMat &dst, Size ksize, Point anchor = Point(-1, -1),
748                                 int borderType = BORDER_CONSTANT)
749         {
750             boxFilter(src, dst, -1, ksize, anchor, borderType);
751         }
752
753         //! applies non-separable 2D linear filter to the image
754         //  Note, at the moment this function only works when anchor point is in the kernel center
755         //  and kernel size supported is either 3x3 or 5x5; otherwise the function will fail to output valid result
756         CV_EXPORTS void filter2D(const oclMat &src, oclMat &dst, int ddepth, const Mat &kernel,
757                                  Point anchor = Point(-1, -1), int borderType = BORDER_DEFAULT);
758
759         //! applies separable 2D linear filter to the image
760         CV_EXPORTS void sepFilter2D(const oclMat &src, oclMat &dst, int ddepth, const Mat &kernelX, const Mat &kernelY,
761                                     Point anchor = Point(-1, -1), double delta = 0.0, int bordertype = BORDER_DEFAULT);
762
763         //! applies generalized Sobel operator to the image
764         // dst.type must equalize src.type
765         // supports data type: CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4
766         // supports border type: BORDER_CONSTANT, BORDER_REPLICATE, BORDER_REFLECT,BORDER_REFLECT_101
767         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);
768
769         //! applies the vertical or horizontal Scharr operator to the image
770         // dst.type must equalize src.type
771         // supports data type: CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4
772         // supports border type: BORDER_CONSTANT, BORDER_REPLICATE, BORDER_REFLECT,BORDER_REFLECT_101
773         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);
774
775         //! smooths the image using Gaussian filter.
776         // dst.type must equalize src.type
777         // supports data type: CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4
778         // supports border type: BORDER_CONSTANT, BORDER_REPLICATE, BORDER_REFLECT,BORDER_REFLECT_101
779         CV_EXPORTS void GaussianBlur(const oclMat &src, oclMat &dst, Size ksize, double sigma1, double sigma2 = 0, int bordertype = BORDER_DEFAULT);
780
781         //! erodes the image (applies the local minimum operator)
782         // supports data type: CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4
783         CV_EXPORTS void erode( const oclMat &src, oclMat &dst, const Mat &kernel, Point anchor = Point(-1, -1), int iterations = 1,
784
785                                int borderType = BORDER_CONSTANT, const Scalar &borderValue = morphologyDefaultBorderValue());
786
787
788         //! dilates the image (applies the local maximum operator)
789         // supports data type: CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4
790         CV_EXPORTS void dilate( const oclMat &src, oclMat &dst, const Mat &kernel, Point anchor = Point(-1, -1), int iterations = 1,
791
792                                 int borderType = BORDER_CONSTANT, const Scalar &borderValue = morphologyDefaultBorderValue());
793
794
795         //! applies an advanced morphological operation to the image
796         CV_EXPORTS void morphologyEx( const oclMat &src, oclMat &dst, int op, const Mat &kernel, Point anchor = Point(-1, -1), int iterations = 1,
797
798                                       int borderType = BORDER_CONSTANT, const Scalar &borderValue = morphologyDefaultBorderValue());
799
800
801         ////////////////////////////// Image processing //////////////////////////////
802         //! Does mean shift filtering on GPU.
803         CV_EXPORTS void meanShiftFiltering(const oclMat &src, oclMat &dst, int sp, int sr,
804                                            TermCriteria criteria = TermCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 5, 1));
805
806         //! Does mean shift procedure on GPU.
807         CV_EXPORTS void meanShiftProc(const oclMat &src, oclMat &dstr, oclMat &dstsp, int sp, int sr,
808                                       TermCriteria criteria = TermCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 5, 1));
809
810         //! Does mean shift segmentation with elimiation of small regions.
811         CV_EXPORTS void meanShiftSegmentation(const oclMat &src, Mat &dst, int sp, int sr, int minsize,
812                                               TermCriteria criteria = TermCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 5, 1));
813
814         //! applies fixed threshold to the image.
815         // supports CV_8UC1 and CV_32FC1 data type
816         // supports threshold type: THRESH_BINARY, THRESH_BINARY_INV, THRESH_TRUNC, THRESH_TOZERO, THRESH_TOZERO_INV
817         CV_EXPORTS double threshold(const oclMat &src, oclMat &dst, double thresh, double maxVal, int type = THRESH_TRUNC);
818
819         //! resizes the image
820         // Supports INTER_NEAREST, INTER_LINEAR
821         // supports CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4 types
822         CV_EXPORTS void resize(const oclMat &src, oclMat &dst, Size dsize, double fx = 0, double fy = 0, int interpolation = INTER_LINEAR);
823
824         //! Applies a generic geometrical transformation to an image.
825
826         // Supports INTER_NEAREST, INTER_LINEAR.
827
828         // Map1 supports CV_16SC2, CV_32FC2  types.
829
830         // Src supports CV_8UC1, CV_8UC2, CV_8UC4.
831
832         CV_EXPORTS void remap(const oclMat &src, oclMat &dst, oclMat &map1, oclMat &map2, int interpolation, int bordertype, const Scalar &value = Scalar());
833
834         //! copies 2D array to a larger destination array and pads borders with user-specifiable constant
835         // supports CV_8UC1, CV_8UC4, CV_32SC1 types
836         CV_EXPORTS void copyMakeBorder(const oclMat &src, oclMat &dst, int top, int bottom, int left, int right, int boardtype, const Scalar &value = Scalar());
837
838         //! Smoothes image using median filter
839         // The source 1- or 4-channel image. When m is 3 or 5, the image depth should be CV 8U or CV 32F.
840         CV_EXPORTS void medianFilter(const oclMat &src, oclMat &dst, int m);
841
842         //! warps the image using affine transformation
843         // Supports INTER_NEAREST, INTER_LINEAR, INTER_CUBIC
844         // supports CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4 types
845         CV_EXPORTS void warpAffine(const oclMat &src, oclMat &dst, const Mat &M, Size dsize, int flags = INTER_LINEAR);
846
847         //! warps the image using perspective transformation
848         // Supports INTER_NEAREST, INTER_LINEAR, INTER_CUBIC
849         // supports CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4 types
850         CV_EXPORTS void warpPerspective(const oclMat &src, oclMat &dst, const Mat &M, Size dsize, int flags = INTER_LINEAR);
851
852         //! computes the integral image and integral for the squared image
853         // sum will have CV_32S type, sqsum - CV32F type
854         // supports only CV_8UC1 source type
855         CV_EXPORTS void integral(const oclMat &src, oclMat &sum, oclMat &sqsum);
856         CV_EXPORTS void integral(const oclMat &src, oclMat &sum);
857         CV_EXPORTS void cornerHarris(const oclMat &src, oclMat &dst, int blockSize, int ksize, double k, int bordertype = cv::BORDER_DEFAULT);
858         CV_EXPORTS void cornerHarris_dxdy(const oclMat &src, oclMat &dst, oclMat &Dx, oclMat &Dy,
859             int blockSize, int ksize, double k, int bordertype = cv::BORDER_DEFAULT);
860         CV_EXPORTS void cornerMinEigenVal(const oclMat &src, oclMat &dst, int blockSize, int ksize, int bordertype = cv::BORDER_DEFAULT);
861         CV_EXPORTS void cornerMinEigenVal_dxdy(const oclMat &src, oclMat &dst, oclMat &Dx, oclMat &Dy,
862             int blockSize, int ksize, int bordertype = cv::BORDER_DEFAULT);
863
864
865         /////////////////////////////////// ML ///////////////////////////////////////////
866
867         //! Compute closest centers for each lines in source and lable it after center's index
868         // supports CV_32FC1/CV_32FC2/CV_32FC4 data type
869         CV_EXPORTS void distanceToCenters(oclMat &dists, oclMat &labels, const oclMat &src, const oclMat &centers);
870
871         //!Does k-means procedure on GPU
872         // supports CV_32FC1/CV_32FC2/CV_32FC4 data type
873         CV_EXPORTS double kmeans(const oclMat &src, int K, oclMat &bestLabels,
874                                      TermCriteria criteria, int attemps, int flags, oclMat &centers);
875
876
877         ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
878         ///////////////////////////////////////////CascadeClassifier//////////////////////////////////////////////////////////////////
879         ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
880         class CV_EXPORTS OclCascadeClassifier : public  cv::CascadeClassifier
881         {
882         public:
883             void detectMultiScale(oclMat &image, CV_OUT std::vector<cv::Rect>& faces,
884                                   double scaleFactor = 1.1, int minNeighbors = 3, int flags = 0,
885                                   Size minSize = Size(), Size maxSize = Size());
886         };
887
888         /////////////////////////////// Pyramid /////////////////////////////////////
889         CV_EXPORTS void pyrDown(const oclMat &src, oclMat &dst);
890
891         //! upsamples the source image and then smoothes it
892         CV_EXPORTS void pyrUp(const oclMat &src, oclMat &dst);
893
894         //! performs linear blending of two images
895         //! to avoid accuracy errors sum of weigths shouldn't be very close to zero
896         // supports only CV_8UC1 source type
897         CV_EXPORTS void blendLinear(const oclMat &img1, const oclMat &img2, const oclMat &weights1, const oclMat &weights2, oclMat &result);
898
899         //! computes vertical sum, supports only CV_32FC1 images
900         CV_EXPORTS void columnSum(const oclMat &src, oclMat &sum);
901
902         ///////////////////////////////////////// match_template /////////////////////////////////////////////////////////////
903         struct CV_EXPORTS MatchTemplateBuf
904         {
905             Size user_block_size;
906             oclMat imagef, templf;
907             std::vector<oclMat> images;
908             std::vector<oclMat> image_sums;
909             std::vector<oclMat> image_sqsums;
910         };
911
912         //! computes the proximity map for the raster template and the image where the template is searched for
913         // Supports TM_SQDIFF, TM_SQDIFF_NORMED, TM_CCORR, TM_CCORR_NORMED, TM_CCOEFF, TM_CCOEFF_NORMED for type 8UC1 and 8UC4
914         // Supports TM_SQDIFF, TM_CCORR for type 32FC1 and 32FC4
915         CV_EXPORTS void matchTemplate(const oclMat &image, const oclMat &templ, oclMat &result, int method);
916
917         //! computes the proximity map for the raster template and the image where the template is searched for
918         // Supports TM_SQDIFF, TM_SQDIFF_NORMED, TM_CCORR, TM_CCORR_NORMED, TM_CCOEFF, TM_CCOEFF_NORMED for type 8UC1 and 8UC4
919         // Supports TM_SQDIFF, TM_CCORR for type 32FC1 and 32FC4
920         CV_EXPORTS void matchTemplate(const oclMat &image, const oclMat &templ, oclMat &result, int method, MatchTemplateBuf &buf);
921
922
923
924         ///////////////////////////////////////////// Canny /////////////////////////////////////////////
925         struct CV_EXPORTS CannyBuf;
926
927         //! compute edges of the input image using Canny operator
928         // Support CV_8UC1 only
929         CV_EXPORTS void Canny(const oclMat &image, oclMat &edges, double low_thresh, double high_thresh, int apperture_size = 3, bool L2gradient = false);
930         CV_EXPORTS void Canny(const oclMat &image, CannyBuf &buf, oclMat &edges, double low_thresh, double high_thresh, int apperture_size = 3, bool L2gradient = false);
931         CV_EXPORTS void Canny(const oclMat &dx, const oclMat &dy, oclMat &edges, double low_thresh, double high_thresh, bool L2gradient = false);
932         CV_EXPORTS void Canny(const oclMat &dx, const oclMat &dy, CannyBuf &buf, oclMat &edges, double low_thresh, double high_thresh, bool L2gradient = false);
933
934         struct CV_EXPORTS CannyBuf
935         {
936             CannyBuf() : counter(NULL) {}
937             ~CannyBuf()
938             {
939                 release();
940             }
941             explicit CannyBuf(const Size &image_size, int apperture_size = 3) : counter(NULL)
942             {
943                 create(image_size, apperture_size);
944             }
945             CannyBuf(const oclMat &dx_, const oclMat &dy_);
946             void create(const Size &image_size, int apperture_size = 3);
947             void release();
948
949             oclMat dx, dy;
950             oclMat dx_buf, dy_buf;
951             oclMat magBuf, mapBuf;
952             oclMat trackBuf1, trackBuf2;
953             void *counter;
954             Ptr<FilterEngine_GPU> filterDX, filterDY;
955         };
956
957         ///////////////////////////////////////// Hough Transform /////////////////////////////////////////
958         //! HoughCircles
959         struct HoughCirclesBuf
960         {
961             oclMat edges;
962             oclMat accum;
963             oclMat srcPoints;
964             oclMat centers;
965             CannyBuf cannyBuf;
966         };
967
968         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);
969         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);
970         CV_EXPORTS void HoughCirclesDownload(const oclMat& d_circles, OutputArray h_circles);
971
972
973         ///////////////////////////////////////// clAmdFft related /////////////////////////////////////////
974         //! Performs a forward or inverse discrete Fourier transform (1D or 2D) of floating point matrix.
975         //! Param dft_size is the size of DFT transform.
976         //!
977         //! For complex-to-real transform it is assumed that the source matrix is packed in CLFFT's format.
978         // support src type of CV32FC1, CV32FC2
979         // support flags: DFT_INVERSE, DFT_REAL_OUTPUT, DFT_COMPLEX_OUTPUT, DFT_ROWS
980         // dft_size is the size of original input, which is used for transformation from complex to real.
981         // dft_size must be powers of 2, 3 and 5
982         // real to complex dft requires at least v1.8 clAmdFft
983         // real to complex dft output is not the same with cpu version
984         // real to complex and complex to real does not support DFT_ROWS
985         CV_EXPORTS void dft(const oclMat &src, oclMat &dst, Size dft_size = Size(0, 0), int flags = 0);
986
987         //! implements generalized matrix product algorithm GEMM from BLAS
988         // The functionality requires clAmdBlas library
989         // only support type CV_32FC1
990         // flag GEMM_3_T is not supported
991         CV_EXPORTS void gemm(const oclMat &src1, const oclMat &src2, double alpha,
992                              const oclMat &src3, double beta, oclMat &dst, int flags = 0);
993
994         //////////////// HOG (Histogram-of-Oriented-Gradients) Descriptor and Object Detector //////////////
995
996         struct CV_EXPORTS HOGDescriptor
997
998         {
999
1000             enum { DEFAULT_WIN_SIGMA = -1 };
1001
1002             enum { DEFAULT_NLEVELS = 64 };
1003
1004             enum { DESCR_FORMAT_ROW_BY_ROW, DESCR_FORMAT_COL_BY_COL };
1005
1006
1007
1008             HOGDescriptor(Size win_size = Size(64, 128), Size block_size = Size(16, 16),
1009
1010                           Size block_stride = Size(8, 8), Size cell_size = Size(8, 8),
1011
1012                           int nbins = 9, double win_sigma = DEFAULT_WIN_SIGMA,
1013
1014                           double threshold_L2hys = 0.2, bool gamma_correction = true,
1015
1016                           int nlevels = DEFAULT_NLEVELS);
1017
1018
1019
1020             size_t getDescriptorSize() const;
1021
1022             size_t getBlockHistogramSize() const;
1023
1024
1025
1026             void setSVMDetector(const std::vector<float> &detector);
1027
1028
1029
1030             static std::vector<float> getDefaultPeopleDetector();
1031
1032             static std::vector<float> getPeopleDetector48x96();
1033
1034             static std::vector<float> getPeopleDetector64x128();
1035
1036
1037
1038             void detect(const oclMat &img, std::vector<Point> &found_locations,
1039
1040                         double hit_threshold = 0, Size win_stride = Size(),
1041
1042                         Size padding = Size());
1043
1044
1045
1046             void detectMultiScale(const oclMat &img, std::vector<Rect> &found_locations,
1047
1048                                   double hit_threshold = 0, Size win_stride = Size(),
1049
1050                                   Size padding = Size(), double scale0 = 1.05,
1051
1052                                   int group_threshold = 2);
1053
1054
1055
1056             void getDescriptors(const oclMat &img, Size win_stride,
1057
1058                                 oclMat &descriptors,
1059
1060                                 int descr_format = DESCR_FORMAT_COL_BY_COL);
1061
1062
1063
1064             Size win_size;
1065
1066             Size block_size;
1067
1068             Size block_stride;
1069
1070             Size cell_size;
1071
1072             int nbins;
1073
1074             double win_sigma;
1075
1076             double threshold_L2hys;
1077
1078             bool gamma_correction;
1079
1080             int nlevels;
1081
1082
1083
1084         protected:
1085
1086             // initialize buffers; only need to do once in case of multiscale detection
1087
1088             void init_buffer(const oclMat &img, Size win_stride);
1089
1090
1091
1092             void computeBlockHistograms(const oclMat &img);
1093
1094             void computeGradient(const oclMat &img, oclMat &grad, oclMat &qangle);
1095
1096
1097
1098             double getWinSigma() const;
1099
1100             bool checkDetectorSize() const;
1101
1102
1103
1104             static int numPartsWithin(int size, int part_size, int stride);
1105
1106             static Size numPartsWithin(Size size, Size part_size, Size stride);
1107
1108
1109
1110             // Coefficients of the separating plane
1111
1112             float free_coef;
1113
1114             oclMat detector;
1115
1116
1117
1118             // Results of the last classification step
1119
1120             oclMat labels;
1121
1122             Mat labels_host;
1123
1124
1125
1126             // Results of the last histogram evaluation step
1127
1128             oclMat block_hists;
1129
1130
1131
1132             // Gradients conputation results
1133
1134             oclMat grad, qangle;
1135
1136
1137
1138             // scaled image
1139
1140             oclMat image_scale;
1141
1142
1143
1144             // effect size of input image (might be different from original size after scaling)
1145
1146             Size effect_size;
1147
1148         };
1149
1150
1151         ////////////////////////feature2d_ocl/////////////////
1152         /****************************************************************************************\
1153         *                                      Distance                                          *
1154         \****************************************************************************************/
1155         template<typename T>
1156         struct CV_EXPORTS Accumulator
1157         {
1158             typedef T Type;
1159         };
1160         template<> struct Accumulator<unsigned char>
1161         {
1162             typedef float Type;
1163         };
1164         template<> struct Accumulator<unsigned short>
1165         {
1166             typedef float Type;
1167         };
1168         template<> struct Accumulator<char>
1169         {
1170             typedef float Type;
1171         };
1172         template<> struct Accumulator<short>
1173         {
1174             typedef float Type;
1175         };
1176
1177         /*
1178          * Manhattan distance (city block distance) functor
1179          */
1180         template<class T>
1181         struct CV_EXPORTS L1
1182         {
1183             enum { normType = NORM_L1 };
1184             typedef T ValueType;
1185             typedef typename Accumulator<T>::Type ResultType;
1186
1187             ResultType operator()( const T *a, const T *b, int size ) const
1188             {
1189                 return normL1<ValueType, ResultType>(a, b, size);
1190             }
1191         };
1192
1193         /*
1194          * Euclidean distance functor
1195          */
1196         template<class T>
1197         struct CV_EXPORTS L2
1198         {
1199             enum { normType = NORM_L2 };
1200             typedef T ValueType;
1201             typedef typename Accumulator<T>::Type ResultType;
1202
1203             ResultType operator()( const T *a, const T *b, int size ) const
1204             {
1205                 return (ResultType)std::sqrt((double)normL2Sqr<ValueType, ResultType>(a, b, size));
1206             }
1207         };
1208
1209         /*
1210          * Hamming distance functor - counts the bit differences between two strings - useful for the Brief descriptor
1211          * bit count of A exclusive XOR'ed with B
1212          */
1213         struct CV_EXPORTS Hamming
1214         {
1215             enum { normType = NORM_HAMMING };
1216             typedef unsigned char ValueType;
1217             typedef int ResultType;
1218
1219             /** this will count the bits in a ^ b
1220              */
1221             ResultType operator()( const unsigned char *a, const unsigned char *b, int size ) const
1222             {
1223                 return normHamming(a, b, size);
1224             }
1225         };
1226
1227         ////////////////////////////////// BruteForceMatcher //////////////////////////////////
1228
1229         class CV_EXPORTS BruteForceMatcher_OCL_base
1230         {
1231         public:
1232             enum DistType {L1Dist = 0, L2Dist, HammingDist};
1233             explicit BruteForceMatcher_OCL_base(DistType distType = L2Dist);
1234
1235             // Add descriptors to train descriptor collection
1236             void add(const std::vector<oclMat> &descCollection);
1237
1238             // Get train descriptors collection
1239             const std::vector<oclMat> &getTrainDescriptors() const;
1240
1241             // Clear train descriptors collection
1242             void clear();
1243
1244             // Return true if there are not train descriptors in collection
1245             bool empty() const;
1246
1247             // Return true if the matcher supports mask in match methods
1248             bool isMaskSupported() const;
1249
1250             // Find one best match for each query descriptor
1251             void matchSingle(const oclMat &query, const oclMat &train,
1252                              oclMat &trainIdx, oclMat &distance,
1253                              const oclMat &mask = oclMat());
1254
1255             // Download trainIdx and distance and convert it to CPU vector with DMatch
1256             static void matchDownload(const oclMat &trainIdx, const oclMat &distance, std::vector<DMatch> &matches);
1257             // Convert trainIdx and distance to vector with DMatch
1258             static void matchConvert(const Mat &trainIdx, const Mat &distance, std::vector<DMatch> &matches);
1259
1260             // Find one best match for each query descriptor
1261             void match(const oclMat &query, const oclMat &train, std::vector<DMatch> &matches, const oclMat &mask = oclMat());
1262
1263             // Make gpu collection of trains and masks in suitable format for matchCollection function
1264             void makeGpuCollection(oclMat &trainCollection, oclMat &maskCollection, const std::vector<oclMat> &masks = std::vector<oclMat>());
1265
1266             // Find one best match from train collection for each query descriptor
1267             void matchCollection(const oclMat &query, const oclMat &trainCollection,
1268                                  oclMat &trainIdx, oclMat &imgIdx, oclMat &distance,
1269                                  const oclMat &masks = oclMat());
1270
1271             // Download trainIdx, imgIdx and distance and convert it to vector with DMatch
1272             static void matchDownload(const oclMat &trainIdx, const oclMat &imgIdx, const oclMat &distance, std::vector<DMatch> &matches);
1273             // Convert trainIdx, imgIdx and distance to vector with DMatch
1274             static void matchConvert(const Mat &trainIdx, const Mat &imgIdx, const Mat &distance, std::vector<DMatch> &matches);
1275
1276             // Find one best match from train collection for each query descriptor.
1277             void match(const oclMat &query, std::vector<DMatch> &matches, const std::vector<oclMat> &masks = std::vector<oclMat>());
1278
1279             // Find k best matches for each query descriptor (in increasing order of distances)
1280             void knnMatchSingle(const oclMat &query, const oclMat &train,
1281                                 oclMat &trainIdx, oclMat &distance, oclMat &allDist, int k,
1282                                 const oclMat &mask = oclMat());
1283
1284             // Download trainIdx and distance and convert it to vector with DMatch
1285             // compactResult is used when mask is not empty. If compactResult is false matches
1286             // vector will have the same size as queryDescriptors rows. If compactResult is true
1287             // matches vector will not contain matches for fully masked out query descriptors.
1288             static void knnMatchDownload(const oclMat &trainIdx, const oclMat &distance,
1289                                          std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
1290             // Convert trainIdx and distance to vector with DMatch
1291             static void knnMatchConvert(const Mat &trainIdx, const Mat &distance,
1292                                         std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
1293
1294             // Find k best matches for each query descriptor (in increasing order of distances).
1295             // compactResult is used when mask is not empty. If compactResult is false matches
1296             // vector will have the same size as queryDescriptors rows. If compactResult is true
1297             // matches vector will not contain matches for fully masked out query descriptors.
1298             void knnMatch(const oclMat &query, const oclMat &train,
1299                           std::vector< std::vector<DMatch> > &matches, int k, const oclMat &mask = oclMat(),
1300                           bool compactResult = false);
1301
1302             // Find k best matches from train collection for each query descriptor (in increasing order of distances)
1303             void knnMatch2Collection(const oclMat &query, const oclMat &trainCollection,
1304                                      oclMat &trainIdx, oclMat &imgIdx, oclMat &distance,
1305                                      const oclMat &maskCollection = oclMat());
1306
1307             // Download trainIdx and distance and convert it to vector with DMatch
1308             // compactResult is used when mask is not empty. If compactResult is false matches
1309             // vector will have the same size as queryDescriptors rows. If compactResult is true
1310             // matches vector will not contain matches for fully masked out query descriptors.
1311             static void knnMatch2Download(const oclMat &trainIdx, const oclMat &imgIdx, const oclMat &distance,
1312                                           std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
1313             // Convert trainIdx and distance to vector with DMatch
1314             static void knnMatch2Convert(const Mat &trainIdx, const Mat &imgIdx, const Mat &distance,
1315                                          std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
1316
1317             // Find k best matches  for each query descriptor (in increasing order of distances).
1318             // compactResult is used when mask is not empty. If compactResult is false matches
1319             // vector will have the same size as queryDescriptors rows. If compactResult is true
1320             // matches vector will not contain matches for fully masked out query descriptors.
1321             void knnMatch(const oclMat &query, std::vector< std::vector<DMatch> > &matches, int k,
1322                           const std::vector<oclMat> &masks = std::vector<oclMat>(), bool compactResult = false);
1323
1324             // Find best matches for each query descriptor which have distance less than maxDistance.
1325             // nMatches.at<int>(0, queryIdx) will contain matches count for queryIdx.
1326             // carefully nMatches can be greater than trainIdx.cols - it means that matcher didn't find all matches,
1327             // because it didn't have enough memory.
1328             // If trainIdx is empty, then trainIdx and distance will be created with size nQuery x max((nTrain / 100), 10),
1329             // otherwize user can pass own allocated trainIdx and distance with size nQuery x nMaxMatches
1330             // Matches doesn't sorted.
1331             void radiusMatchSingle(const oclMat &query, const oclMat &train,
1332                                    oclMat &trainIdx, oclMat &distance, oclMat &nMatches, float maxDistance,
1333                                    const oclMat &mask = oclMat());
1334
1335             // Download trainIdx, nMatches and distance and convert it to vector with DMatch.
1336             // matches will be sorted in increasing order of distances.
1337             // compactResult is used when mask is not empty. If compactResult is false matches
1338             // vector will have the same size as queryDescriptors rows. If compactResult is true
1339             // matches vector will not contain matches for fully masked out query descriptors.
1340             static void radiusMatchDownload(const oclMat &trainIdx, const oclMat &distance, const oclMat &nMatches,
1341                                             std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
1342             // Convert trainIdx, nMatches and distance to vector with DMatch.
1343             static void radiusMatchConvert(const Mat &trainIdx, const Mat &distance, const Mat &nMatches,
1344                                            std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
1345
1346             // Find best matches for each query descriptor which have distance less than maxDistance
1347             // in increasing order of distances).
1348             void radiusMatch(const oclMat &query, const oclMat &train,
1349                              std::vector< std::vector<DMatch> > &matches, float maxDistance,
1350                              const oclMat &mask = oclMat(), bool compactResult = false);
1351
1352             // Find best matches for each query descriptor which have distance less than maxDistance.
1353             // If trainIdx is empty, then trainIdx and distance will be created with size nQuery x max((nQuery / 100), 10),
1354             // otherwize user can pass own allocated trainIdx and distance with size nQuery x nMaxMatches
1355             // Matches doesn't sorted.
1356             void radiusMatchCollection(const oclMat &query, oclMat &trainIdx, oclMat &imgIdx, oclMat &distance, oclMat &nMatches, float maxDistance,
1357                                        const std::vector<oclMat> &masks = std::vector<oclMat>());
1358
1359             // Download trainIdx, imgIdx, nMatches and distance and convert it to vector with DMatch.
1360             // matches will be sorted in increasing order of distances.
1361             // compactResult is used when mask is not empty. If compactResult is false matches
1362             // vector will have the same size as queryDescriptors rows. If compactResult is true
1363             // matches vector will not contain matches for fully masked out query descriptors.
1364             static void radiusMatchDownload(const oclMat &trainIdx, const oclMat &imgIdx, const oclMat &distance, const oclMat &nMatches,
1365                                             std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
1366             // Convert trainIdx, nMatches and distance to vector with DMatch.
1367             static void radiusMatchConvert(const Mat &trainIdx, const Mat &imgIdx, const Mat &distance, const Mat &nMatches,
1368                                            std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
1369
1370             // Find best matches from train collection for each query descriptor which have distance less than
1371             // maxDistance (in increasing order of distances).
1372             void radiusMatch(const oclMat &query, std::vector< std::vector<DMatch> > &matches, float maxDistance,
1373                              const std::vector<oclMat> &masks = std::vector<oclMat>(), bool compactResult = false);
1374
1375             DistType distType;
1376
1377         private:
1378             std::vector<oclMat> trainDescCollection;
1379         };
1380
1381         template <class Distance>
1382         class CV_EXPORTS BruteForceMatcher_OCL;
1383
1384         template <typename T>
1385         class CV_EXPORTS BruteForceMatcher_OCL< L1<T> > : public BruteForceMatcher_OCL_base
1386         {
1387         public:
1388             explicit BruteForceMatcher_OCL() : BruteForceMatcher_OCL_base(L1Dist) {}
1389             explicit BruteForceMatcher_OCL(L1<T> /*d*/) : BruteForceMatcher_OCL_base(L1Dist) {}
1390         };
1391         template <typename T>
1392         class CV_EXPORTS BruteForceMatcher_OCL< L2<T> > : public BruteForceMatcher_OCL_base
1393         {
1394         public:
1395             explicit BruteForceMatcher_OCL() : BruteForceMatcher_OCL_base(L2Dist) {}
1396             explicit BruteForceMatcher_OCL(L2<T> /*d*/) : BruteForceMatcher_OCL_base(L2Dist) {}
1397         };
1398         template <> class CV_EXPORTS BruteForceMatcher_OCL< Hamming > : public BruteForceMatcher_OCL_base
1399         {
1400         public:
1401             explicit BruteForceMatcher_OCL() : BruteForceMatcher_OCL_base(HammingDist) {}
1402             explicit BruteForceMatcher_OCL(Hamming /*d*/) : BruteForceMatcher_OCL_base(HammingDist) {}
1403         };
1404
1405         class CV_EXPORTS BFMatcher_OCL : public BruteForceMatcher_OCL_base
1406         {
1407         public:
1408             explicit BFMatcher_OCL(int norm = NORM_L2) : BruteForceMatcher_OCL_base(norm == NORM_L1 ? L1Dist : norm == NORM_L2 ? L2Dist : HammingDist) {}
1409         };
1410
1411         class CV_EXPORTS GoodFeaturesToTrackDetector_OCL
1412         {
1413         public:
1414             explicit GoodFeaturesToTrackDetector_OCL(int maxCorners = 1000, double qualityLevel = 0.01, double minDistance = 0.0,
1415                 int blockSize = 3, bool useHarrisDetector = false, double harrisK = 0.04);
1416
1417             //! return 1 rows matrix with CV_32FC2 type
1418             void operator ()(const oclMat& image, oclMat& corners, const oclMat& mask = oclMat());
1419             //! download points of type Point2f to a vector. the vector's content will be erased
1420             void downloadPoints(const oclMat &points, std::vector<Point2f> &points_v);
1421
1422             int maxCorners;
1423             double qualityLevel;
1424             double minDistance;
1425
1426             int blockSize;
1427             bool useHarrisDetector;
1428             double harrisK;
1429             void releaseMemory()
1430             {
1431                 Dx_.release();
1432                 Dy_.release();
1433                 eig_.release();
1434                 minMaxbuf_.release();
1435                 tmpCorners_.release();
1436             }
1437         private:
1438             oclMat Dx_;
1439             oclMat Dy_;
1440             oclMat eig_;
1441             oclMat minMaxbuf_;
1442             oclMat tmpCorners_;
1443         };
1444
1445         inline GoodFeaturesToTrackDetector_OCL::GoodFeaturesToTrackDetector_OCL(int maxCorners_, double qualityLevel_, double minDistance_,
1446             int blockSize_, bool useHarrisDetector_, double harrisK_)
1447         {
1448             maxCorners = maxCorners_;
1449             qualityLevel = qualityLevel_;
1450             minDistance = minDistance_;
1451             blockSize = blockSize_;
1452             useHarrisDetector = useHarrisDetector_;
1453             harrisK = harrisK_;
1454         }
1455
1456         /////////////////////////////// PyrLKOpticalFlow /////////////////////////////////////
1457
1458         class CV_EXPORTS PyrLKOpticalFlow
1459         {
1460         public:
1461             PyrLKOpticalFlow()
1462             {
1463                 winSize = Size(21, 21);
1464                 maxLevel = 3;
1465                 iters = 30;
1466                 derivLambda = 0.5;
1467                 useInitialFlow = false;
1468                 minEigThreshold = 1e-4f;
1469                 getMinEigenVals = false;
1470                 isDeviceArch11_ = false;
1471             }
1472
1473             void sparse(const oclMat &prevImg, const oclMat &nextImg, const oclMat &prevPts, oclMat &nextPts,
1474                         oclMat &status, oclMat *err = 0);
1475
1476             void dense(const oclMat &prevImg, const oclMat &nextImg, oclMat &u, oclMat &v, oclMat *err = 0);
1477
1478             Size winSize;
1479             int maxLevel;
1480             int iters;
1481             double derivLambda;
1482             bool useInitialFlow;
1483             float minEigThreshold;
1484             bool getMinEigenVals;
1485
1486             void releaseMemory()
1487             {
1488                 dx_calcBuf_.release();
1489                 dy_calcBuf_.release();
1490
1491                 prevPyr_.clear();
1492                 nextPyr_.clear();
1493
1494                 dx_buf_.release();
1495                 dy_buf_.release();
1496             }
1497
1498         private:
1499             void calcSharrDeriv(const oclMat &src, oclMat &dx, oclMat &dy);
1500
1501             void buildImagePyramid(const oclMat &img0, std::vector<oclMat> &pyr, bool withBorder);
1502
1503             oclMat dx_calcBuf_;
1504             oclMat dy_calcBuf_;
1505
1506             std::vector<oclMat> prevPyr_;
1507             std::vector<oclMat> nextPyr_;
1508
1509             oclMat dx_buf_;
1510             oclMat dy_buf_;
1511
1512             oclMat uPyr_[2];
1513             oclMat vPyr_[2];
1514
1515             bool isDeviceArch11_;
1516         };
1517
1518         class CV_EXPORTS FarnebackOpticalFlow
1519         {
1520         public:
1521             FarnebackOpticalFlow();
1522
1523             int numLevels;
1524             double pyrScale;
1525             bool fastPyramids;
1526             int winSize;
1527             int numIters;
1528             int polyN;
1529             double polySigma;
1530             int flags;
1531
1532             void operator ()(const oclMat &frame0, const oclMat &frame1, oclMat &flowx, oclMat &flowy);
1533
1534             void releaseMemory();
1535
1536         private:
1537             void prepareGaussian(
1538                 int n, double sigma, float *g, float *xg, float *xxg,
1539                 double &ig11, double &ig03, double &ig33, double &ig55);
1540
1541             void setPolynomialExpansionConsts(int n, double sigma);
1542
1543             void updateFlow_boxFilter(
1544                 const oclMat& R0, const oclMat& R1, oclMat& flowx, oclMat &flowy,
1545                 oclMat& M, oclMat &bufM, int blockSize, bool updateMatrices);
1546
1547             void updateFlow_gaussianBlur(
1548                 const oclMat& R0, const oclMat& R1, oclMat& flowx, oclMat& flowy,
1549                 oclMat& M, oclMat &bufM, int blockSize, bool updateMatrices);
1550
1551             oclMat frames_[2];
1552             oclMat pyrLevel_[2], M_, bufM_, R_[2], blurredFrame_[2];
1553             std::vector<oclMat> pyramid0_, pyramid1_;
1554         };
1555
1556         //////////////// build warping maps ////////////////////
1557         //! builds plane warping maps
1558         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);
1559         //! builds cylindrical warping maps
1560         CV_EXPORTS void buildWarpCylindricalMaps(Size src_size, Rect dst_roi, const Mat &K, const Mat &R, float scale, oclMat &map_x, oclMat &map_y);
1561         //! builds spherical warping maps
1562         CV_EXPORTS void buildWarpSphericalMaps(Size src_size, Rect dst_roi, const Mat &K, const Mat &R, float scale, oclMat &map_x, oclMat &map_y);
1563         //! builds Affine warping maps
1564         CV_EXPORTS void buildWarpAffineMaps(const Mat &M, bool inverse, Size dsize, oclMat &xmap, oclMat &ymap);
1565
1566         //! builds Perspective warping maps
1567         CV_EXPORTS void buildWarpPerspectiveMaps(const Mat &M, bool inverse, Size dsize, oclMat &xmap, oclMat &ymap);
1568
1569         ///////////////////////////////////// interpolate frames //////////////////////////////////////////////
1570         //! Interpolate frames (images) using provided optical flow (displacement field).
1571         //! frame0   - frame 0 (32-bit floating point images, single channel)
1572         //! frame1   - frame 1 (the same type and size)
1573         //! fu       - forward horizontal displacement
1574         //! fv       - forward vertical displacement
1575         //! bu       - backward horizontal displacement
1576         //! bv       - backward vertical displacement
1577         //! pos      - new frame position
1578         //! newFrame - new frame
1579         //! buf      - temporary buffer, will have width x 6*height size, CV_32FC1 type and contain 6 oclMat;
1580         //!            occlusion masks            0, occlusion masks            1,
1581         //!            interpolated forward flow  0, interpolated forward flow  1,
1582         //!            interpolated backward flow 0, interpolated backward flow 1
1583         //!
1584         CV_EXPORTS void interpolateFrames(const oclMat &frame0, const oclMat &frame1,
1585                                           const oclMat &fu, const oclMat &fv,
1586                                           const oclMat &bu, const oclMat &bv,
1587                                           float pos, oclMat &newFrame, oclMat &buf);
1588
1589         //! computes moments of the rasterized shape or a vector of points
1590         CV_EXPORTS Moments ocl_moments(InputArray _array, bool binaryImage);
1591
1592         class CV_EXPORTS StereoBM_OCL
1593         {
1594         public:
1595             enum { BASIC_PRESET = 0, PREFILTER_XSOBEL = 1 };
1596
1597             enum { DEFAULT_NDISP = 64, DEFAULT_WINSZ = 19 };
1598
1599             //! the default constructor
1600             StereoBM_OCL();
1601             //! the full constructor taking the camera-specific preset, number of disparities and the SAD window size. ndisparities must be multiple of 8.
1602             StereoBM_OCL(int preset, int ndisparities = DEFAULT_NDISP, int winSize = DEFAULT_WINSZ);
1603
1604             //! the stereo correspondence operator. Finds the disparity for the specified rectified stereo pair
1605             //! Output disparity has CV_8U type.
1606             void operator() ( const oclMat &left, const oclMat &right, oclMat &disparity);
1607
1608             //! Some heuristics that tries to estmate
1609             // if current GPU will be faster then CPU in this algorithm.
1610             // It queries current active device.
1611             static bool checkIfGpuCallReasonable();
1612
1613             int preset;
1614             int ndisp;
1615             int winSize;
1616
1617             // If avergeTexThreshold  == 0 => post procesing is disabled
1618             // If avergeTexThreshold != 0 then disparity is set 0 in each point (x,y) where for left image
1619             // SumOfHorizontalGradiensInWindow(x, y, winSize) < (winSize * winSize) * avergeTexThreshold
1620             // i.e. input left image is low textured.
1621             float avergeTexThreshold;
1622         private:
1623             oclMat minSSD, leBuf, riBuf;
1624         };
1625
1626         class CV_EXPORTS StereoBeliefPropagation
1627         {
1628         public:
1629             enum { DEFAULT_NDISP  = 64 };
1630             enum { DEFAULT_ITERS  = 5  };
1631             enum { DEFAULT_LEVELS = 5  };
1632             static void estimateRecommendedParams(int width, int height, int &ndisp, int &iters, int &levels);
1633             explicit StereoBeliefPropagation(int ndisp  = DEFAULT_NDISP,
1634                                              int iters  = DEFAULT_ITERS,
1635                                              int levels = DEFAULT_LEVELS,
1636                                              int msg_type = CV_16S);
1637             StereoBeliefPropagation(int ndisp, int iters, int levels,
1638                                     float max_data_term, float data_weight,
1639                                     float max_disc_term, float disc_single_jump,
1640                                     int msg_type = CV_32F);
1641             void operator()(const oclMat &left, const oclMat &right, oclMat &disparity);
1642             void operator()(const oclMat &data, oclMat &disparity);
1643             int ndisp;
1644             int iters;
1645             int levels;
1646             float max_data_term;
1647             float data_weight;
1648             float max_disc_term;
1649             float disc_single_jump;
1650             int msg_type;
1651         private:
1652             oclMat u, d, l, r, u2, d2, l2, r2;
1653             std::vector<oclMat> datas;
1654             oclMat out;
1655         };
1656
1657         class CV_EXPORTS StereoConstantSpaceBP
1658         {
1659         public:
1660             enum { DEFAULT_NDISP    = 128 };
1661             enum { DEFAULT_ITERS    = 8   };
1662             enum { DEFAULT_LEVELS   = 4   };
1663             enum { DEFAULT_NR_PLANE = 4   };
1664             static void estimateRecommendedParams(int width, int height, int &ndisp, int &iters, int &levels, int &nr_plane);
1665             explicit StereoConstantSpaceBP(
1666                 int ndisp    = DEFAULT_NDISP,
1667                 int iters    = DEFAULT_ITERS,
1668                 int levels   = DEFAULT_LEVELS,
1669                 int nr_plane = DEFAULT_NR_PLANE,
1670                 int msg_type = CV_32F);
1671             StereoConstantSpaceBP(int ndisp, int iters, int levels, int nr_plane,
1672                 float max_data_term, float data_weight, float max_disc_term, float disc_single_jump,
1673                 int min_disp_th = 0,
1674                 int msg_type = CV_32F);
1675             void operator()(const oclMat &left, const oclMat &right, oclMat &disparity);
1676             int ndisp;
1677             int iters;
1678             int levels;
1679             int nr_plane;
1680             float max_data_term;
1681             float data_weight;
1682             float max_disc_term;
1683             float disc_single_jump;
1684             int min_disp_th;
1685             int msg_type;
1686             bool use_local_init_data_cost;
1687         private:
1688             oclMat u[2], d[2], l[2], r[2];
1689             oclMat disp_selected_pyr[2];
1690             oclMat data_cost;
1691             oclMat data_cost_selected;
1692             oclMat temp;
1693             oclMat out;
1694         };
1695
1696         // Implementation of the Zach, Pock and Bischof Dual TV-L1 Optical Flow method
1697         //
1698         // see reference:
1699         //   [1] C. Zach, T. Pock and H. Bischof, "A Duality Based Approach for Realtime TV-L1 Optical Flow".
1700         //   [2] Javier Sanchez, Enric Meinhardt-Llopis and Gabriele Facciolo. "TV-L1 Optical Flow Estimation".
1701         class CV_EXPORTS OpticalFlowDual_TVL1_OCL
1702         {
1703         public:
1704             OpticalFlowDual_TVL1_OCL();
1705
1706             void operator ()(const oclMat& I0, const oclMat& I1, oclMat& flowx, oclMat& flowy);
1707
1708             void collectGarbage();
1709
1710             /**
1711             * Time step of the numerical scheme.
1712             */
1713             double tau;
1714
1715             /**
1716             * Weight parameter for the data term, attachment parameter.
1717             * This is the most relevant parameter, which determines the smoothness of the output.
1718             * The smaller this parameter is, the smoother the solutions we obtain.
1719             * It depends on the range of motions of the images, so its value should be adapted to each image sequence.
1720             */
1721             double lambda;
1722
1723             /**
1724             * Weight parameter for (u - v)^2, tightness parameter.
1725             * It serves as a link between the attachment and the regularization terms.
1726             * In theory, it should have a small value in order to maintain both parts in correspondence.
1727             * The method is stable for a large range of values of this parameter.
1728             */
1729             double theta;
1730
1731             /**
1732             * Number of scales used to create the pyramid of images.
1733             */
1734             int nscales;
1735
1736             /**
1737             * Number of warpings per scale.
1738             * Represents the number of times that I1(x+u0) and grad( I1(x+u0) ) are computed per scale.
1739             * This is a parameter that assures the stability of the method.
1740             * It also affects the running time, so it is a compromise between speed and accuracy.
1741             */
1742             int warps;
1743
1744             /**
1745             * Stopping criterion threshold used in the numerical scheme, which is a trade-off between precision and running time.
1746             * A small value will yield more accurate solutions at the expense of a slower convergence.
1747             */
1748             double epsilon;
1749
1750             /**
1751             * Stopping criterion iterations number used in the numerical scheme.
1752             */
1753             int iterations;
1754
1755             bool useInitialFlow;
1756
1757         private:
1758             void procOneScale(const oclMat& I0, const oclMat& I1, oclMat& u1, oclMat& u2);
1759
1760             std::vector<oclMat> I0s;
1761             std::vector<oclMat> I1s;
1762             std::vector<oclMat> u1s;
1763             std::vector<oclMat> u2s;
1764
1765             oclMat I1x_buf;
1766             oclMat I1y_buf;
1767
1768             oclMat I1w_buf;
1769             oclMat I1wx_buf;
1770             oclMat I1wy_buf;
1771
1772             oclMat grad_buf;
1773             oclMat rho_c_buf;
1774
1775             oclMat p11_buf;
1776             oclMat p12_buf;
1777             oclMat p21_buf;
1778             oclMat p22_buf;
1779
1780             oclMat diff_buf;
1781             oclMat norm_buf;
1782         };
1783         // current supported sorting methods
1784         enum
1785         {
1786             SORT_BITONIC,   // only support power-of-2 buffer size
1787             SORT_SELECTION, // cannot sort duplicate keys
1788             SORT_MERGE,
1789             SORT_RADIX      // only support signed int/float keys(CV_32S/CV_32F)
1790         };
1791         //! Returns the sorted result of all the elements in input based on equivalent keys.
1792         //
1793         //  The element unit in the values to be sorted is determined from the data type,
1794         //  i.e., a CV_32FC2 input {a1a2, b1b2} will be considered as two elements, regardless its
1795         //  matrix dimension.
1796         //  both keys and values will be sorted inplace
1797         //  Key needs to be single channel oclMat.
1798         //
1799         //  Example:
1800         //  input -
1801         //    keys   = {2,    3,   1}   (CV_8UC1)
1802         //    values = {10,5, 4,3, 6,2} (CV_8UC2)
1803         //  sortByKey(keys, values, SORT_SELECTION, false);
1804         //  output -
1805         //    keys   = {1,    2,   3}   (CV_8UC1)
1806         //    values = {6,2, 10,5, 4,3} (CV_8UC2)
1807         void CV_EXPORTS sortByKey(oclMat& keys, oclMat& values, int method, bool isGreaterThan = false);
1808         /*!Base class for MOG and MOG2!*/
1809         class CV_EXPORTS BackgroundSubtractor
1810         {
1811         public:
1812             //! the virtual destructor
1813             virtual ~BackgroundSubtractor();
1814             //! the update operator that takes the next video frame and returns the current foreground mask as 8-bit binary image.
1815             virtual void operator()(const oclMat& image, oclMat& fgmask, float learningRate);
1816
1817             //! computes a background image
1818             virtual void getBackgroundImage(oclMat& backgroundImage) const = 0;
1819         };
1820                 /*!
1821         Gaussian Mixture-based Backbround/Foreground Segmentation Algorithm
1822
1823         The class implements the following algorithm:
1824         "An improved adaptive background mixture model for real-time tracking with shadow detection"
1825         P. KadewTraKuPong and R. Bowden,
1826         Proc. 2nd European Workshp on Advanced Video-Based Surveillance Systems, 2001."
1827         http://personal.ee.surrey.ac.uk/Personal/R.Bowden/publications/avbs01/avbs01.pdf
1828         */
1829         class CV_EXPORTS MOG: public cv::ocl::BackgroundSubtractor
1830         {
1831         public:
1832             //! the default constructor
1833             MOG(int nmixtures = -1);
1834
1835             //! re-initiaization method
1836             void initialize(Size frameSize, int frameType);
1837
1838             //! the update operator
1839             void operator()(const oclMat& frame, oclMat& fgmask, float learningRate = 0.f);
1840
1841             //! computes a background image which are the mean of all background gaussians
1842             void getBackgroundImage(oclMat& backgroundImage) const;
1843
1844             //! releases all inner buffers
1845             void release();
1846
1847             int history;
1848             float varThreshold;
1849             float backgroundRatio;
1850             float noiseSigma;
1851
1852         private:
1853             int nmixtures_;
1854
1855             Size frameSize_;
1856             int frameType_;
1857             int nframes_;
1858
1859             oclMat weight_;
1860             oclMat sortKey_;
1861             oclMat mean_;
1862             oclMat var_;
1863         };
1864
1865         /*!
1866         The class implements the following algorithm:
1867         "Improved adaptive Gausian mixture model for background subtraction"
1868         Z.Zivkovic
1869         International Conference Pattern Recognition, UK, August, 2004.
1870         http://www.zoranz.net/Publications/zivkovic2004ICPR.pdf
1871         */
1872         class CV_EXPORTS MOG2: public cv::ocl::BackgroundSubtractor
1873         {
1874         public:
1875             //! the default constructor
1876             MOG2(int nmixtures = -1);
1877
1878             //! re-initiaization method
1879             void initialize(Size frameSize, int frameType);
1880
1881             //! the update operator
1882             void operator()(const oclMat& frame, oclMat& fgmask, float learningRate = -1.0f);
1883
1884             //! computes a background image which are the mean of all background gaussians
1885             void getBackgroundImage(oclMat& backgroundImage) const;
1886
1887             //! releases all inner buffers
1888             void release();
1889
1890             // parameters
1891             // you should call initialize after parameters changes
1892
1893             int history;
1894
1895             //! here it is the maximum allowed number of mixture components.
1896             //! Actual number is determined dynamically per pixel
1897             float varThreshold;
1898             // threshold on the squared Mahalanobis distance to decide if it is well described
1899             // by the background model or not. Related to Cthr from the paper.
1900             // This does not influence the update of the background. A typical value could be 4 sigma
1901             // and that is varThreshold=4*4=16; Corresponds to Tb in the paper.
1902
1903             /////////////////////////
1904             // less important parameters - things you might change but be carefull
1905             ////////////////////////
1906
1907             float backgroundRatio;
1908             // corresponds to fTB=1-cf from the paper
1909             // TB - threshold when the component becomes significant enough to be included into
1910             // the background model. It is the TB=1-cf from the paper. So I use cf=0.1 => TB=0.
1911             // For alpha=0.001 it means that the mode should exist for approximately 105 frames before
1912             // it is considered foreground
1913             // float noiseSigma;
1914             float varThresholdGen;
1915
1916             //correspondts to Tg - threshold on the squared Mahalan. dist. to decide
1917             //when a sample is close to the existing components. If it is not close
1918             //to any a new component will be generated. I use 3 sigma => Tg=3*3=9.
1919             //Smaller Tg leads to more generated components and higher Tg might make
1920             //lead to small number of components but they can grow too large
1921             float fVarInit;
1922             float fVarMin;
1923             float fVarMax;
1924
1925             //initial variance  for the newly generated components.
1926             //It will will influence the speed of adaptation. A good guess should be made.
1927             //A simple way is to estimate the typical standard deviation from the images.
1928             //I used here 10 as a reasonable value
1929             // min and max can be used to further control the variance
1930             float fCT; //CT - complexity reduction prior
1931             //this is related to the number of samples needed to accept that a component
1932             //actually exists. We use CT=0.05 of all the samples. By setting CT=0 you get
1933             //the standard Stauffer&Grimson algorithm (maybe not exact but very similar)
1934
1935             //shadow detection parameters
1936             bool bShadowDetection; //default 1 - do shadow detection
1937             unsigned char nShadowDetection; //do shadow detection - insert this value as the detection result - 127 default value
1938             float fTau;
1939             // Tau - shadow threshold. The shadow is detected if the pixel is darker
1940             //version of the background. Tau is a threshold on how much darker the shadow can be.
1941             //Tau= 0.5 means that if pixel is more than 2 times darker then it is not shadow
1942             //See: Prati,Mikic,Trivedi,Cucchiarra,"Detecting Moving Shadows...",IEEE PAMI,2003.
1943
1944         private:
1945             int nmixtures_;
1946
1947             Size frameSize_;
1948             int frameType_;
1949             int nframes_;
1950
1951             oclMat weight_;
1952             oclMat variance_;
1953             oclMat mean_;
1954
1955             oclMat bgmodelUsedModes_; //keep track of number of modes per pixel
1956         };
1957     }
1958 }
1959 #if defined _MSC_VER && _MSC_VER >= 1200
1960 #  pragma warning( push)
1961 #  pragma warning( disable: 4267)
1962 #endif
1963 #include "opencv2/ocl/matrix_operations.hpp"
1964 #if defined _MSC_VER && _MSC_VER >= 1200
1965 #  pragma warning( pop)
1966 #endif
1967
1968 #endif /* __OPENCV_OCL_HPP__ */