1 // This file is part of OpenCV project.
2 // It is subject to the license terms in the LICENSE file found in the top-level directory
3 // of this distribution and at http://opencv.org/license.html.
5 // Copyright (C) 2010-2012, Institute Of Software Chinese Academy Of Science, all rights reserved.
6 // Copyright (C) 2010-2012, Advanced Micro Devices, Inc., all rights reserved.
7 // Copyright (C) 2010-2012, Multicoreware, Inc., all rights reserved.
8 // Third party copyrights are property of their respective owners.
10 #ifndef __OPENCV_OCL_HPP__
11 #define __OPENCV_OCL_HPP__
16 #include "opencv2/core.hpp"
17 #include "opencv2/imgproc.hpp"
18 #include "opencv2/objdetect.hpp"
19 #include "opencv2/ml.hpp"
27 CVCL_DEVICE_TYPE_DEFAULT = (1 << 0),
28 CVCL_DEVICE_TYPE_CPU = (1 << 1),
29 CVCL_DEVICE_TYPE_GPU = (1 << 2),
30 CVCL_DEVICE_TYPE_ACCELERATOR = (1 << 3),
31 //CVCL_DEVICE_TYPE_CUSTOM = (1 << 4)
32 CVCL_DEVICE_TYPE_ALL = 0xFFFFFFFF
44 DEVICE_MEM_DEFAULT = 0,
45 DEVICE_MEM_AHP, //alloc host pointer
46 DEVICE_MEM_UHP, //use host pointer
47 DEVICE_MEM_CHP, //copy host pointer
48 DEVICE_MEM_PM //persistent memory
51 // these classes contain OpenCL runtime information
58 int _id; // reserved, don't use it
60 DeviceType deviceType;
61 std::string deviceProfile;
62 std::string deviceVersion;
63 std::string deviceName;
64 std::string deviceVendor;
66 std::string deviceDriverVersion;
67 std::string deviceExtensions;
69 size_t maxWorkGroupSize;
70 std::vector<size_t> maxWorkItemSizes;
72 size_t localMemorySize;
73 size_t maxMemAllocSize;
75 int deviceVersionMajor;
76 int deviceVersionMinor;
78 bool haveDoubleSupport;
79 bool isUnifiedMemory; // 1 means integrated GPU, otherwise this value is 0
82 std::string compilationExtraOptions;
84 const PlatformInfo* platform;
91 int _id; // reserved, don't use it
93 std::string platformProfile;
94 std::string platformVersion;
95 std::string platformName;
96 std::string platformVendor;
97 std::string platformExtensons;
99 int platformVersionMajor;
100 int platformVersionMinor;
102 std::vector<const DeviceInfo*> devices;
107 //////////////////////////////// Initialization & Info ////////////////////////
108 typedef std::vector<const PlatformInfo*> PlatformsInfo;
110 CV_EXPORTS int getOpenCLPlatforms(PlatformsInfo& platforms);
112 typedef std::vector<const DeviceInfo*> DevicesInfo;
114 CV_EXPORTS int getOpenCLDevices(DevicesInfo& devices, int deviceType = CVCL_DEVICE_TYPE_GPU,
115 const PlatformInfo* platform = NULL);
117 // set device you want to use
118 CV_EXPORTS void setDevice(const DeviceInfo* info);
122 FEATURE_CL_DOUBLE = 1,
123 FEATURE_CL_UNIFIED_MEM,
125 FEATURE_CL_INTEL_DEVICE
128 // Represents OpenCL context, interface
129 class CV_EXPORTS Context
135 static Context *getContext();
137 bool supportsFeature(FEATURE_TYPE featureType) const;
138 const DeviceInfo& getDeviceInfo() const;
140 const void* getOpenCLContextPtr() const;
141 const void* getOpenCLCommandQueuePtr() const;
142 const void* getOpenCLDeviceIDPtr() const;
145 inline const void *getClContextPtr()
147 return Context::getContext()->getOpenCLContextPtr();
150 inline const void *getClCommandQueuePtr()
152 return Context::getContext()->getOpenCLCommandQueuePtr();
155 CV_EXPORTS bool supportsFeature(FEATURE_TYPE featureType);
157 CV_EXPORTS void finish();
159 enum BINARY_CACHE_MODE
161 CACHE_NONE = 0, // do not cache OpenCL binary
162 CACHE_DEBUG = 0x1 << 0, // cache OpenCL binary when built in debug mode
163 CACHE_RELEASE = 0x1 << 1, // default behavior, only cache when built in release mode
164 CACHE_ALL = CACHE_DEBUG | CACHE_RELEASE, // cache opencl binary
166 //! Enable or disable OpenCL program binary caching onto local disk
167 // After a program (*.cl files in opencl/ folder) is built at runtime, we allow the
168 // compiled OpenCL program to be cached to the path automatically as "path/*.clb"
169 // binary file, which will be reused when the OpenCV executable is started again.
171 // This feature is enabled by default.
172 CV_EXPORTS void setBinaryDiskCache(int mode = CACHE_RELEASE, cv::String path = "./");
174 //! set where binary cache to be saved to
175 CV_EXPORTS void setBinaryPath(const char *path);
180 const char* programStr;
181 const char* programHash;
183 // Cache in memory by name (should be unique). Caching on disk disabled.
184 inline ProgramSource(const char* _name, const char* _programStr)
185 : name(_name), programStr(_programStr), programHash(NULL)
189 // Cache in memory by name (should be unique). Caching on disk uses programHash mark.
190 inline ProgramSource(const char* _name, const char* _programStr, const char* _programHash)
191 : name(_name), programStr(_programStr), programHash(_programHash)
196 //! Calls OpenCL kernel. Pass globalThreads = NULL, and cleanUp = true, to finally clean-up without executing.
197 //! Deprecated, will be replaced
198 CV_EXPORTS void openCLExecuteKernelInterop(Context *clCxt,
199 const cv::ocl::ProgramSource& source, String kernelName,
200 size_t globalThreads[3], size_t localThreads[3],
201 std::vector< std::pair<size_t, const void *> > &args,
202 int channels, int depth, const char *build_options);
204 class CV_EXPORTS oclMatExpr;
205 //////////////////////////////// oclMat ////////////////////////////////
206 class CV_EXPORTS oclMat
209 //! default constructor
211 //! constructs oclMatrix of the specified size and type (_type is CV_8UC1, CV_64FC3, CV_32SC(12) etc.)
212 oclMat(int rows, int cols, int type);
213 oclMat(Size size, int type);
214 //! constucts oclMatrix and fills it with the specified value _s.
215 oclMat(int rows, int cols, int type, const Scalar &s);
216 oclMat(Size size, int type, const Scalar &s);
218 oclMat(const oclMat &m);
220 //! constructor for oclMatrix headers pointing to user-allocated data
221 oclMat(int rows, int cols, int type, void *data, size_t step = Mat::AUTO_STEP);
222 oclMat(Size size, int type, void *data, size_t step = Mat::AUTO_STEP);
224 //! creates a matrix header for a part of the bigger matrix
225 oclMat(const oclMat &m, const Range &rowRange, const Range &colRange);
226 oclMat(const oclMat &m, const Rect &roi);
228 //! builds oclMat from Mat. Perfom blocking upload to device.
229 explicit oclMat (const Mat &m);
231 //! destructor - calls release()
234 //! assignment operators
235 oclMat &operator = (const oclMat &m);
236 //! assignment operator. Perfom blocking upload to device.
237 oclMat &operator = (const Mat &m);
238 oclMat &operator = (const oclMatExpr& expr);
240 //! pefroms blocking upload data to oclMat.
241 void upload(const cv::Mat &m);
244 //! downloads data from device to host memory. Blocking calls.
245 operator Mat() const;
246 void download(cv::Mat &m) const;
248 //! convert to _InputArray
249 operator _InputArray();
251 //! convert to _OutputArray
252 operator _OutputArray();
254 //! returns a new oclMatrix header for the specified row
255 oclMat row(int y) const;
256 //! returns a new oclMatrix header for the specified column
257 oclMat col(int x) const;
258 //! ... for the specified row span
259 oclMat rowRange(int startrow, int endrow) const;
260 oclMat rowRange(const Range &r) const;
261 //! ... for the specified column span
262 oclMat colRange(int startcol, int endcol) const;
263 oclMat colRange(const Range &r) const;
265 //! returns deep copy of the oclMatrix, i.e. the data is copied
266 oclMat clone() const;
268 //! copies those oclMatrix elements to "m" that are marked with non-zero mask elements.
269 // It calls m.create(this->size(), this->type()).
270 // It supports any data type
271 void copyTo( oclMat &m, const oclMat &mask = oclMat()) const;
273 //! converts oclMatrix to another datatype with optional scalng. See cvConvertScale.
274 void convertTo( oclMat &m, int rtype, double alpha = 1, double beta = 0 ) const;
276 void assignTo( oclMat &m, int type = -1 ) const;
278 //! sets every oclMatrix element to s
279 oclMat& operator = (const Scalar &s);
280 //! sets some of the oclMatrix elements to s, according to the mask
281 oclMat& setTo(const Scalar &s, const oclMat &mask = oclMat());
282 //! creates alternative oclMatrix header for the same data, with different
283 // number of channels and/or different number of rows. see cvReshape.
284 oclMat reshape(int cn, int rows = 0) const;
286 //! allocates new oclMatrix data unless the oclMatrix already has specified size and type.
287 // previous data is unreferenced if needed.
288 void create(int rows, int cols, int type);
289 void create(Size size, int type);
291 //! allocates new oclMatrix with specified device memory type.
292 void createEx(int rows, int cols, int type,
293 DevMemRW rw_type, DevMemType mem_type);
294 void createEx(Size size, int type, DevMemRW rw_type,
295 DevMemType mem_type);
297 //! decreases reference counter;
298 // deallocate the data when reference counter reaches 0.
301 //! swaps with other smart pointer
302 void swap(oclMat &mat);
304 //! locates oclMatrix header within a parent oclMatrix. See below
305 void locateROI( Size &wholeSize, Point &ofs ) const;
306 //! moves/resizes the current oclMatrix ROI inside the parent oclMatrix.
307 oclMat& adjustROI( int dtop, int dbottom, int dleft, int dright );
308 //! extracts a rectangular sub-oclMatrix
309 // (this is a generalized form of row, rowRange etc.)
310 oclMat operator()( Range rowRange, Range colRange ) const;
311 oclMat operator()( const Rect &roi ) const;
313 oclMat& operator+=( const oclMat& m );
314 oclMat& operator-=( const oclMat& m );
315 oclMat& operator*=( const oclMat& m );
316 oclMat& operator/=( const oclMat& m );
318 //! returns true if the oclMatrix data is continuous
319 // (i.e. when there are no gaps between successive rows).
320 // similar to CV_IS_oclMat_CONT(cvoclMat->type)
321 bool isContinuous() const;
322 //! returns element size in bytes,
323 // similar to CV_ELEM_SIZE(cvMat->type)
324 size_t elemSize() const;
325 //! returns the size of element channel in bytes.
326 size_t elemSize1() const;
327 //! returns element type, similar to CV_MAT_TYPE(cvMat->type)
329 //! returns element type, i.e. 8UC3 returns 8UC4 because in ocl
330 //! 3 channels element actually use 4 channel space
332 //! returns element type, similar to CV_MAT_DEPTH(cvMat->type)
334 //! returns element type, similar to CV_MAT_CN(cvMat->type)
335 int channels() const;
336 //! returns element type, return 4 for 3 channels element,
337 //!becuase 3 channels element actually use 4 channel space
338 int oclchannels() const;
339 //! returns step/elemSize1()
340 size_t step1() const;
341 //! returns oclMatrix size:
342 // width == number of columns, height == number of rows
344 //! returns true if oclMatrix data is NULL
347 //! matrix transposition
350 /*! includes several bit-fields:
351 - the magic signature
357 //! the number of rows and columns
359 //! a distance between successive rows in bytes; includes the gap if any
361 //! pointer to the data(OCL memory object)
364 //! pointer to the reference counter;
365 // when oclMatrix points to user-allocated data, the pointer is NULL
368 //! helper fields used in locateROI and adjustROI
369 //datastart and dataend are not used in current version
373 //! OpenCL context associated with the oclMat object.
374 Context *clCxt; // TODO clCtx
375 //add offset for handle ROI, calculated in byte
377 //add wholerows and wholecols for the whole matrix, datastart and dataend are no longer used
382 // convert InputArray/OutputArray to oclMat references
383 CV_EXPORTS oclMat& getOclMatRef(InputArray src);
384 CV_EXPORTS oclMat& getOclMatRef(OutputArray src);
386 ///////////////////// mat split and merge /////////////////////////////////
387 //! Compose a multi-channel array from several single-channel arrays
389 CV_EXPORTS void merge(const oclMat *src, size_t n, oclMat &dst);
390 CV_EXPORTS void merge(const std::vector<oclMat> &src, oclMat &dst);
392 //! Divides multi-channel array into several single-channel arrays
394 CV_EXPORTS void split(const oclMat &src, oclMat *dst);
395 CV_EXPORTS void split(const oclMat &src, std::vector<oclMat> &dst);
397 ////////////////////////////// Arithmetics ///////////////////////////////////
399 //! adds one matrix to another with scale (dst = src1 * alpha + src2 * beta + gama)
400 // supports all data types
401 CV_EXPORTS void addWeighted(const oclMat &src1, double alpha, const oclMat &src2, double beta, double gama, oclMat &dst);
403 //! adds one matrix to another (dst = src1 + src2)
404 // supports all data types
405 CV_EXPORTS void add(const oclMat &src1, const oclMat &src2, oclMat &dst, const oclMat &mask = oclMat());
406 //! adds scalar to a matrix (dst = src1 + s)
407 // supports all data types
408 CV_EXPORTS void add(const oclMat &src1, const Scalar &s, oclMat &dst, const oclMat &mask = oclMat());
410 //! subtracts one matrix from another (dst = src1 - src2)
411 // supports all data types
412 CV_EXPORTS void subtract(const oclMat &src1, const oclMat &src2, oclMat &dst, const oclMat &mask = oclMat());
413 //! subtracts scalar from a matrix (dst = src1 - s)
414 // supports all data types
415 CV_EXPORTS void subtract(const oclMat &src1, const Scalar &s, oclMat &dst, const oclMat &mask = oclMat());
417 //! computes element-wise product of the two arrays (dst = src1 * scale * src2)
418 // supports all data types
419 CV_EXPORTS void multiply(const oclMat &src1, const oclMat &src2, oclMat &dst, double scale = 1);
420 //! multiplies matrix to a number (dst = scalar * src)
421 // supports all data types
422 CV_EXPORTS void multiply(double scalar, const oclMat &src, oclMat &dst);
424 //! computes element-wise quotient of the two arrays (dst = src1 * scale / src2)
425 // supports all data types
426 CV_EXPORTS void divide(const oclMat &src1, const oclMat &src2, oclMat &dst, double scale = 1);
427 //! computes element-wise quotient of the two arrays (dst = scale / src)
428 // supports all data types
429 CV_EXPORTS void divide(double scale, const oclMat &src1, oclMat &dst);
431 //! computes element-wise minimum of the two arrays (dst = min(src1, src2))
432 // supports all data types
433 CV_EXPORTS void min(const oclMat &src1, const oclMat &src2, oclMat &dst);
435 //! computes element-wise maximum of the two arrays (dst = max(src1, src2))
436 // supports all data types
437 CV_EXPORTS void max(const oclMat &src1, const oclMat &src2, oclMat &dst);
439 //! compares elements of two arrays (dst = src1 <cmpop> src2)
440 // supports all data types
441 CV_EXPORTS void compare(const oclMat &src1, const oclMat &src2, oclMat &dst, int cmpop);
443 //! transposes the matrix
444 // supports all data types
445 CV_EXPORTS void transpose(const oclMat &src, oclMat &dst);
447 //! computes element-wise absolute values of an array (dst = abs(src))
448 // supports all data types
449 CV_EXPORTS void abs(const oclMat &src, oclMat &dst);
451 //! computes element-wise absolute difference of two arrays (dst = abs(src1 - src2))
452 // supports all data types
453 CV_EXPORTS void absdiff(const oclMat &src1, const oclMat &src2, oclMat &dst);
454 //! computes element-wise absolute difference of array and scalar (dst = abs(src1 - s))
455 // supports all data types
456 CV_EXPORTS void absdiff(const oclMat &src1, const Scalar &s, oclMat &dst);
458 //! computes mean value and standard deviation of all or selected array elements
459 // supports all data types
460 CV_EXPORTS void meanStdDev(const oclMat &mtx, Scalar &mean, Scalar &stddev);
462 //! computes norm of array
463 // supports NORM_INF, NORM_L1, NORM_L2
464 // supports all data types
465 CV_EXPORTS double norm(const oclMat &src1, int normType = NORM_L2);
467 //! computes norm of the difference between two arrays
468 // supports NORM_INF, NORM_L1, NORM_L2
469 // supports all data types
470 CV_EXPORTS double norm(const oclMat &src1, const oclMat &src2, int normType = NORM_L2);
472 //! reverses the order of the rows, columns or both in a matrix
473 // supports all types
474 CV_EXPORTS void flip(const oclMat &src, oclMat &dst, int flipCode);
476 //! computes sum of array elements
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());
486 //! finds global minimum and maximum array elements and returns their values with locations
487 // support all C1 types
488 CV_EXPORTS void minMaxLoc(const oclMat &src, double *minVal, double *maxVal = 0, Point *minLoc = 0, Point *maxLoc = 0,
489 const oclMat &mask = oclMat());
491 //! counts non-zero array elements
493 CV_EXPORTS int countNonZero(const oclMat &src);
495 //! transforms 8-bit unsigned integers using lookup table: dst(i)=lut(src(i))
496 // destination array will have the depth type as lut and the same channels number as source
497 //It supports 8UC1 8UC4 only
498 CV_EXPORTS void LUT(const oclMat &src, const oclMat &lut, oclMat &dst);
500 //! only 8UC1 and 256 bins is supported now
501 CV_EXPORTS void calcHist(const oclMat &mat_src, oclMat &mat_hist);
502 //! only 8UC1 and 256 bins is supported now
503 CV_EXPORTS void equalizeHist(const oclMat &mat_src, oclMat &mat_dst);
505 //! only 8UC1 is supported now
506 CV_EXPORTS Ptr<cv::CLAHE> createCLAHE(double clipLimit = 40.0, Size tileGridSize = Size(8, 8));
509 // supports 8UC1 8UC4
510 CV_EXPORTS void bilateralFilter(const oclMat& src, oclMat& dst, int d, double sigmaColor, double sigmaSpace, int borderType=BORDER_DEFAULT);
512 //! Applies an adaptive bilateral filter to the input image
513 // Unlike the usual bilateral filter that uses fixed value for sigmaColor,
514 // the adaptive version calculates the local variance in he ksize neighborhood
515 // and use this as sigmaColor, for the value filtering. However, the local standard deviation is
516 // clamped to the maxSigmaColor.
517 // supports 8UC1, 8UC3
518 CV_EXPORTS void adaptiveBilateralFilter(const oclMat& src, oclMat& dst, Size ksize, double sigmaSpace, double maxSigmaColor=20.0, Point anchor = Point(-1, -1), int borderType=BORDER_DEFAULT);
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 square root of each matrix element
529 // supports only CV_32FC1, CV_64FC1 type
530 CV_EXPORTS void sqrt(const oclMat &src, oclMat &dst);
532 //! computes magnitude of each (x(i), y(i)) vector
533 // supports only CV_32F, CV_64F type
534 CV_EXPORTS void magnitude(const oclMat &x, const oclMat &y, oclMat &magnitude);
536 //! computes angle (angle(i)) of each (x(i), y(i)) vector
537 // supports only CV_32F, CV_64F type
538 CV_EXPORTS void phase(const oclMat &x, const oclMat &y, oclMat &angle, bool angleInDegrees = false);
540 //! the function raises every element of tne input array to p
541 // support only CV_32F, CV_64F type
542 CV_EXPORTS void pow(const oclMat &x, double p, oclMat &y);
544 //! converts Cartesian coordinates to polar
545 // supports only CV_32F CV_64F type
546 CV_EXPORTS void cartToPolar(const oclMat &x, const oclMat &y, oclMat &magnitude, oclMat &angle, bool angleInDegrees = false);
548 //! converts polar coordinates to Cartesian
549 // supports only CV_32F CV_64F type
550 CV_EXPORTS void polarToCart(const oclMat &magnitude, const oclMat &angle, oclMat &x, oclMat &y, bool angleInDegrees = false);
552 //! perfroms per-elements bit-wise inversion
553 // supports all types
554 CV_EXPORTS void bitwise_not(const oclMat &src, oclMat &dst);
556 //! calculates per-element bit-wise disjunction of two arrays
557 // supports all types
558 CV_EXPORTS void bitwise_or(const oclMat &src1, const oclMat &src2, oclMat &dst, const oclMat &mask = oclMat());
559 CV_EXPORTS void bitwise_or(const oclMat &src1, const Scalar &s, oclMat &dst, const oclMat &mask = oclMat());
561 //! calculates per-element bit-wise conjunction of two arrays
562 // supports all types
563 CV_EXPORTS void bitwise_and(const oclMat &src1, const oclMat &src2, oclMat &dst, const oclMat &mask = oclMat());
564 CV_EXPORTS void bitwise_and(const oclMat &src1, const Scalar &s, oclMat &dst, const oclMat &mask = oclMat());
566 //! calculates per-element bit-wise "exclusive or" operation
567 // supports all types
568 CV_EXPORTS void bitwise_xor(const oclMat &src1, const oclMat &src2, oclMat &dst, const oclMat &mask = oclMat());
569 CV_EXPORTS void bitwise_xor(const oclMat &src1, const Scalar &s, oclMat &dst, const oclMat &mask = oclMat());
571 //! Logical operators
572 CV_EXPORTS oclMat operator ~ (const oclMat &);
573 CV_EXPORTS oclMat operator | (const oclMat &, const oclMat &);
574 CV_EXPORTS oclMat operator & (const oclMat &, const oclMat &);
575 CV_EXPORTS oclMat operator ^ (const oclMat &, const oclMat &);
578 //! Mathematics operators
579 CV_EXPORTS oclMatExpr operator + (const oclMat &src1, const oclMat &src2);
580 CV_EXPORTS oclMatExpr operator - (const oclMat &src1, const oclMat &src2);
581 CV_EXPORTS oclMatExpr operator * (const oclMat &src1, const oclMat &src2);
582 CV_EXPORTS oclMatExpr operator / (const oclMat &src1, const oclMat &src2);
584 struct CV_EXPORTS ConvolveBuf
588 Size user_block_size;
591 oclMat image_spect, templ_spect, result_spect;
592 oclMat image_block, templ_block, result_data;
594 void create(Size image_size, Size templ_size);
595 static Size estimateBlockSize(Size result_size, Size templ_size);
598 //! computes convolution of two images, may use discrete Fourier transform
599 // support only CV_32FC1 type
600 CV_EXPORTS void convolve(const oclMat &image, const oclMat &temp1, oclMat &result, bool ccorr = false);
601 CV_EXPORTS void convolve(const oclMat &image, const oclMat &temp1, oclMat &result, bool ccorr, ConvolveBuf& buf);
603 //! Performs a per-element multiplication of two Fourier spectrums.
604 //! Only full (not packed) CV_32FC2 complex spectrums in the interleaved format are supported for now.
605 //! support only CV_32FC2 type
606 CV_EXPORTS void mulSpectrums(const oclMat &a, const oclMat &b, oclMat &c, int flags, float scale, bool conjB = false);
608 CV_EXPORTS void cvtColor(const oclMat &src, oclMat &dst, int code, int dcn = 0);
610 //! initializes a scaled identity matrix
611 CV_EXPORTS void setIdentity(oclMat& src, const Scalar & val = Scalar(1));
613 //! fills the output array with repeated copies of the input array
614 CV_EXPORTS void repeat(const oclMat & src, int ny, int nx, oclMat & dst);
616 //////////////////////////////// Filter Engine ////////////////////////////////
619 The Base Class for 1D or Row-wise Filters
621 This is the base class for linear or non-linear filters that process 1D data.
622 In particular, such filters are used for the "horizontal" filtering parts in separable filters.
624 class CV_EXPORTS BaseRowFilter_GPU
627 BaseRowFilter_GPU(int ksize_, int anchor_, int bordertype_) : ksize(ksize_), anchor(anchor_), bordertype(bordertype_) {}
628 virtual ~BaseRowFilter_GPU() {}
629 virtual void operator()(const oclMat &src, oclMat &dst) = 0;
630 int ksize, anchor, bordertype;
634 The Base Class for Column-wise Filters
636 This is the base class for linear or non-linear filters that process columns of 2D arrays.
637 Such filters are used for the "vertical" filtering parts in separable filters.
639 class CV_EXPORTS BaseColumnFilter_GPU
642 BaseColumnFilter_GPU(int ksize_, int anchor_, int bordertype_) : ksize(ksize_), anchor(anchor_), bordertype(bordertype_) {}
643 virtual ~BaseColumnFilter_GPU() {}
644 virtual void operator()(const oclMat &src, oclMat &dst) = 0;
645 int ksize, anchor, bordertype;
649 The Base Class for Non-Separable 2D Filters.
651 This is the base class for linear or non-linear 2D filters.
653 class CV_EXPORTS BaseFilter_GPU
656 BaseFilter_GPU(const Size &ksize_, const Point &anchor_, const int &borderType_)
657 : ksize(ksize_), anchor(anchor_), borderType(borderType_) {}
658 virtual ~BaseFilter_GPU() {}
659 virtual void operator()(const oclMat &src, oclMat &dst) = 0;
666 The Base Class for Filter Engine.
668 The class can be used to apply an arbitrary filtering operation to an image.
669 It contains all the necessary intermediate buffers.
671 class CV_EXPORTS FilterEngine_GPU
674 virtual ~FilterEngine_GPU() {}
676 virtual void apply(const oclMat &src, oclMat &dst, Rect roi = Rect(0, 0, -1, -1)) = 0;
679 //! returns the non-separable filter engine with the specified filter
680 CV_EXPORTS Ptr<FilterEngine_GPU> createFilter2D_GPU(const Ptr<BaseFilter_GPU> filter2D);
682 //! returns the primitive row filter with the specified kernel
683 CV_EXPORTS Ptr<BaseRowFilter_GPU> getLinearRowFilter_GPU(int srcType, int bufType, const Mat &rowKernel,
684 int anchor = -1, int bordertype = BORDER_DEFAULT);
686 //! returns the primitive column filter with the specified kernel
687 CV_EXPORTS Ptr<BaseColumnFilter_GPU> getLinearColumnFilter_GPU(int bufType, int dstType, const Mat &columnKernel,
688 int anchor = -1, int bordertype = BORDER_DEFAULT, double delta = 0.0);
690 //! returns the separable linear filter engine
691 CV_EXPORTS Ptr<FilterEngine_GPU> createSeparableLinearFilter_GPU(int srcType, int dstType, const Mat &rowKernel,
692 const Mat &columnKernel, const Point &anchor = Point(-1, -1), double delta = 0.0, int bordertype = BORDER_DEFAULT);
694 //! returns the separable filter engine with the specified filters
695 CV_EXPORTS Ptr<FilterEngine_GPU> createSeparableFilter_GPU(const Ptr<BaseRowFilter_GPU> &rowFilter,
696 const Ptr<BaseColumnFilter_GPU> &columnFilter);
698 //! returns the Gaussian filter engine
699 CV_EXPORTS Ptr<FilterEngine_GPU> createGaussianFilter_GPU(int type, Size ksize, double sigma1, double sigma2 = 0, int bordertype = BORDER_DEFAULT);
701 //! returns filter engine for the generalized Sobel operator
702 CV_EXPORTS Ptr<FilterEngine_GPU> createDerivFilter_GPU( int srcType, int dstType, int dx, int dy, int ksize, int borderType = BORDER_DEFAULT );
704 //! applies Laplacian operator to the image
705 // supports only ksize = 1 and ksize = 3
706 CV_EXPORTS void Laplacian(const oclMat &src, oclMat &dst, int ddepth, int ksize = 1, double scale = 1,
707 double delta=0, int borderType=BORDER_DEFAULT);
709 //! returns 2D box filter
710 // dst type must be the same as source type
711 CV_EXPORTS Ptr<BaseFilter_GPU> getBoxFilter_GPU(int srcType, int dstType,
712 const Size &ksize, Point anchor = Point(-1, -1), int borderType = BORDER_DEFAULT);
714 //! returns box filter engine
715 CV_EXPORTS Ptr<FilterEngine_GPU> createBoxFilter_GPU(int srcType, int dstType, const Size &ksize,
716 const Point &anchor = Point(-1, -1), int borderType = BORDER_DEFAULT);
718 //! returns 2D filter with the specified kernel
719 // supports: dst type must be the same as source type
720 CV_EXPORTS Ptr<BaseFilter_GPU> getLinearFilter_GPU(int srcType, int dstType, const Mat &kernel, const Size &ksize,
721 const Point &anchor = Point(-1, -1), int borderType = BORDER_DEFAULT);
723 //! returns the non-separable linear filter engine
724 // supports: dst type must be the same as source type
725 CV_EXPORTS Ptr<FilterEngine_GPU> createLinearFilter_GPU(int srcType, int dstType, const Mat &kernel,
726 const Point &anchor = Point(-1, -1), int borderType = BORDER_DEFAULT);
728 //! smooths the image using the normalized box filter
729 CV_EXPORTS void boxFilter(const oclMat &src, oclMat &dst, int ddepth, Size ksize,
730 Point anchor = Point(-1, -1), int borderType = BORDER_DEFAULT);
732 //! returns 2D morphological filter
733 //! only MORPH_ERODE and MORPH_DILATE are supported
734 // supports CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4 types
735 // kernel must have CV_8UC1 type, one rows and cols == ksize.width * ksize.height
736 CV_EXPORTS Ptr<BaseFilter_GPU> getMorphologyFilter_GPU(int op, int type, const Mat &kernel, const Size &ksize,
737 Point anchor = Point(-1, -1));
739 //! returns morphological filter engine. Only MORPH_ERODE and MORPH_DILATE are supported.
740 CV_EXPORTS Ptr<FilterEngine_GPU> createMorphologyFilter_GPU(int op, int type, const Mat &kernel,
741 const Point &anchor = Point(-1, -1), int iterations = 1);
743 //! a synonym for normalized box filter
744 static inline void blur(const oclMat &src, oclMat &dst, Size ksize, Point anchor = Point(-1, -1),
745 int borderType = BORDER_CONSTANT)
747 boxFilter(src, dst, -1, ksize, anchor, borderType);
750 //! applies non-separable 2D linear filter to the image
751 CV_EXPORTS void filter2D(const oclMat &src, oclMat &dst, int ddepth, const Mat &kernel,
752 Point anchor = Point(-1, -1), double delta = 0.0, int borderType = BORDER_DEFAULT);
754 //! applies separable 2D linear filter to the image
755 CV_EXPORTS void sepFilter2D(const oclMat &src, oclMat &dst, int ddepth, const Mat &kernelX, const Mat &kernelY,
756 Point anchor = Point(-1, -1), double delta = 0.0, int bordertype = BORDER_DEFAULT);
758 //! applies generalized Sobel operator to the image
759 // dst.type must equalize src.type
760 // supports data type: CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4
761 // supports border type: BORDER_CONSTANT, BORDER_REPLICATE, BORDER_REFLECT,BORDER_REFLECT_101
762 CV_EXPORTS void 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);
764 //! applies the vertical or horizontal Scharr operator to the image
765 // dst.type must equalize src.type
766 // supports data type: CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4
767 // supports border type: BORDER_CONSTANT, BORDER_REPLICATE, BORDER_REFLECT,BORDER_REFLECT_101
768 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);
770 //! smooths the image using Gaussian filter.
771 // dst.type must equalize src.type
772 // supports data type: CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4
773 // supports border type: BORDER_CONSTANT, BORDER_REPLICATE, BORDER_REFLECT,BORDER_REFLECT_101
774 CV_EXPORTS void GaussianBlur(const oclMat &src, oclMat &dst, Size ksize, double sigma1, double sigma2 = 0, int bordertype = BORDER_DEFAULT);
776 //! erodes the image (applies the local minimum operator)
777 // supports data type: CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4
778 CV_EXPORTS void erode( const oclMat &src, oclMat &dst, const Mat &kernel, Point anchor = Point(-1, -1), int iterations = 1,
780 int borderType = BORDER_CONSTANT, const Scalar &borderValue = morphologyDefaultBorderValue());
783 //! dilates the image (applies the local maximum operator)
784 // supports data type: CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4
785 CV_EXPORTS void dilate( const oclMat &src, oclMat &dst, const Mat &kernel, Point anchor = Point(-1, -1), int iterations = 1,
787 int borderType = BORDER_CONSTANT, const Scalar &borderValue = morphologyDefaultBorderValue());
790 //! applies an advanced morphological operation to the image
791 CV_EXPORTS void morphologyEx( const oclMat &src, oclMat &dst, int op, const Mat &kernel, Point anchor = Point(-1, -1), int iterations = 1,
793 int borderType = BORDER_CONSTANT, const Scalar &borderValue = morphologyDefaultBorderValue());
796 ////////////////////////////// Image processing //////////////////////////////
797 //! Does mean shift filtering on GPU.
798 CV_EXPORTS void meanShiftFiltering(const oclMat &src, oclMat &dst, int sp, int sr,
799 TermCriteria criteria = TermCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 5, 1));
801 //! Does mean shift procedure on GPU.
802 CV_EXPORTS void meanShiftProc(const oclMat &src, oclMat &dstr, oclMat &dstsp, int sp, int sr,
803 TermCriteria criteria = TermCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 5, 1));
805 //! Does mean shift segmentation with elimiation of small regions.
806 CV_EXPORTS void meanShiftSegmentation(const oclMat &src, Mat &dst, int sp, int sr, int minsize,
807 TermCriteria criteria = TermCriteria(TermCriteria::MAX_ITER + TermCriteria::EPS, 5, 1));
809 //! applies fixed threshold to the image.
810 // supports CV_8UC1 and CV_32FC1 data type
811 // supports threshold type: THRESH_BINARY, THRESH_BINARY_INV, THRESH_TRUNC, THRESH_TOZERO, THRESH_TOZERO_INV
812 CV_EXPORTS double threshold(const oclMat &src, oclMat &dst, double thresh, double maxVal, int type = THRESH_TRUNC);
814 //! resizes the image
815 // Supports INTER_NEAREST, INTER_LINEAR
816 // supports CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4 types
817 CV_EXPORTS void resize(const oclMat &src, oclMat &dst, Size dsize, double fx = 0, double fy = 0, int interpolation = INTER_LINEAR);
819 //! Applies a generic geometrical transformation to an image.
821 // Supports INTER_NEAREST, INTER_LINEAR.
822 // Map1 supports CV_16SC2, CV_32FC2 types.
823 // Src supports CV_8UC1, CV_8UC2, CV_8UC4.
824 CV_EXPORTS void remap(const oclMat &src, oclMat &dst, oclMat &map1, oclMat &map2, int interpolation, int bordertype, const Scalar &value = Scalar());
826 //! copies 2D array to a larger destination array and pads borders with user-specifiable constant
827 // supports CV_8UC1, CV_8UC4, CV_32SC1 types
828 CV_EXPORTS void copyMakeBorder(const oclMat &src, oclMat &dst, int top, int bottom, int left, int right, int boardtype, const Scalar &value = Scalar());
830 //! Smoothes image using median filter
831 // The source 1- or 4-channel image. m should be 3 or 5, the image depth should be CV_8U or CV_32F.
832 CV_EXPORTS void medianFilter(const oclMat &src, oclMat &dst, int m);
834 //! warps the image using affine transformation
835 // Supports INTER_NEAREST, INTER_LINEAR, INTER_CUBIC
836 // supports CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4 types
837 CV_EXPORTS void warpAffine(const oclMat &src, oclMat &dst, const Mat &M, Size dsize, int flags = INTER_LINEAR);
839 //! warps the image using perspective transformation
840 // Supports INTER_NEAREST, INTER_LINEAR, INTER_CUBIC
841 // supports CV_8UC1, CV_8UC4, CV_32FC1 and CV_32FC4 types
842 CV_EXPORTS void warpPerspective(const oclMat &src, oclMat &dst, const Mat &M, Size dsize, int flags = INTER_LINEAR);
844 //! computes the integral image and integral for the squared image
845 // sum will support CV_32S, CV_32F, sqsum - support CV32F, CV_64F
846 // supports only CV_8UC1 source type
847 CV_EXPORTS void integral(const oclMat &src, oclMat &sum, oclMat &sqsum, int sdepth=-1 );
848 CV_EXPORTS void integral(const oclMat &src, oclMat &sum, int sdepth=-1 );
849 CV_EXPORTS void cornerHarris(const oclMat &src, oclMat &dst, int blockSize, int ksize, double k, int bordertype = cv::BORDER_DEFAULT);
850 CV_EXPORTS void cornerHarris_dxdy(const oclMat &src, oclMat &dst, oclMat &Dx, oclMat &Dy,
851 int blockSize, int ksize, double k, int bordertype = cv::BORDER_DEFAULT);
852 CV_EXPORTS void cornerMinEigenVal(const oclMat &src, oclMat &dst, int blockSize, int ksize, int bordertype = cv::BORDER_DEFAULT);
853 CV_EXPORTS void cornerMinEigenVal_dxdy(const oclMat &src, oclMat &dst, oclMat &Dx, oclMat &Dy,
854 int blockSize, int ksize, int bordertype = cv::BORDER_DEFAULT);
857 /////////////////////////////////// ML ///////////////////////////////////////////
859 //! Compute closest centers for each lines in source and lable it after center's index
860 // supports CV_32FC1/CV_32FC2/CV_32FC4 data type
861 // supports NORM_L1 and NORM_L2 distType
862 // if indices is provided, only the indexed rows will be calculated and their results are in the same
864 CV_EXPORTS void distanceToCenters(const oclMat &src, const oclMat ¢ers, Mat &dists, Mat &labels, int distType = NORM_L2SQR);
866 //!Does k-means procedure on GPU
867 // supports CV_32FC1/CV_32FC2/CV_32FC4 data type
868 CV_EXPORTS double kmeans(const oclMat &src, int K, oclMat &bestLabels,
869 TermCriteria criteria, int attemps, int flags, oclMat ¢ers);
872 ////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
873 ///////////////////////////////////////////CascadeClassifier//////////////////////////////////////////////////////////////////
874 ///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
875 class CV_EXPORTS OclCascadeClassifier : public cv::CascadeClassifier
878 void detectMultiScale(oclMat &image, CV_OUT std::vector<cv::Rect>& faces,
879 double scaleFactor = 1.1, int minNeighbors = 3, int flags = 0,
880 Size minSize = Size(), Size maxSize = Size());
883 /////////////////////////////// Pyramid /////////////////////////////////////
884 CV_EXPORTS void pyrDown(const oclMat &src, oclMat &dst);
886 //! upsamples the source image and then smoothes it
887 CV_EXPORTS void pyrUp(const oclMat &src, oclMat &dst);
889 //! performs linear blending of two images
890 //! to avoid accuracy errors sum of weigths shouldn't be very close to zero
891 // supports only CV_8UC1 source type
892 CV_EXPORTS void blendLinear(const oclMat &img1, const oclMat &img2, const oclMat &weights1, const oclMat &weights2, oclMat &result);
894 //! computes vertical sum, supports only CV_32FC1 images
895 CV_EXPORTS void columnSum(const oclMat &src, oclMat &sum);
897 ///////////////////////////////////////// match_template /////////////////////////////////////////////////////////////
898 struct CV_EXPORTS MatchTemplateBuf
900 Size user_block_size;
901 oclMat imagef, templf;
902 std::vector<oclMat> images;
903 std::vector<oclMat> image_sums;
904 std::vector<oclMat> image_sqsums;
907 //! computes the proximity map for the raster template and the image where the template is searched for
908 // Supports TM_SQDIFF, TM_SQDIFF_NORMED, TM_CCORR, TM_CCORR_NORMED, TM_CCOEFF, TM_CCOEFF_NORMED for type 8UC1 and 8UC4
909 // Supports TM_SQDIFF, TM_CCORR for type 32FC1 and 32FC4
910 CV_EXPORTS void matchTemplate(const oclMat &image, const oclMat &templ, oclMat &result, int method);
912 //! computes the proximity map for the raster template and the image where the template is searched for
913 // Supports TM_SQDIFF, TM_SQDIFF_NORMED, TM_CCORR, TM_CCORR_NORMED, TM_CCOEFF, TM_CCOEFF_NORMED for type 8UC1 and 8UC4
914 // Supports TM_SQDIFF, TM_CCORR for type 32FC1 and 32FC4
915 CV_EXPORTS void matchTemplate(const oclMat &image, const oclMat &templ, oclMat &result, int method, MatchTemplateBuf &buf);
919 ///////////////////////////////////////////// Canny /////////////////////////////////////////////
920 struct CV_EXPORTS CannyBuf;
922 //! compute edges of the input image using Canny operator
923 // Support CV_8UC1 only
924 CV_EXPORTS void Canny(const oclMat &image, oclMat &edges, double low_thresh, double high_thresh, int apperture_size = 3, bool L2gradient = false);
925 CV_EXPORTS void Canny(const oclMat &image, CannyBuf &buf, oclMat &edges, double low_thresh, double high_thresh, int apperture_size = 3, bool L2gradient = false);
926 CV_EXPORTS void Canny(const oclMat &dx, const oclMat &dy, oclMat &edges, double low_thresh, double high_thresh, bool L2gradient = false);
927 CV_EXPORTS void Canny(const oclMat &dx, const oclMat &dy, CannyBuf &buf, oclMat &edges, double low_thresh, double high_thresh, bool L2gradient = false);
929 struct CV_EXPORTS CannyBuf
931 CannyBuf() : counter(1, 1, CV_32S) { }
936 explicit CannyBuf(const Size &image_size, int apperture_size = 3) : counter(1, 1, CV_32S)
938 create(image_size, apperture_size);
940 CannyBuf(const oclMat &dx_, const oclMat &dy_);
941 void create(const Size &image_size, int apperture_size = 3);
945 oclMat dx_buf, dy_buf;
946 oclMat magBuf, mapBuf;
947 oclMat trackBuf1, trackBuf2;
949 Ptr<FilterEngine_GPU> filterDX, filterDY;
952 ///////////////////////////////////////// Hough Transform /////////////////////////////////////////
954 struct HoughCirclesBuf
963 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);
964 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);
965 CV_EXPORTS void HoughCirclesDownload(const oclMat& d_circles, OutputArray h_circles);
968 ///////////////////////////////////////// clAmdFft related /////////////////////////////////////////
969 //! Performs a forward or inverse discrete Fourier transform (1D or 2D) of floating point matrix.
970 //! Param dft_size is the size of DFT transform.
972 //! For complex-to-real transform it is assumed that the source matrix is packed in CLFFT's format.
973 // support src type of CV32FC1, CV32FC2
974 // support flags: DFT_INVERSE, DFT_REAL_OUTPUT, DFT_COMPLEX_OUTPUT, DFT_ROWS
975 // dft_size is the size of original input, which is used for transformation from complex to real.
976 // dft_size must be powers of 2, 3 and 5
977 // real to complex dft requires at least v1.8 clAmdFft
978 // real to complex dft output is not the same with cpu version
979 // real to complex and complex to real does not support DFT_ROWS
980 CV_EXPORTS void dft(const oclMat &src, oclMat &dst, Size dft_size = Size(), int flags = 0);
982 //! implements generalized matrix product algorithm GEMM from BLAS
983 // The functionality requires clAmdBlas library
984 // only support type CV_32FC1
985 // flag GEMM_3_T is not supported
986 CV_EXPORTS void gemm(const oclMat &src1, const oclMat &src2, double alpha,
987 const oclMat &src3, double beta, oclMat &dst, int flags = 0);
989 //////////////// HOG (Histogram-of-Oriented-Gradients) Descriptor and Object Detector //////////////
991 struct CV_EXPORTS HOGDescriptor
995 enum { DEFAULT_WIN_SIGMA = -1 };
997 enum { DEFAULT_NLEVELS = 64 };
999 enum { DESCR_FORMAT_ROW_BY_ROW, DESCR_FORMAT_COL_BY_COL };
1003 HOGDescriptor(Size win_size = Size(64, 128), Size block_size = Size(16, 16),
1005 Size block_stride = Size(8, 8), Size cell_size = Size(8, 8),
1007 int nbins = 9, double win_sigma = DEFAULT_WIN_SIGMA,
1009 double threshold_L2hys = 0.2, bool gamma_correction = true,
1011 int nlevels = DEFAULT_NLEVELS);
1015 size_t getDescriptorSize() const;
1017 size_t getBlockHistogramSize() const;
1021 void setSVMDetector(const std::vector<float> &detector);
1025 static std::vector<float> getDefaultPeopleDetector();
1027 static std::vector<float> getPeopleDetector48x96();
1029 static std::vector<float> getPeopleDetector64x128();
1033 void detect(const oclMat &img, std::vector<Point> &found_locations,
1035 double hit_threshold = 0, Size win_stride = Size(),
1037 Size padding = Size());
1041 void detectMultiScale(const oclMat &img, std::vector<Rect> &found_locations,
1043 double hit_threshold = 0, Size win_stride = Size(),
1045 Size padding = Size(), double scale0 = 1.05,
1047 int group_threshold = 2);
1051 void getDescriptors(const oclMat &img, Size win_stride,
1053 oclMat &descriptors,
1055 int descr_format = DESCR_FORMAT_COL_BY_COL);
1071 double threshold_L2hys;
1073 bool gamma_correction;
1081 // initialize buffers; only need to do once in case of multiscale detection
1083 void init_buffer(const oclMat &img, Size win_stride);
1087 void computeBlockHistograms(const oclMat &img);
1089 void computeGradient(const oclMat &img, oclMat &grad, oclMat &qangle);
1093 double getWinSigma() const;
1095 bool checkDetectorSize() const;
1099 static int numPartsWithin(int size, int part_size, int stride);
1101 static Size numPartsWithin(Size size, Size part_size, Size stride);
1105 // Coefficients of the separating plane
1113 // Results of the last classification step
1121 // Results of the last histogram evaluation step
1127 // Gradients conputation results
1129 oclMat grad, qangle;
1139 // effect size of input image (might be different from original size after scaling)
1146 ////////////////////////feature2d_ocl/////////////////
1147 /****************************************************************************************\
1149 \****************************************************************************************/
1150 template<typename T>
1151 struct CV_EXPORTS Accumulator
1155 template<> struct Accumulator<unsigned char>
1159 template<> struct Accumulator<unsigned short>
1163 template<> struct Accumulator<char>
1167 template<> struct Accumulator<short>
1173 * Manhattan distance (city block distance) functor
1176 struct CV_EXPORTS L1
1178 enum { normType = NORM_L1 };
1179 typedef T ValueType;
1180 typedef typename Accumulator<T>::Type ResultType;
1182 ResultType operator()( const T *a, const T *b, int size ) const
1184 return normL1<ValueType, ResultType>(a, b, size);
1189 * Euclidean distance functor
1192 struct CV_EXPORTS L2
1194 enum { normType = NORM_L2 };
1195 typedef T ValueType;
1196 typedef typename Accumulator<T>::Type ResultType;
1198 ResultType operator()( const T *a, const T *b, int size ) const
1200 return (ResultType)std::sqrt((double)normL2Sqr<ValueType, ResultType>(a, b, size));
1205 * Hamming distance functor - counts the bit differences between two strings - useful for the Brief descriptor
1206 * bit count of A exclusive XOR'ed with B
1208 struct CV_EXPORTS Hamming
1210 enum { normType = NORM_HAMMING };
1211 typedef unsigned char ValueType;
1212 typedef int ResultType;
1214 /** this will count the bits in a ^ b
1216 ResultType operator()( const unsigned char *a, const unsigned char *b, int size ) const
1218 return normHamming(a, b, size);
1222 ////////////////////////////////// BruteForceMatcher //////////////////////////////////
1224 class CV_EXPORTS BruteForceMatcher_OCL_base
1227 enum DistType {L1Dist = 0, L2Dist, HammingDist};
1228 explicit BruteForceMatcher_OCL_base(DistType distType = L2Dist);
1230 // Add descriptors to train descriptor collection
1231 void add(const std::vector<oclMat> &descCollection);
1233 // Get train descriptors collection
1234 const std::vector<oclMat> &getTrainDescriptors() const;
1236 // Clear train descriptors collection
1239 // Return true if there are not train descriptors in collection
1242 // Return true if the matcher supports mask in match methods
1243 bool isMaskSupported() const;
1245 // Find one best match for each query descriptor
1246 void matchSingle(const oclMat &query, const oclMat &train,
1247 oclMat &trainIdx, oclMat &distance,
1248 const oclMat &mask = oclMat());
1250 // Download trainIdx and distance and convert it to CPU vector with DMatch
1251 static void matchDownload(const oclMat &trainIdx, const oclMat &distance, std::vector<DMatch> &matches);
1252 // Convert trainIdx and distance to vector with DMatch
1253 static void matchConvert(const Mat &trainIdx, const Mat &distance, std::vector<DMatch> &matches);
1255 // Find one best match for each query descriptor
1256 void match(const oclMat &query, const oclMat &train, std::vector<DMatch> &matches, const oclMat &mask = oclMat());
1258 // Make gpu collection of trains and masks in suitable format for matchCollection function
1259 void makeGpuCollection(oclMat &trainCollection, oclMat &maskCollection, const std::vector<oclMat> &masks = std::vector<oclMat>());
1261 // Find one best match from train collection for each query descriptor
1262 void matchCollection(const oclMat &query, const oclMat &trainCollection,
1263 oclMat &trainIdx, oclMat &imgIdx, oclMat &distance,
1264 const oclMat &masks = oclMat());
1266 // Download trainIdx, imgIdx and distance and convert it to vector with DMatch
1267 static void matchDownload(const oclMat &trainIdx, const oclMat &imgIdx, const oclMat &distance, std::vector<DMatch> &matches);
1268 // Convert trainIdx, imgIdx and distance to vector with DMatch
1269 static void matchConvert(const Mat &trainIdx, const Mat &imgIdx, const Mat &distance, std::vector<DMatch> &matches);
1271 // Find one best match from train collection for each query descriptor.
1272 void match(const oclMat &query, std::vector<DMatch> &matches, const std::vector<oclMat> &masks = std::vector<oclMat>());
1274 // Find k best matches for each query descriptor (in increasing order of distances)
1275 void knnMatchSingle(const oclMat &query, const oclMat &train,
1276 oclMat &trainIdx, oclMat &distance, oclMat &allDist, int k,
1277 const oclMat &mask = oclMat());
1279 // Download trainIdx and distance and convert it to vector with DMatch
1280 // compactResult is used when mask is not empty. If compactResult is false matches
1281 // vector will have the same size as queryDescriptors rows. If compactResult is true
1282 // matches vector will not contain matches for fully masked out query descriptors.
1283 static void knnMatchDownload(const oclMat &trainIdx, const oclMat &distance,
1284 std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
1285 // Convert trainIdx and distance to vector with DMatch
1286 static void knnMatchConvert(const Mat &trainIdx, const Mat &distance,
1287 std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
1289 // Find k best matches for each query descriptor (in increasing order of distances).
1290 // compactResult is used when mask is not empty. If compactResult is false matches
1291 // vector will have the same size as queryDescriptors rows. If compactResult is true
1292 // matches vector will not contain matches for fully masked out query descriptors.
1293 void knnMatch(const oclMat &query, const oclMat &train,
1294 std::vector< std::vector<DMatch> > &matches, int k, const oclMat &mask = oclMat(),
1295 bool compactResult = false);
1297 // Find k best matches from train collection for each query descriptor (in increasing order of distances)
1298 void knnMatch2Collection(const oclMat &query, const oclMat &trainCollection,
1299 oclMat &trainIdx, oclMat &imgIdx, oclMat &distance,
1300 const oclMat &maskCollection = oclMat());
1302 // Download trainIdx and distance and convert it to vector with DMatch
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 static void knnMatch2Download(const oclMat &trainIdx, const oclMat &imgIdx, const oclMat &distance,
1307 std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
1308 // Convert trainIdx and distance to vector with DMatch
1309 static void knnMatch2Convert(const Mat &trainIdx, const Mat &imgIdx, const Mat &distance,
1310 std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
1312 // Find k best matches for each query descriptor (in increasing order of distances).
1313 // compactResult is used when mask is not empty. If compactResult is false matches
1314 // vector will have the same size as queryDescriptors rows. If compactResult is true
1315 // matches vector will not contain matches for fully masked out query descriptors.
1316 void knnMatch(const oclMat &query, std::vector< std::vector<DMatch> > &matches, int k,
1317 const std::vector<oclMat> &masks = std::vector<oclMat>(), bool compactResult = false);
1319 // Find best matches for each query descriptor which have distance less than maxDistance.
1320 // nMatches.at<int>(0, queryIdx) will contain matches count for queryIdx.
1321 // carefully nMatches can be greater than trainIdx.cols - it means that matcher didn't find all matches,
1322 // because it didn't have enough memory.
1323 // If trainIdx is empty, then trainIdx and distance will be created with size nQuery x max((nTrain / 100), 10),
1324 // otherwize user can pass own allocated trainIdx and distance with size nQuery x nMaxMatches
1325 // Matches doesn't sorted.
1326 void radiusMatchSingle(const oclMat &query, const oclMat &train,
1327 oclMat &trainIdx, oclMat &distance, oclMat &nMatches, float maxDistance,
1328 const oclMat &mask = oclMat());
1330 // Download trainIdx, nMatches and distance and convert it to vector with DMatch.
1331 // matches will be sorted in increasing order of distances.
1332 // compactResult is used when mask is not empty. If compactResult is false matches
1333 // vector will have the same size as queryDescriptors rows. If compactResult is true
1334 // matches vector will not contain matches for fully masked out query descriptors.
1335 static void radiusMatchDownload(const oclMat &trainIdx, const oclMat &distance, const oclMat &nMatches,
1336 std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
1337 // Convert trainIdx, nMatches and distance to vector with DMatch.
1338 static void radiusMatchConvert(const Mat &trainIdx, const Mat &distance, const Mat &nMatches,
1339 std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
1341 // Find best matches for each query descriptor which have distance less than maxDistance
1342 // in increasing order of distances).
1343 void radiusMatch(const oclMat &query, const oclMat &train,
1344 std::vector< std::vector<DMatch> > &matches, float maxDistance,
1345 const oclMat &mask = oclMat(), bool compactResult = false);
1347 // Find best matches for each query descriptor which have distance less than maxDistance.
1348 // If trainIdx is empty, then trainIdx and distance will be created with size nQuery x max((nQuery / 100), 10),
1349 // otherwize user can pass own allocated trainIdx and distance with size nQuery x nMaxMatches
1350 // Matches doesn't sorted.
1351 void radiusMatchCollection(const oclMat &query, oclMat &trainIdx, oclMat &imgIdx, oclMat &distance, oclMat &nMatches, float maxDistance,
1352 const std::vector<oclMat> &masks = std::vector<oclMat>());
1354 // Download trainIdx, imgIdx, nMatches and distance and convert it to vector with DMatch.
1355 // matches will be sorted in increasing order of distances.
1356 // compactResult is used when mask is not empty. If compactResult is false matches
1357 // vector will have the same size as queryDescriptors rows. If compactResult is true
1358 // matches vector will not contain matches for fully masked out query descriptors.
1359 static void radiusMatchDownload(const oclMat &trainIdx, const oclMat &imgIdx, const oclMat &distance, const oclMat &nMatches,
1360 std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
1361 // Convert trainIdx, nMatches and distance to vector with DMatch.
1362 static void radiusMatchConvert(const Mat &trainIdx, const Mat &imgIdx, const Mat &distance, const Mat &nMatches,
1363 std::vector< std::vector<DMatch> > &matches, bool compactResult = false);
1365 // Find best matches from train collection for each query descriptor which have distance less than
1366 // maxDistance (in increasing order of distances).
1367 void radiusMatch(const oclMat &query, std::vector< std::vector<DMatch> > &matches, float maxDistance,
1368 const std::vector<oclMat> &masks = std::vector<oclMat>(), bool compactResult = false);
1373 std::vector<oclMat> trainDescCollection;
1376 template <class Distance>
1377 class CV_EXPORTS BruteForceMatcher_OCL;
1379 template <typename T>
1380 class CV_EXPORTS BruteForceMatcher_OCL< L1<T> > : public BruteForceMatcher_OCL_base
1383 explicit BruteForceMatcher_OCL() : BruteForceMatcher_OCL_base(L1Dist) {}
1384 explicit BruteForceMatcher_OCL(L1<T> /*d*/) : BruteForceMatcher_OCL_base(L1Dist) {}
1386 template <typename T>
1387 class CV_EXPORTS BruteForceMatcher_OCL< L2<T> > : public BruteForceMatcher_OCL_base
1390 explicit BruteForceMatcher_OCL() : BruteForceMatcher_OCL_base(L2Dist) {}
1391 explicit BruteForceMatcher_OCL(L2<T> /*d*/) : BruteForceMatcher_OCL_base(L2Dist) {}
1393 template <> class CV_EXPORTS BruteForceMatcher_OCL< Hamming > : public BruteForceMatcher_OCL_base
1396 explicit BruteForceMatcher_OCL() : BruteForceMatcher_OCL_base(HammingDist) {}
1397 explicit BruteForceMatcher_OCL(Hamming /*d*/) : BruteForceMatcher_OCL_base(HammingDist) {}
1400 class CV_EXPORTS BFMatcher_OCL : public BruteForceMatcher_OCL_base
1403 explicit BFMatcher_OCL(int norm = NORM_L2) : BruteForceMatcher_OCL_base(norm == NORM_L1 ? L1Dist : norm == NORM_L2 ? L2Dist : HammingDist) {}
1406 class CV_EXPORTS GoodFeaturesToTrackDetector_OCL
1409 explicit GoodFeaturesToTrackDetector_OCL(int maxCorners = 1000, double qualityLevel = 0.01, double minDistance = 0.0,
1410 int blockSize = 3, bool useHarrisDetector = false, double harrisK = 0.04);
1412 //! return 1 rows matrix with CV_32FC2 type
1413 void operator ()(const oclMat& image, oclMat& corners, const oclMat& mask = oclMat());
1414 //! download points of type Point2f to a vector. the vector's content will be erased
1415 void downloadPoints(const oclMat &points, std::vector<Point2f> &points_v);
1418 double qualityLevel;
1422 bool useHarrisDetector;
1424 void releaseMemory()
1429 minMaxbuf_.release();
1430 tmpCorners_.release();
1440 inline GoodFeaturesToTrackDetector_OCL::GoodFeaturesToTrackDetector_OCL(int maxCorners_, double qualityLevel_, double minDistance_,
1441 int blockSize_, bool useHarrisDetector_, double harrisK_)
1443 maxCorners = maxCorners_;
1444 qualityLevel = qualityLevel_;
1445 minDistance = minDistance_;
1446 blockSize = blockSize_;
1447 useHarrisDetector = useHarrisDetector_;
1451 ////////////////////////////////// FAST Feature Detector //////////////////////////////////
1452 class CV_EXPORTS FAST_OCL
1463 // all features have same size
1464 static const int FEATURE_SIZE = 7;
1466 explicit FAST_OCL(int threshold, bool nonmaxSupression = true, double keypointsRatio = 0.05);
1468 //! finds the keypoints using FAST detector
1469 //! supports only CV_8UC1 images
1470 void operator ()(const oclMat& image, const oclMat& mask, oclMat& keypoints);
1471 void operator ()(const oclMat& image, const oclMat& mask, std::vector<KeyPoint>& keypoints);
1473 //! download keypoints from device to host memory
1474 static void downloadKeypoints(const oclMat& d_keypoints, std::vector<KeyPoint>& keypoints);
1476 //! convert keypoints to KeyPoint vector
1477 static void convertKeypoints(const Mat& h_keypoints, std::vector<KeyPoint>& keypoints);
1479 //! release temporary buffer's memory
1482 bool nonmaxSupression;
1486 //! max keypoints = keypointsRatio * img.size().area()
1487 double keypointsRatio;
1489 //! find keypoints and compute it's response if nonmaxSupression is true
1490 //! return count of detected keypoints
1491 int calcKeyPointsLocation(const oclMat& image, const oclMat& mask);
1493 //! get final array of keypoints
1494 //! performs nonmax supression if needed
1495 //! return final count of keypoints
1496 int getKeyPoints(oclMat& keypoints);
1504 oclMat d_keypoints_;
1506 int calcKeypointsOCL(const oclMat& img, const oclMat& mask, int maxKeypoints);
1507 int nonmaxSupressionOCL(oclMat& keypoints);
1510 /////////////////////////////// PyrLKOpticalFlow /////////////////////////////////////
1512 class CV_EXPORTS PyrLKOpticalFlow
1517 winSize = Size(21, 21);
1521 useInitialFlow = false;
1522 minEigThreshold = 1e-4f;
1523 getMinEigenVals = false;
1524 isDeviceArch11_ = false;
1527 void sparse(const oclMat &prevImg, const oclMat &nextImg, const oclMat &prevPts, oclMat &nextPts,
1528 oclMat &status, oclMat *err = 0);
1530 void dense(const oclMat &prevImg, const oclMat &nextImg, oclMat &u, oclMat &v, oclMat *err = 0);
1536 bool useInitialFlow;
1537 float minEigThreshold;
1538 bool getMinEigenVals;
1540 void releaseMemory()
1542 dx_calcBuf_.release();
1543 dy_calcBuf_.release();
1553 void calcSharrDeriv(const oclMat &src, oclMat &dx, oclMat &dy);
1555 void buildImagePyramid(const oclMat &img0, std::vector<oclMat> &pyr, bool withBorder);
1560 std::vector<oclMat> prevPyr_;
1561 std::vector<oclMat> nextPyr_;
1569 bool isDeviceArch11_;
1572 class CV_EXPORTS FarnebackOpticalFlow
1575 FarnebackOpticalFlow();
1586 void operator ()(const oclMat &frame0, const oclMat &frame1, oclMat &flowx, oclMat &flowy);
1588 void releaseMemory();
1591 void prepareGaussian(
1592 int n, double sigma, float *g, float *xg, float *xxg,
1593 double &ig11, double &ig03, double &ig33, double &ig55);
1595 void setPolynomialExpansionConsts(int n, double sigma);
1597 void updateFlow_boxFilter(
1598 const oclMat& R0, const oclMat& R1, oclMat& flowx, oclMat &flowy,
1599 oclMat& M, oclMat &bufM, int blockSize, bool updateMatrices);
1601 void updateFlow_gaussianBlur(
1602 const oclMat& R0, const oclMat& R1, oclMat& flowx, oclMat& flowy,
1603 oclMat& M, oclMat &bufM, int blockSize, bool updateMatrices);
1606 oclMat pyrLevel_[2], M_, bufM_, R_[2], blurredFrame_[2];
1607 std::vector<oclMat> pyramid0_, pyramid1_;
1610 //////////////// build warping maps ////////////////////
1611 //! builds plane warping maps
1612 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);
1613 //! builds cylindrical warping maps
1614 CV_EXPORTS void buildWarpCylindricalMaps(Size src_size, Rect dst_roi, const Mat &K, const Mat &R, float scale, oclMat &map_x, oclMat &map_y);
1615 //! builds spherical warping maps
1616 CV_EXPORTS void buildWarpSphericalMaps(Size src_size, Rect dst_roi, const Mat &K, const Mat &R, float scale, oclMat &map_x, oclMat &map_y);
1617 //! builds Affine warping maps
1618 CV_EXPORTS void buildWarpAffineMaps(const Mat &M, bool inverse, Size dsize, oclMat &xmap, oclMat &ymap);
1620 //! builds Perspective warping maps
1621 CV_EXPORTS void buildWarpPerspectiveMaps(const Mat &M, bool inverse, Size dsize, oclMat &xmap, oclMat &ymap);
1623 ///////////////////////////////////// interpolate frames //////////////////////////////////////////////
1624 //! Interpolate frames (images) using provided optical flow (displacement field).
1625 //! frame0 - frame 0 (32-bit floating point images, single channel)
1626 //! frame1 - frame 1 (the same type and size)
1627 //! fu - forward horizontal displacement
1628 //! fv - forward vertical displacement
1629 //! bu - backward horizontal displacement
1630 //! bv - backward vertical displacement
1631 //! pos - new frame position
1632 //! newFrame - new frame
1633 //! buf - temporary buffer, will have width x 6*height size, CV_32FC1 type and contain 6 oclMat;
1634 //! occlusion masks 0, occlusion masks 1,
1635 //! interpolated forward flow 0, interpolated forward flow 1,
1636 //! interpolated backward flow 0, interpolated backward flow 1
1638 CV_EXPORTS void interpolateFrames(const oclMat &frame0, const oclMat &frame1,
1639 const oclMat &fu, const oclMat &fv,
1640 const oclMat &bu, const oclMat &bv,
1641 float pos, oclMat &newFrame, oclMat &buf);
1643 //! computes moments of the rasterized shape or a vector of points
1644 //! _array should be a vector a points standing for the contour
1645 CV_EXPORTS Moments ocl_moments(InputArray contour);
1646 //! src should be a general image uploaded to the GPU.
1647 //! the supported oclMat type are CV_8UC1, CV_16UC1, CV_16SC1, CV_32FC1 and CV_64FC1
1648 //! to use type of CV_64FC1, the GPU should support CV_64FC1
1649 CV_EXPORTS Moments ocl_moments(oclMat& src, bool binary);
1651 class CV_EXPORTS StereoBM_OCL
1654 enum { BASIC_PRESET = 0, PREFILTER_XSOBEL = 1 };
1656 enum { DEFAULT_NDISP = 64, DEFAULT_WINSZ = 19 };
1658 //! the default constructor
1660 //! the full constructor taking the camera-specific preset, number of disparities and the SAD window size. ndisparities must be multiple of 8.
1661 StereoBM_OCL(int preset, int ndisparities = DEFAULT_NDISP, int winSize = DEFAULT_WINSZ);
1663 //! the stereo correspondence operator. Finds the disparity for the specified rectified stereo pair
1664 //! Output disparity has CV_8U type.
1665 void operator() ( const oclMat &left, const oclMat &right, oclMat &disparity);
1667 //! Some heuristics that tries to estmate
1668 // if current GPU will be faster then CPU in this algorithm.
1669 // It queries current active device.
1670 static bool checkIfGpuCallReasonable();
1676 // If avergeTexThreshold == 0 => post procesing is disabled
1677 // If avergeTexThreshold != 0 then disparity is set 0 in each point (x,y) where for left image
1678 // SumOfHorizontalGradiensInWindow(x, y, winSize) < (winSize * winSize) * avergeTexThreshold
1679 // i.e. input left image is low textured.
1680 float avergeTexThreshold;
1682 oclMat minSSD, leBuf, riBuf;
1685 class CV_EXPORTS StereoBeliefPropagation
1688 enum { DEFAULT_NDISP = 64 };
1689 enum { DEFAULT_ITERS = 5 };
1690 enum { DEFAULT_LEVELS = 5 };
1691 static void estimateRecommendedParams(int width, int height, int &ndisp, int &iters, int &levels);
1692 explicit StereoBeliefPropagation(int ndisp = DEFAULT_NDISP,
1693 int iters = DEFAULT_ITERS,
1694 int levels = DEFAULT_LEVELS,
1695 int msg_type = CV_16S);
1696 StereoBeliefPropagation(int ndisp, int iters, int levels,
1697 float max_data_term, float data_weight,
1698 float max_disc_term, float disc_single_jump,
1699 int msg_type = CV_32F);
1700 void operator()(const oclMat &left, const oclMat &right, oclMat &disparity);
1701 void operator()(const oclMat &data, oclMat &disparity);
1705 float max_data_term;
1707 float max_disc_term;
1708 float disc_single_jump;
1711 oclMat u, d, l, r, u2, d2, l2, r2;
1712 std::vector<oclMat> datas;
1716 class CV_EXPORTS StereoConstantSpaceBP
1719 enum { DEFAULT_NDISP = 128 };
1720 enum { DEFAULT_ITERS = 8 };
1721 enum { DEFAULT_LEVELS = 4 };
1722 enum { DEFAULT_NR_PLANE = 4 };
1723 static void estimateRecommendedParams(int width, int height, int &ndisp, int &iters, int &levels, int &nr_plane);
1724 explicit StereoConstantSpaceBP(
1725 int ndisp = DEFAULT_NDISP,
1726 int iters = DEFAULT_ITERS,
1727 int levels = DEFAULT_LEVELS,
1728 int nr_plane = DEFAULT_NR_PLANE,
1729 int msg_type = CV_32F);
1730 StereoConstantSpaceBP(int ndisp, int iters, int levels, int nr_plane,
1731 float max_data_term, float data_weight, float max_disc_term, float disc_single_jump,
1732 int min_disp_th = 0,
1733 int msg_type = CV_32F);
1734 void operator()(const oclMat &left, const oclMat &right, oclMat &disparity);
1739 float max_data_term;
1741 float max_disc_term;
1742 float disc_single_jump;
1745 bool use_local_init_data_cost;
1747 oclMat u[2], d[2], l[2], r[2];
1748 oclMat disp_selected_pyr[2];
1750 oclMat data_cost_selected;
1755 // Implementation of the Zach, Pock and Bischof Dual TV-L1 Optical Flow method
1758 // [1] C. Zach, T. Pock and H. Bischof, "A Duality Based Approach for Realtime TV-L1 Optical Flow".
1759 // [2] Javier Sanchez, Enric Meinhardt-Llopis and Gabriele Facciolo. "TV-L1 Optical Flow Estimation".
1760 class CV_EXPORTS OpticalFlowDual_TVL1_OCL
1763 OpticalFlowDual_TVL1_OCL();
1765 void operator ()(const oclMat& I0, const oclMat& I1, oclMat& flowx, oclMat& flowy);
1767 void collectGarbage();
1770 * Time step of the numerical scheme.
1775 * Weight parameter for the data term, attachment parameter.
1776 * This is the most relevant parameter, which determines the smoothness of the output.
1777 * The smaller this parameter is, the smoother the solutions we obtain.
1778 * It depends on the range of motions of the images, so its value should be adapted to each image sequence.
1783 * Weight parameter for (u - v)^2, tightness parameter.
1784 * It serves as a link between the attachment and the regularization terms.
1785 * In theory, it should have a small value in order to maintain both parts in correspondence.
1786 * The method is stable for a large range of values of this parameter.
1791 * Number of scales used to create the pyramid of images.
1796 * Number of warpings per scale.
1797 * Represents the number of times that I1(x+u0) and grad( I1(x+u0) ) are computed per scale.
1798 * This is a parameter that assures the stability of the method.
1799 * It also affects the running time, so it is a compromise between speed and accuracy.
1804 * Stopping criterion threshold used in the numerical scheme, which is a trade-off between precision and running time.
1805 * A small value will yield more accurate solutions at the expense of a slower convergence.
1810 * Stopping criterion iterations number used in the numerical scheme.
1814 bool useInitialFlow;
1817 void procOneScale(const oclMat& I0, const oclMat& I1, oclMat& u1, oclMat& u2);
1819 std::vector<oclMat> I0s;
1820 std::vector<oclMat> I1s;
1821 std::vector<oclMat> u1s;
1822 std::vector<oclMat> u2s;
1842 // current supported sorting methods
1845 SORT_BITONIC, // only support power-of-2 buffer size
1846 SORT_SELECTION, // cannot sort duplicate keys
1848 SORT_RADIX // only support signed int/float keys(CV_32S/CV_32F)
1850 //! Returns the sorted result of all the elements in input based on equivalent keys.
1852 // The element unit in the values to be sorted is determined from the data type,
1853 // i.e., a CV_32FC2 input {a1a2, b1b2} will be considered as two elements, regardless its
1854 // matrix dimension.
1855 // both keys and values will be sorted inplace
1856 // Key needs to be single channel oclMat.
1860 // keys = {2, 3, 1} (CV_8UC1)
1861 // values = {10,5, 4,3, 6,2} (CV_8UC2)
1862 // sortByKey(keys, values, SORT_SELECTION, false);
1864 // keys = {1, 2, 3} (CV_8UC1)
1865 // values = {6,2, 10,5, 4,3} (CV_8UC2)
1866 CV_EXPORTS void sortByKey(oclMat& keys, oclMat& values, int method, bool isGreaterThan = false);
1867 /*!Base class for MOG and MOG2!*/
1868 class CV_EXPORTS BackgroundSubtractor
1871 //! the virtual destructor
1872 virtual ~BackgroundSubtractor();
1873 //! the update operator that takes the next video frame and returns the current foreground mask as 8-bit binary image.
1874 virtual void operator()(const oclMat& image, oclMat& fgmask, float learningRate);
1876 //! computes a background image
1877 virtual void getBackgroundImage(oclMat& backgroundImage) const = 0;
1880 Gaussian Mixture-based Backbround/Foreground Segmentation Algorithm
1882 The class implements the following algorithm:
1883 "An improved adaptive background mixture model for real-time tracking with shadow detection"
1884 P. KadewTraKuPong and R. Bowden,
1885 Proc. 2nd European Workshp on Advanced Video-Based Surveillance Systems, 2001."
1886 http://personal.ee.surrey.ac.uk/Personal/R.Bowden/publications/avbs01/avbs01.pdf
1888 class CV_EXPORTS MOG: public cv::ocl::BackgroundSubtractor
1891 //! the default constructor
1892 MOG(int nmixtures = -1);
1894 //! re-initiaization method
1895 void initialize(Size frameSize, int frameType);
1897 //! the update operator
1898 void operator()(const oclMat& frame, oclMat& fgmask, float learningRate = 0.f);
1900 //! computes a background image which are the mean of all background gaussians
1901 void getBackgroundImage(oclMat& backgroundImage) const;
1903 //! releases all inner buffers
1908 float backgroundRatio;
1925 The class implements the following algorithm:
1926 "Improved adaptive Gausian mixture model for background subtraction"
1928 International Conference Pattern Recognition, UK, August, 2004.
1929 http://www.zoranz.net/Publications/zivkovic2004ICPR.pdf
1931 class CV_EXPORTS MOG2: public cv::ocl::BackgroundSubtractor
1934 //! the default constructor
1935 MOG2(int nmixtures = -1);
1937 //! re-initiaization method
1938 void initialize(Size frameSize, int frameType);
1940 //! the update operator
1941 void operator()(const oclMat& frame, oclMat& fgmask, float learningRate = -1.0f);
1943 //! computes a background image which are the mean of all background gaussians
1944 void getBackgroundImage(oclMat& backgroundImage) const;
1946 //! releases all inner buffers
1950 // you should call initialize after parameters changes
1954 //! here it is the maximum allowed number of mixture components.
1955 //! Actual number is determined dynamically per pixel
1957 // threshold on the squared Mahalanobis distance to decide if it is well described
1958 // by the background model or not. Related to Cthr from the paper.
1959 // This does not influence the update of the background. A typical value could be 4 sigma
1960 // and that is varThreshold=4*4=16; Corresponds to Tb in the paper.
1962 /////////////////////////
1963 // less important parameters - things you might change but be carefull
1964 ////////////////////////
1966 float backgroundRatio;
1967 // corresponds to fTB=1-cf from the paper
1968 // TB - threshold when the component becomes significant enough to be included into
1969 // the background model. It is the TB=1-cf from the paper. So I use cf=0.1 => TB=0.
1970 // For alpha=0.001 it means that the mode should exist for approximately 105 frames before
1971 // it is considered foreground
1972 // float noiseSigma;
1973 float varThresholdGen;
1975 //correspondts to Tg - threshold on the squared Mahalan. dist. to decide
1976 //when a sample is close to the existing components. If it is not close
1977 //to any a new component will be generated. I use 3 sigma => Tg=3*3=9.
1978 //Smaller Tg leads to more generated components and higher Tg might make
1979 //lead to small number of components but they can grow too large
1984 //initial variance for the newly generated components.
1985 //It will will influence the speed of adaptation. A good guess should be made.
1986 //A simple way is to estimate the typical standard deviation from the images.
1987 //I used here 10 as a reasonable value
1988 // min and max can be used to further control the variance
1989 float fCT; //CT - complexity reduction prior
1990 //this is related to the number of samples needed to accept that a component
1991 //actually exists. We use CT=0.05 of all the samples. By setting CT=0 you get
1992 //the standard Stauffer&Grimson algorithm (maybe not exact but very similar)
1994 //shadow detection parameters
1995 bool bShadowDetection; //default 1 - do shadow detection
1996 unsigned char nShadowDetection; //do shadow detection - insert this value as the detection result - 127 default value
1998 // Tau - shadow threshold. The shadow is detected if the pixel is darker
1999 //version of the background. Tau is a threshold on how much darker the shadow can be.
2000 //Tau= 0.5 means that if pixel is more than 2 times darker then it is not shadow
2001 //See: Prati,Mikic,Trivedi,Cucchiarra,"Detecting Moving Shadows...",IEEE PAMI,2003.
2014 oclMat bgmodelUsedModes_; //keep track of number of modes per pixel
2017 /*!***************Kalman Filter*************!*/
2018 class CV_EXPORTS KalmanFilter
2022 //! the full constructor taking the dimensionality of the state, of the measurement and of the control vector
2023 KalmanFilter(int dynamParams, int measureParams, int controlParams=0, int type=CV_32F);
2024 //! re-initializes Kalman filter. The previous content is destroyed.
2025 void init(int dynamParams, int measureParams, int controlParams=0, int type=CV_32F);
2027 const oclMat& predict(const oclMat& control=oclMat());
2028 const oclMat& correct(const oclMat& measurement);
2030 oclMat statePre; //!< predicted state (x'(k)): x(k)=A*x(k-1)+B*u(k)
2031 oclMat statePost; //!< corrected state (x(k)): x(k)=x'(k)+K(k)*(z(k)-H*x'(k))
2032 oclMat transitionMatrix; //!< state transition matrix (A)
2033 oclMat controlMatrix; //!< control matrix (B) (not used if there is no control)
2034 oclMat measurementMatrix; //!< measurement matrix (H)
2035 oclMat processNoiseCov; //!< process noise covariance matrix (Q)
2036 oclMat measurementNoiseCov;//!< measurement noise covariance matrix (R)
2037 oclMat errorCovPre; //!< priori error estimate covariance matrix (P'(k)): P'(k)=A*P(k-1)*At + Q)*/
2038 oclMat gain; //!< Kalman gain matrix (K(k)): K(k)=P'(k)*Ht*inv(H*P'(k)*Ht+R)
2039 oclMat errorCovPost; //!< posteriori error estimate covariance matrix (P(k)): P(k)=(I-K(k)*H)*P'(k)
2048 /*!***************K Nearest Neighbour*************!*/
2049 class CV_EXPORTS KNearestNeighbour: public CvKNearest
2052 KNearestNeighbour();
2053 ~KNearestNeighbour();
2055 bool train(const Mat& trainData, Mat& labels, Mat& sampleIdx = Mat().setTo(Scalar::all(0)),
2056 bool isRegression = false, int max_k = 32, bool updateBase = false);
2060 void find_nearest(const oclMat& samples, int k, oclMat& lables);
2066 /*!*************** SVM *************!*/
2067 class CV_EXPORTS CvSVM_OCL : public CvSVM
2072 CvSVM_OCL(const cv::Mat& trainData, const cv::Mat& responses,
2073 const cv::Mat& varIdx=cv::Mat(), const cv::Mat& sampleIdx=cv::Mat(),
2074 CvSVMParams params=CvSVMParams());
2075 CV_WRAP float predict( const int row_index, Mat& src, bool returnDFVal=false ) const;
2076 CV_WRAP void predict( cv::InputArray samples, cv::OutputArray results ) const;
2077 CV_WRAP float predict( const cv::Mat& sample, bool returnDFVal=false ) const;
2078 float predict( const CvMat* samples, CV_OUT CvMat* results ) const;
2081 float predict( const int row_index, int row_len, Mat& src, bool returnDFVal=false ) const;
2082 void create_kernel();
2083 void create_solver();
2086 /*!*************** END *************!*/
2089 #if defined _MSC_VER && _MSC_VER >= 1200
2090 # pragma warning( push)
2091 # pragma warning( disable: 4267)
2093 #include "opencv2/ocl/matrix_operations.hpp"
2094 #if defined _MSC_VER && _MSC_VER >= 1200
2095 # pragma warning( pop)
2098 #endif /* __OPENCV_OCL_HPP__ */