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.hpp"
51 #include "opencv2/imgproc.hpp"
52 #include "opencv2/objdetect.hpp"
53 #include "opencv2/ml.hpp"
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
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
85 //Get the global device memory and read/write type
86 //return 1 if unified memory system supported, otherwise return 0
87 CV_EXPORTS int getDevMemType(DevMemRW& rw_type, DevMemType& mem_type);
89 //Set the global device memory and read/write type,
90 //the newly generated oclMat will all use this type
91 //return -1 if the target type is unsupported, otherwise return 0
92 CV_EXPORTS int setDevMemType(DevMemRW rw_type = DEVICE_MEM_R_W, DevMemType mem_type = DEVICE_MEM_DEFAULT);
94 //this class contains ocl runtime information
105 Info &operator = (const Info &m);
106 std::vector<String> DeviceName;
109 //////////////////////////////// Initialization & Info ////////////////////////
110 //this function may be obsoleted
111 //CV_EXPORTS cl_device_id getDevice();
112 //the function must be called before any other cv::ocl::functions, it initialize ocl runtime
113 //each Info relates to an OpenCL platform
114 //there is one or more devices in each platform, each one has a separate name
115 CV_EXPORTS int getDevice(std::vector<Info> &oclinfo, int devicetype = CVCL_DEVICE_TYPE_GPU);
117 //set device you want to use, optional function after getDevice be called
118 //the devnum is the index of the selected device in DeviceName vector of INfo
119 CV_EXPORTS void setDevice(Info &oclinfo, int devnum = 0);
121 //The two functions below enable other opencl program to use ocl module's cl_context and cl_command_queue
122 //returns cl_context *
123 CV_EXPORTS void* getoclContext();
124 //returns cl_command_queue *
125 CV_EXPORTS void* getoclCommandQueue();
127 //explicit call clFinish. The global command queue will be used.
128 CV_EXPORTS void finish();
130 //this function enable ocl module to use customized cl_context and cl_command_queue
131 //getDevice also need to be called before this function
132 CV_EXPORTS void setDeviceEx(Info &oclinfo, void *ctx, void *qu, int devnum = 0);
134 //returns true when global OpenCL context is initialized
135 CV_EXPORTS bool initialized();
137 //////////////////////////////// OpenCL context ////////////////////////
138 //This is a global singleton class used to represent a OpenCL context.
139 class CV_EXPORTS Context
143 friend class std::auto_ptr<Context>;
144 friend bool initialized();
146 static std::auto_ptr<Context> clCxt;
153 static Context *getContext();
154 static void setContext(Info &oclinfo);
156 enum {CL_DOUBLE, CL_UNIFIED_MEM, CL_VER_1_2};
157 bool supportsFeature(int ftype) const;
158 size_t computeUnits() const;
159 size_t maxWorkGroupSize();
161 void* oclCommandQueue();
164 //! Calls a kernel, by string. Pass globalThreads = NULL, and cleanUp = true, to finally clean-up without executing.
165 CV_EXPORTS double openCLExecuteKernelInterop(Context *clCxt ,
166 const char **source, String kernelName,
167 size_t globalThreads[3], size_t localThreads[3],
168 std::vector< std::pair<size_t, const void *> > &args,
169 int channels, int depth, const char *build_options,
170 bool finish = true, bool measureKernelTime = false,
171 bool cleanUp = true);
173 //! Calls a kernel, by file. Pass globalThreads = NULL, and cleanUp = true, to finally clean-up without executing.
174 CV_EXPORTS double openCLExecuteKernelInterop(Context *clCxt ,
175 const char **fileName, const int numFiles, String kernelName,
176 size_t globalThreads[3], size_t localThreads[3],
177 std::vector< std::pair<size_t, const void *> > &args,
178 int channels, int depth, const char *build_options,
179 bool finish = true, bool measureKernelTime = false,
180 bool cleanUp = true);
182 //! Enable or disable OpenCL program binary caching onto local disk
183 // After a program (*.cl files in opencl/ folder) is built at runtime, we allow the
184 // compiled OpenCL program to be cached to the path automatically as "path/*.clb"
185 // binary file, which will be reused when the OpenCV executable is started again.
187 // Caching mode is controlled by the following enums
189 // 1. the feature is by default enabled when OpenCV is built in release mode.
190 // 2. the CACHE_DEBUG / CACHE_RELEASE flags only effectively work with MSVC compiler;
191 // for GNU compilers, the function always treats the build as release mode (enabled by default).
194 CACHE_NONE = 0, // do not cache OpenCL binary
195 CACHE_DEBUG = 0x1 << 0, // cache OpenCL binary when built in debug mode (only work with MSVC)
196 CACHE_RELEASE = 0x1 << 1, // default behavior, only cache when built in release mode (only work with MSVC)
197 CACHE_ALL = CACHE_DEBUG | CACHE_RELEASE, // always cache opencl binary
198 CACHE_UPDATE = 0x1 << 2 // if the binary cache file with the same name is already on the disk, it will be updated.
200 CV_EXPORTS void setBinaryDiskCache(int mode = CACHE_RELEASE, cv::String path = "./");
202 //! set where binary cache to be saved to
203 CV_EXPORTS void setBinpath(const char *path);
205 class CV_EXPORTS oclMatExpr;
206 //////////////////////////////// oclMat ////////////////////////////////
207 class CV_EXPORTS oclMat
210 //! default constructor
212 //! constructs oclMatrix of the specified size and type (_type is CV_8UC1, CV_64FC3, CV_32SC(12) etc.)
213 oclMat(int rows, int cols, int type);
214 oclMat(Size size, int type);
215 //! constucts oclMatrix and fills it with the specified value _s.
216 oclMat(int rows, int cols, int type, const Scalar &s);
217 oclMat(Size size, int type, const Scalar &s);
219 oclMat(const oclMat &m);
221 //! constructor for oclMatrix headers pointing to user-allocated data
222 oclMat(int rows, int cols, int type, void *data, size_t step = Mat::AUTO_STEP);
223 oclMat(Size size, int type, void *data, size_t step = Mat::AUTO_STEP);
225 //! creates a matrix header for a part of the bigger matrix
226 oclMat(const oclMat &m, const Range &rowRange, const Range &colRange);
227 oclMat(const oclMat &m, const Rect &roi);
229 //! builds oclMat from Mat. Perfom blocking upload to device.
230 explicit oclMat (const Mat &m);
232 //! destructor - calls release()
235 //! assignment operators
236 oclMat &operator = (const oclMat &m);
237 //! assignment operator. Perfom blocking upload to device.
238 oclMat &operator = (const Mat &m);
239 oclMat &operator = (const oclMatExpr& expr);
241 //! pefroms blocking upload data to oclMat.
242 void upload(const cv::Mat &m);
245 //! downloads data from device to host memory. Blocking calls.
246 operator Mat() const;
247 void download(cv::Mat &m) const;
249 //! convert to _InputArray
250 operator _InputArray();
252 //! convert to _OutputArray
253 operator _OutputArray();
255 //! returns a new oclMatrix header for the specified row
256 oclMat row(int y) const;
257 //! returns a new oclMatrix header for the specified column
258 oclMat col(int x) const;
259 //! ... for the specified row span
260 oclMat rowRange(int startrow, int endrow) const;
261 oclMat rowRange(const Range &r) const;
262 //! ... for the specified column span
263 oclMat colRange(int startcol, int endcol) const;
264 oclMat colRange(const Range &r) const;
266 //! returns deep copy of the oclMatrix, i.e. the data is copied
267 oclMat clone() const;
269 //! copies those oclMatrix elements to "m" that are marked with non-zero mask elements.
270 // It calls m.create(this->size(), this->type()).
271 // It supports any data type
272 void copyTo( oclMat &m, const oclMat &mask = oclMat()) const;
274 //! converts oclMatrix to another datatype with optional scalng. See cvConvertScale.
275 //It supports 8UC1 8UC4 32SC1 32SC4 32FC1 32FC4
276 void convertTo( oclMat &m, int rtype, double alpha = 1, double beta = 0 ) const;
278 void assignTo( oclMat &m, int type = -1 ) const;
280 //! sets every oclMatrix element to s
281 //It supports 8UC1 8UC4 32SC1 32SC4 32FC1 32FC4
282 oclMat& operator = (const Scalar &s);
283 //! sets some of the oclMatrix elements to s, according to the mask
284 //It supports 8UC1 8UC4 32SC1 32SC4 32FC1 32FC4
285 oclMat& setTo(const Scalar &s, const oclMat &mask = oclMat());
286 //! creates alternative oclMatrix header for the same data, with different
287 // number of channels and/or different number of rows. see cvReshape.
288 oclMat reshape(int cn, int rows = 0) const;
290 //! allocates new oclMatrix data unless the oclMatrix already has specified size and type.
291 // previous data is unreferenced if needed.
292 void create(int rows, int cols, int type);
293 void create(Size size, int type);
295 //! allocates new oclMatrix with specified device memory type.
296 void createEx(int rows, int cols, int type,
297 DevMemRW rw_type, DevMemType mem_type, void* hptr = 0);
298 void createEx(Size size, int type, DevMemRW rw_type,
299 DevMemType mem_type, void* hptr = 0);
301 //! decreases reference counter;
302 // deallocate the data when reference counter reaches 0.
305 //! swaps with other smart pointer
306 void swap(oclMat &mat);
308 //! locates oclMatrix header within a parent oclMatrix. See below
309 void locateROI( Size &wholeSize, Point &ofs ) const;
310 //! moves/resizes the current oclMatrix ROI inside the parent oclMatrix.
311 oclMat& adjustROI( int dtop, int dbottom, int dleft, int dright );
312 //! extracts a rectangular sub-oclMatrix
313 // (this is a generalized form of row, rowRange etc.)
314 oclMat operator()( Range rowRange, Range colRange ) const;
315 oclMat operator()( const Rect &roi ) const;
317 oclMat& operator+=( const oclMat& m );
318 oclMat& operator-=( const oclMat& m );
319 oclMat& operator*=( const oclMat& m );
320 oclMat& operator/=( const oclMat& m );
322 //! returns true if the oclMatrix data is continuous
323 // (i.e. when there are no gaps between successive rows).
324 // similar to CV_IS_oclMat_CONT(cvoclMat->type)
325 bool isContinuous() const;
326 //! returns element size in bytes,
327 // similar to CV_ELEM_SIZE(cvMat->type)
328 size_t elemSize() const;
329 //! returns the size of element channel in bytes.
330 size_t elemSize1() const;
331 //! returns element type, similar to CV_MAT_TYPE(cvMat->type)
333 //! returns element type, i.e. 8UC3 returns 8UC4 because in ocl
334 //! 3 channels element actually use 4 channel space
336 //! returns element type, similar to CV_MAT_DEPTH(cvMat->type)
338 //! returns element type, similar to CV_MAT_CN(cvMat->type)
339 int channels() const;
340 //! returns element type, return 4 for 3 channels element,
341 //!becuase 3 channels element actually use 4 channel space
342 int oclchannels() const;
343 //! returns step/elemSize1()
344 size_t step1() const;
345 //! returns oclMatrix size:
346 // width == number of columns, height == number of rows
348 //! returns true if oclMatrix data is NULL
351 //! returns pointer to y-th row
352 uchar* ptr(int y = 0);
353 const uchar *ptr(int y = 0) const;
355 //! template version of the above method
356 template<typename _Tp> _Tp *ptr(int y = 0);
357 template<typename _Tp> const _Tp *ptr(int y = 0) const;
359 //! matrix transposition
362 /*! includes several bit-fields:
363 - the magic signature
369 //! the number of rows and columns
371 //! a distance between successive rows in bytes; includes the gap if any
373 //! pointer to the data(OCL memory object)
376 //! pointer to the reference counter;
377 // when oclMatrix points to user-allocated data, the pointer is NULL
380 //! helper fields used in locateROI and adjustROI
381 //datastart and dataend are not used in current version
385 //! OpenCL context associated with the oclMat object.
387 //add offset for handle ROI, calculated in byte
389 //add wholerows and wholecols for the whole matrix, datastart and dataend are no longer used
394 // convert InputArray/OutputArray to oclMat references
395 CV_EXPORTS oclMat& getOclMatRef(InputArray src);
396 CV_EXPORTS oclMat& getOclMatRef(OutputArray src);
398 ///////////////////// mat split and merge /////////////////////////////////
399 //! Compose a multi-channel array from several single-channel arrays
401 CV_EXPORTS void merge(const oclMat *src, size_t n, oclMat &dst);
402 CV_EXPORTS void merge(const std::vector<oclMat> &src, oclMat &dst);
404 //! Divides multi-channel array into several single-channel arrays
406 CV_EXPORTS void split(const oclMat &src, oclMat *dst);
407 CV_EXPORTS void split(const oclMat &src, std::vector<oclMat> &dst);
409 ////////////////////////////// Arithmetics ///////////////////////////////////
411 //! adds one matrix to another with scale (dst = src1 * alpha + src2 * beta + gama)
412 CV_EXPORTS void addWeighted(const oclMat &src1, double alpha, const oclMat &src2, double beta, double gama, oclMat &dst);
414 //! adds one matrix to another (dst = src1 + src2)
415 // supports all data types
416 CV_EXPORTS void add(const oclMat &src1, const oclMat &src2, oclMat &dst, const oclMat &mask = oclMat());
417 //! adds scalar to a matrix (dst = src1 + s)
418 // supports all data types
419 CV_EXPORTS void add(const oclMat &src1, const Scalar &s, oclMat &dst, const oclMat &mask = oclMat());
421 //! subtracts one matrix from another (dst = src1 - src2)
422 // supports all data types
423 CV_EXPORTS void subtract(const oclMat &src1, const oclMat &src2, oclMat &dst, const oclMat &mask = oclMat());
424 //! subtracts scalar from a matrix (dst = src1 - s)
425 // supports all data types
426 CV_EXPORTS void subtract(const oclMat &src1, const Scalar &s, oclMat &dst, const oclMat &mask = oclMat());
428 //! computes element-wise product of the two arrays (dst = src1 * scale * src2)
429 // supports all data types
430 CV_EXPORTS void multiply(const oclMat &src1, const oclMat &src2, oclMat &dst, double scale = 1);
431 //! multiplies matrix to a number (dst = scalar * src)
432 // supports all data types
433 CV_EXPORTS void multiply(double scalar, const oclMat &src, oclMat &dst);
435 //! computes element-wise quotient of the two arrays (dst = src1 * scale / src2)
436 // supports all data types
437 CV_EXPORTS void divide(const oclMat &src1, const oclMat &src2, oclMat &dst, double scale = 1);
438 //! computes element-wise quotient of the two arrays (dst = scale / src)
439 // supports all data types
440 CV_EXPORTS void divide(double scale, const oclMat &src1, oclMat &dst);
442 //! compares elements of two arrays (dst = src1 <cmpop> src2)
443 // supports all data types
444 CV_EXPORTS void compare(const oclMat &src1, const oclMat &src2, oclMat &dst, int cmpop);
446 //! transposes the matrix
447 // supports all data types
448 CV_EXPORTS void transpose(const oclMat &src, oclMat &dst);
450 //! computes element-wise absolute difference of two arrays (dst = abs(src1 - src2))
451 // supports all data types
452 CV_EXPORTS void absdiff(const oclMat &src1, const oclMat &src2, oclMat &dst);
453 //! computes element-wise absolute difference of array and scalar (dst = abs(src1 - s))
454 // supports all data types
455 CV_EXPORTS void absdiff(const oclMat &src1, const Scalar &s, oclMat &dst);
457 //! computes mean value and standard deviation of all or selected array elements
458 // supports except CV_32F,CV_64F
459 CV_EXPORTS void meanStdDev(const oclMat &mtx, Scalar &mean, Scalar &stddev);
461 //! computes norm of array
462 // supports NORM_INF, NORM_L1, NORM_L2
463 // supports only CV_8UC1 type
464 CV_EXPORTS double norm(const oclMat &src1, int normType = NORM_L2);
466 //! computes norm of the difference between two arrays
467 // supports NORM_INF, NORM_L1, NORM_L2
468 // supports only CV_8UC1 type
469 CV_EXPORTS double norm(const oclMat &src1, const oclMat &src2, int normType = NORM_L2);
471 //! reverses the order of the rows, columns or both in a matrix
472 // supports all types
473 CV_EXPORTS void flip(const oclMat &src, oclMat &dst, int flipCode);
475 //! computes sum of array elements
476 // disabled until fix crash
478 CV_EXPORTS Scalar sum(const oclMat &m);
479 CV_EXPORTS Scalar absSum(const oclMat &m);
480 CV_EXPORTS Scalar sqrSum(const oclMat &m);
482 //! finds global minimum and maximum array elements and returns their values
483 // support all C1 types
484 CV_EXPORTS void minMax(const oclMat &src, double *minVal, double *maxVal = 0, const oclMat &mask = oclMat());
485 CV_EXPORTS void minMax_buf(const oclMat &src, double *minVal, double *maxVal, const oclMat &mask, oclMat& buf);
487 //! finds global minimum and maximum array elements and returns their values with locations
488 // support all C1 types
489 CV_EXPORTS void minMaxLoc(const oclMat &src, double *minVal, double *maxVal = 0, Point *minLoc = 0, Point *maxLoc = 0,
490 const oclMat &mask = oclMat());
492 //! counts non-zero array elements
494 CV_EXPORTS int countNonZero(const oclMat &src);
496 //! transforms 8-bit unsigned integers using lookup table: dst(i)=lut(src(i))
497 // destination array will have the depth type as lut and the same channels number as source
498 //It supports 8UC1 8UC4 only
499 CV_EXPORTS void LUT(const oclMat &src, const oclMat &lut, oclMat &dst);
501 //! only 8UC1 and 256 bins is supported now
502 CV_EXPORTS void calcHist(const oclMat &mat_src, oclMat &mat_hist);
503 //! only 8UC1 and 256 bins is supported now
504 CV_EXPORTS void equalizeHist(const oclMat &mat_src, oclMat &mat_dst);
506 //! only 8UC1 is supported now
507 CV_EXPORTS Ptr<cv::CLAHE> createCLAHE(double clipLimit = 40.0, Size tileGridSize = Size(8, 8));
510 // supports 8UC1 8UC4
511 CV_EXPORTS void bilateralFilter(const oclMat& src, oclMat& dst, int d, double sigmaColor, double sigmaSpace, int borderType=BORDER_DEFAULT);
513 //! Applies an adaptive bilateral filter to the input image
514 // This is not truly a bilateral filter. Instead of using user provided fixed parameters,
515 // the function calculates a constant at each window based on local standard deviation,
516 // and use this constant to do filtering.
517 // supports 8UC1, 8UC3
518 CV_EXPORTS void adaptiveBilateralFilter(const oclMat& src, oclMat& dst, Size ksize, double sigmaSpace, Point anchor = Point(-1, -1), int borderType=BORDER_DEFAULT);
520 //! computes exponent of each matrix element (dst = e**src)
521 // supports only CV_32FC1, CV_64FC1 type
522 CV_EXPORTS void exp(const oclMat &src, oclMat &dst);
524 //! computes natural logarithm of absolute value of each matrix element: dst = log(abs(src))
525 // supports only CV_32FC1, CV_64FC1 type
526 CV_EXPORTS void log(const oclMat &src, oclMat &dst);
528 //! computes magnitude of each (x(i), y(i)) vector
529 // supports only CV_32F, CV_64F type
530 CV_EXPORTS void magnitude(const oclMat &x, const oclMat &y, oclMat &magnitude);
532 //! computes angle (angle(i)) of each (x(i), y(i)) vector
533 // supports only CV_32F, CV_64F type
534 CV_EXPORTS void phase(const oclMat &x, const oclMat &y, oclMat &angle, bool angleInDegrees = false);
536 //! the function raises every element of tne input array to p
537 // support only CV_32F, CV_64F type
538 CV_EXPORTS void pow(const oclMat &x, double p, oclMat &y);
540 //! converts Cartesian coordinates to polar
541 // supports only CV_32F CV_64F type
542 CV_EXPORTS void cartToPolar(const oclMat &x, const oclMat &y, oclMat &magnitude, oclMat &angle, bool angleInDegrees = false);
544 //! converts polar coordinates to Cartesian
545 // supports only CV_32F CV_64F type
546 CV_EXPORTS void polarToCart(const oclMat &magnitude, const oclMat &angle, oclMat &x, oclMat &y, bool angleInDegrees = false);
548 //! perfroms per-elements bit-wise inversion
549 // supports all types
550 CV_EXPORTS void bitwise_not(const oclMat &src, oclMat &dst);
552 //! calculates per-element bit-wise disjunction of two arrays
553 // supports all types
554 CV_EXPORTS void bitwise_or(const oclMat &src1, const oclMat &src2, oclMat &dst, const oclMat &mask = oclMat());
555 CV_EXPORTS void bitwise_or(const oclMat &src1, const Scalar &s, oclMat &dst, const oclMat &mask = oclMat());
557 //! calculates per-element bit-wise conjunction of two arrays
558 // supports all types
559 CV_EXPORTS void bitwise_and(const oclMat &src1, const oclMat &src2, oclMat &dst, const oclMat &mask = oclMat());
560 CV_EXPORTS void bitwise_and(const oclMat &src1, const Scalar &s, oclMat &dst, const oclMat &mask = oclMat());
562 //! calculates per-element bit-wise "exclusive or" operation
563 // supports all types
564 CV_EXPORTS void bitwise_xor(const oclMat &src1, const oclMat &src2, oclMat &dst, const oclMat &mask = oclMat());
565 CV_EXPORTS void bitwise_xor(const oclMat &src1, const Scalar &s, oclMat &dst, const oclMat &mask = oclMat());
567 //! Logical operators
568 CV_EXPORTS oclMat operator ~ (const oclMat &);
569 CV_EXPORTS oclMat operator | (const oclMat &, const oclMat &);
570 CV_EXPORTS oclMat operator & (const oclMat &, const oclMat &);
571 CV_EXPORTS oclMat operator ^ (const oclMat &, const oclMat &);
574 //! Mathematics operators
575 CV_EXPORTS oclMatExpr operator + (const oclMat &src1, const oclMat &src2);
576 CV_EXPORTS oclMatExpr operator - (const oclMat &src1, const oclMat &src2);
577 CV_EXPORTS oclMatExpr operator * (const oclMat &src1, const oclMat &src2);
578 CV_EXPORTS oclMatExpr operator / (const oclMat &src1, const oclMat &src2);
580 struct CV_EXPORTS ConvolveBuf
584 Size user_block_size;
587 oclMat image_spect, templ_spect, result_spect;
588 oclMat image_block, templ_block, result_data;
590 void create(Size image_size, Size templ_size);
591 static Size estimateBlockSize(Size result_size, Size templ_size);
594 //! computes convolution of two images, may use discrete Fourier transform
595 // support only CV_32FC1 type
596 CV_EXPORTS void convolve(const oclMat &image, const oclMat &temp1, oclMat &result, bool ccorr = false);
597 CV_EXPORTS void convolve(const oclMat &image, const oclMat &temp1, oclMat &result, bool ccorr, ConvolveBuf& buf);
599 //! Performs a per-element multiplication of two Fourier spectrums.
600 //! Only full (not packed) CV_32FC2 complex spectrums in the interleaved format are supported for now.
601 //! support only CV_32FC2 type
602 CV_EXPORTS void mulSpectrums(const oclMat &a, const oclMat &b, oclMat &c, int flags, float scale, bool conjB = false);
604 CV_EXPORTS void cvtColor(const oclMat &src, oclMat &dst, int code , int dcn = 0);
606 CV_EXPORTS void setIdentity(oclMat& src, double val);
608 //////////////////////////////// Filter Engine ////////////////////////////////
611 The Base Class for 1D or Row-wise Filters
613 This is the base class for linear or non-linear filters that process 1D data.
614 In particular, such filters are used for the "horizontal" filtering parts in separable filters.
616 class CV_EXPORTS BaseRowFilter_GPU
619 BaseRowFilter_GPU(int ksize_, int anchor_, int bordertype_) : ksize(ksize_), anchor(anchor_), bordertype(bordertype_) {}
620 virtual ~BaseRowFilter_GPU() {}
621 virtual void operator()(const oclMat &src, oclMat &dst) = 0;
622 int ksize, anchor, bordertype;
626 The Base Class for Column-wise Filters
628 This is the base class for linear or non-linear filters that process columns of 2D arrays.
629 Such filters are used for the "vertical" filtering parts in separable filters.
631 class CV_EXPORTS BaseColumnFilter_GPU
634 BaseColumnFilter_GPU(int ksize_, int anchor_, int bordertype_) : ksize(ksize_), anchor(anchor_), bordertype(bordertype_) {}
635 virtual ~BaseColumnFilter_GPU() {}
636 virtual void operator()(const oclMat &src, oclMat &dst) = 0;
637 int ksize, anchor, bordertype;
641 The Base Class for Non-Separable 2D Filters.
643 This is the base class for linear or non-linear 2D filters.
645 class CV_EXPORTS BaseFilter_GPU
648 BaseFilter_GPU(const Size &ksize_, const Point &anchor_, const int &borderType_)
649 : ksize(ksize_), anchor(anchor_), borderType(borderType_) {}
650 virtual ~BaseFilter_GPU() {}
651 virtual void operator()(const oclMat &src, oclMat &dst) = 0;
658 The Base Class for Filter Engine.
660 The class can be used to apply an arbitrary filtering operation to an image.
661 It contains all the necessary intermediate buffers.
663 class CV_EXPORTS FilterEngine_GPU
666 virtual ~FilterEngine_GPU() {}
668 virtual void apply(const oclMat &src, oclMat &dst, Rect roi = Rect(0, 0, -1, -1)) = 0;
671 //! returns the non-separable filter engine with the specified filter
672 CV_EXPORTS Ptr<FilterEngine_GPU> createFilter2D_GPU(const Ptr<BaseFilter_GPU> filter2D);
674 //! returns the primitive row filter with the specified kernel
675 CV_EXPORTS Ptr<BaseRowFilter_GPU> getLinearRowFilter_GPU(int srcType, int bufType, const Mat &rowKernel,
676 int anchor = -1, int bordertype = BORDER_DEFAULT);
678 //! returns the primitive column filter with the specified kernel
679 CV_EXPORTS Ptr<BaseColumnFilter_GPU> getLinearColumnFilter_GPU(int bufType, int dstType, const Mat &columnKernel,
680 int anchor = -1, int bordertype = BORDER_DEFAULT, double delta = 0.0);
682 //! returns the separable linear filter engine
683 CV_EXPORTS Ptr<FilterEngine_GPU> createSeparableLinearFilter_GPU(int srcType, int dstType, const Mat &rowKernel,
684 const Mat &columnKernel, const Point &anchor = Point(-1, -1), double delta = 0.0, int bordertype = BORDER_DEFAULT);
686 //! returns the separable filter engine with the specified filters
687 CV_EXPORTS Ptr<FilterEngine_GPU> createSeparableFilter_GPU(const Ptr<BaseRowFilter_GPU> &rowFilter,
688 const Ptr<BaseColumnFilter_GPU> &columnFilter);
690 //! returns the Gaussian filter engine
691 CV_EXPORTS Ptr<FilterEngine_GPU> createGaussianFilter_GPU(int type, Size ksize, double sigma1, double sigma2 = 0, int bordertype = BORDER_DEFAULT);
693 //! returns filter engine for the generalized Sobel operator
694 CV_EXPORTS Ptr<FilterEngine_GPU> createDerivFilter_GPU( int srcType, int dstType, int dx, int dy, int ksize, int borderType = BORDER_DEFAULT );
696 //! applies Laplacian operator to the image
697 // supports only ksize = 1 and ksize = 3 8UC1 8UC4 32FC1 32FC4 data type
698 CV_EXPORTS void Laplacian(const oclMat &src, oclMat &dst, int ddepth, int ksize = 1, double scale = 1);
700 //! returns 2D box filter
701 // supports CV_8UC1 and CV_8UC4 source type, dst type must be the same as source type
702 CV_EXPORTS Ptr<BaseFilter_GPU> getBoxFilter_GPU(int srcType, int dstType,
703 const Size &ksize, Point anchor = Point(-1, -1), int borderType = BORDER_DEFAULT);
705 //! returns box filter engine
706 CV_EXPORTS Ptr<FilterEngine_GPU> createBoxFilter_GPU(int srcType, int dstType, const Size &ksize,
707 const Point &anchor = Point(-1, -1), int borderType = BORDER_DEFAULT);
709 //! returns 2D filter with the specified kernel
710 // supports CV_8UC1 and CV_8UC4 types
711 CV_EXPORTS Ptr<BaseFilter_GPU> getLinearFilter_GPU(int srcType, int dstType, const Mat &kernel, const Size &ksize,
712 const Point &anchor = Point(-1, -1), int borderType = BORDER_DEFAULT);
714 //! returns the non-separable linear filter engine
715 CV_EXPORTS Ptr<FilterEngine_GPU> createLinearFilter_GPU(int srcType, int dstType, const Mat &kernel,
716 const Point &anchor = Point(-1, -1), int borderType = BORDER_DEFAULT);
718 //! smooths the image using the normalized box filter
719 // supports data type: CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4
720 // supports border type: BORDER_CONSTANT, BORDER_REPLICATE, BORDER_REFLECT,BORDER_REFLECT_101,BORDER_WRAP
721 CV_EXPORTS void boxFilter(const oclMat &src, oclMat &dst, int ddepth, Size ksize,
722 Point anchor = Point(-1, -1), int borderType = BORDER_DEFAULT);
724 //! returns 2D morphological filter
725 //! only MORPH_ERODE and MORPH_DILATE are supported
726 // supports CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4 types
727 // kernel must have CV_8UC1 type, one rows and cols == ksize.width * ksize.height
728 CV_EXPORTS Ptr<BaseFilter_GPU> getMorphologyFilter_GPU(int op, int type, const Mat &kernel, const Size &ksize,
729 Point anchor = Point(-1, -1));
731 //! returns morphological filter engine. Only MORPH_ERODE and MORPH_DILATE are supported.
732 CV_EXPORTS Ptr<FilterEngine_GPU> createMorphologyFilter_GPU(int op, int type, const Mat &kernel,
733 const Point &anchor = Point(-1, -1), int iterations = 1);
735 //! a synonym for normalized box filter
736 // supports data type: CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4
737 // supports border type: BORDER_CONSTANT, BORDER_REPLICATE, BORDER_REFLECT,BORDER_REFLECT_101
738 static inline void blur(const oclMat &src, oclMat &dst, Size ksize, Point anchor = Point(-1, -1),
739 int borderType = BORDER_CONSTANT)
741 boxFilter(src, dst, -1, ksize, anchor, borderType);
744 //! applies non-separable 2D linear filter to the image
745 // Note, at the moment this function only works when anchor point is in the kernel center
746 // and kernel size supported is either 3x3 or 5x5; otherwise the function will fail to output valid result
747 CV_EXPORTS void filter2D(const oclMat &src, oclMat &dst, int ddepth, const Mat &kernel,
748 Point anchor = Point(-1, -1), int borderType = BORDER_DEFAULT);
750 //! applies separable 2D linear filter to the image
751 CV_EXPORTS void sepFilter2D(const oclMat &src, oclMat &dst, int ddepth, const Mat &kernelX, const Mat &kernelY,
752 Point anchor = Point(-1, -1), double delta = 0.0, int bordertype = BORDER_DEFAULT);
754 //! applies generalized Sobel operator to the image
755 // dst.type must equalize src.type
756 // supports data type: CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4
757 // supports border type: BORDER_CONSTANT, BORDER_REPLICATE, BORDER_REFLECT,BORDER_REFLECT_101
758 CV_EXPORTS void Sobel(const oclMat &src, oclMat &dst, int ddepth, int dx, int dy, int ksize = 3, double scale = 1, double delta = 0.0, int bordertype = BORDER_DEFAULT);
760 //! applies the vertical or horizontal Scharr operator to the image
761 // dst.type must equalize src.type
762 // supports data type: CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4
763 // supports border type: BORDER_CONSTANT, BORDER_REPLICATE, BORDER_REFLECT,BORDER_REFLECT_101
764 CV_EXPORTS void Scharr(const oclMat &src, oclMat &dst, int ddepth, int dx, int dy, double scale = 1, double delta = 0.0, int bordertype = BORDER_DEFAULT);
766 //! smooths the image using Gaussian filter.
767 // dst.type must equalize src.type
768 // supports data type: CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4
769 // supports border type: BORDER_CONSTANT, BORDER_REPLICATE, BORDER_REFLECT,BORDER_REFLECT_101
770 CV_EXPORTS void GaussianBlur(const oclMat &src, oclMat &dst, Size ksize, double sigma1, double sigma2 = 0, int bordertype = BORDER_DEFAULT);
772 //! erodes the image (applies the local minimum operator)
773 // supports data type: CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4
774 CV_EXPORTS void erode( const oclMat &src, oclMat &dst, const Mat &kernel, Point anchor = Point(-1, -1), int iterations = 1,
776 int borderType = BORDER_CONSTANT, const Scalar &borderValue = morphologyDefaultBorderValue());
779 //! dilates the image (applies the local maximum operator)
780 // supports data type: CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4
781 CV_EXPORTS void dilate( const oclMat &src, oclMat &dst, const Mat &kernel, Point anchor = Point(-1, -1), int iterations = 1,
783 int borderType = BORDER_CONSTANT, const Scalar &borderValue = morphologyDefaultBorderValue());
786 //! applies an advanced morphological operation to the image
787 CV_EXPORTS void morphologyEx( const oclMat &src, oclMat &dst, int op, const Mat &kernel, Point anchor = Point(-1, -1), int iterations = 1,
789 int borderType = BORDER_CONSTANT, const Scalar &borderValue = morphologyDefaultBorderValue());
792 ////////////////////////////// Image processing //////////////////////////////
793 //! Does mean shift filtering on GPU.
794 CV_EXPORTS void meanShiftFiltering(const oclMat &src, oclMat &dst, int sp, int sr,
795 TermCriteria criteria = TermCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 5, 1));
797 //! Does mean shift procedure on GPU.
798 CV_EXPORTS void meanShiftProc(const oclMat &src, oclMat &dstr, oclMat &dstsp, int sp, int sr,
799 TermCriteria criteria = TermCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 5, 1));
801 //! Does mean shift segmentation with elimiation of small regions.
802 CV_EXPORTS void meanShiftSegmentation(const oclMat &src, Mat &dst, int sp, int sr, int minsize,
803 TermCriteria criteria = TermCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 5, 1));
805 //! applies fixed threshold to the image.
806 // supports CV_8UC1 and CV_32FC1 data type
807 // supports threshold type: THRESH_BINARY, THRESH_BINARY_INV, THRESH_TRUNC, THRESH_TOZERO, THRESH_TOZERO_INV
808 CV_EXPORTS double threshold(const oclMat &src, oclMat &dst, double thresh, double maxVal, int type = THRESH_TRUNC);
810 //! resizes the image
811 // Supports INTER_NEAREST, INTER_LINEAR
812 // supports CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4 types
813 CV_EXPORTS void resize(const oclMat &src, oclMat &dst, Size dsize, double fx = 0, double fy = 0, int interpolation = INTER_LINEAR);
815 //! Applies a generic geometrical transformation to an image.
817 // Supports INTER_NEAREST, INTER_LINEAR.
819 // Map1 supports CV_16SC2, CV_32FC2 types.
821 // Src supports CV_8UC1, CV_8UC2, CV_8UC4.
823 CV_EXPORTS void remap(const oclMat &src, oclMat &dst, oclMat &map1, oclMat &map2, int interpolation, int bordertype, const Scalar &value = Scalar());
825 //! copies 2D array to a larger destination array and pads borders with user-specifiable constant
826 // supports CV_8UC1, CV_8UC4, CV_32SC1 types
827 CV_EXPORTS void copyMakeBorder(const oclMat &src, oclMat &dst, int top, int bottom, int left, int right, int boardtype, const Scalar &value = Scalar());
829 //! Smoothes image using median filter
830 // The source 1- or 4-channel image. When m is 3 or 5, the image depth should be CV 8U or CV 32F.
831 CV_EXPORTS void medianFilter(const oclMat &src, oclMat &dst, int m);
833 //! warps the image using affine transformation
834 // Supports INTER_NEAREST, INTER_LINEAR, INTER_CUBIC
835 // supports CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4 types
836 CV_EXPORTS void warpAffine(const oclMat &src, oclMat &dst, const Mat &M, Size dsize, int flags = INTER_LINEAR);
838 //! warps the image using perspective transformation
839 // Supports INTER_NEAREST, INTER_LINEAR, INTER_CUBIC
840 // supports CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4 types
841 CV_EXPORTS void warpPerspective(const oclMat &src, oclMat &dst, const Mat &M, Size dsize, int flags = INTER_LINEAR);
843 //! computes the integral image and integral for the squared image
844 // sum will have CV_32S type, sqsum - CV32F type
845 // supports only CV_8UC1 source type
846 CV_EXPORTS void integral(const oclMat &src, oclMat &sum, oclMat &sqsum);
847 CV_EXPORTS void integral(const oclMat &src, oclMat &sum);
848 CV_EXPORTS void cornerHarris(const oclMat &src, oclMat &dst, int blockSize, int ksize, double k, int bordertype = cv::BORDER_DEFAULT);
849 CV_EXPORTS void cornerHarris_dxdy(const oclMat &src, oclMat &dst, oclMat &Dx, oclMat &Dy,
850 int blockSize, int ksize, double k, int bordertype = cv::BORDER_DEFAULT);
851 CV_EXPORTS void cornerMinEigenVal(const oclMat &src, oclMat &dst, int blockSize, int ksize, int bordertype = cv::BORDER_DEFAULT);
852 CV_EXPORTS void cornerMinEigenVal_dxdy(const oclMat &src, oclMat &dst, oclMat &Dx, oclMat &Dy,
853 int blockSize, int ksize, int bordertype = cv::BORDER_DEFAULT);
856 /////////////////////////////////// ML ///////////////////////////////////////////
858 //! Compute closest centers for each lines in source and lable it after center's index
859 // supports CV_32FC1/CV_32FC2/CV_32FC4 data type
860 CV_EXPORTS void distanceToCenters(oclMat &dists, oclMat &labels, const oclMat &src, const oclMat ¢ers);
862 //!Does k-means procedure on GPU
863 // supports CV_32FC1/CV_32FC2/CV_32FC4 data type
864 CV_EXPORTS double kmeans(const oclMat &src, int K, oclMat &bestLabels,
865 TermCriteria criteria, int attemps, int flags, oclMat ¢ers);
868 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
869 ///////////////////////////////////////////CascadeClassifier//////////////////////////////////////////////////////////////////
870 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
871 class CV_EXPORTS OclCascadeClassifier : public cv::CascadeClassifier
874 void detectMultiScale(oclMat &image, CV_OUT std::vector<cv::Rect>& faces,
875 double scaleFactor = 1.1, int minNeighbors = 3, int flags = 0,
876 Size minSize = Size(), Size maxSize = Size());
879 /////////////////////////////// Pyramid /////////////////////////////////////
880 CV_EXPORTS void pyrDown(const oclMat &src, oclMat &dst);
882 //! upsamples the source image and then smoothes it
883 CV_EXPORTS void pyrUp(const oclMat &src, oclMat &dst);
885 //! performs linear blending of two images
886 //! to avoid accuracy errors sum of weigths shouldn't be very close to zero
887 // supports only CV_8UC1 source type
888 CV_EXPORTS void blendLinear(const oclMat &img1, const oclMat &img2, const oclMat &weights1, const oclMat &weights2, oclMat &result);
890 //! computes vertical sum, supports only CV_32FC1 images
891 CV_EXPORTS void columnSum(const oclMat &src, oclMat &sum);
893 ///////////////////////////////////////// match_template /////////////////////////////////////////////////////////////
894 struct CV_EXPORTS MatchTemplateBuf
896 Size user_block_size;
897 oclMat imagef, templf;
898 std::vector<oclMat> images;
899 std::vector<oclMat> image_sums;
900 std::vector<oclMat> image_sqsums;
903 //! computes the proximity map for the raster template and the image where the template is searched for
904 // Supports TM_SQDIFF, TM_SQDIFF_NORMED, TM_CCORR, TM_CCORR_NORMED, TM_CCOEFF, TM_CCOEFF_NORMED for type 8UC1 and 8UC4
905 // Supports TM_SQDIFF, TM_CCORR for type 32FC1 and 32FC4
906 CV_EXPORTS void matchTemplate(const oclMat &image, const oclMat &templ, oclMat &result, int method);
908 //! computes the proximity map for the raster template and the image where the template is searched for
909 // Supports TM_SQDIFF, TM_SQDIFF_NORMED, TM_CCORR, TM_CCORR_NORMED, TM_CCOEFF, TM_CCOEFF_NORMED for type 8UC1 and 8UC4
910 // Supports TM_SQDIFF, TM_CCORR for type 32FC1 and 32FC4
911 CV_EXPORTS void matchTemplate(const oclMat &image, const oclMat &templ, oclMat &result, int method, MatchTemplateBuf &buf);
915 ///////////////////////////////////////////// Canny /////////////////////////////////////////////
916 struct CV_EXPORTS CannyBuf;
918 //! compute edges of the input image using Canny operator
919 // Support CV_8UC1 only
920 CV_EXPORTS void Canny(const oclMat &image, oclMat &edges, double low_thresh, double high_thresh, int apperture_size = 3, bool L2gradient = false);
921 CV_EXPORTS void Canny(const oclMat &image, CannyBuf &buf, oclMat &edges, double low_thresh, double high_thresh, int apperture_size = 3, bool L2gradient = false);
922 CV_EXPORTS void Canny(const oclMat &dx, const oclMat &dy, oclMat &edges, double low_thresh, double high_thresh, bool L2gradient = false);
923 CV_EXPORTS void Canny(const oclMat &dx, const oclMat &dy, CannyBuf &buf, oclMat &edges, double low_thresh, double high_thresh, bool L2gradient = false);
925 struct CV_EXPORTS CannyBuf
927 CannyBuf() : counter(NULL) {}
932 explicit CannyBuf(const Size &image_size, int apperture_size = 3) : counter(NULL)
934 create(image_size, apperture_size);
936 CannyBuf(const oclMat &dx_, const oclMat &dy_);
937 void create(const Size &image_size, int apperture_size = 3);
941 oclMat dx_buf, dy_buf;
942 oclMat magBuf, mapBuf;
943 oclMat trackBuf1, trackBuf2;
945 Ptr<FilterEngine_GPU> filterDX, filterDY;
948 ///////////////////////////////////////// Hough Transform /////////////////////////////////////////
950 struct HoughCirclesBuf
959 CV_EXPORTS void HoughCircles(const oclMat& src, oclMat& circles, int method, float dp, float minDist, int cannyThreshold, int votesThreshold, int minRadius, int maxRadius, int maxCircles = 4096);
960 CV_EXPORTS void HoughCircles(const oclMat& src, oclMat& circles, HoughCirclesBuf& buf, int method, float dp, float minDist, int cannyThreshold, int votesThreshold, int minRadius, int maxRadius, int maxCircles = 4096);
961 CV_EXPORTS void HoughCirclesDownload(const oclMat& d_circles, OutputArray h_circles);
964 ///////////////////////////////////////// clAmdFft related /////////////////////////////////////////
965 //! Performs a forward or inverse discrete Fourier transform (1D or 2D) of floating point matrix.
966 //! Param dft_size is the size of DFT transform.
968 //! For complex-to-real transform it is assumed that the source matrix is packed in CLFFT's format.
969 // support src type of CV32FC1, CV32FC2
970 // support flags: DFT_INVERSE, DFT_REAL_OUTPUT, DFT_COMPLEX_OUTPUT, DFT_ROWS
971 // dft_size is the size of original input, which is used for transformation from complex to real.
972 // dft_size must be powers of 2, 3 and 5
973 // real to complex dft requires at least v1.8 clAmdFft
974 // real to complex dft output is not the same with cpu version
975 // real to complex and complex to real does not support DFT_ROWS
976 CV_EXPORTS void dft(const oclMat &src, oclMat &dst, Size dft_size = Size(), int flags = 0);
978 //! implements generalized matrix product algorithm GEMM from BLAS
979 // The functionality requires clAmdBlas library
980 // only support type CV_32FC1
981 // flag GEMM_3_T is not supported
982 CV_EXPORTS void gemm(const oclMat &src1, const oclMat &src2, double alpha,
983 const oclMat &src3, double beta, oclMat &dst, int flags = 0);
985 //////////////// HOG (Histogram-of-Oriented-Gradients) Descriptor and Object Detector //////////////
987 struct CV_EXPORTS HOGDescriptor
991 enum { DEFAULT_WIN_SIGMA = -1 };
993 enum { DEFAULT_NLEVELS = 64 };
995 enum { DESCR_FORMAT_ROW_BY_ROW, DESCR_FORMAT_COL_BY_COL };
999 HOGDescriptor(Size win_size = Size(64, 128), Size block_size = Size(16, 16),
1001 Size block_stride = Size(8, 8), Size cell_size = Size(8, 8),
1003 int nbins = 9, double win_sigma = DEFAULT_WIN_SIGMA,
1005 double threshold_L2hys = 0.2, bool gamma_correction = true,
1007 int nlevels = DEFAULT_NLEVELS);
1011 size_t getDescriptorSize() const;
1013 size_t getBlockHistogramSize() const;
1017 void setSVMDetector(const std::vector<float> &detector);
1021 static std::vector<float> getDefaultPeopleDetector();
1023 static std::vector<float> getPeopleDetector48x96();
1025 static std::vector<float> getPeopleDetector64x128();
1029 void detect(const oclMat &img, std::vector<Point> &found_locations,
1031 double hit_threshold = 0, Size win_stride = Size(),
1033 Size padding = Size());
1037 void detectMultiScale(const oclMat &img, std::vector<Rect> &found_locations,
1039 double hit_threshold = 0, Size win_stride = Size(),
1041 Size padding = Size(), double scale0 = 1.05,
1043 int group_threshold = 2);
1047 void getDescriptors(const oclMat &img, Size win_stride,
1049 oclMat &descriptors,
1051 int descr_format = DESCR_FORMAT_COL_BY_COL);
1067 double threshold_L2hys;
1069 bool gamma_correction;
1077 // initialize buffers; only need to do once in case of multiscale detection
1079 void init_buffer(const oclMat &img, Size win_stride);
1083 void computeBlockHistograms(const oclMat &img);
1085 void computeGradient(const oclMat &img, oclMat &grad, oclMat &qangle);
1089 double getWinSigma() const;
1091 bool checkDetectorSize() const;
1095 static int numPartsWithin(int size, int part_size, int stride);
1097 static Size numPartsWithin(Size size, Size part_size, Size stride);
1101 // Coefficients of the separating plane
1109 // Results of the last classification step
1117 // Results of the last histogram evaluation step
1123 // Gradients conputation results
1125 oclMat grad, qangle;
1135 // effect size of input image (might be different from original size after scaling)
1142 ////////////////////////feature2d_ocl/////////////////
1143 /****************************************************************************************\
1145 \****************************************************************************************/
1146 template<typename T>
1147 struct CV_EXPORTS Accumulator
1151 template<> struct Accumulator<unsigned char>
1155 template<> struct Accumulator<unsigned short>
1159 template<> struct Accumulator<char>
1163 template<> struct Accumulator<short>
1169 * Manhattan distance (city block distance) functor
1172 struct CV_EXPORTS L1
1174 enum { normType = NORM_L1 };
1175 typedef T ValueType;
1176 typedef typename Accumulator<T>::Type ResultType;
1178 ResultType operator()( const T *a, const T *b, int size ) const
1180 return normL1<ValueType, ResultType>(a, b, size);
1185 * Euclidean distance functor
1188 struct CV_EXPORTS L2
1190 enum { normType = NORM_L2 };
1191 typedef T ValueType;
1192 typedef typename Accumulator<T>::Type ResultType;
1194 ResultType operator()( const T *a, const T *b, int size ) const
1196 return (ResultType)std::sqrt((double)normL2Sqr<ValueType, ResultType>(a, b, size));
1201 * Hamming distance functor - counts the bit differences between two strings - useful for the Brief descriptor
1202 * bit count of A exclusive XOR'ed with B
1204 struct CV_EXPORTS Hamming
1206 enum { normType = NORM_HAMMING };
1207 typedef unsigned char ValueType;
1208 typedef int ResultType;
1210 /** this will count the bits in a ^ b
1212 ResultType operator()( const unsigned char *a, const unsigned char *b, int size ) const
1214 return normHamming(a, b, size);
1218 ////////////////////////////////// BruteForceMatcher //////////////////////////////////
1220 class CV_EXPORTS BruteForceMatcher_OCL_base
1223 enum DistType {L1Dist = 0, L2Dist, HammingDist};
1224 explicit BruteForceMatcher_OCL_base(DistType distType = L2Dist);
1226 // Add descriptors to train descriptor collection
1227 void add(const std::vector<oclMat> &descCollection);
1229 // Get train descriptors collection
1230 const std::vector<oclMat> &getTrainDescriptors() const;
1232 // Clear train descriptors collection
1235 // Return true if there are not train descriptors in collection
1238 // Return true if the matcher supports mask in match methods
1239 bool isMaskSupported() const;
1241 // Find one best match for each query descriptor
1242 void matchSingle(const oclMat &query, const oclMat &train,
1243 oclMat &trainIdx, oclMat &distance,
1244 const oclMat &mask = oclMat());
1246 // Download trainIdx and distance and convert it to CPU vector with DMatch
1247 static void matchDownload(const oclMat &trainIdx, const oclMat &distance, std::vector<DMatch> &matches);
1248 // Convert trainIdx and distance to vector with DMatch
1249 static void matchConvert(const Mat &trainIdx, const Mat &distance, std::vector<DMatch> &matches);
1251 // Find one best match for each query descriptor
1252 void match(const oclMat &query, const oclMat &train, std::vector<DMatch> &matches, const oclMat &mask = oclMat());
1254 // Make gpu collection of trains and masks in suitable format for matchCollection function
1255 void makeGpuCollection(oclMat &trainCollection, oclMat &maskCollection, const std::vector<oclMat> &masks = std::vector<oclMat>());
1257 // Find one best match from train collection for each query descriptor
1258 void matchCollection(const oclMat &query, const oclMat &trainCollection,
1259 oclMat &trainIdx, oclMat &imgIdx, oclMat &distance,
1260 const oclMat &masks = oclMat());
1262 // Download trainIdx, imgIdx and distance and convert it to vector with DMatch
1263 static void matchDownload(const oclMat &trainIdx, const oclMat &imgIdx, const oclMat &distance, std::vector<DMatch> &matches);
1264 // Convert trainIdx, imgIdx and distance to vector with DMatch
1265 static void matchConvert(const Mat &trainIdx, const Mat &imgIdx, const Mat &distance, std::vector<DMatch> &matches);
1267 // Find one best match from train collection for each query descriptor.
1268 void match(const oclMat &query, std::vector<DMatch> &matches, const std::vector<oclMat> &masks = std::vector<oclMat>());
1270 // Find k best matches for each query descriptor (in increasing order of distances)
1271 void knnMatchSingle(const oclMat &query, const oclMat &train,
1272 oclMat &trainIdx, oclMat &distance, oclMat &allDist, int k,
1273 const oclMat &mask = oclMat());
1275 // Download trainIdx and distance and convert it to vector with DMatch
1276 // compactResult is used when mask is not empty. If compactResult is false matches
1277 // vector will have the same size as queryDescriptors rows. If compactResult is true
1278 // matches vector will not contain matches for fully masked out query descriptors.
1279 static void knnMatchDownload(const oclMat &trainIdx, const oclMat &distance,
1280 std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
1281 // Convert trainIdx and distance to vector with DMatch
1282 static void knnMatchConvert(const Mat &trainIdx, const Mat &distance,
1283 std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
1285 // Find k best matches for each query descriptor (in increasing order of distances).
1286 // compactResult is used when mask is not empty. If compactResult is false matches
1287 // vector will have the same size as queryDescriptors rows. If compactResult is true
1288 // matches vector will not contain matches for fully masked out query descriptors.
1289 void knnMatch(const oclMat &query, const oclMat &train,
1290 std::vector< std::vector<DMatch> > &matches, int k, const oclMat &mask = oclMat(),
1291 bool compactResult = false);
1293 // Find k best matches from train collection for each query descriptor (in increasing order of distances)
1294 void knnMatch2Collection(const oclMat &query, const oclMat &trainCollection,
1295 oclMat &trainIdx, oclMat &imgIdx, oclMat &distance,
1296 const oclMat &maskCollection = oclMat());
1298 // Download trainIdx and distance and convert it to vector with DMatch
1299 // compactResult is used when mask is not empty. If compactResult is false matches
1300 // vector will have the same size as queryDescriptors rows. If compactResult is true
1301 // matches vector will not contain matches for fully masked out query descriptors.
1302 static void knnMatch2Download(const oclMat &trainIdx, const oclMat &imgIdx, const oclMat &distance,
1303 std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
1304 // Convert trainIdx and distance to vector with DMatch
1305 static void knnMatch2Convert(const Mat &trainIdx, const Mat &imgIdx, const Mat &distance,
1306 std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
1308 // Find k best matches for each query descriptor (in increasing order of distances).
1309 // compactResult is used when mask is not empty. If compactResult is false matches
1310 // vector will have the same size as queryDescriptors rows. If compactResult is true
1311 // matches vector will not contain matches for fully masked out query descriptors.
1312 void knnMatch(const oclMat &query, std::vector< std::vector<DMatch> > &matches, int k,
1313 const std::vector<oclMat> &masks = std::vector<oclMat>(), bool compactResult = false);
1315 // Find best matches for each query descriptor which have distance less than maxDistance.
1316 // nMatches.at<int>(0, queryIdx) will contain matches count for queryIdx.
1317 // carefully nMatches can be greater than trainIdx.cols - it means that matcher didn't find all matches,
1318 // because it didn't have enough memory.
1319 // If trainIdx is empty, then trainIdx and distance will be created with size nQuery x max((nTrain / 100), 10),
1320 // otherwize user can pass own allocated trainIdx and distance with size nQuery x nMaxMatches
1321 // Matches doesn't sorted.
1322 void radiusMatchSingle(const oclMat &query, const oclMat &train,
1323 oclMat &trainIdx, oclMat &distance, oclMat &nMatches, float maxDistance,
1324 const oclMat &mask = oclMat());
1326 // Download trainIdx, nMatches and distance and convert it to vector with DMatch.
1327 // matches will be sorted in increasing order of distances.
1328 // compactResult is used when mask is not empty. If compactResult is false matches
1329 // vector will have the same size as queryDescriptors rows. If compactResult is true
1330 // matches vector will not contain matches for fully masked out query descriptors.
1331 static void radiusMatchDownload(const oclMat &trainIdx, const oclMat &distance, const oclMat &nMatches,
1332 std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
1333 // Convert trainIdx, nMatches and distance to vector with DMatch.
1334 static void radiusMatchConvert(const Mat &trainIdx, const Mat &distance, const Mat &nMatches,
1335 std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
1337 // Find best matches for each query descriptor which have distance less than maxDistance
1338 // in increasing order of distances).
1339 void radiusMatch(const oclMat &query, const oclMat &train,
1340 std::vector< std::vector<DMatch> > &matches, float maxDistance,
1341 const oclMat &mask = oclMat(), bool compactResult = false);
1343 // Find best matches for each query descriptor which have distance less than maxDistance.
1344 // If trainIdx is empty, then trainIdx and distance will be created with size nQuery x max((nQuery / 100), 10),
1345 // otherwize user can pass own allocated trainIdx and distance with size nQuery x nMaxMatches
1346 // Matches doesn't sorted.
1347 void radiusMatchCollection(const oclMat &query, oclMat &trainIdx, oclMat &imgIdx, oclMat &distance, oclMat &nMatches, float maxDistance,
1348 const std::vector<oclMat> &masks = std::vector<oclMat>());
1350 // Download trainIdx, imgIdx, nMatches and distance and convert it to vector with DMatch.
1351 // matches will be sorted in increasing order of distances.
1352 // compactResult is used when mask is not empty. If compactResult is false matches
1353 // vector will have the same size as queryDescriptors rows. If compactResult is true
1354 // matches vector will not contain matches for fully masked out query descriptors.
1355 static void radiusMatchDownload(const oclMat &trainIdx, const oclMat &imgIdx, const oclMat &distance, const oclMat &nMatches,
1356 std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
1357 // Convert trainIdx, nMatches and distance to vector with DMatch.
1358 static void radiusMatchConvert(const Mat &trainIdx, const Mat &imgIdx, const Mat &distance, const Mat &nMatches,
1359 std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
1361 // Find best matches from train collection for each query descriptor which have distance less than
1362 // maxDistance (in increasing order of distances).
1363 void radiusMatch(const oclMat &query, std::vector< std::vector<DMatch> > &matches, float maxDistance,
1364 const std::vector<oclMat> &masks = std::vector<oclMat>(), bool compactResult = false);
1369 std::vector<oclMat> trainDescCollection;
1372 template <class Distance>
1373 class CV_EXPORTS BruteForceMatcher_OCL;
1375 template <typename T>
1376 class CV_EXPORTS BruteForceMatcher_OCL< L1<T> > : public BruteForceMatcher_OCL_base
1379 explicit BruteForceMatcher_OCL() : BruteForceMatcher_OCL_base(L1Dist) {}
1380 explicit BruteForceMatcher_OCL(L1<T> /*d*/) : BruteForceMatcher_OCL_base(L1Dist) {}
1382 template <typename T>
1383 class CV_EXPORTS BruteForceMatcher_OCL< L2<T> > : public BruteForceMatcher_OCL_base
1386 explicit BruteForceMatcher_OCL() : BruteForceMatcher_OCL_base(L2Dist) {}
1387 explicit BruteForceMatcher_OCL(L2<T> /*d*/) : BruteForceMatcher_OCL_base(L2Dist) {}
1389 template <> class CV_EXPORTS BruteForceMatcher_OCL< Hamming > : public BruteForceMatcher_OCL_base
1392 explicit BruteForceMatcher_OCL() : BruteForceMatcher_OCL_base(HammingDist) {}
1393 explicit BruteForceMatcher_OCL(Hamming /*d*/) : BruteForceMatcher_OCL_base(HammingDist) {}
1396 class CV_EXPORTS BFMatcher_OCL : public BruteForceMatcher_OCL_base
1399 explicit BFMatcher_OCL(int norm = NORM_L2) : BruteForceMatcher_OCL_base(norm == NORM_L1 ? L1Dist : norm == NORM_L2 ? L2Dist : HammingDist) {}
1402 class CV_EXPORTS GoodFeaturesToTrackDetector_OCL
1405 explicit GoodFeaturesToTrackDetector_OCL(int maxCorners = 1000, double qualityLevel = 0.01, double minDistance = 0.0,
1406 int blockSize = 3, bool useHarrisDetector = false, double harrisK = 0.04);
1408 //! return 1 rows matrix with CV_32FC2 type
1409 void operator ()(const oclMat& image, oclMat& corners, const oclMat& mask = oclMat());
1410 //! download points of type Point2f to a vector. the vector's content will be erased
1411 void downloadPoints(const oclMat &points, std::vector<Point2f> &points_v);
1414 double qualityLevel;
1418 bool useHarrisDetector;
1420 void releaseMemory()
1425 minMaxbuf_.release();
1426 tmpCorners_.release();
1436 inline GoodFeaturesToTrackDetector_OCL::GoodFeaturesToTrackDetector_OCL(int maxCorners_, double qualityLevel_, double minDistance_,
1437 int blockSize_, bool useHarrisDetector_, double harrisK_)
1439 maxCorners = maxCorners_;
1440 qualityLevel = qualityLevel_;
1441 minDistance = minDistance_;
1442 blockSize = blockSize_;
1443 useHarrisDetector = useHarrisDetector_;
1447 /////////////////////////////// PyrLKOpticalFlow /////////////////////////////////////
1449 class CV_EXPORTS PyrLKOpticalFlow
1454 winSize = Size(21, 21);
1458 useInitialFlow = false;
1459 minEigThreshold = 1e-4f;
1460 getMinEigenVals = false;
1461 isDeviceArch11_ = false;
1464 void sparse(const oclMat &prevImg, const oclMat &nextImg, const oclMat &prevPts, oclMat &nextPts,
1465 oclMat &status, oclMat *err = 0);
1467 void dense(const oclMat &prevImg, const oclMat &nextImg, oclMat &u, oclMat &v, oclMat *err = 0);
1473 bool useInitialFlow;
1474 float minEigThreshold;
1475 bool getMinEigenVals;
1477 void releaseMemory()
1479 dx_calcBuf_.release();
1480 dy_calcBuf_.release();
1490 void calcSharrDeriv(const oclMat &src, oclMat &dx, oclMat &dy);
1492 void buildImagePyramid(const oclMat &img0, std::vector<oclMat> &pyr, bool withBorder);
1497 std::vector<oclMat> prevPyr_;
1498 std::vector<oclMat> nextPyr_;
1506 bool isDeviceArch11_;
1509 class CV_EXPORTS FarnebackOpticalFlow
1512 FarnebackOpticalFlow();
1523 void operator ()(const oclMat &frame0, const oclMat &frame1, oclMat &flowx, oclMat &flowy);
1525 void releaseMemory();
1528 void prepareGaussian(
1529 int n, double sigma, float *g, float *xg, float *xxg,
1530 double &ig11, double &ig03, double &ig33, double &ig55);
1532 void setPolynomialExpansionConsts(int n, double sigma);
1534 void updateFlow_boxFilter(
1535 const oclMat& R0, const oclMat& R1, oclMat& flowx, oclMat &flowy,
1536 oclMat& M, oclMat &bufM, int blockSize, bool updateMatrices);
1538 void updateFlow_gaussianBlur(
1539 const oclMat& R0, const oclMat& R1, oclMat& flowx, oclMat& flowy,
1540 oclMat& M, oclMat &bufM, int blockSize, bool updateMatrices);
1543 oclMat pyrLevel_[2], M_, bufM_, R_[2], blurredFrame_[2];
1544 std::vector<oclMat> pyramid0_, pyramid1_;
1547 //////////////// build warping maps ////////////////////
1548 //! builds plane warping maps
1549 CV_EXPORTS void buildWarpPlaneMaps(Size src_size, Rect dst_roi, const Mat &K, const Mat &R, const Mat &T, float scale, oclMat &map_x, oclMat &map_y);
1550 //! builds cylindrical warping maps
1551 CV_EXPORTS void buildWarpCylindricalMaps(Size src_size, Rect dst_roi, const Mat &K, const Mat &R, float scale, oclMat &map_x, oclMat &map_y);
1552 //! builds spherical warping maps
1553 CV_EXPORTS void buildWarpSphericalMaps(Size src_size, Rect dst_roi, const Mat &K, const Mat &R, float scale, oclMat &map_x, oclMat &map_y);
1554 //! builds Affine warping maps
1555 CV_EXPORTS void buildWarpAffineMaps(const Mat &M, bool inverse, Size dsize, oclMat &xmap, oclMat &ymap);
1557 //! builds Perspective warping maps
1558 CV_EXPORTS void buildWarpPerspectiveMaps(const Mat &M, bool inverse, Size dsize, oclMat &xmap, oclMat &ymap);
1560 ///////////////////////////////////// interpolate frames //////////////////////////////////////////////
1561 //! Interpolate frames (images) using provided optical flow (displacement field).
1562 //! frame0 - frame 0 (32-bit floating point images, single channel)
1563 //! frame1 - frame 1 (the same type and size)
1564 //! fu - forward horizontal displacement
1565 //! fv - forward vertical displacement
1566 //! bu - backward horizontal displacement
1567 //! bv - backward vertical displacement
1568 //! pos - new frame position
1569 //! newFrame - new frame
1570 //! buf - temporary buffer, will have width x 6*height size, CV_32FC1 type and contain 6 oclMat;
1571 //! occlusion masks 0, occlusion masks 1,
1572 //! interpolated forward flow 0, interpolated forward flow 1,
1573 //! interpolated backward flow 0, interpolated backward flow 1
1575 CV_EXPORTS void interpolateFrames(const oclMat &frame0, const oclMat &frame1,
1576 const oclMat &fu, const oclMat &fv,
1577 const oclMat &bu, const oclMat &bv,
1578 float pos, oclMat &newFrame, oclMat &buf);
1580 //! computes moments of the rasterized shape or a vector of points
1581 CV_EXPORTS Moments ocl_moments(InputArray _array, bool binaryImage);
1583 class CV_EXPORTS StereoBM_OCL
1586 enum { BASIC_PRESET = 0, PREFILTER_XSOBEL = 1 };
1588 enum { DEFAULT_NDISP = 64, DEFAULT_WINSZ = 19 };
1590 //! the default constructor
1592 //! the full constructor taking the camera-specific preset, number of disparities and the SAD window size. ndisparities must be multiple of 8.
1593 StereoBM_OCL(int preset, int ndisparities = DEFAULT_NDISP, int winSize = DEFAULT_WINSZ);
1595 //! the stereo correspondence operator. Finds the disparity for the specified rectified stereo pair
1596 //! Output disparity has CV_8U type.
1597 void operator() ( const oclMat &left, const oclMat &right, oclMat &disparity);
1599 //! Some heuristics that tries to estmate
1600 // if current GPU will be faster then CPU in this algorithm.
1601 // It queries current active device.
1602 static bool checkIfGpuCallReasonable();
1608 // If avergeTexThreshold == 0 => post procesing is disabled
1609 // If avergeTexThreshold != 0 then disparity is set 0 in each point (x,y) where for left image
1610 // SumOfHorizontalGradiensInWindow(x, y, winSize) < (winSize * winSize) * avergeTexThreshold
1611 // i.e. input left image is low textured.
1612 float avergeTexThreshold;
1614 oclMat minSSD, leBuf, riBuf;
1617 class CV_EXPORTS StereoBeliefPropagation
1620 enum { DEFAULT_NDISP = 64 };
1621 enum { DEFAULT_ITERS = 5 };
1622 enum { DEFAULT_LEVELS = 5 };
1623 static void estimateRecommendedParams(int width, int height, int &ndisp, int &iters, int &levels);
1624 explicit StereoBeliefPropagation(int ndisp = DEFAULT_NDISP,
1625 int iters = DEFAULT_ITERS,
1626 int levels = DEFAULT_LEVELS,
1627 int msg_type = CV_16S);
1628 StereoBeliefPropagation(int ndisp, int iters, int levels,
1629 float max_data_term, float data_weight,
1630 float max_disc_term, float disc_single_jump,
1631 int msg_type = CV_32F);
1632 void operator()(const oclMat &left, const oclMat &right, oclMat &disparity);
1633 void operator()(const oclMat &data, oclMat &disparity);
1637 float max_data_term;
1639 float max_disc_term;
1640 float disc_single_jump;
1643 oclMat u, d, l, r, u2, d2, l2, r2;
1644 std::vector<oclMat> datas;
1648 class CV_EXPORTS StereoConstantSpaceBP
1651 enum { DEFAULT_NDISP = 128 };
1652 enum { DEFAULT_ITERS = 8 };
1653 enum { DEFAULT_LEVELS = 4 };
1654 enum { DEFAULT_NR_PLANE = 4 };
1655 static void estimateRecommendedParams(int width, int height, int &ndisp, int &iters, int &levels, int &nr_plane);
1656 explicit StereoConstantSpaceBP(
1657 int ndisp = DEFAULT_NDISP,
1658 int iters = DEFAULT_ITERS,
1659 int levels = DEFAULT_LEVELS,
1660 int nr_plane = DEFAULT_NR_PLANE,
1661 int msg_type = CV_32F);
1662 StereoConstantSpaceBP(int ndisp, int iters, int levels, int nr_plane,
1663 float max_data_term, float data_weight, float max_disc_term, float disc_single_jump,
1664 int min_disp_th = 0,
1665 int msg_type = CV_32F);
1666 void operator()(const oclMat &left, const oclMat &right, oclMat &disparity);
1671 float max_data_term;
1673 float max_disc_term;
1674 float disc_single_jump;
1677 bool use_local_init_data_cost;
1679 oclMat u[2], d[2], l[2], r[2];
1680 oclMat disp_selected_pyr[2];
1682 oclMat data_cost_selected;
1687 // Implementation of the Zach, Pock and Bischof Dual TV-L1 Optical Flow method
1690 // [1] C. Zach, T. Pock and H. Bischof, "A Duality Based Approach for Realtime TV-L1 Optical Flow".
1691 // [2] Javier Sanchez, Enric Meinhardt-Llopis and Gabriele Facciolo. "TV-L1 Optical Flow Estimation".
1692 class CV_EXPORTS OpticalFlowDual_TVL1_OCL
1695 OpticalFlowDual_TVL1_OCL();
1697 void operator ()(const oclMat& I0, const oclMat& I1, oclMat& flowx, oclMat& flowy);
1699 void collectGarbage();
1702 * Time step of the numerical scheme.
1707 * Weight parameter for the data term, attachment parameter.
1708 * This is the most relevant parameter, which determines the smoothness of the output.
1709 * The smaller this parameter is, the smoother the solutions we obtain.
1710 * It depends on the range of motions of the images, so its value should be adapted to each image sequence.
1715 * Weight parameter for (u - v)^2, tightness parameter.
1716 * It serves as a link between the attachment and the regularization terms.
1717 * In theory, it should have a small value in order to maintain both parts in correspondence.
1718 * The method is stable for a large range of values of this parameter.
1723 * Number of scales used to create the pyramid of images.
1728 * Number of warpings per scale.
1729 * Represents the number of times that I1(x+u0) and grad( I1(x+u0) ) are computed per scale.
1730 * This is a parameter that assures the stability of the method.
1731 * It also affects the running time, so it is a compromise between speed and accuracy.
1736 * Stopping criterion threshold used in the numerical scheme, which is a trade-off between precision and running time.
1737 * A small value will yield more accurate solutions at the expense of a slower convergence.
1742 * Stopping criterion iterations number used in the numerical scheme.
1746 bool useInitialFlow;
1749 void procOneScale(const oclMat& I0, const oclMat& I1, oclMat& u1, oclMat& u2);
1751 std::vector<oclMat> I0s;
1752 std::vector<oclMat> I1s;
1753 std::vector<oclMat> u1s;
1754 std::vector<oclMat> u2s;
1774 // current supported sorting methods
1777 SORT_BITONIC, // only support power-of-2 buffer size
1778 SORT_SELECTION, // cannot sort duplicate keys
1780 SORT_RADIX // only support signed int/float keys(CV_32S/CV_32F)
1782 //! Returns the sorted result of all the elements in input based on equivalent keys.
1784 // The element unit in the values to be sorted is determined from the data type,
1785 // i.e., a CV_32FC2 input {a1a2, b1b2} will be considered as two elements, regardless its
1786 // matrix dimension.
1787 // both keys and values will be sorted inplace
1788 // Key needs to be single channel oclMat.
1792 // keys = {2, 3, 1} (CV_8UC1)
1793 // values = {10,5, 4,3, 6,2} (CV_8UC2)
1794 // sortByKey(keys, values, SORT_SELECTION, false);
1796 // keys = {1, 2, 3} (CV_8UC1)
1797 // values = {6,2, 10,5, 4,3} (CV_8UC2)
1798 void CV_EXPORTS sortByKey(oclMat& keys, oclMat& values, int method, bool isGreaterThan = false);
1799 /*!Base class for MOG and MOG2!*/
1800 class CV_EXPORTS BackgroundSubtractor
1803 //! the virtual destructor
1804 virtual ~BackgroundSubtractor();
1805 //! the update operator that takes the next video frame and returns the current foreground mask as 8-bit binary image.
1806 virtual void operator()(const oclMat& image, oclMat& fgmask, float learningRate);
1808 //! computes a background image
1809 virtual void getBackgroundImage(oclMat& backgroundImage) const = 0;
1812 Gaussian Mixture-based Backbround/Foreground Segmentation Algorithm
1814 The class implements the following algorithm:
1815 "An improved adaptive background mixture model for real-time tracking with shadow detection"
1816 P. KadewTraKuPong and R. Bowden,
1817 Proc. 2nd European Workshp on Advanced Video-Based Surveillance Systems, 2001."
1818 http://personal.ee.surrey.ac.uk/Personal/R.Bowden/publications/avbs01/avbs01.pdf
1820 class CV_EXPORTS MOG: public cv::ocl::BackgroundSubtractor
1823 //! the default constructor
1824 MOG(int nmixtures = -1);
1826 //! re-initiaization method
1827 void initialize(Size frameSize, int frameType);
1829 //! the update operator
1830 void operator()(const oclMat& frame, oclMat& fgmask, float learningRate = 0.f);
1832 //! computes a background image which are the mean of all background gaussians
1833 void getBackgroundImage(oclMat& backgroundImage) const;
1835 //! releases all inner buffers
1840 float backgroundRatio;
1857 The class implements the following algorithm:
1858 "Improved adaptive Gausian mixture model for background subtraction"
1860 International Conference Pattern Recognition, UK, August, 2004.
1861 http://www.zoranz.net/Publications/zivkovic2004ICPR.pdf
1863 class CV_EXPORTS MOG2: public cv::ocl::BackgroundSubtractor
1866 //! the default constructor
1867 MOG2(int nmixtures = -1);
1869 //! re-initiaization method
1870 void initialize(Size frameSize, int frameType);
1872 //! the update operator
1873 void operator()(const oclMat& frame, oclMat& fgmask, float learningRate = -1.0f);
1875 //! computes a background image which are the mean of all background gaussians
1876 void getBackgroundImage(oclMat& backgroundImage) const;
1878 //! releases all inner buffers
1882 // you should call initialize after parameters changes
1886 //! here it is the maximum allowed number of mixture components.
1887 //! Actual number is determined dynamically per pixel
1889 // threshold on the squared Mahalanobis distance to decide if it is well described
1890 // by the background model or not. Related to Cthr from the paper.
1891 // This does not influence the update of the background. A typical value could be 4 sigma
1892 // and that is varThreshold=4*4=16; Corresponds to Tb in the paper.
1894 /////////////////////////
1895 // less important parameters - things you might change but be carefull
1896 ////////////////////////
1898 float backgroundRatio;
1899 // corresponds to fTB=1-cf from the paper
1900 // TB - threshold when the component becomes significant enough to be included into
1901 // the background model. It is the TB=1-cf from the paper. So I use cf=0.1 => TB=0.
1902 // For alpha=0.001 it means that the mode should exist for approximately 105 frames before
1903 // it is considered foreground
1904 // float noiseSigma;
1905 float varThresholdGen;
1907 //correspondts to Tg - threshold on the squared Mahalan. dist. to decide
1908 //when a sample is close to the existing components. If it is not close
1909 //to any a new component will be generated. I use 3 sigma => Tg=3*3=9.
1910 //Smaller Tg leads to more generated components and higher Tg might make
1911 //lead to small number of components but they can grow too large
1916 //initial variance for the newly generated components.
1917 //It will will influence the speed of adaptation. A good guess should be made.
1918 //A simple way is to estimate the typical standard deviation from the images.
1919 //I used here 10 as a reasonable value
1920 // min and max can be used to further control the variance
1921 float fCT; //CT - complexity reduction prior
1922 //this is related to the number of samples needed to accept that a component
1923 //actually exists. We use CT=0.05 of all the samples. By setting CT=0 you get
1924 //the standard Stauffer&Grimson algorithm (maybe not exact but very similar)
1926 //shadow detection parameters
1927 bool bShadowDetection; //default 1 - do shadow detection
1928 unsigned char nShadowDetection; //do shadow detection - insert this value as the detection result - 127 default value
1930 // Tau - shadow threshold. The shadow is detected if the pixel is darker
1931 //version of the background. Tau is a threshold on how much darker the shadow can be.
1932 //Tau= 0.5 means that if pixel is more than 2 times darker then it is not shadow
1933 //See: Prati,Mikic,Trivedi,Cucchiarra,"Detecting Moving Shadows...",IEEE PAMI,2003.
1946 oclMat bgmodelUsedModes_; //keep track of number of modes per pixel
1949 /*!***************Kalman Filter*************!*/
1950 class CV_EXPORTS KalmanFilter
1954 //! the full constructor taking the dimensionality of the state, of the measurement and of the control vector
1955 KalmanFilter(int dynamParams, int measureParams, int controlParams=0, int type=CV_32F);
1956 //! re-initializes Kalman filter. The previous content is destroyed.
1957 void init(int dynamParams, int measureParams, int controlParams=0, int type=CV_32F);
1959 const oclMat& predict(const oclMat& control=oclMat());
1960 const oclMat& correct(const oclMat& measurement);
1962 oclMat statePre; //!< predicted state (x'(k)): x(k)=A*x(k-1)+B*u(k)
1963 oclMat statePost; //!< corrected state (x(k)): x(k)=x'(k)+K(k)*(z(k)-H*x'(k))
1964 oclMat transitionMatrix; //!< state transition matrix (A)
1965 oclMat controlMatrix; //!< control matrix (B) (not used if there is no control)
1966 oclMat measurementMatrix; //!< measurement matrix (H)
1967 oclMat processNoiseCov; //!< process noise covariance matrix (Q)
1968 oclMat measurementNoiseCov;//!< measurement noise covariance matrix (R)
1969 oclMat errorCovPre; //!< priori error estimate covariance matrix (P'(k)): P'(k)=A*P(k-1)*At + Q)*/
1970 oclMat gain; //!< Kalman gain matrix (K(k)): K(k)=P'(k)*Ht*inv(H*P'(k)*Ht+R)
1971 oclMat errorCovPost; //!< posteriori error estimate covariance matrix (P(k)): P(k)=(I-K(k)*H)*P'(k)
1980 static inline size_t divUp(size_t total, size_t grain)
1982 return (total + grain - 1) / grain;
1985 /*!***************K Nearest Neighbour*************!*/
1986 class CV_EXPORTS KNearestNeighbour: public CvKNearest
1989 KNearestNeighbour();
1990 ~KNearestNeighbour();
1992 bool train(const Mat& trainData, Mat& labels, Mat& sampleIdx = Mat().setTo(Scalar::all(0)),
1993 bool isRegression = false, int max_k = 32, bool updateBase = false);
1997 void find_nearest(const oclMat& samples, int k, oclMat& lables);
2002 /*!*************** SVM *************!*/
2003 class CV_EXPORTS CvSVM_OCL : public CvSVM
2008 CvSVM_OCL(const cv::Mat& trainData, const cv::Mat& responses,
2009 const cv::Mat& varIdx=cv::Mat(), const cv::Mat& sampleIdx=cv::Mat(),
2010 CvSVMParams params=CvSVMParams());
2011 CV_WRAP float predict( const int row_index, Mat& src, bool returnDFVal=false ) const;
2012 CV_WRAP void predict( cv::InputArray samples, cv::OutputArray results ) const;
2013 CV_WRAP float predict( const cv::Mat& sample, bool returnDFVal=false ) const;
2014 float predict( const CvMat* samples, CV_OUT CvMat* results ) const;
2017 float predict( const int row_index, int row_len, Mat& src, bool returnDFVal=false ) const;
2018 void create_kernel();
2019 void create_solver();
2021 /*!*************** END *************!*/
2024 #if defined _MSC_VER && _MSC_VER >= 1200
2025 # pragma warning( push)
2026 # pragma warning( disable: 4267)
2028 #include "opencv2/ocl/matrix_operations.hpp"
2029 #if defined _MSC_VER && _MSC_VER >= 1200
2030 # pragma warning( pop)
2033 #endif /* __OPENCV_OCL_HPP__ */