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