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 // these classes contain OpenCL runtime information
101 int _id; // reserved, don't use it
103 DeviceType deviceType;
104 std::string deviceProfile;
105 std::string deviceVersion;
106 std::string deviceName;
107 std::string deviceVendor;
109 std::string deviceDriverVersion;
110 std::string deviceExtensions;
112 size_t maxWorkGroupSize;
113 std::vector<size_t> maxWorkItemSizes;
115 size_t localMemorySize;
117 int deviceVersionMajor;
118 int deviceVersionMinor;
120 bool haveDoubleSupport;
121 bool isUnifiedMemory; // 1 means integrated GPU, otherwise this value is 0
123 std::string compilationExtraOptions;
125 const PlatformInfo* platform;
129 //////////////////////////////// Initialization & Info ////////////////////////
133 int _id; // reserved, don't use it
135 std::string platformProfile;
136 std::string platformVersion;
137 std::string platformName;
138 std::string platformVendor;
139 std::string platformExtensons;
141 int platformVersionMajor;
142 int platformVersionMinor;
144 std::vector<const DeviceInfo*> devices;
149 //////////////////////////////// Initialization & Info ////////////////////////
150 typedef std::vector<const PlatformInfo*> PlatformsInfo;
152 CV_EXPORTS int getOpenCLPlatforms(PlatformsInfo& platforms);
154 typedef std::vector<const DeviceInfo*> DevicesInfo;
156 CV_EXPORTS int getOpenCLDevices(DevicesInfo& devices, int deviceType = CVCL_DEVICE_TYPE_GPU,
157 const PlatformInfo* platform = NULL);
159 // set device you want to use
160 CV_EXPORTS void setDevice(const DeviceInfo* info);
164 FEATURE_CL_DOUBLE = 1,
165 FEATURE_CL_UNIFIED_MEM,
169 // Represents OpenCL context, interface
170 class CV_EXPORTS Context
176 static Context *getContext();
178 bool supportsFeature(FEATURE_TYPE featureType) const;
179 const DeviceInfo& getDeviceInfo() const;
181 const void* getOpenCLContextPtr() const;
182 const void* getOpenCLCommandQueuePtr() const;
183 const void* getOpenCLDeviceIDPtr() const;
186 inline const void *getClContextPtr()
188 return Context::getContext()->getOpenCLContextPtr();
191 inline const void *getClCommandQueuePtr()
193 return Context::getContext()->getOpenCLCommandQueuePtr();
196 bool CV_EXPORTS supportsFeature(FEATURE_TYPE featureType);
198 void CV_EXPORTS finish();
200 //! Enable or disable OpenCL program binary caching onto local disk
201 // After a program (*.cl files in opencl/ folder) is built at runtime, we allow the
202 // compiled OpenCL program to be cached to the path automatically as "path/*.clb"
203 // binary file, which will be reused when the OpenCV executable is started again.
205 // Caching mode is controlled by the following enums
207 // 1. the feature is by default enabled when OpenCV is built in release mode.
208 // 2. the CACHE_DEBUG / CACHE_RELEASE flags only effectively work with MSVC compiler;
209 // for GNU compilers, the function always treats the build as release mode (enabled by default).
212 CACHE_NONE = 0, // do not cache OpenCL binary
213 CACHE_DEBUG = 0x1 << 0, // cache OpenCL binary when built in debug mode (only work with MSVC)
214 CACHE_RELEASE = 0x1 << 1, // default behavior, only cache when built in release mode (only work with MSVC)
215 CACHE_ALL = CACHE_DEBUG | CACHE_RELEASE, // always cache opencl binary
217 CV_EXPORTS void setBinaryDiskCache(int mode = CACHE_RELEASE, cv::String path = "./");
219 //! set where binary cache to be saved to
220 CV_EXPORTS void setBinaryPath(const char *path);
222 class CV_EXPORTS oclMatExpr;
223 //////////////////////////////// oclMat ////////////////////////////////
224 class CV_EXPORTS oclMat
227 //! default constructor
229 //! constructs oclMatrix of the specified size and type (_type is CV_8UC1, CV_64FC3, CV_32SC(12) etc.)
230 oclMat(int rows, int cols, int type);
231 oclMat(Size size, int type);
232 //! constucts oclMatrix and fills it with the specified value _s.
233 oclMat(int rows, int cols, int type, const Scalar &s);
234 oclMat(Size size, int type, const Scalar &s);
236 oclMat(const oclMat &m);
238 //! constructor for oclMatrix headers pointing to user-allocated data
239 oclMat(int rows, int cols, int type, void *data, size_t step = Mat::AUTO_STEP);
240 oclMat(Size size, int type, void *data, size_t step = Mat::AUTO_STEP);
242 //! creates a matrix header for a part of the bigger matrix
243 oclMat(const oclMat &m, const Range &rowRange, const Range &colRange);
244 oclMat(const oclMat &m, const Rect &roi);
246 //! builds oclMat from Mat. Perfom blocking upload to device.
247 explicit oclMat (const Mat &m);
249 //! destructor - calls release()
252 //! assignment operators
253 oclMat &operator = (const oclMat &m);
254 //! assignment operator. Perfom blocking upload to device.
255 oclMat &operator = (const Mat &m);
256 oclMat &operator = (const oclMatExpr& expr);
258 //! pefroms blocking upload data to oclMat.
259 void upload(const cv::Mat &m);
262 //! downloads data from device to host memory. Blocking calls.
263 operator Mat() const;
264 void download(cv::Mat &m) const;
266 //! convert to _InputArray
267 operator _InputArray();
269 //! convert to _OutputArray
270 operator _OutputArray();
272 //! returns a new oclMatrix header for the specified row
273 oclMat row(int y) const;
274 //! returns a new oclMatrix header for the specified column
275 oclMat col(int x) const;
276 //! ... for the specified row span
277 oclMat rowRange(int startrow, int endrow) const;
278 oclMat rowRange(const Range &r) const;
279 //! ... for the specified column span
280 oclMat colRange(int startcol, int endcol) const;
281 oclMat colRange(const Range &r) const;
283 //! returns deep copy of the oclMatrix, i.e. the data is copied
284 oclMat clone() const;
286 //! copies those oclMatrix elements to "m" that are marked with non-zero mask elements.
287 // It calls m.create(this->size(), this->type()).
288 // It supports any data type
289 void copyTo( oclMat &m, const oclMat &mask = oclMat()) const;
291 //! converts oclMatrix to another datatype with optional scalng. See cvConvertScale.
292 //It supports 8UC1 8UC4 32SC1 32SC4 32FC1 32FC4
293 void convertTo( oclMat &m, int rtype, double alpha = 1, double beta = 0 ) const;
295 void assignTo( oclMat &m, int type = -1 ) const;
297 //! sets every oclMatrix element to s
298 //It supports 8UC1 8UC4 32SC1 32SC4 32FC1 32FC4
299 oclMat& operator = (const Scalar &s);
300 //! sets some of the oclMatrix elements to s, according to the mask
301 //It supports 8UC1 8UC4 32SC1 32SC4 32FC1 32FC4
302 oclMat& setTo(const Scalar &s, const oclMat &mask = oclMat());
303 //! creates alternative oclMatrix header for the same data, with different
304 // number of channels and/or different number of rows. see cvReshape.
305 oclMat reshape(int cn, int rows = 0) const;
307 //! allocates new oclMatrix data unless the oclMatrix already has specified size and type.
308 // previous data is unreferenced if needed.
309 void create(int rows, int cols, int type);
310 void create(Size size, int type);
312 //! allocates new oclMatrix with specified device memory type.
313 void createEx(int rows, int cols, int type,
314 DevMemRW rw_type, DevMemType mem_type, void* hptr = 0);
315 void createEx(Size size, int type, DevMemRW rw_type,
316 DevMemType mem_type, void* hptr = 0);
318 //! decreases reference counter;
319 // deallocate the data when reference counter reaches 0.
322 //! swaps with other smart pointer
323 void swap(oclMat &mat);
325 //! locates oclMatrix header within a parent oclMatrix. See below
326 void locateROI( Size &wholeSize, Point &ofs ) const;
327 //! moves/resizes the current oclMatrix ROI inside the parent oclMatrix.
328 oclMat& adjustROI( int dtop, int dbottom, int dleft, int dright );
329 //! extracts a rectangular sub-oclMatrix
330 // (this is a generalized form of row, rowRange etc.)
331 oclMat operator()( Range rowRange, Range colRange ) const;
332 oclMat operator()( const Rect &roi ) const;
334 oclMat& operator+=( const oclMat& m );
335 oclMat& operator-=( const oclMat& m );
336 oclMat& operator*=( const oclMat& m );
337 oclMat& operator/=( const oclMat& m );
339 //! returns true if the oclMatrix data is continuous
340 // (i.e. when there are no gaps between successive rows).
341 // similar to CV_IS_oclMat_CONT(cvoclMat->type)
342 bool isContinuous() const;
343 //! returns element size in bytes,
344 // similar to CV_ELEM_SIZE(cvMat->type)
345 size_t elemSize() const;
346 //! returns the size of element channel in bytes.
347 size_t elemSize1() const;
348 //! returns element type, similar to CV_MAT_TYPE(cvMat->type)
350 //! returns element type, i.e. 8UC3 returns 8UC4 because in ocl
351 //! 3 channels element actually use 4 channel space
353 //! returns element type, similar to CV_MAT_DEPTH(cvMat->type)
355 //! returns element type, similar to CV_MAT_CN(cvMat->type)
356 int channels() const;
357 //! returns element type, return 4 for 3 channels element,
358 //!becuase 3 channels element actually use 4 channel space
359 int oclchannels() const;
360 //! returns step/elemSize1()
361 size_t step1() const;
362 //! returns oclMatrix size:
363 // width == number of columns, height == number of rows
365 //! returns true if oclMatrix data is NULL
368 //! returns pointer to y-th row
369 uchar* ptr(int y = 0);
370 const uchar *ptr(int y = 0) const;
372 //! template version of the above method
373 template<typename _Tp> _Tp *ptr(int y = 0);
374 template<typename _Tp> const _Tp *ptr(int y = 0) const;
376 //! matrix transposition
379 /*! includes several bit-fields:
380 - the magic signature
386 //! the number of rows and columns
388 //! a distance between successive rows in bytes; includes the gap if any
390 //! pointer to the data(OCL memory object)
393 //! pointer to the reference counter;
394 // when oclMatrix points to user-allocated data, the pointer is NULL
397 //! helper fields used in locateROI and adjustROI
398 //datastart and dataend are not used in current version
402 //! OpenCL context associated with the oclMat object.
403 Context *clCxt; // TODO clCtx
404 //add offset for handle ROI, calculated in byte
406 //add wholerows and wholecols for the whole matrix, datastart and dataend are no longer used
411 // convert InputArray/OutputArray to oclMat references
412 CV_EXPORTS oclMat& getOclMatRef(InputArray src);
413 CV_EXPORTS oclMat& getOclMatRef(OutputArray src);
415 ///////////////////// mat split and merge /////////////////////////////////
416 //! Compose a multi-channel array from several single-channel arrays
418 CV_EXPORTS void merge(const oclMat *src, size_t n, oclMat &dst);
419 CV_EXPORTS void merge(const std::vector<oclMat> &src, oclMat &dst);
421 //! Divides multi-channel array into several single-channel arrays
423 CV_EXPORTS void split(const oclMat &src, oclMat *dst);
424 CV_EXPORTS void split(const oclMat &src, std::vector<oclMat> &dst);
426 ////////////////////////////// Arithmetics ///////////////////////////////////
428 //! adds one matrix to another with scale (dst = src1 * alpha + src2 * beta + gama)
429 // supports all data types
430 CV_EXPORTS void addWeighted(const oclMat &src1, double alpha, const oclMat &src2, double beta, double gama, oclMat &dst);
432 //! adds one matrix to another (dst = src1 + src2)
433 // supports all data types
434 CV_EXPORTS void add(const oclMat &src1, const oclMat &src2, oclMat &dst, const oclMat &mask = oclMat());
435 //! adds scalar to a matrix (dst = src1 + s)
436 // supports all data types
437 CV_EXPORTS void add(const oclMat &src1, const Scalar &s, oclMat &dst, const oclMat &mask = oclMat());
439 //! subtracts one matrix from another (dst = src1 - src2)
440 // supports all data types
441 CV_EXPORTS void subtract(const oclMat &src1, const oclMat &src2, oclMat &dst, const oclMat &mask = oclMat());
442 //! subtracts scalar from a matrix (dst = src1 - s)
443 // supports all data types
444 CV_EXPORTS void subtract(const oclMat &src1, const Scalar &s, oclMat &dst, const oclMat &mask = oclMat());
446 //! computes element-wise product of the two arrays (dst = src1 * scale * src2)
447 // supports all data types
448 CV_EXPORTS void multiply(const oclMat &src1, const oclMat &src2, oclMat &dst, double scale = 1);
449 //! multiplies matrix to a number (dst = scalar * src)
450 // supports all data types
451 CV_EXPORTS void multiply(double scalar, const oclMat &src, oclMat &dst);
453 //! computes element-wise quotient of the two arrays (dst = src1 * scale / src2)
454 // supports all data types
455 CV_EXPORTS void divide(const oclMat &src1, const oclMat &src2, oclMat &dst, double scale = 1);
456 //! computes element-wise quotient of the two arrays (dst = scale / src)
457 // supports all data types
458 CV_EXPORTS void divide(double scale, const oclMat &src1, oclMat &dst);
460 //! compares elements of two arrays (dst = src1 <cmpop> src2)
461 // supports all data types
462 CV_EXPORTS void compare(const oclMat &src1, const oclMat &src2, oclMat &dst, int cmpop);
464 //! transposes the matrix
465 // supports all data types
466 CV_EXPORTS void transpose(const oclMat &src, oclMat &dst);
468 //! computes element-wise absolute difference of two arrays (dst = abs(src1 - src2))
469 // supports all data types
470 CV_EXPORTS void absdiff(const oclMat &src1, const oclMat &src2, oclMat &dst);
471 //! computes element-wise absolute difference of array and scalar (dst = abs(src1 - s))
472 // supports all data types
473 CV_EXPORTS void absdiff(const oclMat &src1, const Scalar &s, oclMat &dst);
475 //! computes mean value and standard deviation of all or selected array elements
476 // supports all data types
477 CV_EXPORTS void meanStdDev(const oclMat &mtx, Scalar &mean, Scalar &stddev);
479 //! computes norm of array
480 // supports NORM_INF, NORM_L1, NORM_L2
481 // supports all data types
482 CV_EXPORTS double norm(const oclMat &src1, int normType = NORM_L2);
484 //! computes norm of the difference between two arrays
485 // supports NORM_INF, NORM_L1, NORM_L2
486 // supports all data types
487 CV_EXPORTS double norm(const oclMat &src1, const oclMat &src2, int normType = NORM_L2);
489 //! reverses the order of the rows, columns or both in a matrix
490 // supports all types
491 CV_EXPORTS void flip(const oclMat &src, oclMat &dst, int flipCode);
493 //! computes sum of array elements
495 CV_EXPORTS Scalar sum(const oclMat &m);
496 CV_EXPORTS Scalar absSum(const oclMat &m);
497 CV_EXPORTS Scalar sqrSum(const oclMat &m);
499 //! finds global minimum and maximum array elements and returns their values
500 // support all C1 types
501 CV_EXPORTS void minMax(const oclMat &src, double *minVal, double *maxVal = 0, const oclMat &mask = oclMat());
503 //! finds global minimum and maximum array elements and returns their values with locations
504 // support all C1 types
505 CV_EXPORTS void minMaxLoc(const oclMat &src, double *minVal, double *maxVal = 0, Point *minLoc = 0, Point *maxLoc = 0,
506 const oclMat &mask = oclMat());
508 //! counts non-zero array elements
510 CV_EXPORTS int countNonZero(const oclMat &src);
512 //! transforms 8-bit unsigned integers using lookup table: dst(i)=lut(src(i))
513 // destination array will have the depth type as lut and the same channels number as source
514 //It supports 8UC1 8UC4 only
515 CV_EXPORTS void LUT(const oclMat &src, const oclMat &lut, oclMat &dst);
517 //! only 8UC1 and 256 bins is supported now
518 CV_EXPORTS void calcHist(const oclMat &mat_src, oclMat &mat_hist);
519 //! only 8UC1 and 256 bins is supported now
520 CV_EXPORTS void equalizeHist(const oclMat &mat_src, oclMat &mat_dst);
522 //! only 8UC1 is supported now
523 CV_EXPORTS Ptr<cv::CLAHE> createCLAHE(double clipLimit = 40.0, Size tileGridSize = Size(8, 8));
526 // supports 8UC1 8UC4
527 CV_EXPORTS void bilateralFilter(const oclMat& src, oclMat& dst, int d, double sigmaColor, double sigmaSpace, int borderType=BORDER_DEFAULT);
529 //! Applies an adaptive bilateral filter to the input image
530 // This is not truly a bilateral filter. Instead of using user provided fixed parameters,
531 // the function calculates a constant at each window based on local standard deviation,
532 // and use this constant to do filtering.
533 // supports 8UC1, 8UC3
534 CV_EXPORTS void adaptiveBilateralFilter(const oclMat& src, oclMat& dst, Size ksize, double sigmaSpace, Point anchor = Point(-1, -1), int borderType=BORDER_DEFAULT);
536 //! computes exponent of each matrix element (dst = e**src)
537 // supports only CV_32FC1, CV_64FC1 type
538 CV_EXPORTS void exp(const oclMat &src, oclMat &dst);
540 //! computes natural logarithm of absolute value of each matrix element: dst = log(abs(src))
541 // supports only CV_32FC1, CV_64FC1 type
542 CV_EXPORTS void log(const oclMat &src, oclMat &dst);
544 //! computes magnitude of each (x(i), y(i)) vector
545 // supports only CV_32F, CV_64F type
546 CV_EXPORTS void magnitude(const oclMat &x, const oclMat &y, 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);
568 //! calculates per-element bit-wise disjunction of two arrays
569 // supports all types
570 CV_EXPORTS void bitwise_or(const oclMat &src1, const oclMat &src2, oclMat &dst, const oclMat &mask = oclMat());
571 CV_EXPORTS void bitwise_or(const oclMat &src1, const Scalar &s, oclMat &dst, const oclMat &mask = oclMat());
573 //! calculates per-element bit-wise conjunction of two arrays
574 // supports all types
575 CV_EXPORTS void bitwise_and(const oclMat &src1, const oclMat &src2, oclMat &dst, const oclMat &mask = oclMat());
576 CV_EXPORTS void bitwise_and(const oclMat &src1, const Scalar &s, oclMat &dst, const oclMat &mask = oclMat());
578 //! calculates per-element bit-wise "exclusive or" operation
579 // supports all types
580 CV_EXPORTS void bitwise_xor(const oclMat &src1, const oclMat &src2, oclMat &dst, const oclMat &mask = oclMat());
581 CV_EXPORTS void bitwise_xor(const oclMat &src1, const Scalar &s, oclMat &dst, const oclMat &mask = oclMat());
583 //! Logical operators
584 CV_EXPORTS oclMat operator ~ (const oclMat &);
585 CV_EXPORTS oclMat operator | (const oclMat &, const oclMat &);
586 CV_EXPORTS oclMat operator & (const oclMat &, const oclMat &);
587 CV_EXPORTS oclMat operator ^ (const oclMat &, const oclMat &);
590 //! Mathematics operators
591 CV_EXPORTS oclMatExpr operator + (const oclMat &src1, const oclMat &src2);
592 CV_EXPORTS oclMatExpr operator - (const oclMat &src1, const oclMat &src2);
593 CV_EXPORTS oclMatExpr operator * (const oclMat &src1, const oclMat &src2);
594 CV_EXPORTS oclMatExpr operator / (const oclMat &src1, const oclMat &src2);
596 struct CV_EXPORTS ConvolveBuf
600 Size user_block_size;
603 oclMat image_spect, templ_spect, result_spect;
604 oclMat image_block, templ_block, result_data;
606 void create(Size image_size, Size templ_size);
607 static Size estimateBlockSize(Size result_size, Size templ_size);
610 //! computes convolution of two images, may use discrete Fourier transform
611 // support only CV_32FC1 type
612 CV_EXPORTS void convolve(const oclMat &image, const oclMat &temp1, oclMat &result, bool ccorr = false);
613 CV_EXPORTS void convolve(const oclMat &image, const oclMat &temp1, oclMat &result, bool ccorr, ConvolveBuf& buf);
615 //! Performs a per-element multiplication of two Fourier spectrums.
616 //! Only full (not packed) CV_32FC2 complex spectrums in the interleaved format are supported for now.
617 //! support only CV_32FC2 type
618 CV_EXPORTS void mulSpectrums(const oclMat &a, const oclMat &b, oclMat &c, int flags, float scale, bool conjB = false);
620 CV_EXPORTS void cvtColor(const oclMat &src, oclMat &dst, int code, int dcn = 0);
622 //! initializes a scaled identity matrix
623 CV_EXPORTS void setIdentity(oclMat& src, const Scalar & val = Scalar(1));
625 //////////////////////////////// Filter Engine ////////////////////////////////
628 The Base Class for 1D or Row-wise Filters
630 This is the base class for linear or non-linear filters that process 1D data.
631 In particular, such filters are used for the "horizontal" filtering parts in separable filters.
633 class CV_EXPORTS BaseRowFilter_GPU
636 BaseRowFilter_GPU(int ksize_, int anchor_, int bordertype_) : ksize(ksize_), anchor(anchor_), bordertype(bordertype_) {}
637 virtual ~BaseRowFilter_GPU() {}
638 virtual void operator()(const oclMat &src, oclMat &dst) = 0;
639 int ksize, anchor, bordertype;
643 The Base Class for Column-wise Filters
645 This is the base class for linear or non-linear filters that process columns of 2D arrays.
646 Such filters are used for the "vertical" filtering parts in separable filters.
648 class CV_EXPORTS BaseColumnFilter_GPU
651 BaseColumnFilter_GPU(int ksize_, int anchor_, int bordertype_) : ksize(ksize_), anchor(anchor_), bordertype(bordertype_) {}
652 virtual ~BaseColumnFilter_GPU() {}
653 virtual void operator()(const oclMat &src, oclMat &dst) = 0;
654 int ksize, anchor, bordertype;
658 The Base Class for Non-Separable 2D Filters.
660 This is the base class for linear or non-linear 2D filters.
662 class CV_EXPORTS BaseFilter_GPU
665 BaseFilter_GPU(const Size &ksize_, const Point &anchor_, const int &borderType_)
666 : ksize(ksize_), anchor(anchor_), borderType(borderType_) {}
667 virtual ~BaseFilter_GPU() {}
668 virtual void operator()(const oclMat &src, oclMat &dst) = 0;
675 The Base Class for Filter Engine.
677 The class can be used to apply an arbitrary filtering operation to an image.
678 It contains all the necessary intermediate buffers.
680 class CV_EXPORTS FilterEngine_GPU
683 virtual ~FilterEngine_GPU() {}
685 virtual void apply(const oclMat &src, oclMat &dst, Rect roi = Rect(0, 0, -1, -1)) = 0;
688 //! returns the non-separable filter engine with the specified filter
689 CV_EXPORTS Ptr<FilterEngine_GPU> createFilter2D_GPU(const Ptr<BaseFilter_GPU> filter2D);
691 //! returns the primitive row filter with the specified kernel
692 CV_EXPORTS Ptr<BaseRowFilter_GPU> getLinearRowFilter_GPU(int srcType, int bufType, const Mat &rowKernel,
693 int anchor = -1, int bordertype = BORDER_DEFAULT);
695 //! returns the primitive column filter with the specified kernel
696 CV_EXPORTS Ptr<BaseColumnFilter_GPU> getLinearColumnFilter_GPU(int bufType, int dstType, const Mat &columnKernel,
697 int anchor = -1, int bordertype = BORDER_DEFAULT, double delta = 0.0);
699 //! returns the separable linear filter engine
700 CV_EXPORTS Ptr<FilterEngine_GPU> createSeparableLinearFilter_GPU(int srcType, int dstType, const Mat &rowKernel,
701 const Mat &columnKernel, const Point &anchor = Point(-1, -1), double delta = 0.0, int bordertype = BORDER_DEFAULT);
703 //! returns the separable filter engine with the specified filters
704 CV_EXPORTS Ptr<FilterEngine_GPU> createSeparableFilter_GPU(const Ptr<BaseRowFilter_GPU> &rowFilter,
705 const Ptr<BaseColumnFilter_GPU> &columnFilter);
707 //! returns the Gaussian filter engine
708 CV_EXPORTS Ptr<FilterEngine_GPU> createGaussianFilter_GPU(int type, Size ksize, double sigma1, double sigma2 = 0, int bordertype = BORDER_DEFAULT);
710 //! returns filter engine for the generalized Sobel operator
711 CV_EXPORTS Ptr<FilterEngine_GPU> createDerivFilter_GPU( int srcType, int dstType, int dx, int dy, int ksize, int borderType = BORDER_DEFAULT );
713 //! applies Laplacian operator to the image
714 // supports only ksize = 1 and ksize = 3 8UC1 8UC4 32FC1 32FC4 data type
715 CV_EXPORTS void Laplacian(const oclMat &src, oclMat &dst, int ddepth, int ksize = 1, double scale = 1);
717 //! returns 2D box filter
718 // supports CV_8UC1 and CV_8UC4 source type, dst type must be the same as source type
719 CV_EXPORTS Ptr<BaseFilter_GPU> getBoxFilter_GPU(int srcType, int dstType,
720 const Size &ksize, Point anchor = Point(-1, -1), int borderType = BORDER_DEFAULT);
722 //! returns box filter engine
723 CV_EXPORTS Ptr<FilterEngine_GPU> createBoxFilter_GPU(int srcType, int dstType, const Size &ksize,
724 const Point &anchor = Point(-1, -1), int borderType = BORDER_DEFAULT);
726 //! returns 2D filter with the specified kernel
727 // supports CV_8UC1 and CV_8UC4 types
728 CV_EXPORTS Ptr<BaseFilter_GPU> getLinearFilter_GPU(int srcType, int dstType, const Mat &kernel, const Size &ksize,
729 const Point &anchor = Point(-1, -1), int borderType = BORDER_DEFAULT);
731 //! returns the non-separable linear filter engine
732 CV_EXPORTS Ptr<FilterEngine_GPU> createLinearFilter_GPU(int srcType, int dstType, const Mat &kernel,
733 const Point &anchor = Point(-1, -1), int borderType = BORDER_DEFAULT);
735 //! smooths the image using the 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,BORDER_WRAP
738 CV_EXPORTS void boxFilter(const oclMat &src, oclMat &dst, int ddepth, Size ksize,
739 Point anchor = Point(-1, -1), int borderType = BORDER_DEFAULT);
741 //! returns 2D morphological filter
742 //! only MORPH_ERODE and MORPH_DILATE are supported
743 // supports CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4 types
744 // kernel must have CV_8UC1 type, one rows and cols == ksize.width * ksize.height
745 CV_EXPORTS Ptr<BaseFilter_GPU> getMorphologyFilter_GPU(int op, int type, const Mat &kernel, const Size &ksize,
746 Point anchor = Point(-1, -1));
748 //! returns morphological filter engine. Only MORPH_ERODE and MORPH_DILATE are supported.
749 CV_EXPORTS Ptr<FilterEngine_GPU> createMorphologyFilter_GPU(int op, int type, const Mat &kernel,
750 const Point &anchor = Point(-1, -1), int iterations = 1);
752 //! a synonym for normalized box filter
753 // supports data type: CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4
754 // supports border type: BORDER_CONSTANT, BORDER_REPLICATE, BORDER_REFLECT,BORDER_REFLECT_101
755 static inline void blur(const oclMat &src, oclMat &dst, Size ksize, Point anchor = Point(-1, -1),
756 int borderType = BORDER_CONSTANT)
758 boxFilter(src, dst, -1, ksize, anchor, borderType);
761 //! applies non-separable 2D linear filter to the image
762 // Note, at the moment this function only works when anchor point is in the kernel center
763 // and kernel size supported is either 3x3 or 5x5; otherwise the function will fail to output valid result
764 CV_EXPORTS void filter2D(const oclMat &src, oclMat &dst, int ddepth, const Mat &kernel,
765 Point anchor = Point(-1, -1), int borderType = BORDER_DEFAULT);
767 //! applies separable 2D linear filter to the image
768 CV_EXPORTS void sepFilter2D(const oclMat &src, oclMat &dst, int ddepth, const Mat &kernelX, const Mat &kernelY,
769 Point anchor = Point(-1, -1), double delta = 0.0, int bordertype = BORDER_DEFAULT);
771 //! applies generalized Sobel operator to the image
772 // dst.type must equalize src.type
773 // supports data type: CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4
774 // supports border type: BORDER_CONSTANT, BORDER_REPLICATE, BORDER_REFLECT,BORDER_REFLECT_101
775 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);
777 //! applies the vertical or horizontal Scharr operator to the image
778 // dst.type must equalize src.type
779 // supports data type: CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4
780 // supports border type: BORDER_CONSTANT, BORDER_REPLICATE, BORDER_REFLECT,BORDER_REFLECT_101
781 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);
783 //! smooths the image using Gaussian filter.
784 // dst.type must equalize src.type
785 // supports data type: CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4
786 // supports border type: BORDER_CONSTANT, BORDER_REPLICATE, BORDER_REFLECT,BORDER_REFLECT_101
787 CV_EXPORTS void GaussianBlur(const oclMat &src, oclMat &dst, Size ksize, double sigma1, double sigma2 = 0, int bordertype = BORDER_DEFAULT);
789 //! erodes the image (applies the local minimum operator)
790 // supports data type: CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4
791 CV_EXPORTS void erode( const oclMat &src, oclMat &dst, const Mat &kernel, Point anchor = Point(-1, -1), int iterations = 1,
793 int borderType = BORDER_CONSTANT, const Scalar &borderValue = morphologyDefaultBorderValue());
796 //! dilates the image (applies the local maximum operator)
797 // supports data type: CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4
798 CV_EXPORTS void dilate( const oclMat &src, oclMat &dst, const Mat &kernel, Point anchor = Point(-1, -1), int iterations = 1,
800 int borderType = BORDER_CONSTANT, const Scalar &borderValue = morphologyDefaultBorderValue());
803 //! applies an advanced morphological operation to the image
804 CV_EXPORTS void morphologyEx( const oclMat &src, oclMat &dst, int op, const Mat &kernel, Point anchor = Point(-1, -1), int iterations = 1,
806 int borderType = BORDER_CONSTANT, const Scalar &borderValue = morphologyDefaultBorderValue());
809 ////////////////////////////// Image processing //////////////////////////////
810 //! Does mean shift filtering on GPU.
811 CV_EXPORTS void meanShiftFiltering(const oclMat &src, oclMat &dst, int sp, int sr,
812 TermCriteria criteria = TermCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 5, 1));
814 //! Does mean shift procedure on GPU.
815 CV_EXPORTS void meanShiftProc(const oclMat &src, oclMat &dstr, oclMat &dstsp, int sp, int sr,
816 TermCriteria criteria = TermCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 5, 1));
818 //! Does mean shift segmentation with elimiation of small regions.
819 CV_EXPORTS void meanShiftSegmentation(const oclMat &src, Mat &dst, int sp, int sr, int minsize,
820 TermCriteria criteria = TermCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 5, 1));
822 //! applies fixed threshold to the image.
823 // supports CV_8UC1 and CV_32FC1 data type
824 // supports threshold type: THRESH_BINARY, THRESH_BINARY_INV, THRESH_TRUNC, THRESH_TOZERO, THRESH_TOZERO_INV
825 CV_EXPORTS double threshold(const oclMat &src, oclMat &dst, double thresh, double maxVal, int type = THRESH_TRUNC);
827 //! resizes the image
828 // Supports INTER_NEAREST, INTER_LINEAR
829 // supports CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4 types
830 CV_EXPORTS void resize(const oclMat &src, oclMat &dst, Size dsize, double fx = 0, double fy = 0, int interpolation = INTER_LINEAR);
832 //! Applies a generic geometrical transformation to an image.
834 // Supports INTER_NEAREST, INTER_LINEAR.
836 // Map1 supports CV_16SC2, CV_32FC2 types.
838 // Src supports CV_8UC1, CV_8UC2, CV_8UC4.
840 CV_EXPORTS void remap(const oclMat &src, oclMat &dst, oclMat &map1, oclMat &map2, int interpolation, int bordertype, const Scalar &value = Scalar());
842 //! copies 2D array to a larger destination array and pads borders with user-specifiable constant
843 // supports CV_8UC1, CV_8UC4, CV_32SC1 types
844 CV_EXPORTS void copyMakeBorder(const oclMat &src, oclMat &dst, int top, int bottom, int left, int right, int boardtype, const Scalar &value = Scalar());
846 //! Smoothes image using median filter
847 // The source 1- or 4-channel image. When m is 3 or 5, the image depth should be CV 8U or CV 32F.
848 CV_EXPORTS void medianFilter(const oclMat &src, oclMat &dst, int m);
850 //! warps the image using affine transformation
851 // Supports INTER_NEAREST, INTER_LINEAR, INTER_CUBIC
852 // supports CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4 types
853 CV_EXPORTS void warpAffine(const oclMat &src, oclMat &dst, const Mat &M, Size dsize, int flags = INTER_LINEAR);
855 //! warps the image using perspective transformation
856 // Supports INTER_NEAREST, INTER_LINEAR, INTER_CUBIC
857 // supports CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4 types
858 CV_EXPORTS void warpPerspective(const oclMat &src, oclMat &dst, const Mat &M, Size dsize, int flags = INTER_LINEAR);
860 //! computes the integral image and integral for the squared image
861 // sum will have CV_32S type, sqsum - CV32F type
862 // supports only CV_8UC1 source type
863 CV_EXPORTS void integral(const oclMat &src, oclMat &sum, oclMat &sqsum);
864 CV_EXPORTS void integral(const oclMat &src, oclMat &sum);
865 CV_EXPORTS void cornerHarris(const oclMat &src, oclMat &dst, int blockSize, int ksize, double k, int bordertype = cv::BORDER_DEFAULT);
866 CV_EXPORTS void cornerHarris_dxdy(const oclMat &src, oclMat &dst, oclMat &Dx, oclMat &Dy,
867 int blockSize, int ksize, double k, int bordertype = cv::BORDER_DEFAULT);
868 CV_EXPORTS void cornerMinEigenVal(const oclMat &src, oclMat &dst, int blockSize, int ksize, int bordertype = cv::BORDER_DEFAULT);
869 CV_EXPORTS void cornerMinEigenVal_dxdy(const oclMat &src, oclMat &dst, oclMat &Dx, oclMat &Dy,
870 int blockSize, int ksize, int bordertype = cv::BORDER_DEFAULT);
873 /////////////////////////////////// ML ///////////////////////////////////////////
875 //! Compute closest centers for each lines in source and lable it after center's index
876 // supports CV_32FC1/CV_32FC2/CV_32FC4 data type
877 CV_EXPORTS void distanceToCenters(oclMat &dists, oclMat &labels, const oclMat &src, const oclMat ¢ers);
879 //!Does k-means procedure on GPU
880 // supports CV_32FC1/CV_32FC2/CV_32FC4 data type
881 CV_EXPORTS double kmeans(const oclMat &src, int K, oclMat &bestLabels,
882 TermCriteria criteria, int attemps, int flags, oclMat ¢ers);
885 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
886 ///////////////////////////////////////////CascadeClassifier//////////////////////////////////////////////////////////////////
887 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
888 class CV_EXPORTS OclCascadeClassifier : public cv::CascadeClassifier
891 void detectMultiScale(oclMat &image, CV_OUT std::vector<cv::Rect>& faces,
892 double scaleFactor = 1.1, int minNeighbors = 3, int flags = 0,
893 Size minSize = Size(), Size maxSize = Size());
896 /////////////////////////////// Pyramid /////////////////////////////////////
897 CV_EXPORTS void pyrDown(const oclMat &src, oclMat &dst);
899 //! upsamples the source image and then smoothes it
900 CV_EXPORTS void pyrUp(const oclMat &src, oclMat &dst);
902 //! performs linear blending of two images
903 //! to avoid accuracy errors sum of weigths shouldn't be very close to zero
904 // supports only CV_8UC1 source type
905 CV_EXPORTS void blendLinear(const oclMat &img1, const oclMat &img2, const oclMat &weights1, const oclMat &weights2, oclMat &result);
907 //! computes vertical sum, supports only CV_32FC1 images
908 CV_EXPORTS void columnSum(const oclMat &src, oclMat &sum);
910 ///////////////////////////////////////// match_template /////////////////////////////////////////////////////////////
911 struct CV_EXPORTS MatchTemplateBuf
913 Size user_block_size;
914 oclMat imagef, templf;
915 std::vector<oclMat> images;
916 std::vector<oclMat> image_sums;
917 std::vector<oclMat> image_sqsums;
920 //! computes the proximity map for the raster template and the image where the template is searched for
921 // Supports TM_SQDIFF, TM_SQDIFF_NORMED, TM_CCORR, TM_CCORR_NORMED, TM_CCOEFF, TM_CCOEFF_NORMED for type 8UC1 and 8UC4
922 // Supports TM_SQDIFF, TM_CCORR for type 32FC1 and 32FC4
923 CV_EXPORTS void matchTemplate(const oclMat &image, const oclMat &templ, oclMat &result, int method);
925 //! computes the proximity map for the raster template and the image where the template is searched for
926 // Supports TM_SQDIFF, TM_SQDIFF_NORMED, TM_CCORR, TM_CCORR_NORMED, TM_CCOEFF, TM_CCOEFF_NORMED for type 8UC1 and 8UC4
927 // Supports TM_SQDIFF, TM_CCORR for type 32FC1 and 32FC4
928 CV_EXPORTS void matchTemplate(const oclMat &image, const oclMat &templ, oclMat &result, int method, MatchTemplateBuf &buf);
932 ///////////////////////////////////////////// Canny /////////////////////////////////////////////
933 struct CV_EXPORTS CannyBuf;
935 //! compute edges of the input image using Canny operator
936 // Support CV_8UC1 only
937 CV_EXPORTS void Canny(const oclMat &image, oclMat &edges, double low_thresh, double high_thresh, int apperture_size = 3, bool L2gradient = false);
938 CV_EXPORTS void Canny(const oclMat &image, CannyBuf &buf, oclMat &edges, double low_thresh, double high_thresh, int apperture_size = 3, bool L2gradient = false);
939 CV_EXPORTS void Canny(const oclMat &dx, const oclMat &dy, oclMat &edges, double low_thresh, double high_thresh, bool L2gradient = false);
940 CV_EXPORTS void Canny(const oclMat &dx, const oclMat &dy, CannyBuf &buf, oclMat &edges, double low_thresh, double high_thresh, bool L2gradient = false);
942 struct CV_EXPORTS CannyBuf
944 CannyBuf() : counter(NULL) {}
949 explicit CannyBuf(const Size &image_size, int apperture_size = 3) : counter(NULL)
951 create(image_size, apperture_size);
953 CannyBuf(const oclMat &dx_, const oclMat &dy_);
954 void create(const Size &image_size, int apperture_size = 3);
958 oclMat dx_buf, dy_buf;
959 oclMat magBuf, mapBuf;
960 oclMat trackBuf1, trackBuf2;
962 Ptr<FilterEngine_GPU> filterDX, filterDY;
965 ///////////////////////////////////////// Hough Transform /////////////////////////////////////////
967 struct HoughCirclesBuf
976 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);
977 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);
978 CV_EXPORTS void HoughCirclesDownload(const oclMat& d_circles, OutputArray h_circles);
981 ///////////////////////////////////////// clAmdFft related /////////////////////////////////////////
982 //! Performs a forward or inverse discrete Fourier transform (1D or 2D) of floating point matrix.
983 //! Param dft_size is the size of DFT transform.
985 //! For complex-to-real transform it is assumed that the source matrix is packed in CLFFT's format.
986 // support src type of CV32FC1, CV32FC2
987 // support flags: DFT_INVERSE, DFT_REAL_OUTPUT, DFT_COMPLEX_OUTPUT, DFT_ROWS
988 // dft_size is the size of original input, which is used for transformation from complex to real.
989 // dft_size must be powers of 2, 3 and 5
990 // real to complex dft requires at least v1.8 clAmdFft
991 // real to complex dft output is not the same with cpu version
992 // real to complex and complex to real does not support DFT_ROWS
993 CV_EXPORTS void dft(const oclMat &src, oclMat &dst, Size dft_size = Size(), int flags = 0);
995 //! implements generalized matrix product algorithm GEMM from BLAS
996 // The functionality requires clAmdBlas library
997 // only support type CV_32FC1
998 // flag GEMM_3_T is not supported
999 CV_EXPORTS void gemm(const oclMat &src1, const oclMat &src2, double alpha,
1000 const oclMat &src3, double beta, oclMat &dst, int flags = 0);
1002 //////////////// HOG (Histogram-of-Oriented-Gradients) Descriptor and Object Detector //////////////
1004 struct CV_EXPORTS HOGDescriptor
1008 enum { DEFAULT_WIN_SIGMA = -1 };
1010 enum { DEFAULT_NLEVELS = 64 };
1012 enum { DESCR_FORMAT_ROW_BY_ROW, DESCR_FORMAT_COL_BY_COL };
1016 HOGDescriptor(Size win_size = Size(64, 128), Size block_size = Size(16, 16),
1018 Size block_stride = Size(8, 8), Size cell_size = Size(8, 8),
1020 int nbins = 9, double win_sigma = DEFAULT_WIN_SIGMA,
1022 double threshold_L2hys = 0.2, bool gamma_correction = true,
1024 int nlevels = DEFAULT_NLEVELS);
1028 size_t getDescriptorSize() const;
1030 size_t getBlockHistogramSize() const;
1034 void setSVMDetector(const std::vector<float> &detector);
1038 static std::vector<float> getDefaultPeopleDetector();
1040 static std::vector<float> getPeopleDetector48x96();
1042 static std::vector<float> getPeopleDetector64x128();
1046 void detect(const oclMat &img, std::vector<Point> &found_locations,
1048 double hit_threshold = 0, Size win_stride = Size(),
1050 Size padding = Size());
1054 void detectMultiScale(const oclMat &img, std::vector<Rect> &found_locations,
1056 double hit_threshold = 0, Size win_stride = Size(),
1058 Size padding = Size(), double scale0 = 1.05,
1060 int group_threshold = 2);
1064 void getDescriptors(const oclMat &img, Size win_stride,
1066 oclMat &descriptors,
1068 int descr_format = DESCR_FORMAT_COL_BY_COL);
1084 double threshold_L2hys;
1086 bool gamma_correction;
1094 // initialize buffers; only need to do once in case of multiscale detection
1096 void init_buffer(const oclMat &img, Size win_stride);
1100 void computeBlockHistograms(const oclMat &img);
1102 void computeGradient(const oclMat &img, oclMat &grad, oclMat &qangle);
1106 double getWinSigma() const;
1108 bool checkDetectorSize() const;
1112 static int numPartsWithin(int size, int part_size, int stride);
1114 static Size numPartsWithin(Size size, Size part_size, Size stride);
1118 // Coefficients of the separating plane
1126 // Results of the last classification step
1134 // Results of the last histogram evaluation step
1140 // Gradients conputation results
1142 oclMat grad, qangle;
1152 // effect size of input image (might be different from original size after scaling)
1159 ////////////////////////feature2d_ocl/////////////////
1160 /****************************************************************************************\
1162 \****************************************************************************************/
1163 template<typename T>
1164 struct CV_EXPORTS Accumulator
1168 template<> struct Accumulator<unsigned char>
1172 template<> struct Accumulator<unsigned short>
1176 template<> struct Accumulator<char>
1180 template<> struct Accumulator<short>
1186 * Manhattan distance (city block distance) functor
1189 struct CV_EXPORTS L1
1191 enum { normType = NORM_L1 };
1192 typedef T ValueType;
1193 typedef typename Accumulator<T>::Type ResultType;
1195 ResultType operator()( const T *a, const T *b, int size ) const
1197 return normL1<ValueType, ResultType>(a, b, size);
1202 * Euclidean distance functor
1205 struct CV_EXPORTS L2
1207 enum { normType = NORM_L2 };
1208 typedef T ValueType;
1209 typedef typename Accumulator<T>::Type ResultType;
1211 ResultType operator()( const T *a, const T *b, int size ) const
1213 return (ResultType)std::sqrt((double)normL2Sqr<ValueType, ResultType>(a, b, size));
1218 * Hamming distance functor - counts the bit differences between two strings - useful for the Brief descriptor
1219 * bit count of A exclusive XOR'ed with B
1221 struct CV_EXPORTS Hamming
1223 enum { normType = NORM_HAMMING };
1224 typedef unsigned char ValueType;
1225 typedef int ResultType;
1227 /** this will count the bits in a ^ b
1229 ResultType operator()( const unsigned char *a, const unsigned char *b, int size ) const
1231 return normHamming(a, b, size);
1235 ////////////////////////////////// BruteForceMatcher //////////////////////////////////
1237 class CV_EXPORTS BruteForceMatcher_OCL_base
1240 enum DistType {L1Dist = 0, L2Dist, HammingDist};
1241 explicit BruteForceMatcher_OCL_base(DistType distType = L2Dist);
1243 // Add descriptors to train descriptor collection
1244 void add(const std::vector<oclMat> &descCollection);
1246 // Get train descriptors collection
1247 const std::vector<oclMat> &getTrainDescriptors() const;
1249 // Clear train descriptors collection
1252 // Return true if there are not train descriptors in collection
1255 // Return true if the matcher supports mask in match methods
1256 bool isMaskSupported() const;
1258 // Find one best match for each query descriptor
1259 void matchSingle(const oclMat &query, const oclMat &train,
1260 oclMat &trainIdx, oclMat &distance,
1261 const oclMat &mask = oclMat());
1263 // Download trainIdx and distance and convert it to CPU vector with DMatch
1264 static void matchDownload(const oclMat &trainIdx, const oclMat &distance, std::vector<DMatch> &matches);
1265 // Convert trainIdx and distance to vector with DMatch
1266 static void matchConvert(const Mat &trainIdx, const Mat &distance, std::vector<DMatch> &matches);
1268 // Find one best match for each query descriptor
1269 void match(const oclMat &query, const oclMat &train, std::vector<DMatch> &matches, const oclMat &mask = oclMat());
1271 // Make gpu collection of trains and masks in suitable format for matchCollection function
1272 void makeGpuCollection(oclMat &trainCollection, oclMat &maskCollection, const std::vector<oclMat> &masks = std::vector<oclMat>());
1274 // Find one best match from train collection for each query descriptor
1275 void matchCollection(const oclMat &query, const oclMat &trainCollection,
1276 oclMat &trainIdx, oclMat &imgIdx, oclMat &distance,
1277 const oclMat &masks = oclMat());
1279 // Download trainIdx, imgIdx and distance and convert it to vector with DMatch
1280 static void matchDownload(const oclMat &trainIdx, const oclMat &imgIdx, const oclMat &distance, std::vector<DMatch> &matches);
1281 // Convert trainIdx, imgIdx and distance to vector with DMatch
1282 static void matchConvert(const Mat &trainIdx, const Mat &imgIdx, const Mat &distance, std::vector<DMatch> &matches);
1284 // Find one best match from train collection for each query descriptor.
1285 void match(const oclMat &query, std::vector<DMatch> &matches, const std::vector<oclMat> &masks = std::vector<oclMat>());
1287 // Find k best matches for each query descriptor (in increasing order of distances)
1288 void knnMatchSingle(const oclMat &query, const oclMat &train,
1289 oclMat &trainIdx, oclMat &distance, oclMat &allDist, int k,
1290 const oclMat &mask = oclMat());
1292 // Download trainIdx and distance and convert it to vector with DMatch
1293 // compactResult is used when mask is not empty. If compactResult is false matches
1294 // vector will have the same size as queryDescriptors rows. If compactResult is true
1295 // matches vector will not contain matches for fully masked out query descriptors.
1296 static void knnMatchDownload(const oclMat &trainIdx, const oclMat &distance,
1297 std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
1298 // Convert trainIdx and distance to vector with DMatch
1299 static void knnMatchConvert(const Mat &trainIdx, const Mat &distance,
1300 std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
1302 // Find k best matches for each query descriptor (in increasing order of distances).
1303 // compactResult is used when mask is not empty. If compactResult is false matches
1304 // vector will have the same size as queryDescriptors rows. If compactResult is true
1305 // matches vector will not contain matches for fully masked out query descriptors.
1306 void knnMatch(const oclMat &query, const oclMat &train,
1307 std::vector< std::vector<DMatch> > &matches, int k, const oclMat &mask = oclMat(),
1308 bool compactResult = false);
1310 // Find k best matches from train collection for each query descriptor (in increasing order of distances)
1311 void knnMatch2Collection(const oclMat &query, const oclMat &trainCollection,
1312 oclMat &trainIdx, oclMat &imgIdx, oclMat &distance,
1313 const oclMat &maskCollection = oclMat());
1315 // Download trainIdx and distance and convert it to vector with DMatch
1316 // compactResult is used when mask is not empty. If compactResult is false matches
1317 // vector will have the same size as queryDescriptors rows. If compactResult is true
1318 // matches vector will not contain matches for fully masked out query descriptors.
1319 static void knnMatch2Download(const oclMat &trainIdx, const oclMat &imgIdx, const oclMat &distance,
1320 std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
1321 // Convert trainIdx and distance to vector with DMatch
1322 static void knnMatch2Convert(const Mat &trainIdx, const Mat &imgIdx, const Mat &distance,
1323 std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
1325 // Find k best matches for each query descriptor (in increasing order of distances).
1326 // compactResult is used when mask is not empty. If compactResult is false matches
1327 // vector will have the same size as queryDescriptors rows. If compactResult is true
1328 // matches vector will not contain matches for fully masked out query descriptors.
1329 void knnMatch(const oclMat &query, std::vector< std::vector<DMatch> > &matches, int k,
1330 const std::vector<oclMat> &masks = std::vector<oclMat>(), bool compactResult = false);
1332 // Find best matches for each query descriptor which have distance less than maxDistance.
1333 // nMatches.at<int>(0, queryIdx) will contain matches count for queryIdx.
1334 // carefully nMatches can be greater than trainIdx.cols - it means that matcher didn't find all matches,
1335 // because it didn't have enough memory.
1336 // If trainIdx is empty, then trainIdx and distance will be created with size nQuery x max((nTrain / 100), 10),
1337 // otherwize user can pass own allocated trainIdx and distance with size nQuery x nMaxMatches
1338 // Matches doesn't sorted.
1339 void radiusMatchSingle(const oclMat &query, const oclMat &train,
1340 oclMat &trainIdx, oclMat &distance, oclMat &nMatches, float maxDistance,
1341 const oclMat &mask = oclMat());
1343 // Download trainIdx, nMatches and distance and convert it to vector with DMatch.
1344 // matches will be sorted in increasing order of distances.
1345 // compactResult is used when mask is not empty. If compactResult is false matches
1346 // vector will have the same size as queryDescriptors rows. If compactResult is true
1347 // matches vector will not contain matches for fully masked out query descriptors.
1348 static void radiusMatchDownload(const oclMat &trainIdx, const oclMat &distance, const oclMat &nMatches,
1349 std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
1350 // Convert trainIdx, nMatches and distance to vector with DMatch.
1351 static void radiusMatchConvert(const Mat &trainIdx, const Mat &distance, const Mat &nMatches,
1352 std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
1354 // Find best matches for each query descriptor which have distance less than maxDistance
1355 // in increasing order of distances).
1356 void radiusMatch(const oclMat &query, const oclMat &train,
1357 std::vector< std::vector<DMatch> > &matches, float maxDistance,
1358 const oclMat &mask = oclMat(), bool compactResult = false);
1360 // Find best matches for each query descriptor which have distance less than maxDistance.
1361 // If trainIdx is empty, then trainIdx and distance will be created with size nQuery x max((nQuery / 100), 10),
1362 // otherwize user can pass own allocated trainIdx and distance with size nQuery x nMaxMatches
1363 // Matches doesn't sorted.
1364 void radiusMatchCollection(const oclMat &query, oclMat &trainIdx, oclMat &imgIdx, oclMat &distance, oclMat &nMatches, float maxDistance,
1365 const std::vector<oclMat> &masks = std::vector<oclMat>());
1367 // Download trainIdx, imgIdx, nMatches and distance and convert it to vector with DMatch.
1368 // matches will be sorted in increasing order of distances.
1369 // compactResult is used when mask is not empty. If compactResult is false matches
1370 // vector will have the same size as queryDescriptors rows. If compactResult is true
1371 // matches vector will not contain matches for fully masked out query descriptors.
1372 static void radiusMatchDownload(const oclMat &trainIdx, const oclMat &imgIdx, const oclMat &distance, const oclMat &nMatches,
1373 std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
1374 // Convert trainIdx, nMatches and distance to vector with DMatch.
1375 static void radiusMatchConvert(const Mat &trainIdx, const Mat &imgIdx, const Mat &distance, const Mat &nMatches,
1376 std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
1378 // Find best matches from train collection for each query descriptor which have distance less than
1379 // maxDistance (in increasing order of distances).
1380 void radiusMatch(const oclMat &query, std::vector< std::vector<DMatch> > &matches, float maxDistance,
1381 const std::vector<oclMat> &masks = std::vector<oclMat>(), bool compactResult = false);
1386 std::vector<oclMat> trainDescCollection;
1389 template <class Distance>
1390 class CV_EXPORTS BruteForceMatcher_OCL;
1392 template <typename T>
1393 class CV_EXPORTS BruteForceMatcher_OCL< L1<T> > : public BruteForceMatcher_OCL_base
1396 explicit BruteForceMatcher_OCL() : BruteForceMatcher_OCL_base(L1Dist) {}
1397 explicit BruteForceMatcher_OCL(L1<T> /*d*/) : BruteForceMatcher_OCL_base(L1Dist) {}
1399 template <typename T>
1400 class CV_EXPORTS BruteForceMatcher_OCL< L2<T> > : public BruteForceMatcher_OCL_base
1403 explicit BruteForceMatcher_OCL() : BruteForceMatcher_OCL_base(L2Dist) {}
1404 explicit BruteForceMatcher_OCL(L2<T> /*d*/) : BruteForceMatcher_OCL_base(L2Dist) {}
1406 template <> class CV_EXPORTS BruteForceMatcher_OCL< Hamming > : public BruteForceMatcher_OCL_base
1409 explicit BruteForceMatcher_OCL() : BruteForceMatcher_OCL_base(HammingDist) {}
1410 explicit BruteForceMatcher_OCL(Hamming /*d*/) : BruteForceMatcher_OCL_base(HammingDist) {}
1413 class CV_EXPORTS BFMatcher_OCL : public BruteForceMatcher_OCL_base
1416 explicit BFMatcher_OCL(int norm = NORM_L2) : BruteForceMatcher_OCL_base(norm == NORM_L1 ? L1Dist : norm == NORM_L2 ? L2Dist : HammingDist) {}
1419 class CV_EXPORTS GoodFeaturesToTrackDetector_OCL
1422 explicit GoodFeaturesToTrackDetector_OCL(int maxCorners = 1000, double qualityLevel = 0.01, double minDistance = 0.0,
1423 int blockSize = 3, bool useHarrisDetector = false, double harrisK = 0.04);
1425 //! return 1 rows matrix with CV_32FC2 type
1426 void operator ()(const oclMat& image, oclMat& corners, const oclMat& mask = oclMat());
1427 //! download points of type Point2f to a vector. the vector's content will be erased
1428 void downloadPoints(const oclMat &points, std::vector<Point2f> &points_v);
1431 double qualityLevel;
1435 bool useHarrisDetector;
1437 void releaseMemory()
1442 minMaxbuf_.release();
1443 tmpCorners_.release();
1453 inline GoodFeaturesToTrackDetector_OCL::GoodFeaturesToTrackDetector_OCL(int maxCorners_, double qualityLevel_, double minDistance_,
1454 int blockSize_, bool useHarrisDetector_, double harrisK_)
1456 maxCorners = maxCorners_;
1457 qualityLevel = qualityLevel_;
1458 minDistance = minDistance_;
1459 blockSize = blockSize_;
1460 useHarrisDetector = useHarrisDetector_;
1464 /////////////////////////////// PyrLKOpticalFlow /////////////////////////////////////
1466 class CV_EXPORTS PyrLKOpticalFlow
1471 winSize = Size(21, 21);
1475 useInitialFlow = false;
1476 minEigThreshold = 1e-4f;
1477 getMinEigenVals = false;
1478 isDeviceArch11_ = false;
1481 void sparse(const oclMat &prevImg, const oclMat &nextImg, const oclMat &prevPts, oclMat &nextPts,
1482 oclMat &status, oclMat *err = 0);
1484 void dense(const oclMat &prevImg, const oclMat &nextImg, oclMat &u, oclMat &v, oclMat *err = 0);
1490 bool useInitialFlow;
1491 float minEigThreshold;
1492 bool getMinEigenVals;
1494 void releaseMemory()
1496 dx_calcBuf_.release();
1497 dy_calcBuf_.release();
1507 void calcSharrDeriv(const oclMat &src, oclMat &dx, oclMat &dy);
1509 void buildImagePyramid(const oclMat &img0, std::vector<oclMat> &pyr, bool withBorder);
1514 std::vector<oclMat> prevPyr_;
1515 std::vector<oclMat> nextPyr_;
1523 bool isDeviceArch11_;
1526 class CV_EXPORTS FarnebackOpticalFlow
1529 FarnebackOpticalFlow();
1540 void operator ()(const oclMat &frame0, const oclMat &frame1, oclMat &flowx, oclMat &flowy);
1542 void releaseMemory();
1545 void prepareGaussian(
1546 int n, double sigma, float *g, float *xg, float *xxg,
1547 double &ig11, double &ig03, double &ig33, double &ig55);
1549 void setPolynomialExpansionConsts(int n, double sigma);
1551 void updateFlow_boxFilter(
1552 const oclMat& R0, const oclMat& R1, oclMat& flowx, oclMat &flowy,
1553 oclMat& M, oclMat &bufM, int blockSize, bool updateMatrices);
1555 void updateFlow_gaussianBlur(
1556 const oclMat& R0, const oclMat& R1, oclMat& flowx, oclMat& flowy,
1557 oclMat& M, oclMat &bufM, int blockSize, bool updateMatrices);
1560 oclMat pyrLevel_[2], M_, bufM_, R_[2], blurredFrame_[2];
1561 std::vector<oclMat> pyramid0_, pyramid1_;
1564 //////////////// build warping maps ////////////////////
1565 //! builds plane warping maps
1566 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);
1567 //! builds cylindrical warping maps
1568 CV_EXPORTS void buildWarpCylindricalMaps(Size src_size, Rect dst_roi, const Mat &K, const Mat &R, float scale, oclMat &map_x, oclMat &map_y);
1569 //! builds spherical warping maps
1570 CV_EXPORTS void buildWarpSphericalMaps(Size src_size, Rect dst_roi, const Mat &K, const Mat &R, float scale, oclMat &map_x, oclMat &map_y);
1571 //! builds Affine warping maps
1572 CV_EXPORTS void buildWarpAffineMaps(const Mat &M, bool inverse, Size dsize, oclMat &xmap, oclMat &ymap);
1574 //! builds Perspective warping maps
1575 CV_EXPORTS void buildWarpPerspectiveMaps(const Mat &M, bool inverse, Size dsize, oclMat &xmap, oclMat &ymap);
1577 ///////////////////////////////////// interpolate frames //////////////////////////////////////////////
1578 //! Interpolate frames (images) using provided optical flow (displacement field).
1579 //! frame0 - frame 0 (32-bit floating point images, single channel)
1580 //! frame1 - frame 1 (the same type and size)
1581 //! fu - forward horizontal displacement
1582 //! fv - forward vertical displacement
1583 //! bu - backward horizontal displacement
1584 //! bv - backward vertical displacement
1585 //! pos - new frame position
1586 //! newFrame - new frame
1587 //! buf - temporary buffer, will have width x 6*height size, CV_32FC1 type and contain 6 oclMat;
1588 //! occlusion masks 0, occlusion masks 1,
1589 //! interpolated forward flow 0, interpolated forward flow 1,
1590 //! interpolated backward flow 0, interpolated backward flow 1
1592 CV_EXPORTS void interpolateFrames(const oclMat &frame0, const oclMat &frame1,
1593 const oclMat &fu, const oclMat &fv,
1594 const oclMat &bu, const oclMat &bv,
1595 float pos, oclMat &newFrame, oclMat &buf);
1597 //! computes moments of the rasterized shape or a vector of points
1598 CV_EXPORTS Moments ocl_moments(InputArray _array, bool binaryImage);
1600 class CV_EXPORTS StereoBM_OCL
1603 enum { BASIC_PRESET = 0, PREFILTER_XSOBEL = 1 };
1605 enum { DEFAULT_NDISP = 64, DEFAULT_WINSZ = 19 };
1607 //! the default constructor
1609 //! the full constructor taking the camera-specific preset, number of disparities and the SAD window size. ndisparities must be multiple of 8.
1610 StereoBM_OCL(int preset, int ndisparities = DEFAULT_NDISP, int winSize = DEFAULT_WINSZ);
1612 //! the stereo correspondence operator. Finds the disparity for the specified rectified stereo pair
1613 //! Output disparity has CV_8U type.
1614 void operator() ( const oclMat &left, const oclMat &right, oclMat &disparity);
1616 //! Some heuristics that tries to estmate
1617 // if current GPU will be faster then CPU in this algorithm.
1618 // It queries current active device.
1619 static bool checkIfGpuCallReasonable();
1625 // If avergeTexThreshold == 0 => post procesing is disabled
1626 // If avergeTexThreshold != 0 then disparity is set 0 in each point (x,y) where for left image
1627 // SumOfHorizontalGradiensInWindow(x, y, winSize) < (winSize * winSize) * avergeTexThreshold
1628 // i.e. input left image is low textured.
1629 float avergeTexThreshold;
1631 oclMat minSSD, leBuf, riBuf;
1634 class CV_EXPORTS StereoBeliefPropagation
1637 enum { DEFAULT_NDISP = 64 };
1638 enum { DEFAULT_ITERS = 5 };
1639 enum { DEFAULT_LEVELS = 5 };
1640 static void estimateRecommendedParams(int width, int height, int &ndisp, int &iters, int &levels);
1641 explicit StereoBeliefPropagation(int ndisp = DEFAULT_NDISP,
1642 int iters = DEFAULT_ITERS,
1643 int levels = DEFAULT_LEVELS,
1644 int msg_type = CV_16S);
1645 StereoBeliefPropagation(int ndisp, int iters, int levels,
1646 float max_data_term, float data_weight,
1647 float max_disc_term, float disc_single_jump,
1648 int msg_type = CV_32F);
1649 void operator()(const oclMat &left, const oclMat &right, oclMat &disparity);
1650 void operator()(const oclMat &data, oclMat &disparity);
1654 float max_data_term;
1656 float max_disc_term;
1657 float disc_single_jump;
1660 oclMat u, d, l, r, u2, d2, l2, r2;
1661 std::vector<oclMat> datas;
1665 class CV_EXPORTS StereoConstantSpaceBP
1668 enum { DEFAULT_NDISP = 128 };
1669 enum { DEFAULT_ITERS = 8 };
1670 enum { DEFAULT_LEVELS = 4 };
1671 enum { DEFAULT_NR_PLANE = 4 };
1672 static void estimateRecommendedParams(int width, int height, int &ndisp, int &iters, int &levels, int &nr_plane);
1673 explicit StereoConstantSpaceBP(
1674 int ndisp = DEFAULT_NDISP,
1675 int iters = DEFAULT_ITERS,
1676 int levels = DEFAULT_LEVELS,
1677 int nr_plane = DEFAULT_NR_PLANE,
1678 int msg_type = CV_32F);
1679 StereoConstantSpaceBP(int ndisp, int iters, int levels, int nr_plane,
1680 float max_data_term, float data_weight, float max_disc_term, float disc_single_jump,
1681 int min_disp_th = 0,
1682 int msg_type = CV_32F);
1683 void operator()(const oclMat &left, const oclMat &right, oclMat &disparity);
1688 float max_data_term;
1690 float max_disc_term;
1691 float disc_single_jump;
1694 bool use_local_init_data_cost;
1696 oclMat u[2], d[2], l[2], r[2];
1697 oclMat disp_selected_pyr[2];
1699 oclMat data_cost_selected;
1704 // Implementation of the Zach, Pock and Bischof Dual TV-L1 Optical Flow method
1707 // [1] C. Zach, T. Pock and H. Bischof, "A Duality Based Approach for Realtime TV-L1 Optical Flow".
1708 // [2] Javier Sanchez, Enric Meinhardt-Llopis and Gabriele Facciolo. "TV-L1 Optical Flow Estimation".
1709 class CV_EXPORTS OpticalFlowDual_TVL1_OCL
1712 OpticalFlowDual_TVL1_OCL();
1714 void operator ()(const oclMat& I0, const oclMat& I1, oclMat& flowx, oclMat& flowy);
1716 void collectGarbage();
1719 * Time step of the numerical scheme.
1724 * Weight parameter for the data term, attachment parameter.
1725 * This is the most relevant parameter, which determines the smoothness of the output.
1726 * The smaller this parameter is, the smoother the solutions we obtain.
1727 * It depends on the range of motions of the images, so its value should be adapted to each image sequence.
1732 * Weight parameter for (u - v)^2, tightness parameter.
1733 * It serves as a link between the attachment and the regularization terms.
1734 * In theory, it should have a small value in order to maintain both parts in correspondence.
1735 * The method is stable for a large range of values of this parameter.
1740 * Number of scales used to create the pyramid of images.
1745 * Number of warpings per scale.
1746 * Represents the number of times that I1(x+u0) and grad( I1(x+u0) ) are computed per scale.
1747 * This is a parameter that assures the stability of the method.
1748 * It also affects the running time, so it is a compromise between speed and accuracy.
1753 * Stopping criterion threshold used in the numerical scheme, which is a trade-off between precision and running time.
1754 * A small value will yield more accurate solutions at the expense of a slower convergence.
1759 * Stopping criterion iterations number used in the numerical scheme.
1763 bool useInitialFlow;
1766 void procOneScale(const oclMat& I0, const oclMat& I1, oclMat& u1, oclMat& u2);
1768 std::vector<oclMat> I0s;
1769 std::vector<oclMat> I1s;
1770 std::vector<oclMat> u1s;
1771 std::vector<oclMat> u2s;
1791 // current supported sorting methods
1794 SORT_BITONIC, // only support power-of-2 buffer size
1795 SORT_SELECTION, // cannot sort duplicate keys
1797 SORT_RADIX // only support signed int/float keys(CV_32S/CV_32F)
1799 //! Returns the sorted result of all the elements in input based on equivalent keys.
1801 // The element unit in the values to be sorted is determined from the data type,
1802 // i.e., a CV_32FC2 input {a1a2, b1b2} will be considered as two elements, regardless its
1803 // matrix dimension.
1804 // both keys and values will be sorted inplace
1805 // Key needs to be single channel oclMat.
1809 // keys = {2, 3, 1} (CV_8UC1)
1810 // values = {10,5, 4,3, 6,2} (CV_8UC2)
1811 // sortByKey(keys, values, SORT_SELECTION, false);
1813 // keys = {1, 2, 3} (CV_8UC1)
1814 // values = {6,2, 10,5, 4,3} (CV_8UC2)
1815 void CV_EXPORTS sortByKey(oclMat& keys, oclMat& values, int method, bool isGreaterThan = false);
1816 /*!Base class for MOG and MOG2!*/
1817 class CV_EXPORTS BackgroundSubtractor
1820 //! the virtual destructor
1821 virtual ~BackgroundSubtractor();
1822 //! the update operator that takes the next video frame and returns the current foreground mask as 8-bit binary image.
1823 virtual void operator()(const oclMat& image, oclMat& fgmask, float learningRate);
1825 //! computes a background image
1826 virtual void getBackgroundImage(oclMat& backgroundImage) const = 0;
1829 Gaussian Mixture-based Backbround/Foreground Segmentation Algorithm
1831 The class implements the following algorithm:
1832 "An improved adaptive background mixture model for real-time tracking with shadow detection"
1833 P. KadewTraKuPong and R. Bowden,
1834 Proc. 2nd European Workshp on Advanced Video-Based Surveillance Systems, 2001."
1835 http://personal.ee.surrey.ac.uk/Personal/R.Bowden/publications/avbs01/avbs01.pdf
1837 class CV_EXPORTS MOG: public cv::ocl::BackgroundSubtractor
1840 //! the default constructor
1841 MOG(int nmixtures = -1);
1843 //! re-initiaization method
1844 void initialize(Size frameSize, int frameType);
1846 //! the update operator
1847 void operator()(const oclMat& frame, oclMat& fgmask, float learningRate = 0.f);
1849 //! computes a background image which are the mean of all background gaussians
1850 void getBackgroundImage(oclMat& backgroundImage) const;
1852 //! releases all inner buffers
1857 float backgroundRatio;
1874 The class implements the following algorithm:
1875 "Improved adaptive Gausian mixture model for background subtraction"
1877 International Conference Pattern Recognition, UK, August, 2004.
1878 http://www.zoranz.net/Publications/zivkovic2004ICPR.pdf
1880 class CV_EXPORTS MOG2: public cv::ocl::BackgroundSubtractor
1883 //! the default constructor
1884 MOG2(int nmixtures = -1);
1886 //! re-initiaization method
1887 void initialize(Size frameSize, int frameType);
1889 //! the update operator
1890 void operator()(const oclMat& frame, oclMat& fgmask, float learningRate = -1.0f);
1892 //! computes a background image which are the mean of all background gaussians
1893 void getBackgroundImage(oclMat& backgroundImage) const;
1895 //! releases all inner buffers
1899 // you should call initialize after parameters changes
1903 //! here it is the maximum allowed number of mixture components.
1904 //! Actual number is determined dynamically per pixel
1906 // threshold on the squared Mahalanobis distance to decide if it is well described
1907 // by the background model or not. Related to Cthr from the paper.
1908 // This does not influence the update of the background. A typical value could be 4 sigma
1909 // and that is varThreshold=4*4=16; Corresponds to Tb in the paper.
1911 /////////////////////////
1912 // less important parameters - things you might change but be carefull
1913 ////////////////////////
1915 float backgroundRatio;
1916 // corresponds to fTB=1-cf from the paper
1917 // TB - threshold when the component becomes significant enough to be included into
1918 // the background model. It is the TB=1-cf from the paper. So I use cf=0.1 => TB=0.
1919 // For alpha=0.001 it means that the mode should exist for approximately 105 frames before
1920 // it is considered foreground
1921 // float noiseSigma;
1922 float varThresholdGen;
1924 //correspondts to Tg - threshold on the squared Mahalan. dist. to decide
1925 //when a sample is close to the existing components. If it is not close
1926 //to any a new component will be generated. I use 3 sigma => Tg=3*3=9.
1927 //Smaller Tg leads to more generated components and higher Tg might make
1928 //lead to small number of components but they can grow too large
1933 //initial variance for the newly generated components.
1934 //It will will influence the speed of adaptation. A good guess should be made.
1935 //A simple way is to estimate the typical standard deviation from the images.
1936 //I used here 10 as a reasonable value
1937 // min and max can be used to further control the variance
1938 float fCT; //CT - complexity reduction prior
1939 //this is related to the number of samples needed to accept that a component
1940 //actually exists. We use CT=0.05 of all the samples. By setting CT=0 you get
1941 //the standard Stauffer&Grimson algorithm (maybe not exact but very similar)
1943 //shadow detection parameters
1944 bool bShadowDetection; //default 1 - do shadow detection
1945 unsigned char nShadowDetection; //do shadow detection - insert this value as the detection result - 127 default value
1947 // Tau - shadow threshold. The shadow is detected if the pixel is darker
1948 //version of the background. Tau is a threshold on how much darker the shadow can be.
1949 //Tau= 0.5 means that if pixel is more than 2 times darker then it is not shadow
1950 //See: Prati,Mikic,Trivedi,Cucchiarra,"Detecting Moving Shadows...",IEEE PAMI,2003.
1963 oclMat bgmodelUsedModes_; //keep track of number of modes per pixel
1966 /*!***************Kalman Filter*************!*/
1967 class CV_EXPORTS KalmanFilter
1971 //! the full constructor taking the dimensionality of the state, of the measurement and of the control vector
1972 KalmanFilter(int dynamParams, int measureParams, int controlParams=0, int type=CV_32F);
1973 //! re-initializes Kalman filter. The previous content is destroyed.
1974 void init(int dynamParams, int measureParams, int controlParams=0, int type=CV_32F);
1976 const oclMat& predict(const oclMat& control=oclMat());
1977 const oclMat& correct(const oclMat& measurement);
1979 oclMat statePre; //!< predicted state (x'(k)): x(k)=A*x(k-1)+B*u(k)
1980 oclMat statePost; //!< corrected state (x(k)): x(k)=x'(k)+K(k)*(z(k)-H*x'(k))
1981 oclMat transitionMatrix; //!< state transition matrix (A)
1982 oclMat controlMatrix; //!< control matrix (B) (not used if there is no control)
1983 oclMat measurementMatrix; //!< measurement matrix (H)
1984 oclMat processNoiseCov; //!< process noise covariance matrix (Q)
1985 oclMat measurementNoiseCov;//!< measurement noise covariance matrix (R)
1986 oclMat errorCovPre; //!< priori error estimate covariance matrix (P'(k)): P'(k)=A*P(k-1)*At + Q)*/
1987 oclMat gain; //!< Kalman gain matrix (K(k)): K(k)=P'(k)*Ht*inv(H*P'(k)*Ht+R)
1988 oclMat errorCovPost; //!< posteriori error estimate covariance matrix (P(k)): P(k)=(I-K(k)*H)*P'(k)
1997 /*!***************K Nearest Neighbour*************!*/
1998 class CV_EXPORTS KNearestNeighbour: public CvKNearest
2001 KNearestNeighbour();
2002 ~KNearestNeighbour();
2004 bool train(const Mat& trainData, Mat& labels, Mat& sampleIdx = Mat().setTo(Scalar::all(0)),
2005 bool isRegression = false, int max_k = 32, bool updateBase = false);
2009 void find_nearest(const oclMat& samples, int k, oclMat& lables);
2014 /*!*************** SVM *************!*/
2015 class CV_EXPORTS CvSVM_OCL : public CvSVM
2020 CvSVM_OCL(const cv::Mat& trainData, const cv::Mat& responses,
2021 const cv::Mat& varIdx=cv::Mat(), const cv::Mat& sampleIdx=cv::Mat(),
2022 CvSVMParams params=CvSVMParams());
2023 CV_WRAP float predict( const int row_index, Mat& src, bool returnDFVal=false ) const;
2024 CV_WRAP void predict( cv::InputArray samples, cv::OutputArray results ) const;
2025 CV_WRAP float predict( const cv::Mat& sample, bool returnDFVal=false ) const;
2026 float predict( const CvMat* samples, CV_OUT CvMat* results ) const;
2029 float predict( const int row_index, int row_len, Mat& src, bool returnDFVal=false ) const;
2030 void create_kernel();
2031 void create_solver();
2033 /*!*************** END *************!*/
2036 #if defined _MSC_VER && _MSC_VER >= 1200
2037 # pragma warning( push)
2038 # pragma warning( disable: 4267)
2040 #include "opencv2/ocl/matrix_operations.hpp"
2041 #if defined _MSC_VER && _MSC_VER >= 1200
2042 # pragma warning( pop)
2045 #endif /* __OPENCV_OCL_HPP__ */