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