1 /*M///////////////////////////////////////////////////////////////////////////////////////
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
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.
11 // For Open Source Computer Vision Library
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.
18 // Redistribution and use in source and binary forms, with or without modification,
19 // are permitted provided that the following conditions are met:
21 // * Redistribution's of source code must retain the above copyright notice,
22 // this list of conditions and the following disclaimer.
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.
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.
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.
44 #ifndef __OPENCV_OCL_HPP__
45 #define __OPENCV_OCL_HPP__
50 #include "opencv2/core/core.hpp"
51 #include "opencv2/imgproc/imgproc.hpp"
52 #include "opencv2/objdetect/objdetect.hpp"
53 #include "opencv2/features2d/features2d.hpp"
54 #include "opencv2/ml/ml.hpp"
63 CVCL_DEVICE_TYPE_DEFAULT = (1 << 0),
64 CVCL_DEVICE_TYPE_CPU = (1 << 1),
65 CVCL_DEVICE_TYPE_GPU = (1 << 2),
66 CVCL_DEVICE_TYPE_ACCELERATOR = (1 << 3),
67 //CVCL_DEVICE_TYPE_CUSTOM = (1 << 4)
68 CVCL_DEVICE_TYPE_ALL = 0xFFFFFFFF
80 DEVICE_MEM_DEFAULT = 0,
81 DEVICE_MEM_AHP, //alloc host pointer
82 DEVICE_MEM_UHP, //use host pointer
83 DEVICE_MEM_CHP, //copy host pointer
84 DEVICE_MEM_PM //persistent memory
87 //Get the global device memory and read/write type
88 //return 1 if unified memory system supported, otherwise return 0
89 CV_EXPORTS int getDevMemType(DevMemRW& rw_type, DevMemType& mem_type);
91 //Set the global device memory and read/write type,
92 //the newly generated oclMat will all use this type
93 //return -1 if the target type is unsupported, otherwise return 0
94 CV_EXPORTS int setDevMemType(DevMemRW rw_type = DEVICE_MEM_R_W, DevMemType mem_type = DEVICE_MEM_DEFAULT);
96 //this class contains ocl runtime information
107 Info &operator = (const Info &m);
108 std::vector<string> DeviceName;
110 //////////////////////////////// Initialization & Info ////////////////////////
111 //this function may be obsoleted
112 //CV_EXPORTS cl_device_id getDevice();
113 //the function must be called before any other cv::ocl::functions, it initialize ocl runtime
114 //each Info relates to an OpenCL platform
115 //there is one or more devices in each platform, each one has a separate name
116 CV_EXPORTS int getDevice(std::vector<Info> &oclinfo, int devicetype = CVCL_DEVICE_TYPE_GPU);
118 //set device you want to use, optional function after getDevice be called
119 //the devnum is the index of the selected device in DeviceName vector of INfo
120 CV_EXPORTS void setDevice(Info &oclinfo, int devnum = 0);
122 //The two functions below enable other opencl program to use ocl module's cl_context and cl_command_queue
123 //returns cl_context *
124 CV_EXPORTS void* getoclContext();
125 //returns cl_command_queue *
126 CV_EXPORTS void* getoclCommandQueue();
128 //explicit call clFinish. The global command queue will be used.
129 CV_EXPORTS void finish();
131 //this function enable ocl module to use customized cl_context and cl_command_queue
132 //getDevice also need to be called before this function
133 CV_EXPORTS void setDeviceEx(Info &oclinfo, void *ctx, void *qu, int devnum = 0);
135 //returns true when global OpenCL context is initialized
136 CV_EXPORTS bool initialized();
138 //////////////////////////////// Error handling ////////////////////////
139 CV_EXPORTS void error(const char *error_string, const char *file, const int line, const char *func);
141 //////////////////////////////// OpenCL context ////////////////////////
142 //This is a global singleton class used to represent a OpenCL context.
143 class CV_EXPORTS Context
147 friend class auto_ptr<Context>;
148 friend bool initialized();
150 static auto_ptr<Context> clCxt;
157 static Context* getContext();
158 static void setContext(Info &oclinfo);
160 enum {CL_DOUBLE, CL_UNIFIED_MEM, CL_VER_1_2};
161 bool supportsFeature(int ftype);
162 size_t computeUnits();
164 void* oclCommandQueue();
167 //! Calls a kernel, by string. Pass globalThreads = NULL, and cleanUp = true, to finally clean-up without executing.
168 CV_EXPORTS double openCLExecuteKernelInterop(Context *clCxt ,
169 const char **source, string kernelName,
170 size_t globalThreads[3], size_t localThreads[3],
171 std::vector< std::pair<size_t, const void *> > &args,
172 int channels, int depth, const char *build_options,
173 bool finish = true, bool measureKernelTime = false,
174 bool cleanUp = true);
176 //! Calls a kernel, by file. Pass globalThreads = NULL, and cleanUp = true, to finally clean-up without executing.
177 CV_EXPORTS double openCLExecuteKernelInterop(Context *clCxt ,
178 const char **fileName, const int numFiles, string kernelName,
179 size_t globalThreads[3], size_t localThreads[3],
180 std::vector< std::pair<size_t, const void *> > &args,
181 int channels, int depth, const char *build_options,
182 bool finish = true, bool measureKernelTime = false,
183 bool cleanUp = true);
185 //! Enable or disable OpenCL program binary caching onto local disk
186 // After a program (*.cl files in opencl/ folder) is built at runtime, we allow the
187 // compiled OpenCL program to be cached to the path automatically as "path/*.clb"
188 // binary file, which will be reused when the OpenCV executable is started again.
190 // Caching mode is controlled by the following enums
192 // 1. the feature is by default enabled when OpenCV is built in release mode.
193 // 2. the CACHE_DEBUG / CACHE_RELEASE flags only effectively work with MSVC compiler;
194 // for GNU compilers, the function always treats the build as release mode (enabled by default).
197 CACHE_NONE = 0, // do not cache OpenCL binary
198 CACHE_DEBUG = 0x1 << 0, // cache OpenCL binary when built in debug mode (only work with MSVC)
199 CACHE_RELEASE = 0x1 << 1, // default behavior, only cache when built in release mode (only work with MSVC)
200 CACHE_ALL = CACHE_DEBUG | CACHE_RELEASE, // always cache opencl binary
201 CACHE_UPDATE = 0x1 << 2 // if the binary cache file with the same name is already on the disk, it will be updated.
203 CV_EXPORTS void setBinaryDiskCache(int mode = CACHE_RELEASE, cv::String path = "./");
205 //! set where binary cache to be saved to
206 CV_EXPORTS void setBinpath(const char *path);
208 class CV_EXPORTS oclMatExpr;
209 //////////////////////////////// oclMat ////////////////////////////////
210 class CV_EXPORTS oclMat
213 //! default constructor
215 //! constructs oclMatrix of the specified size and type (_type is CV_8UC1, CV_64FC3, CV_32SC(12) etc.)
216 oclMat(int rows, int cols, int type);
217 oclMat(Size size, int type);
218 //! constucts oclMatrix and fills it with the specified value _s.
219 oclMat(int rows, int cols, int type, const Scalar &s);
220 oclMat(Size size, int type, const Scalar &s);
222 oclMat(const oclMat &m);
224 //! constructor for oclMatrix headers pointing to user-allocated data
225 oclMat(int rows, int cols, int type, void *data, size_t step = Mat::AUTO_STEP);
226 oclMat(Size size, int type, void *data, size_t step = Mat::AUTO_STEP);
228 //! creates a matrix header for a part of the bigger matrix
229 oclMat(const oclMat &m, const Range &rowRange, const Range &colRange);
230 oclMat(const oclMat &m, const Rect &roi);
232 //! builds oclMat from Mat. Perfom blocking upload to device.
233 explicit oclMat (const Mat &m);
235 //! destructor - calls release()
238 //! assignment operators
239 oclMat &operator = (const oclMat &m);
240 //! assignment operator. Perfom blocking upload to device.
241 oclMat &operator = (const Mat &m);
242 oclMat &operator = (const oclMatExpr& expr);
244 //! pefroms blocking upload data to oclMat.
245 void upload(const cv::Mat &m);
248 //! downloads data from device to host memory. Blocking calls.
249 operator Mat() const;
250 void download(cv::Mat &m) const;
252 //! convert to _InputArray
253 operator _InputArray();
255 //! convert to _OutputArray
256 operator _OutputArray();
258 //! returns a new oclMatrix header for the specified row
259 oclMat row(int y) const;
260 //! returns a new oclMatrix header for the specified column
261 oclMat col(int x) const;
262 //! ... for the specified row span
263 oclMat rowRange(int startrow, int endrow) const;
264 oclMat rowRange(const Range &r) const;
265 //! ... for the specified column span
266 oclMat colRange(int startcol, int endcol) const;
267 oclMat colRange(const Range &r) const;
269 //! returns deep copy of the oclMatrix, i.e. the data is copied
270 oclMat clone() const;
271 //! copies the oclMatrix content to "m".
272 // It calls m.create(this->size(), this->type()).
273 // It supports any data type
274 void copyTo( oclMat &m ) const;
275 //! copies those oclMatrix elements to "m" that are marked with non-zero mask elements.
276 //It supports 8UC1 8UC4 32SC1 32SC4 32FC1 32FC4
277 void copyTo( oclMat &m, const oclMat &mask ) const;
278 //! converts oclMatrix to another datatype with optional scalng. See cvConvertScale.
279 //It supports 8UC1 8UC4 32SC1 32SC4 32FC1 32FC4
280 void convertTo( oclMat &m, int rtype, double alpha = 1, double beta = 0 ) const;
282 void assignTo( oclMat &m, int type = -1 ) const;
284 //! sets every oclMatrix element to s
285 //It supports 8UC1 8UC4 32SC1 32SC4 32FC1 32FC4
286 oclMat& operator = (const Scalar &s);
287 //! sets some of the oclMatrix elements to s, according to the mask
288 //It supports 8UC1 8UC4 32SC1 32SC4 32FC1 32FC4
289 oclMat& setTo(const Scalar &s, const oclMat &mask = oclMat());
290 //! creates alternative oclMatrix header for the same data, with different
291 // number of channels and/or different number of rows. see cvReshape.
292 oclMat reshape(int cn, int rows = 0) const;
294 //! allocates new oclMatrix data unless the oclMatrix already has specified size and type.
295 // previous data is unreferenced if needed.
296 void create(int rows, int cols, int type);
297 void create(Size size, int type);
299 //! allocates new oclMatrix with specified device memory type.
300 void createEx(int rows, int cols, int type, DevMemRW rw_type, DevMemType mem_type);
301 void createEx(Size size, int type, DevMemRW rw_type, DevMemType mem_type);
303 //! decreases reference counter;
304 // deallocate the data when reference counter reaches 0.
307 //! swaps with other smart pointer
308 void swap(oclMat &mat);
310 //! locates oclMatrix header within a parent oclMatrix. See below
311 void locateROI( Size &wholeSize, Point &ofs ) const;
312 //! moves/resizes the current oclMatrix ROI inside the parent oclMatrix.
313 oclMat& adjustROI( int dtop, int dbottom, int dleft, int dright );
314 //! extracts a rectangular sub-oclMatrix
315 // (this is a generalized form of row, rowRange etc.)
316 oclMat operator()( Range rowRange, Range colRange ) const;
317 oclMat operator()( const Rect &roi ) const;
319 oclMat& operator+=( const oclMat& m );
320 oclMat& operator-=( const oclMat& m );
321 oclMat& operator*=( const oclMat& m );
322 oclMat& operator/=( const oclMat& m );
324 //! returns true if the oclMatrix data is continuous
325 // (i.e. when there are no gaps between successive rows).
326 // similar to CV_IS_oclMat_CONT(cvoclMat->type)
327 bool isContinuous() const;
328 //! returns element size in bytes,
329 // similar to CV_ELEM_SIZE(cvMat->type)
330 size_t elemSize() const;
331 //! returns the size of element channel in bytes.
332 size_t elemSize1() const;
333 //! returns element type, similar to CV_MAT_TYPE(cvMat->type)
335 //! returns element type, i.e. 8UC3 returns 8UC4 because in ocl
336 //! 3 channels element actually use 4 channel space
338 //! returns element type, similar to CV_MAT_DEPTH(cvMat->type)
340 //! returns element type, similar to CV_MAT_CN(cvMat->type)
341 int channels() const;
342 //! returns element type, return 4 for 3 channels element,
343 //!becuase 3 channels element actually use 4 channel space
344 int oclchannels() const;
345 //! returns step/elemSize1()
346 size_t step1() const;
347 //! returns oclMatrix size:
348 // width == number of columns, height == number of rows
350 //! returns true if oclMatrix data is NULL
353 //! returns pointer to y-th row
354 uchar* ptr(int y = 0);
355 const uchar *ptr(int y = 0) const;
357 //! template version of the above method
358 template<typename _Tp> _Tp *ptr(int y = 0);
359 template<typename _Tp> const _Tp *ptr(int y = 0) const;
361 //! matrix transposition
364 /*! includes several bit-fields:
365 - the magic signature
371 //! the number of rows and columns
373 //! a distance between successive rows in bytes; includes the gap if any
375 //! pointer to the data(OCL memory object)
378 //! pointer to the reference counter;
379 // when oclMatrix points to user-allocated data, the pointer is NULL
382 //! helper fields used in locateROI and adjustROI
383 //datastart and dataend are not used in current version
387 //! OpenCL context associated with the oclMat object.
389 //add offset for handle ROI, calculated in byte
391 //add wholerows and wholecols for the whole matrix, datastart and dataend are no longer used
396 // convert InputArray/OutputArray to oclMat references
397 CV_EXPORTS oclMat& getOclMatRef(InputArray src);
398 CV_EXPORTS oclMat& getOclMatRef(OutputArray src);
400 ///////////////////// mat split and merge /////////////////////////////////
401 //! Compose a multi-channel array from several single-channel arrays
403 CV_EXPORTS void merge(const oclMat *src, size_t n, oclMat &dst);
404 CV_EXPORTS void merge(const vector<oclMat> &src, oclMat &dst);
406 //! Divides multi-channel array into several single-channel arrays
408 CV_EXPORTS void split(const oclMat &src, oclMat *dst);
409 CV_EXPORTS void split(const oclMat &src, vector<oclMat> &dst);
411 ////////////////////////////// Arithmetics ///////////////////////////////////
412 //#if defined DOUBLE_SUPPORT
417 // CV_EXPORTS void addWeighted(const oclMat& a,F alpha, const oclMat& b,F beta,F gama, oclMat& c);
418 CV_EXPORTS void addWeighted(const oclMat &a, double alpha, const oclMat &b, double beta, double gama, oclMat &c);
419 //! adds one matrix to another (c = a + b)
420 // supports all types except CV_8SC1,CV_8SC2,CV8SC3 and CV_8SC4
421 CV_EXPORTS void add(const oclMat &a, const oclMat &b, oclMat &c);
422 //! adds one matrix to another (c = a + b)
423 // supports all types except CV_8SC1,CV_8SC2,CV8SC3 and CV_8SC4
424 CV_EXPORTS void add(const oclMat &a, const oclMat &b, oclMat &c, const oclMat &mask);
425 //! adds scalar to a matrix (c = a + s)
426 // supports all types except CV_8SC1,CV_8SC2,CV8SC3 and CV_8SC4
427 CV_EXPORTS void add(const oclMat &a, const Scalar &sc, oclMat &c, const oclMat &mask = oclMat());
428 //! subtracts one matrix from another (c = a - b)
429 // supports all types except CV_8SC1,CV_8SC2,CV8SC3 and CV_8SC4
430 CV_EXPORTS void subtract(const oclMat &a, const oclMat &b, oclMat &c);
431 //! subtracts one matrix from another (c = a - b)
432 // supports all types except CV_8SC1,CV_8SC2,CV8SC3 and CV_8SC4
433 CV_EXPORTS void subtract(const oclMat &a, const oclMat &b, oclMat &c, const oclMat &mask);
434 //! subtracts scalar from a matrix (c = a - s)
435 // supports all types except CV_8SC1,CV_8SC2,CV8SC3 and CV_8SC4
436 CV_EXPORTS void subtract(const oclMat &a, const Scalar &sc, oclMat &c, const oclMat &mask = oclMat());
437 //! subtracts scalar from a matrix (c = a - s)
438 // supports all types except CV_8SC1,CV_8SC2,CV8SC3 and CV_8SC4
439 CV_EXPORTS void subtract(const Scalar &sc, const oclMat &a, oclMat &c, const oclMat &mask = oclMat());
440 //! computes element-wise product of the two arrays (c = a * b)
441 // supports all types except CV_8SC1,CV_8SC2,CV8SC3 and CV_8SC4
442 CV_EXPORTS void multiply(const oclMat &a, const oclMat &b, oclMat &c, double scale = 1);
443 //! multiplies matrix to a number (dst = scalar * src)
444 // supports CV_32FC1 only
445 CV_EXPORTS void multiply(double scalar, const oclMat &src, oclMat &dst);
446 //! computes element-wise quotient of the two arrays (c = a / b)
447 // supports all types except CV_8SC1,CV_8SC2,CV8SC3 and CV_8SC4
448 CV_EXPORTS void divide(const oclMat &a, const oclMat &b, oclMat &c, double scale = 1);
449 //! computes element-wise quotient of the two arrays (c = a / b)
450 // supports all types except CV_8SC1,CV_8SC2,CV8SC3 and CV_8SC4
451 CV_EXPORTS void divide(double scale, const oclMat &b, oclMat &c);
453 //! compares elements of two arrays (c = a <cmpop> b)
454 // supports except CV_8SC1,CV_8SC2,CV8SC3,CV_8SC4 types
455 CV_EXPORTS void compare(const oclMat &a, const oclMat &b, oclMat &c, int cmpop);
457 //! transposes the matrix
458 // supports CV_8UC1, 8UC4, 8SC4, 16UC2, 16SC2, 32SC1 and 32FC1.(the same as cuda)
459 CV_EXPORTS void transpose(const oclMat &src, oclMat &dst);
461 //! computes element-wise absolute difference of two arrays (c = abs(a - b))
462 // supports all types except CV_8SC1,CV_8SC2,CV8SC3 and CV_8SC4
463 CV_EXPORTS void absdiff(const oclMat &a, const oclMat &b, oclMat &c);
464 //! computes element-wise absolute difference of array and scalar (c = abs(a - s))
465 // supports all types except CV_8SC1,CV_8SC2,CV8SC3 and CV_8SC4
466 CV_EXPORTS void absdiff(const oclMat &a, const Scalar &s, oclMat &c);
468 //! computes mean value and standard deviation of all or selected array elements
469 // supports except CV_32F,CV_64F
470 CV_EXPORTS void meanStdDev(const oclMat &mtx, Scalar &mean, Scalar &stddev);
472 //! computes norm of array
473 // supports NORM_INF, NORM_L1, NORM_L2
474 // supports only CV_8UC1 type
475 CV_EXPORTS double norm(const oclMat &src1, int normType = NORM_L2);
477 //! computes norm of the difference between two arrays
478 // supports NORM_INF, NORM_L1, NORM_L2
479 // supports only CV_8UC1 type
480 CV_EXPORTS double norm(const oclMat &src1, const oclMat &src2, int normType = NORM_L2);
482 //! reverses the order of the rows, columns or both in a matrix
483 // supports all types
484 CV_EXPORTS void flip(const oclMat &a, oclMat &b, int flipCode);
486 //! computes sum of array elements
487 // disabled until fix crash
489 CV_EXPORTS Scalar sum(const oclMat &m);
490 CV_EXPORTS Scalar absSum(const oclMat &m);
491 CV_EXPORTS Scalar sqrSum(const oclMat &m);
493 //! finds global minimum and maximum array elements and returns their values
494 // support all C1 types
496 CV_EXPORTS void minMax(const oclMat &src, double *minVal, double *maxVal = 0, const oclMat &mask = oclMat());
497 CV_EXPORTS void minMax_buf(const oclMat &src, double *minVal, double *maxVal, const oclMat &mask, oclMat& buf);
499 //! finds global minimum and maximum array elements and returns their values with locations
500 // support all C1 types
502 CV_EXPORTS void minMaxLoc(const oclMat &src, double *minVal, double *maxVal = 0, Point *minLoc = 0, Point *maxLoc = 0,
503 const oclMat &mask = oclMat());
505 //! counts non-zero array elements
507 CV_EXPORTS int countNonZero(const oclMat &src);
509 //! transforms 8-bit unsigned integers using lookup table: dst(i)=lut(src(i))
510 // destination array will have the depth type as lut and the same channels number as source
511 //It supports 8UC1 8UC4 only
512 CV_EXPORTS void LUT(const oclMat &src, const oclMat &lut, oclMat &dst);
514 //! only 8UC1 and 256 bins is supported now
515 CV_EXPORTS void calcHist(const oclMat &mat_src, oclMat &mat_hist);
516 //! only 8UC1 and 256 bins is supported now
517 CV_EXPORTS void equalizeHist(const oclMat &mat_src, oclMat &mat_dst);
519 //! only 8UC1 is supported now
520 CV_EXPORTS Ptr<cv::CLAHE> createCLAHE(double clipLimit = 40.0, Size tileGridSize = Size(8, 8));
523 // supports 8UC1 8UC4
524 CV_EXPORTS void bilateralFilter(const oclMat& src, oclMat& dst, int d, double sigmaColor, double sigmaSpace, int borderType=BORDER_DEFAULT);
526 //! Applies an adaptive bilateral filter to the input image
527 // This is not truly a bilateral filter. Instead of using user provided fixed parameters,
528 // the function calculates a constant at each window based on local standard deviation,
529 // and use this constant to do filtering.
530 // supports 8UC1 8UC3
531 CV_EXPORTS void adaptiveBilateralFilter(const oclMat& src, oclMat& dst, Size ksize, double sigmaSpace, Point anchor = Point(-1, -1), int borderType=BORDER_DEFAULT);
533 //! computes exponent of each matrix element (b = e**a)
534 // supports only CV_32FC1 type
535 CV_EXPORTS void exp(const oclMat &a, oclMat &b);
537 //! computes natural logarithm of absolute value of each matrix element: b = log(abs(a))
538 // supports only CV_32FC1 type
539 CV_EXPORTS void log(const oclMat &a, oclMat &b);
541 //! computes magnitude of each (x(i), y(i)) vector
542 // supports only CV_32F CV_64F type
543 CV_EXPORTS void magnitude(const oclMat &x, const oclMat &y, oclMat &magnitude);
544 CV_EXPORTS void magnitudeSqr(const oclMat &x, const oclMat &y, oclMat &magnitude);
546 CV_EXPORTS void magnitudeSqr(const oclMat &x, oclMat &magnitude);
548 //! computes angle (angle(i)) of each (x(i), y(i)) vector
549 // supports only CV_32F CV_64F type
550 CV_EXPORTS void phase(const oclMat &x, const oclMat &y, oclMat &angle, bool angleInDegrees = false);
552 //! the function raises every element of tne input array to p
553 //! support only CV_32F CV_64F type
554 CV_EXPORTS void pow(const oclMat &x, double p, oclMat &y);
556 //! converts Cartesian coordinates to polar
557 // supports only CV_32F CV_64F type
558 CV_EXPORTS void cartToPolar(const oclMat &x, const oclMat &y, oclMat &magnitude, oclMat &angle, bool angleInDegrees = false);
560 //! converts polar coordinates to Cartesian
561 // supports only CV_32F CV_64F type
562 CV_EXPORTS void polarToCart(const oclMat &magnitude, const oclMat &angle, oclMat &x, oclMat &y, bool angleInDegrees = false);
564 //! perfroms per-elements bit-wise inversion
565 // supports all types
566 CV_EXPORTS void bitwise_not(const oclMat &src, oclMat &dst);
567 //! calculates per-element bit-wise disjunction of two arrays
568 // supports all types
569 CV_EXPORTS void bitwise_or(const oclMat &src1, const oclMat &src2, oclMat &dst, const oclMat &mask = oclMat());
570 CV_EXPORTS void bitwise_or(const oclMat &src1, const Scalar &s, oclMat &dst, const oclMat &mask = oclMat());
571 //! calculates per-element bit-wise conjunction of two arrays
572 // supports all types
573 CV_EXPORTS void bitwise_and(const oclMat &src1, const oclMat &src2, oclMat &dst, const oclMat &mask = oclMat());
574 CV_EXPORTS void bitwise_and(const oclMat &src1, const Scalar &s, oclMat &dst, const oclMat &mask = oclMat());
575 //! calculates per-element bit-wise "exclusive or" operation
576 // supports all types
577 CV_EXPORTS void bitwise_xor(const oclMat &src1, const oclMat &src2, oclMat &dst, const oclMat &mask = oclMat());
578 CV_EXPORTS void bitwise_xor(const oclMat &src1, const Scalar &s, oclMat &dst, const oclMat &mask = oclMat());
580 //! Logical operators
581 CV_EXPORTS oclMat operator ~ (const oclMat &);
582 CV_EXPORTS oclMat operator | (const oclMat &, const oclMat &);
583 CV_EXPORTS oclMat operator & (const oclMat &, const oclMat &);
584 CV_EXPORTS oclMat operator ^ (const oclMat &, const oclMat &);
587 //! Mathematics operators
588 CV_EXPORTS oclMatExpr operator + (const oclMat &src1, const oclMat &src2);
589 CV_EXPORTS oclMatExpr operator - (const oclMat &src1, const oclMat &src2);
590 CV_EXPORTS oclMatExpr operator * (const oclMat &src1, const oclMat &src2);
591 CV_EXPORTS oclMatExpr operator / (const oclMat &src1, const oclMat &src2);
593 //! computes convolution of two images
594 //! support only CV_32FC1 type
595 CV_EXPORTS void convolve(const oclMat &image, const oclMat &temp1, oclMat &result);
597 CV_EXPORTS void cvtColor(const oclMat &src, oclMat &dst, int code , int dcn = 0);
599 CV_EXPORTS void setIdentity(oclMat& src, double val);
600 //////////////////////////////// Filter Engine ////////////////////////////////
603 The Base Class for 1D or Row-wise Filters
605 This is the base class for linear or non-linear filters that process 1D data.
606 In particular, such filters are used for the "horizontal" filtering parts in separable filters.
608 class CV_EXPORTS BaseRowFilter_GPU
611 BaseRowFilter_GPU(int ksize_, int anchor_, int bordertype_) : ksize(ksize_), anchor(anchor_), bordertype(bordertype_) {}
612 virtual ~BaseRowFilter_GPU() {}
613 virtual void operator()(const oclMat &src, oclMat &dst) = 0;
614 int ksize, anchor, bordertype;
618 The Base Class for Column-wise Filters
620 This is the base class for linear or non-linear filters that process columns of 2D arrays.
621 Such filters are used for the "vertical" filtering parts in separable filters.
623 class CV_EXPORTS BaseColumnFilter_GPU
626 BaseColumnFilter_GPU(int ksize_, int anchor_, int bordertype_) : ksize(ksize_), anchor(anchor_), bordertype(bordertype_) {}
627 virtual ~BaseColumnFilter_GPU() {}
628 virtual void operator()(const oclMat &src, oclMat &dst) = 0;
629 int ksize, anchor, bordertype;
633 The Base Class for Non-Separable 2D Filters.
635 This is the base class for linear or non-linear 2D filters.
637 class CV_EXPORTS BaseFilter_GPU
640 BaseFilter_GPU(const Size &ksize_, const Point &anchor_, const int &borderType_)
641 : ksize(ksize_), anchor(anchor_), borderType(borderType_) {}
642 virtual ~BaseFilter_GPU() {}
643 virtual void operator()(const oclMat &src, oclMat &dst) = 0;
650 The Base Class for Filter Engine.
652 The class can be used to apply an arbitrary filtering operation to an image.
653 It contains all the necessary intermediate buffers.
655 class CV_EXPORTS FilterEngine_GPU
658 virtual ~FilterEngine_GPU() {}
660 virtual void apply(const oclMat &src, oclMat &dst, Rect roi = Rect(0, 0, -1, -1)) = 0;
663 //! returns the non-separable filter engine with the specified filter
664 CV_EXPORTS Ptr<FilterEngine_GPU> createFilter2D_GPU(const Ptr<BaseFilter_GPU> filter2D);
666 //! returns the primitive row filter with the specified kernel
667 CV_EXPORTS Ptr<BaseRowFilter_GPU> getLinearRowFilter_GPU(int srcType, int bufType, const Mat &rowKernel,
668 int anchor = -1, int bordertype = BORDER_DEFAULT);
670 //! returns the primitive column filter with the specified kernel
671 CV_EXPORTS Ptr<BaseColumnFilter_GPU> getLinearColumnFilter_GPU(int bufType, int dstType, const Mat &columnKernel,
672 int anchor = -1, int bordertype = BORDER_DEFAULT, double delta = 0.0);
674 //! returns the separable linear filter engine
675 CV_EXPORTS Ptr<FilterEngine_GPU> createSeparableLinearFilter_GPU(int srcType, int dstType, const Mat &rowKernel,
676 const Mat &columnKernel, const Point &anchor = Point(-1, -1), double delta = 0.0, int bordertype = BORDER_DEFAULT);
678 //! returns the separable filter engine with the specified filters
679 CV_EXPORTS Ptr<FilterEngine_GPU> createSeparableFilter_GPU(const Ptr<BaseRowFilter_GPU> &rowFilter,
680 const Ptr<BaseColumnFilter_GPU> &columnFilter);
682 //! returns the Gaussian filter engine
683 CV_EXPORTS Ptr<FilterEngine_GPU> createGaussianFilter_GPU(int type, Size ksize, double sigma1, double sigma2 = 0, int bordertype = BORDER_DEFAULT);
685 //! returns filter engine for the generalized Sobel operator
686 CV_EXPORTS Ptr<FilterEngine_GPU> createDerivFilter_GPU( int srcType, int dstType, int dx, int dy, int ksize, int borderType = BORDER_DEFAULT );
688 //! applies Laplacian operator to the image
689 // supports only ksize = 1 and ksize = 3 8UC1 8UC4 32FC1 32FC4 data type
690 CV_EXPORTS void Laplacian(const oclMat &src, oclMat &dst, int ddepth, int ksize = 1, double scale = 1);
692 //! returns 2D box filter
693 // supports CV_8UC1 and CV_8UC4 source type, dst type must be the same as source type
694 CV_EXPORTS Ptr<BaseFilter_GPU> getBoxFilter_GPU(int srcType, int dstType,
695 const Size &ksize, Point anchor = Point(-1, -1), int borderType = BORDER_DEFAULT);
697 //! returns box filter engine
698 CV_EXPORTS Ptr<FilterEngine_GPU> createBoxFilter_GPU(int srcType, int dstType, const Size &ksize,
699 const Point &anchor = Point(-1, -1), int borderType = BORDER_DEFAULT);
701 //! returns 2D filter with the specified kernel
702 // supports CV_8UC1 and CV_8UC4 types
703 CV_EXPORTS Ptr<BaseFilter_GPU> getLinearFilter_GPU(int srcType, int dstType, const Mat &kernel, const Size &ksize,
704 const Point &anchor = Point(-1, -1), int borderType = BORDER_DEFAULT);
706 //! returns the non-separable linear filter engine
707 CV_EXPORTS Ptr<FilterEngine_GPU> createLinearFilter_GPU(int srcType, int dstType, const Mat &kernel,
708 const Point &anchor = Point(-1, -1), int borderType = BORDER_DEFAULT);
710 //! smooths the image using the normalized box filter
711 // supports data type: CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4
712 // supports border type: BORDER_CONSTANT, BORDER_REPLICATE, BORDER_REFLECT,BORDER_REFLECT_101,BORDER_WRAP
713 CV_EXPORTS void boxFilter(const oclMat &src, oclMat &dst, int ddepth, Size ksize,
714 Point anchor = Point(-1, -1), int borderType = BORDER_DEFAULT);
716 //! returns 2D morphological filter
717 //! only MORPH_ERODE and MORPH_DILATE are supported
718 // supports CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4 types
719 // kernel must have CV_8UC1 type, one rows and cols == ksize.width * ksize.height
720 CV_EXPORTS Ptr<BaseFilter_GPU> getMorphologyFilter_GPU(int op, int type, const Mat &kernel, const Size &ksize,
721 Point anchor = Point(-1, -1));
723 //! returns morphological filter engine. Only MORPH_ERODE and MORPH_DILATE are supported.
724 CV_EXPORTS Ptr<FilterEngine_GPU> createMorphologyFilter_GPU(int op, int type, const Mat &kernel,
725 const Point &anchor = Point(-1, -1), int iterations = 1);
727 //! a synonym for normalized box filter
728 // supports data type: CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4
729 // supports border type: BORDER_CONSTANT, BORDER_REPLICATE, BORDER_REFLECT,BORDER_REFLECT_101
730 static inline void blur(const oclMat &src, oclMat &dst, Size ksize, Point anchor = Point(-1, -1),
731 int borderType = BORDER_CONSTANT)
733 boxFilter(src, dst, -1, ksize, anchor, borderType);
736 //! applies non-separable 2D linear filter to the image
737 // Note, at the moment this function only works when anchor point is in the kernel center
738 // and kernel size supported is either 3x3 or 5x5; otherwise the function will fail to output valid result
739 CV_EXPORTS void filter2D(const oclMat &src, oclMat &dst, int ddepth, const Mat &kernel,
740 Point anchor = Point(-1, -1), int borderType = BORDER_DEFAULT);
742 //! applies separable 2D linear filter to the image
743 CV_EXPORTS void sepFilter2D(const oclMat &src, oclMat &dst, int ddepth, const Mat &kernelX, const Mat &kernelY,
744 Point anchor = Point(-1, -1), double delta = 0.0, int bordertype = BORDER_DEFAULT);
746 //! applies generalized Sobel operator to the image
747 // dst.type must equalize src.type
748 // supports data type: CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4
749 // supports border type: BORDER_CONSTANT, BORDER_REPLICATE, BORDER_REFLECT,BORDER_REFLECT_101
750 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);
752 //! applies the vertical or horizontal Scharr operator to the image
753 // dst.type must equalize src.type
754 // supports data type: CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4
755 // supports border type: BORDER_CONSTANT, BORDER_REPLICATE, BORDER_REFLECT,BORDER_REFLECT_101
756 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);
758 //! smooths the image using Gaussian filter.
759 // dst.type must equalize src.type
760 // supports data type: CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4
761 // supports border type: BORDER_CONSTANT, BORDER_REPLICATE, BORDER_REFLECT,BORDER_REFLECT_101
762 CV_EXPORTS void GaussianBlur(const oclMat &src, oclMat &dst, Size ksize, double sigma1, double sigma2 = 0, int bordertype = BORDER_DEFAULT);
764 //! erodes the image (applies the local minimum operator)
765 // supports data type: CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4
766 CV_EXPORTS void erode( const oclMat &src, oclMat &dst, const Mat &kernel, Point anchor = Point(-1, -1), int iterations = 1,
768 int borderType = BORDER_CONSTANT, const Scalar &borderValue = morphologyDefaultBorderValue());
771 //! dilates the image (applies the local maximum operator)
772 // supports data type: CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4
773 CV_EXPORTS void dilate( const oclMat &src, oclMat &dst, const Mat &kernel, Point anchor = Point(-1, -1), int iterations = 1,
775 int borderType = BORDER_CONSTANT, const Scalar &borderValue = morphologyDefaultBorderValue());
778 //! applies an advanced morphological operation to the image
779 CV_EXPORTS void morphologyEx( const oclMat &src, oclMat &dst, int op, const Mat &kernel, Point anchor = Point(-1, -1), int iterations = 1,
781 int borderType = BORDER_CONSTANT, const Scalar &borderValue = morphologyDefaultBorderValue());
784 ////////////////////////////// Image processing //////////////////////////////
785 //! Does mean shift filtering on GPU.
786 CV_EXPORTS void meanShiftFiltering(const oclMat &src, oclMat &dst, int sp, int sr,
787 TermCriteria criteria = TermCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 5, 1));
789 //! Does mean shift procedure on GPU.
790 CV_EXPORTS void meanShiftProc(const oclMat &src, oclMat &dstr, oclMat &dstsp, int sp, int sr,
791 TermCriteria criteria = TermCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 5, 1));
793 //! Does mean shift segmentation with elimiation of small regions.
794 CV_EXPORTS void meanShiftSegmentation(const oclMat &src, Mat &dst, int sp, int sr, int minsize,
795 TermCriteria criteria = TermCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 5, 1));
797 //! applies fixed threshold to the image.
798 // supports CV_8UC1 and CV_32FC1 data type
799 // supports threshold type: THRESH_BINARY, THRESH_BINARY_INV, THRESH_TRUNC, THRESH_TOZERO, THRESH_TOZERO_INV
800 CV_EXPORTS double threshold(const oclMat &src, oclMat &dst, double thresh, double maxVal, int type = THRESH_TRUNC);
802 //! resizes the image
803 // Supports INTER_NEAREST, INTER_LINEAR
804 // supports CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4 types
805 CV_EXPORTS void resize(const oclMat &src, oclMat &dst, Size dsize, double fx = 0, double fy = 0, int interpolation = INTER_LINEAR);
807 //! Applies a generic geometrical transformation to an image.
809 // Supports INTER_NEAREST, INTER_LINEAR.
811 // Map1 supports CV_16SC2, CV_32FC2 types.
813 // Src supports CV_8UC1, CV_8UC2, CV_8UC4.
815 CV_EXPORTS void remap(const oclMat &src, oclMat &dst, oclMat &map1, oclMat &map2, int interpolation, int bordertype, const Scalar &value = Scalar());
817 //! copies 2D array to a larger destination array and pads borders with user-specifiable constant
818 // supports CV_8UC1, CV_8UC4, CV_32SC1 types
819 CV_EXPORTS void copyMakeBorder(const oclMat &src, oclMat &dst, int top, int bottom, int left, int right, int boardtype, const Scalar &value = Scalar());
821 //! Smoothes image using median filter
822 // The source 1- or 4-channel image. When m is 3 or 5, the image depth should be CV 8U or CV 32F.
823 CV_EXPORTS void medianFilter(const oclMat &src, oclMat &dst, int m);
825 //! warps the image using affine transformation
826 // Supports INTER_NEAREST, INTER_LINEAR, INTER_CUBIC
827 // supports CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4 types
828 CV_EXPORTS void warpAffine(const oclMat &src, oclMat &dst, const Mat &M, Size dsize, int flags = INTER_LINEAR);
830 //! warps the image using perspective transformation
831 // Supports INTER_NEAREST, INTER_LINEAR, INTER_CUBIC
832 // supports CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4 types
833 CV_EXPORTS void warpPerspective(const oclMat &src, oclMat &dst, const Mat &M, Size dsize, int flags = INTER_LINEAR);
835 //! computes the integral image and integral for the squared image
836 // sum will have CV_32S type, sqsum - CV32F type
837 // supports only CV_8UC1 source type
838 CV_EXPORTS void integral(const oclMat &src, oclMat &sum, oclMat &sqsum);
839 CV_EXPORTS void integral(const oclMat &src, oclMat &sum);
840 CV_EXPORTS void cornerHarris(const oclMat &src, oclMat &dst, int blockSize, int ksize, double k, int bordertype = cv::BORDER_DEFAULT);
841 CV_EXPORTS void cornerHarris_dxdy(const oclMat &src, oclMat &dst, oclMat &Dx, oclMat &Dy,
842 int blockSize, int ksize, double k, int bordertype = cv::BORDER_DEFAULT);
843 CV_EXPORTS void cornerMinEigenVal(const oclMat &src, oclMat &dst, int blockSize, int ksize, int bordertype = cv::BORDER_DEFAULT);
844 CV_EXPORTS void cornerMinEigenVal_dxdy(const oclMat &src, oclMat &dst, oclMat &Dx, oclMat &Dy,
845 int blockSize, int ksize, int bordertype = cv::BORDER_DEFAULT);
847 /////////////////////////////////// ML ///////////////////////////////////////////
849 //! Compute closest centers for each lines in source and lable it after center's index
850 // supports CV_32FC1/CV_32FC2/CV_32FC4 data type
851 CV_EXPORTS void distanceToCenters(oclMat &dists, oclMat &labels, const oclMat &src, const oclMat ¢ers);
853 //!Does k-means procedure on GPU
854 // supports CV_32FC1/CV_32FC2/CV_32FC4 data type
855 CV_EXPORTS double kmeans(const oclMat &src, int K, oclMat &bestLabels,
856 TermCriteria criteria, int attemps, int flags, oclMat ¢ers);
859 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
860 ///////////////////////////////////////////CascadeClassifier//////////////////////////////////////////////////////////////////
861 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
863 class CV_EXPORTS_W OclCascadeClassifier : public cv::CascadeClassifier
866 OclCascadeClassifier() {};
867 ~OclCascadeClassifier() {};
869 CvSeq* oclHaarDetectObjects(oclMat &gimg, CvMemStorage *storage, double scaleFactor,
870 int minNeighbors, int flags, CvSize minSize = cvSize(0, 0), CvSize maxSize = cvSize(0, 0));
873 class CV_EXPORTS OclCascadeClassifierBuf : public cv::CascadeClassifier
876 OclCascadeClassifierBuf() :
877 m_flags(0), initialized(false), m_scaleFactor(0), buffers(NULL) {}
879 ~OclCascadeClassifierBuf() { release(); }
881 void detectMultiScale(oclMat &image, CV_OUT std::vector<cv::Rect>& faces,
882 double scaleFactor = 1.1, int minNeighbors = 3, int flags = 0,
883 Size minSize = Size(), Size maxSize = Size());
887 void Init(const int rows, const int cols, double scaleFactor, int flags,
888 const int outputsz, const size_t localThreads[],
889 CvSize minSize, CvSize maxSize);
890 void CreateBaseBufs(const int datasize, const int totalclassifier, const int flags, const int outputsz);
891 void CreateFactorRelatedBufs(const int rows, const int cols, const int flags,
892 const double scaleFactor, const size_t localThreads[],
893 CvSize minSize, CvSize maxSize);
894 void GenResult(CV_OUT std::vector<cv::Rect>& faces, const std::vector<cv::Rect> &rectList, const std::vector<int> &rweights);
901 bool findBiggestObject;
903 double m_scaleFactor;
906 vector<CvSize> sizev;
907 vector<float> scalev;
908 oclMat gimg1, gsum, gsqsum;
913 /////////////////////////////// Pyramid /////////////////////////////////////
914 CV_EXPORTS void pyrDown(const oclMat &src, oclMat &dst);
916 //! upsamples the source image and then smoothes it
917 CV_EXPORTS void pyrUp(const oclMat &src, oclMat &dst);
919 //! performs linear blending of two images
920 //! to avoid accuracy errors sum of weigths shouldn't be very close to zero
921 // supports only CV_8UC1 source type
922 CV_EXPORTS void blendLinear(const oclMat &img1, const oclMat &img2, const oclMat &weights1, const oclMat &weights2, oclMat &result);
924 //! computes vertical sum, supports only CV_32FC1 images
925 CV_EXPORTS void columnSum(const oclMat &src, oclMat &sum);
927 ///////////////////////////////////////// match_template /////////////////////////////////////////////////////////////
928 struct CV_EXPORTS MatchTemplateBuf
930 Size user_block_size;
931 oclMat imagef, templf;
932 std::vector<oclMat> images;
933 std::vector<oclMat> image_sums;
934 std::vector<oclMat> image_sqsums;
937 //! computes the proximity map for the raster template and the image where the template is searched for
938 // Supports TM_SQDIFF, TM_SQDIFF_NORMED, TM_CCORR, TM_CCORR_NORMED, TM_CCOEFF, TM_CCOEFF_NORMED for type 8UC1 and 8UC4
939 // Supports TM_SQDIFF, TM_CCORR for type 32FC1 and 32FC4
940 CV_EXPORTS void matchTemplate(const oclMat &image, const oclMat &templ, oclMat &result, int method);
942 //! computes the proximity map for the raster template and the image where the template is searched for
943 // Supports TM_SQDIFF, TM_SQDIFF_NORMED, TM_CCORR, TM_CCORR_NORMED, TM_CCOEFF, TM_CCOEFF_NORMED for type 8UC1 and 8UC4
944 // Supports TM_SQDIFF, TM_CCORR for type 32FC1 and 32FC4
945 CV_EXPORTS void matchTemplate(const oclMat &image, const oclMat &templ, oclMat &result, int method, MatchTemplateBuf &buf);
947 ///////////////////////////////////////////// Canny /////////////////////////////////////////////
948 struct CV_EXPORTS CannyBuf;
949 //! compute edges of the input image using Canny operator
950 // Support CV_8UC1 only
951 CV_EXPORTS void Canny(const oclMat &image, oclMat &edges, double low_thresh, double high_thresh, int apperture_size = 3, bool L2gradient = false);
952 CV_EXPORTS void Canny(const oclMat &image, CannyBuf &buf, oclMat &edges, double low_thresh, double high_thresh, int apperture_size = 3, bool L2gradient = false);
953 CV_EXPORTS void Canny(const oclMat &dx, const oclMat &dy, oclMat &edges, double low_thresh, double high_thresh, bool L2gradient = false);
954 CV_EXPORTS void Canny(const oclMat &dx, const oclMat &dy, CannyBuf &buf, oclMat &edges, double low_thresh, double high_thresh, bool L2gradient = false);
956 struct CV_EXPORTS CannyBuf
958 CannyBuf() : counter(NULL) {}
963 explicit CannyBuf(const Size &image_size, int apperture_size = 3) : counter(NULL)
965 create(image_size, apperture_size);
967 CannyBuf(const oclMat &dx_, const oclMat &dy_);
969 void create(const Size &image_size, int apperture_size = 3);
972 oclMat dx_buf, dy_buf;
974 oclMat trackBuf1, trackBuf2;
976 Ptr<FilterEngine_GPU> filterDX, filterDY;
979 ///////////////////////////////////////// clAmdFft related /////////////////////////////////////////
980 //! Performs a forward or inverse discrete Fourier transform (1D or 2D) of floating point matrix.
981 //! Param dft_size is the size of DFT transform.
983 //! For complex-to-real transform it is assumed that the source matrix is packed in CLFFT's format.
984 // support src type of CV32FC1, CV32FC2
985 // support flags: DFT_INVERSE, DFT_REAL_OUTPUT, DFT_COMPLEX_OUTPUT, DFT_ROWS
986 // dft_size is the size of original input, which is used for transformation from complex to real.
987 // dft_size must be powers of 2, 3 and 5
988 // real to complex dft requires at least v1.8 clAmdFft
989 // real to complex dft output is not the same with cpu version
990 // real to complex and complex to real does not support DFT_ROWS
991 CV_EXPORTS void dft(const oclMat &src, oclMat &dst, Size dft_size = Size(0, 0), int flags = 0);
993 //! implements generalized matrix product algorithm GEMM from BLAS
994 // The functionality requires clAmdBlas library
995 // only support type CV_32FC1
996 // flag GEMM_3_T is not supported
997 CV_EXPORTS void gemm(const oclMat &src1, const oclMat &src2, double alpha,
998 const oclMat &src3, double beta, oclMat &dst, int flags = 0);
1000 //////////////// HOG (Histogram-of-Oriented-Gradients) Descriptor and Object Detector //////////////
1001 struct CV_EXPORTS HOGDescriptor
1003 enum { DEFAULT_WIN_SIGMA = -1 };
1004 enum { DEFAULT_NLEVELS = 64 };
1005 enum { DESCR_FORMAT_ROW_BY_ROW, DESCR_FORMAT_COL_BY_COL };
1006 HOGDescriptor(Size win_size = Size(64, 128), Size block_size = Size(16, 16),
1007 Size block_stride = Size(8, 8), Size cell_size = Size(8, 8),
1008 int nbins = 9, double win_sigma = DEFAULT_WIN_SIGMA,
1009 double threshold_L2hys = 0.2, bool gamma_correction = true,
1010 int nlevels = DEFAULT_NLEVELS);
1012 size_t getDescriptorSize() const;
1013 size_t getBlockHistogramSize() const;
1014 void setSVMDetector(const vector<float> &detector);
1015 static vector<float> getDefaultPeopleDetector();
1016 static vector<float> getPeopleDetector48x96();
1017 static vector<float> getPeopleDetector64x128();
1018 void detect(const oclMat &img, vector<Point> &found_locations,
1019 double hit_threshold = 0, Size win_stride = Size(),
1020 Size padding = Size());
1021 void detectMultiScale(const oclMat &img, vector<Rect> &found_locations,
1022 double hit_threshold = 0, Size win_stride = Size(),
1023 Size padding = Size(), double scale0 = 1.05,
1024 int group_threshold = 2);
1025 void getDescriptors(const oclMat &img, Size win_stride,
1026 oclMat &descriptors,
1027 int descr_format = DESCR_FORMAT_COL_BY_COL);
1035 double threshold_L2hys;
1036 bool gamma_correction;
1040 // initialize buffers; only need to do once in case of multiscale detection
1041 void init_buffer(const oclMat &img, Size win_stride);
1042 void computeBlockHistograms(const oclMat &img);
1043 void computeGradient(const oclMat &img, oclMat &grad, oclMat &qangle);
1044 double getWinSigma() const;
1045 bool checkDetectorSize() const;
1047 static int numPartsWithin(int size, int part_size, int stride);
1048 static Size numPartsWithin(Size size, Size part_size, Size stride);
1050 // Coefficients of the separating plane
1053 // Results of the last classification step
1056 // Results of the last histogram evaluation step
1058 // Gradients conputation results
1059 oclMat grad, qangle;
1062 // effect size of input image (might be different from original size after scaling)
1067 ////////////////////////feature2d_ocl/////////////////
1068 /****************************************************************************************\
1070 \****************************************************************************************/
1071 template<typename T>
1072 struct CV_EXPORTS Accumulator
1076 template<> struct Accumulator<unsigned char>
1080 template<> struct Accumulator<unsigned short>
1084 template<> struct Accumulator<char>
1088 template<> struct Accumulator<short>
1094 * Manhattan distance (city block distance) functor
1097 struct CV_EXPORTS L1
1099 enum { normType = NORM_L1 };
1100 typedef T ValueType;
1101 typedef typename Accumulator<T>::Type ResultType;
1103 ResultType operator()( const T *a, const T *b, int size ) const
1105 return normL1<ValueType, ResultType>(a, b, size);
1110 * Euclidean distance functor
1113 struct CV_EXPORTS L2
1115 enum { normType = NORM_L2 };
1116 typedef T ValueType;
1117 typedef typename Accumulator<T>::Type ResultType;
1119 ResultType operator()( const T *a, const T *b, int size ) const
1121 return (ResultType)sqrt((double)normL2Sqr<ValueType, ResultType>(a, b, size));
1126 * Hamming distance functor - counts the bit differences between two strings - useful for the Brief descriptor
1127 * bit count of A exclusive XOR'ed with B
1129 struct CV_EXPORTS Hamming
1131 enum { normType = NORM_HAMMING };
1132 typedef unsigned char ValueType;
1133 typedef int ResultType;
1135 /** this will count the bits in a ^ b
1137 ResultType operator()( const unsigned char *a, const unsigned char *b, int size ) const
1139 return normHamming(a, b, size);
1143 ////////////////////////////////// BruteForceMatcher //////////////////////////////////
1145 class CV_EXPORTS BruteForceMatcher_OCL_base
1148 enum DistType {L1Dist = 0, L2Dist, HammingDist};
1149 explicit BruteForceMatcher_OCL_base(DistType distType = L2Dist);
1150 // Add descriptors to train descriptor collection
1151 void add(const std::vector<oclMat> &descCollection);
1152 // Get train descriptors collection
1153 const std::vector<oclMat> &getTrainDescriptors() const;
1154 // Clear train descriptors collection
1156 // Return true if there are not train descriptors in collection
1159 // Return true if the matcher supports mask in match methods
1160 bool isMaskSupported() const;
1162 // Find one best match for each query descriptor
1163 void matchSingle(const oclMat &query, const oclMat &train,
1164 oclMat &trainIdx, oclMat &distance,
1165 const oclMat &mask = oclMat());
1167 // Download trainIdx and distance and convert it to CPU vector with DMatch
1168 static void matchDownload(const oclMat &trainIdx, const oclMat &distance, std::vector<DMatch> &matches);
1169 // Convert trainIdx and distance to vector with DMatch
1170 static void matchConvert(const Mat &trainIdx, const Mat &distance, std::vector<DMatch> &matches);
1172 // Find one best match for each query descriptor
1173 void match(const oclMat &query, const oclMat &train, std::vector<DMatch> &matches, const oclMat &mask = oclMat());
1175 // Make gpu collection of trains and masks in suitable format for matchCollection function
1176 void makeGpuCollection(oclMat &trainCollection, oclMat &maskCollection, const std::vector<oclMat> &masks = std::vector<oclMat>());
1179 // Find one best match from train collection for each query descriptor
1180 void matchCollection(const oclMat &query, const oclMat &trainCollection,
1181 oclMat &trainIdx, oclMat &imgIdx, oclMat &distance,
1182 const oclMat &masks = oclMat());
1184 // Download trainIdx, imgIdx and distance and convert it to vector with DMatch
1185 static void matchDownload(const oclMat &trainIdx, const oclMat &imgIdx, const oclMat &distance, std::vector<DMatch> &matches);
1186 // Convert trainIdx, imgIdx and distance to vector with DMatch
1187 static void matchConvert(const Mat &trainIdx, const Mat &imgIdx, const Mat &distance, std::vector<DMatch> &matches);
1189 // Find one best match from train collection for each query descriptor.
1190 void match(const oclMat &query, std::vector<DMatch> &matches, const std::vector<oclMat> &masks = std::vector<oclMat>());
1192 // Find k best matches for each query descriptor (in increasing order of distances)
1193 void knnMatchSingle(const oclMat &query, const oclMat &train,
1194 oclMat &trainIdx, oclMat &distance, oclMat &allDist, int k,
1195 const oclMat &mask = oclMat());
1197 // Download trainIdx and distance and convert it to vector with DMatch
1198 // compactResult is used when mask is not empty. If compactResult is false matches
1199 // vector will have the same size as queryDescriptors rows. If compactResult is true
1200 // matches vector will not contain matches for fully masked out query descriptors.
1201 static void knnMatchDownload(const oclMat &trainIdx, const oclMat &distance,
1202 std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
1204 // Convert trainIdx and distance to vector with DMatch
1205 static void knnMatchConvert(const Mat &trainIdx, const Mat &distance,
1206 std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
1208 // Find k best matches for each query descriptor (in increasing order of distances).
1209 // compactResult is used when mask is not empty. If compactResult is false matches
1210 // vector will have the same size as queryDescriptors rows. If compactResult is true
1211 // matches vector will not contain matches for fully masked out query descriptors.
1212 void knnMatch(const oclMat &query, const oclMat &train,
1213 std::vector< std::vector<DMatch> > &matches, int k, const oclMat &mask = oclMat(),
1214 bool compactResult = false);
1216 // Find k best matches from train collection for each query descriptor (in increasing order of distances)
1217 void knnMatch2Collection(const oclMat &query, const oclMat &trainCollection,
1218 oclMat &trainIdx, oclMat &imgIdx, oclMat &distance,
1219 const oclMat &maskCollection = oclMat());
1221 // Download trainIdx and distance and convert it to vector with DMatch
1222 // compactResult is used when mask is not empty. If compactResult is false matches
1223 // vector will have the same size as queryDescriptors rows. If compactResult is true
1224 // matches vector will not contain matches for fully masked out query descriptors.
1225 static void knnMatch2Download(const oclMat &trainIdx, const oclMat &imgIdx, const oclMat &distance,
1226 std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
1228 // Convert trainIdx and distance to vector with DMatch
1229 static void knnMatch2Convert(const Mat &trainIdx, const Mat &imgIdx, const Mat &distance,
1230 std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
1232 // Find k best matches for each query descriptor (in increasing order of distances).
1233 // compactResult is used when mask is not empty. If compactResult is false matches
1234 // vector will have the same size as queryDescriptors rows. If compactResult is true
1235 // matches vector will not contain matches for fully masked out query descriptors.
1236 void knnMatch(const oclMat &query, std::vector< std::vector<DMatch> > &matches, int k,
1237 const std::vector<oclMat> &masks = std::vector<oclMat>(), bool compactResult = false);
1239 // Find best matches for each query descriptor which have distance less than maxDistance.
1240 // nMatches.at<int>(0, queryIdx) will contain matches count for queryIdx.
1241 // carefully nMatches can be greater than trainIdx.cols - it means that matcher didn't find all matches,
1242 // because it didn't have enough memory.
1243 // If trainIdx is empty, then trainIdx and distance will be created with size nQuery x max((nTrain / 100), 10),
1244 // otherwize user can pass own allocated trainIdx and distance with size nQuery x nMaxMatches
1245 // Matches doesn't sorted.
1246 void radiusMatchSingle(const oclMat &query, const oclMat &train,
1247 oclMat &trainIdx, oclMat &distance, oclMat &nMatches, float maxDistance,
1248 const oclMat &mask = oclMat());
1250 // Download trainIdx, nMatches and distance and convert it to vector with DMatch.
1251 // matches will be sorted in increasing order of distances.
1252 // compactResult is used when mask is not empty. If compactResult is false matches
1253 // vector will have the same size as queryDescriptors rows. If compactResult is true
1254 // matches vector will not contain matches for fully masked out query descriptors.
1255 static void radiusMatchDownload(const oclMat &trainIdx, const oclMat &distance, const oclMat &nMatches,
1256 std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
1257 // Convert trainIdx, nMatches and distance to vector with DMatch.
1258 static void radiusMatchConvert(const Mat &trainIdx, const Mat &distance, const Mat &nMatches,
1259 std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
1260 // Find best matches for each query descriptor which have distance less than maxDistance
1261 // in increasing order of distances).
1262 void radiusMatch(const oclMat &query, const oclMat &train,
1263 std::vector< std::vector<DMatch> > &matches, float maxDistance,
1264 const oclMat &mask = oclMat(), bool compactResult = false);
1265 // Find best matches for each query descriptor which have distance less than maxDistance.
1266 // If trainIdx is empty, then trainIdx and distance will be created with size nQuery x max((nQuery / 100), 10),
1267 // otherwize user can pass own allocated trainIdx and distance with size nQuery x nMaxMatches
1268 // Matches doesn't sorted.
1269 void radiusMatchCollection(const oclMat &query, oclMat &trainIdx, oclMat &imgIdx, oclMat &distance, oclMat &nMatches, float maxDistance,
1270 const std::vector<oclMat> &masks = std::vector<oclMat>());
1271 // Download trainIdx, imgIdx, nMatches and distance and convert it to vector with DMatch.
1272 // matches will be sorted in increasing order of distances.
1273 // compactResult is used when mask is not empty. If compactResult is false matches
1274 // vector will have the same size as queryDescriptors rows. If compactResult is true
1275 // matches vector will not contain matches for fully masked out query descriptors.
1276 static void radiusMatchDownload(const oclMat &trainIdx, const oclMat &imgIdx, const oclMat &distance, const oclMat &nMatches,
1277 std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
1278 // Convert trainIdx, nMatches and distance to vector with DMatch.
1279 static void radiusMatchConvert(const Mat &trainIdx, const Mat &imgIdx, const Mat &distance, const Mat &nMatches,
1280 std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
1281 // Find best matches from train collection for each query descriptor which have distance less than
1282 // maxDistance (in increasing order of distances).
1283 void radiusMatch(const oclMat &query, std::vector< std::vector<DMatch> > &matches, float maxDistance,
1284 const std::vector<oclMat> &masks = std::vector<oclMat>(), bool compactResult = false);
1287 std::vector<oclMat> trainDescCollection;
1290 template <class Distance>
1291 class CV_EXPORTS BruteForceMatcher_OCL;
1293 template <typename T>
1294 class CV_EXPORTS BruteForceMatcher_OCL< L1<T> > : public BruteForceMatcher_OCL_base
1297 explicit BruteForceMatcher_OCL() : BruteForceMatcher_OCL_base(L1Dist) {}
1298 explicit BruteForceMatcher_OCL(L1<T> /*d*/) : BruteForceMatcher_OCL_base(L1Dist) {}
1301 template <typename T>
1302 class CV_EXPORTS BruteForceMatcher_OCL< L2<T> > : public BruteForceMatcher_OCL_base
1305 explicit BruteForceMatcher_OCL() : BruteForceMatcher_OCL_base(L2Dist) {}
1306 explicit BruteForceMatcher_OCL(L2<T> /*d*/) : BruteForceMatcher_OCL_base(L2Dist) {}
1309 template <> class CV_EXPORTS BruteForceMatcher_OCL< Hamming > : public BruteForceMatcher_OCL_base
1312 explicit BruteForceMatcher_OCL() : BruteForceMatcher_OCL_base(HammingDist) {}
1313 explicit BruteForceMatcher_OCL(Hamming /*d*/) : BruteForceMatcher_OCL_base(HammingDist) {}
1316 class CV_EXPORTS BFMatcher_OCL : public BruteForceMatcher_OCL_base
1319 explicit BFMatcher_OCL(int norm = NORM_L2) : BruteForceMatcher_OCL_base(norm == NORM_L1 ? L1Dist : norm == NORM_L2 ? L2Dist : HammingDist) {}
1322 class CV_EXPORTS GoodFeaturesToTrackDetector_OCL
1325 explicit GoodFeaturesToTrackDetector_OCL(int maxCorners = 1000, double qualityLevel = 0.01, double minDistance = 0.0,
1326 int blockSize = 3, bool useHarrisDetector = false, double harrisK = 0.04);
1328 //! return 1 rows matrix with CV_32FC2 type
1329 void operator ()(const oclMat& image, oclMat& corners, const oclMat& mask = oclMat());
1330 //! download points of type Point2f to a vector. the vector's content will be erased
1331 void downloadPoints(const oclMat &points, vector<Point2f> &points_v);
1334 double qualityLevel;
1338 bool useHarrisDetector;
1340 void releaseMemory()
1345 minMaxbuf_.release();
1346 tmpCorners_.release();
1356 inline GoodFeaturesToTrackDetector_OCL::GoodFeaturesToTrackDetector_OCL(int maxCorners_, double qualityLevel_, double minDistance_,
1357 int blockSize_, bool useHarrisDetector_, double harrisK_)
1359 maxCorners = maxCorners_;
1360 qualityLevel = qualityLevel_;
1361 minDistance = minDistance_;
1362 blockSize = blockSize_;
1363 useHarrisDetector = useHarrisDetector_;
1367 /////////////////////////////// PyrLKOpticalFlow /////////////////////////////////////
1368 class CV_EXPORTS PyrLKOpticalFlow
1373 winSize = Size(21, 21);
1377 useInitialFlow = false;
1378 minEigThreshold = 1e-4f;
1379 getMinEigenVals = false;
1380 isDeviceArch11_ = false;
1383 void sparse(const oclMat &prevImg, const oclMat &nextImg, const oclMat &prevPts, oclMat &nextPts,
1384 oclMat &status, oclMat *err = 0);
1385 void dense(const oclMat &prevImg, const oclMat &nextImg, oclMat &u, oclMat &v, oclMat *err = 0);
1390 bool useInitialFlow;
1391 float minEigThreshold;
1392 bool getMinEigenVals;
1393 void releaseMemory()
1395 dx_calcBuf_.release();
1396 dy_calcBuf_.release();
1405 void calcSharrDeriv(const oclMat &src, oclMat &dx, oclMat &dy);
1406 void buildImagePyramid(const oclMat &img0, vector<oclMat> &pyr, bool withBorder);
1411 vector<oclMat> prevPyr_;
1412 vector<oclMat> nextPyr_;
1418 bool isDeviceArch11_;
1421 class CV_EXPORTS FarnebackOpticalFlow
1424 FarnebackOpticalFlow();
1435 void operator ()(const oclMat &frame0, const oclMat &frame1, oclMat &flowx, oclMat &flowy);
1437 void releaseMemory();
1440 void prepareGaussian(
1441 int n, double sigma, float *g, float *xg, float *xxg,
1442 double &ig11, double &ig03, double &ig33, double &ig55);
1444 void setPolynomialExpansionConsts(int n, double sigma);
1446 void updateFlow_boxFilter(
1447 const oclMat& R0, const oclMat& R1, oclMat& flowx, oclMat &flowy,
1448 oclMat& M, oclMat &bufM, int blockSize, bool updateMatrices);
1450 void updateFlow_gaussianBlur(
1451 const oclMat& R0, const oclMat& R1, oclMat& flowx, oclMat& flowy,
1452 oclMat& M, oclMat &bufM, int blockSize, bool updateMatrices);
1455 oclMat pyrLevel_[2], M_, bufM_, R_[2], blurredFrame_[2];
1456 std::vector<oclMat> pyramid0_, pyramid1_;
1459 //////////////// build warping maps ////////////////////
1460 //! builds plane warping maps
1461 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);
1462 //! builds cylindrical warping maps
1463 CV_EXPORTS void buildWarpCylindricalMaps(Size src_size, Rect dst_roi, const Mat &K, const Mat &R, float scale, oclMat &map_x, oclMat &map_y);
1464 //! builds spherical warping maps
1465 CV_EXPORTS void buildWarpSphericalMaps(Size src_size, Rect dst_roi, const Mat &K, const Mat &R, float scale, oclMat &map_x, oclMat &map_y);
1466 //! builds Affine warping maps
1467 CV_EXPORTS void buildWarpAffineMaps(const Mat &M, bool inverse, Size dsize, oclMat &xmap, oclMat &ymap);
1469 //! builds Perspective warping maps
1470 CV_EXPORTS void buildWarpPerspectiveMaps(const Mat &M, bool inverse, Size dsize, oclMat &xmap, oclMat &ymap);
1472 ///////////////////////////////////// interpolate frames //////////////////////////////////////////////
1473 //! Interpolate frames (images) using provided optical flow (displacement field).
1474 //! frame0 - frame 0 (32-bit floating point images, single channel)
1475 //! frame1 - frame 1 (the same type and size)
1476 //! fu - forward horizontal displacement
1477 //! fv - forward vertical displacement
1478 //! bu - backward horizontal displacement
1479 //! bv - backward vertical displacement
1480 //! pos - new frame position
1481 //! newFrame - new frame
1482 //! buf - temporary buffer, will have width x 6*height size, CV_32FC1 type and contain 6 oclMat;
1483 //! occlusion masks 0, occlusion masks 1,
1484 //! interpolated forward flow 0, interpolated forward flow 1,
1485 //! interpolated backward flow 0, interpolated backward flow 1
1487 CV_EXPORTS void interpolateFrames(const oclMat &frame0, const oclMat &frame1,
1488 const oclMat &fu, const oclMat &fv,
1489 const oclMat &bu, const oclMat &bv,
1490 float pos, oclMat &newFrame, oclMat &buf);
1492 //! computes moments of the rasterized shape or a vector of points
1493 CV_EXPORTS Moments ocl_moments(InputArray _array, bool binaryImage);
1495 class CV_EXPORTS StereoBM_OCL
1498 enum { BASIC_PRESET = 0, PREFILTER_XSOBEL = 1 };
1500 enum { DEFAULT_NDISP = 64, DEFAULT_WINSZ = 19 };
1502 //! the default constructor
1504 //! the full constructor taking the camera-specific preset, number of disparities and the SAD window size. ndisparities must be multiple of 8.
1505 StereoBM_OCL(int preset, int ndisparities = DEFAULT_NDISP, int winSize = DEFAULT_WINSZ);
1507 //! the stereo correspondence operator. Finds the disparity for the specified rectified stereo pair
1508 //! Output disparity has CV_8U type.
1509 void operator() ( const oclMat &left, const oclMat &right, oclMat &disparity);
1511 //! Some heuristics that tries to estmate
1512 // if current GPU will be faster then CPU in this algorithm.
1513 // It queries current active device.
1514 static bool checkIfGpuCallReasonable();
1520 // If avergeTexThreshold == 0 => post procesing is disabled
1521 // If avergeTexThreshold != 0 then disparity is set 0 in each point (x,y) where for left image
1522 // SumOfHorizontalGradiensInWindow(x, y, winSize) < (winSize * winSize) * avergeTexThreshold
1523 // i.e. input left image is low textured.
1524 float avergeTexThreshold;
1526 oclMat minSSD, leBuf, riBuf;
1529 class CV_EXPORTS StereoBeliefPropagation
1532 enum { DEFAULT_NDISP = 64 };
1533 enum { DEFAULT_ITERS = 5 };
1534 enum { DEFAULT_LEVELS = 5 };
1535 static void estimateRecommendedParams(int width, int height, int &ndisp, int &iters, int &levels);
1536 explicit StereoBeliefPropagation(int ndisp = DEFAULT_NDISP,
1537 int iters = DEFAULT_ITERS,
1538 int levels = DEFAULT_LEVELS,
1539 int msg_type = CV_16S);
1540 StereoBeliefPropagation(int ndisp, int iters, int levels,
1541 float max_data_term, float data_weight,
1542 float max_disc_term, float disc_single_jump,
1543 int msg_type = CV_32F);
1544 void operator()(const oclMat &left, const oclMat &right, oclMat &disparity);
1545 void operator()(const oclMat &data, oclMat &disparity);
1549 float max_data_term;
1551 float max_disc_term;
1552 float disc_single_jump;
1555 oclMat u, d, l, r, u2, d2, l2, r2;
1556 std::vector<oclMat> datas;
1560 class CV_EXPORTS StereoConstantSpaceBP
1563 enum { DEFAULT_NDISP = 128 };
1564 enum { DEFAULT_ITERS = 8 };
1565 enum { DEFAULT_LEVELS = 4 };
1566 enum { DEFAULT_NR_PLANE = 4 };
1567 static void estimateRecommendedParams(int width, int height, int &ndisp, int &iters, int &levels, int &nr_plane);
1568 explicit StereoConstantSpaceBP(
1569 int ndisp = DEFAULT_NDISP,
1570 int iters = DEFAULT_ITERS,
1571 int levels = DEFAULT_LEVELS,
1572 int nr_plane = DEFAULT_NR_PLANE,
1573 int msg_type = CV_32F);
1574 StereoConstantSpaceBP(int ndisp, int iters, int levels, int nr_plane,
1575 float max_data_term, float data_weight, float max_disc_term, float disc_single_jump,
1576 int min_disp_th = 0,
1577 int msg_type = CV_32F);
1578 void operator()(const oclMat &left, const oclMat &right, oclMat &disparity);
1583 float max_data_term;
1585 float max_disc_term;
1586 float disc_single_jump;
1589 bool use_local_init_data_cost;
1591 oclMat u[2], d[2], l[2], r[2];
1592 oclMat disp_selected_pyr[2];
1594 oclMat data_cost_selected;
1599 // Implementation of the Zach, Pock and Bischof Dual TV-L1 Optical Flow method
1602 // [1] C. Zach, T. Pock and H. Bischof, "A Duality Based Approach for Realtime TV-L1 Optical Flow".
1603 // [2] Javier Sanchez, Enric Meinhardt-Llopis and Gabriele Facciolo. "TV-L1 Optical Flow Estimation".
1604 class CV_EXPORTS OpticalFlowDual_TVL1_OCL
1607 OpticalFlowDual_TVL1_OCL();
1609 void operator ()(const oclMat& I0, const oclMat& I1, oclMat& flowx, oclMat& flowy);
1611 void collectGarbage();
1614 * Time step of the numerical scheme.
1619 * Weight parameter for the data term, attachment parameter.
1620 * This is the most relevant parameter, which determines the smoothness of the output.
1621 * The smaller this parameter is, the smoother the solutions we obtain.
1622 * It depends on the range of motions of the images, so its value should be adapted to each image sequence.
1627 * Weight parameter for (u - v)^2, tightness parameter.
1628 * It serves as a link between the attachment and the regularization terms.
1629 * In theory, it should have a small value in order to maintain both parts in correspondence.
1630 * The method is stable for a large range of values of this parameter.
1635 * Number of scales used to create the pyramid of images.
1640 * Number of warpings per scale.
1641 * Represents the number of times that I1(x+u0) and grad( I1(x+u0) ) are computed per scale.
1642 * This is a parameter that assures the stability of the method.
1643 * It also affects the running time, so it is a compromise between speed and accuracy.
1648 * Stopping criterion threshold used in the numerical scheme, which is a trade-off between precision and running time.
1649 * A small value will yield more accurate solutions at the expense of a slower convergence.
1654 * Stopping criterion iterations number used in the numerical scheme.
1658 bool useInitialFlow;
1661 void procOneScale(const oclMat& I0, const oclMat& I1, oclMat& u1, oclMat& u2);
1663 std::vector<oclMat> I0s;
1664 std::vector<oclMat> I1s;
1665 std::vector<oclMat> u1s;
1666 std::vector<oclMat> u2s;
1686 // current supported sorting methods
1689 SORT_BITONIC, // only support power-of-2 buffer size
1690 SORT_SELECTION, // cannot sort duplicate keys
1692 SORT_RADIX // only support signed int/float keys(CV_32S/CV_32F)
1694 //! Returns the sorted result of all the elements in input based on equivalent keys.
1696 // The element unit in the values to be sorted is determined from the data type,
1697 // i.e., a CV_32FC2 input {a1a2, b1b2} will be considered as two elements, regardless its
1698 // matrix dimension.
1699 // both keys and values will be sorted inplace
1700 // Key needs to be single channel oclMat.
1704 // keys = {2, 3, 1} (CV_8UC1)
1705 // values = {10,5, 4,3, 6,2} (CV_8UC2)
1706 // sortByKey(keys, values, SORT_SELECTION, false);
1708 // keys = {1, 2, 3} (CV_8UC1)
1709 // values = {6,2, 10,5, 4,3} (CV_8UC2)
1710 void CV_EXPORTS sortByKey(oclMat& keys, oclMat& values, int method, bool isGreaterThan = false);
1711 /*!Base class for MOG and MOG2!*/
1712 class CV_EXPORTS BackgroundSubtractor
1715 //! the virtual destructor
1716 virtual ~BackgroundSubtractor();
1717 //! the update operator that takes the next video frame and returns the current foreground mask as 8-bit binary image.
1718 virtual void operator()(const oclMat& image, oclMat& fgmask, float learningRate);
1720 //! computes a background image
1721 virtual void getBackgroundImage(oclMat& backgroundImage) const = 0;
1724 Gaussian Mixture-based Backbround/Foreground Segmentation Algorithm
1726 The class implements the following algorithm:
1727 "An improved adaptive background mixture model for real-time tracking with shadow detection"
1728 P. KadewTraKuPong and R. Bowden,
1729 Proc. 2nd European Workshp on Advanced Video-Based Surveillance Systems, 2001."
1730 http://personal.ee.surrey.ac.uk/Personal/R.Bowden/publications/avbs01/avbs01.pdf
1732 class CV_EXPORTS MOG: public cv::ocl::BackgroundSubtractor
1735 //! the default constructor
1736 MOG(int nmixtures = -1);
1738 //! re-initiaization method
1739 void initialize(Size frameSize, int frameType);
1741 //! the update operator
1742 void operator()(const oclMat& frame, oclMat& fgmask, float learningRate = 0.f);
1744 //! computes a background image which are the mean of all background gaussians
1745 void getBackgroundImage(oclMat& backgroundImage) const;
1747 //! releases all inner buffers
1752 float backgroundRatio;
1769 The class implements the following algorithm:
1770 "Improved adaptive Gausian mixture model for background subtraction"
1772 International Conference Pattern Recognition, UK, August, 2004.
1773 http://www.zoranz.net/Publications/zivkovic2004ICPR.pdf
1775 class CV_EXPORTS MOG2: public cv::ocl::BackgroundSubtractor
1778 //! the default constructor
1779 MOG2(int nmixtures = -1);
1781 //! re-initiaization method
1782 void initialize(Size frameSize, int frameType);
1784 //! the update operator
1785 void operator()(const oclMat& frame, oclMat& fgmask, float learningRate = -1.0f);
1787 //! computes a background image which are the mean of all background gaussians
1788 void getBackgroundImage(oclMat& backgroundImage) const;
1790 //! releases all inner buffers
1794 // you should call initialize after parameters changes
1798 //! here it is the maximum allowed number of mixture components.
1799 //! Actual number is determined dynamically per pixel
1801 // threshold on the squared Mahalanobis distance to decide if it is well described
1802 // by the background model or not. Related to Cthr from the paper.
1803 // This does not influence the update of the background. A typical value could be 4 sigma
1804 // and that is varThreshold=4*4=16; Corresponds to Tb in the paper.
1806 /////////////////////////
1807 // less important parameters - things you might change but be carefull
1808 ////////////////////////
1810 float backgroundRatio;
1811 // corresponds to fTB=1-cf from the paper
1812 // TB - threshold when the component becomes significant enough to be included into
1813 // the background model. It is the TB=1-cf from the paper. So I use cf=0.1 => TB=0.
1814 // For alpha=0.001 it means that the mode should exist for approximately 105 frames before
1815 // it is considered foreground
1816 // float noiseSigma;
1817 float varThresholdGen;
1819 //correspondts to Tg - threshold on the squared Mahalan. dist. to decide
1820 //when a sample is close to the existing components. If it is not close
1821 //to any a new component will be generated. I use 3 sigma => Tg=3*3=9.
1822 //Smaller Tg leads to more generated components and higher Tg might make
1823 //lead to small number of components but they can grow too large
1828 //initial variance for the newly generated components.
1829 //It will will influence the speed of adaptation. A good guess should be made.
1830 //A simple way is to estimate the typical standard deviation from the images.
1831 //I used here 10 as a reasonable value
1832 // min and max can be used to further control the variance
1833 float fCT; //CT - complexity reduction prior
1834 //this is related to the number of samples needed to accept that a component
1835 //actually exists. We use CT=0.05 of all the samples. By setting CT=0 you get
1836 //the standard Stauffer&Grimson algorithm (maybe not exact but very similar)
1838 //shadow detection parameters
1839 bool bShadowDetection; //default 1 - do shadow detection
1840 unsigned char nShadowDetection; //do shadow detection - insert this value as the detection result - 127 default value
1842 // Tau - shadow threshold. The shadow is detected if the pixel is darker
1843 //version of the background. Tau is a threshold on how much darker the shadow can be.
1844 //Tau= 0.5 means that if pixel is more than 2 times darker then it is not shadow
1845 //See: Prati,Mikic,Trivedi,Cucchiarra,"Detecting Moving Shadows...",IEEE PAMI,2003.
1858 oclMat bgmodelUsedModes_; //keep track of number of modes per pixel
1861 /*!***************Kalman Filter*************!*/
1862 class CV_EXPORTS KalmanFilter
1866 //! the full constructor taking the dimensionality of the state, of the measurement and of the control vector
1867 KalmanFilter(int dynamParams, int measureParams, int controlParams=0, int type=CV_32F);
1868 //! re-initializes Kalman filter. The previous content is destroyed.
1869 void init(int dynamParams, int measureParams, int controlParams=0, int type=CV_32F);
1871 const oclMat& predict(const oclMat& control=oclMat());
1872 const oclMat& correct(const oclMat& measurement);
1874 oclMat statePre; //!< predicted state (x'(k)): x(k)=A*x(k-1)+B*u(k)
1875 oclMat statePost; //!< corrected state (x(k)): x(k)=x'(k)+K(k)*(z(k)-H*x'(k))
1876 oclMat transitionMatrix; //!< state transition matrix (A)
1877 oclMat controlMatrix; //!< control matrix (B) (not used if there is no control)
1878 oclMat measurementMatrix; //!< measurement matrix (H)
1879 oclMat processNoiseCov; //!< process noise covariance matrix (Q)
1880 oclMat measurementNoiseCov;//!< measurement noise covariance matrix (R)
1881 oclMat errorCovPre; //!< priori error estimate covariance matrix (P'(k)): P'(k)=A*P(k-1)*At + Q)*/
1882 oclMat gain; //!< Kalman gain matrix (K(k)): K(k)=P'(k)*Ht*inv(H*P'(k)*Ht+R)
1883 oclMat errorCovPost; //!< posteriori error estimate covariance matrix (P(k)): P(k)=(I-K(k)*H)*P'(k)
1892 static inline size_t divUp(size_t total, size_t grain)
1894 return (total + grain - 1) / grain;
1897 /*!***************K Nearest Neighbour*************!*/
1898 class CV_EXPORTS KNearestNeighbour: public CvKNearest
1901 KNearestNeighbour();
1902 ~KNearestNeighbour();
1904 bool train(const Mat& trainData, Mat& labels, Mat& sampleIdx = Mat().setTo(Scalar::all(0)),
1905 bool isRegression = false, int max_k = 32, bool updateBase = false);
1909 void find_nearest(const oclMat& samples, int k, oclMat& lables);
1916 #if defined _MSC_VER && _MSC_VER >= 1200
1917 # pragma warning( push)
1918 # pragma warning( disable: 4267)
1920 #include "opencv2/ocl/matrix_operations.hpp"
1921 #if defined _MSC_VER && _MSC_VER >= 1200
1922 # pragma warning( pop)
1925 #endif /* __OPENCV_OCL_HPP__ */