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