gpumat.hpp
Go to the documentation of this file.
1 /*M///////////////////////////////////////////////////////////////////////////////////////
2 //
3 // IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
4 //
5 // By downloading, copying, installing or using the software you agree to this license.
6 // If you do not agree to this license, do not download, install,
7 // copy or use the software.
8 //
9 //
10 // License Agreement
11 // For Open Source Computer Vision Library
12 //
13 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
14 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
15 // Third party copyrights are property of their respective owners.
16 //
17 // Redistribution and use in source and binary forms, with or without modification,
18 // are permitted provided that the following conditions are met:
19 //
20 // * Redistribution's of source code must retain the above copyright notice,
21 // this list of conditions and the following disclaimer.
22 //
23 // * Redistribution's in binary form must reproduce the above copyright notice,
24 // this list of conditions and the following disclaimer in the documentation
25 // and/or other materials provided with the distribution.
26 //
27 // * The name of the copyright holders may not be used to endorse or promote products
28 // derived from this software without specific prior written permission.
29 //
30 // This software is provided by the copyright holders and contributors "as is" and
31 // any express or implied warranties, including, but not limited to, the implied
32 // warranties of merchantability and fitness for a particular purpose are disclaimed.
33 // In no event shall the Intel Corporation or contributors be liable for any direct,
34 // indirect, incidental, special, exemplary, or consequential damages
35 // (including, but not limited to, procurement of substitute goods or services;
36 // loss of use, data, or profits; or business interruption) however caused
37 // and on any theory of liability, whether in contract, strict liability,
38 // or tort (including negligence or otherwise) arising in any way out of
39 // the use of this software, even if advised of the possibility of such damage.
40 //
41 //M*/
42 
43 #ifndef __OPENCV_GPUMAT_HPP__
44 #define __OPENCV_GPUMAT_HPP__
45 
46 #ifdef __cplusplus
47 
48 #include "opencv2/core/core.hpp"
50 
51 namespace cv { namespace gpu
52 {
54 
56  CV_EXPORTS int getCudaEnabledDeviceCount();
57 
59 
60  CV_EXPORTS void setDevice(int device);
61  CV_EXPORTS int getDevice();
62 
65  CV_EXPORTS void resetDevice();
66 
68  {
77 
83  };
84 
85  // Checks whether current device supports the given feature
86  CV_EXPORTS bool deviceSupports(FeatureSet feature_set);
87 
88  // Gives information about what GPU archs this OpenCV GPU module was
89  // compiled for
90  class CV_EXPORTS TargetArchs
91  {
92  public:
93  static bool builtWith(FeatureSet feature_set);
94  static bool has(int major, int minor);
95  static bool hasPtx(int major, int minor);
96  static bool hasBin(int major, int minor);
97  static bool hasEqualOrLessPtx(int major, int minor);
98  static bool hasEqualOrGreater(int major, int minor);
99  static bool hasEqualOrGreaterPtx(int major, int minor);
100  static bool hasEqualOrGreaterBin(int major, int minor);
101  private:
102  TargetArchs();
103  };
104 
105  // Gives information about the given GPU
106  class CV_EXPORTS DeviceInfo
107  {
108  public:
109  // Creates DeviceInfo object for the current GPU
110  DeviceInfo() : device_id_(getDevice()) { query(); }
111 
112  // Creates DeviceInfo object for the given GPU
113  DeviceInfo(int device_id) : device_id_(device_id) { query(); }
114 
115  std::string name() const { return name_; }
116 
117  // Return compute capability versions
118  int majorVersion() const { return majorVersion_; }
119  int minorVersion() const { return minorVersion_; }
120 
121  int multiProcessorCount() const { return multi_processor_count_; }
122 
123  size_t sharedMemPerBlock() const;
124 
125  void queryMemory(size_t& totalMemory, size_t& freeMemory) const;
126  size_t freeMemory() const;
127  size_t totalMemory() const;
128 
129  // Checks whether device supports the given feature
130  bool supports(FeatureSet feature_set) const;
131 
132  // Checks whether the GPU module can be run on the given device
133  bool isCompatible() const;
134 
135  int deviceID() const { return device_id_; }
136 
137  private:
138  void query();
139 
140  int device_id_;
141 
142  std::string name_;
143  int multi_processor_count_;
144  int majorVersion_;
145  int minorVersion_;
146  };
147 
148  CV_EXPORTS void printCudaDeviceInfo(int device);
149  CV_EXPORTS void printShortCudaDeviceInfo(int device);
150 
152 
154  class CV_EXPORTS GpuMat
155  {
156  public:
158  GpuMat();
159 
161  GpuMat(int rows, int cols, int type);
162  GpuMat(Size size, int type);
163 
165  GpuMat(int rows, int cols, int type, Scalar s);
166  GpuMat(Size size, int type, Scalar s);
167 
169  GpuMat(const GpuMat& m);
170 
172  GpuMat(int rows, int cols, int type, void* data, size_t step = Mat::AUTO_STEP);
173  GpuMat(Size size, int type, void* data, size_t step = Mat::AUTO_STEP);
174 
176  GpuMat(const GpuMat& m, Range rowRange, Range colRange);
177  GpuMat(const GpuMat& m, Rect roi);
178 
180  explicit GpuMat(const Mat& m);
181 
183  ~GpuMat();
184 
186  GpuMat& operator = (const GpuMat& m);
187 
189  void upload(const Mat& m);
190 
192  void download(Mat& m) const;
193 
195  GpuMat row(int y) const;
197  GpuMat col(int x) const;
199  GpuMat rowRange(int startrow, int endrow) const;
200  GpuMat rowRange(Range r) const;
202  GpuMat colRange(int startcol, int endcol) const;
203  GpuMat colRange(Range r) const;
204 
206  GpuMat clone() const;
208  // It calls m.create(this->size(), this->type()).
209  void copyTo(GpuMat& m) const;
211  void copyTo(GpuMat& m, const GpuMat& mask) const;
213  void convertTo(GpuMat& m, int rtype, double alpha = 1, double beta = 0) const;
214 
215  void assignTo(GpuMat& m, int type=-1) const;
216 
218  GpuMat& operator = (Scalar s);
220  GpuMat& setTo(Scalar s, const GpuMat& mask = GpuMat());
222  // number of channels and/or different number of rows. see cvReshape.
223  GpuMat reshape(int cn, int rows = 0) const;
224 
226  // previous data is unreferenced if needed.
227  void create(int rows, int cols, int type);
228  void create(Size size, int type);
230  // deallocate the data when reference counter reaches 0.
231  void release();
232 
234  void swap(GpuMat& mat);
235 
237  void locateROI(Size& wholeSize, Point& ofs) const;
239  GpuMat& adjustROI(int dtop, int dbottom, int dleft, int dright);
241  // (this is a generalized form of row, rowRange etc.)
242  GpuMat operator()(Range rowRange, Range colRange) const;
243  GpuMat operator()(Rect roi) const;
244 
246  // (i.e. when there are no gaps between successive rows).
247  // similar to CV_IS_GpuMat_CONT(cvGpuMat->type)
248  bool isContinuous() const;
250  // similar to CV_ELEM_SIZE(cvMat->type)
251  size_t elemSize() const;
253  size_t elemSize1() const;
255  int type() const;
257  int depth() const;
259  int channels() const;
261  size_t step1() const;
263  // width == number of columns, height == number of rows
264  Size size() const;
266  bool empty() const;
267 
269  uchar* ptr(int y = 0);
270  const uchar* ptr(int y = 0) const;
271 
273  template<typename _Tp> _Tp* ptr(int y = 0);
274  template<typename _Tp> const _Tp* ptr(int y = 0) const;
275 
276  template <typename _Tp> operator PtrStepSz<_Tp>() const;
277  template <typename _Tp> operator PtrStep<_Tp>() const;
278 
279  // Deprecated function
280  __CV_GPU_DEPR_BEFORE__ template <typename _Tp> operator DevMem2D_<_Tp>() const __CV_GPU_DEPR_AFTER__;
281  __CV_GPU_DEPR_BEFORE__ template <typename _Tp> operator PtrStep_<_Tp>() const __CV_GPU_DEPR_AFTER__;
282  #undef __CV_GPU_DEPR_BEFORE__
283  #undef __CV_GPU_DEPR_AFTER__
284 
291  int flags;
292 
294  int rows, cols;
295 
297  size_t step;
298 
301 
303  // when GpuMatrix points to user-allocated data, the pointer is NULL
304  int* refcount;
305 
309  };
310 
312  CV_EXPORTS void createContinuous(int rows, int cols, int type, GpuMat& m);
313  CV_EXPORTS GpuMat createContinuous(int rows, int cols, int type);
314  CV_EXPORTS void createContinuous(Size size, int type, GpuMat& m);
315  CV_EXPORTS GpuMat createContinuous(Size size, int type);
316 
319  CV_EXPORTS void ensureSizeIsEnough(int rows, int cols, int type, GpuMat& m);
320  CV_EXPORTS void ensureSizeIsEnough(Size size, int type, GpuMat& m);
321 
322  CV_EXPORTS GpuMat allocMatFromBuf(int rows, int cols, int type, GpuMat &mat);
323 
325  // Error handling
326 
327  CV_EXPORTS void error(const char* error_string, const char* file, const int line, const char* func = "");
328 
332 
333  inline GpuMat::GpuMat()
334  : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0)
335  {
336  }
337 
338  inline GpuMat::GpuMat(int rows_, int cols_, int type_)
339  : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0)
340  {
341  if (rows_ > 0 && cols_ > 0)
342  create(rows_, cols_, type_);
343  }
344 
345  inline GpuMat::GpuMat(Size size_, int type_)
346  : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0)
347  {
348  if (size_.height > 0 && size_.width > 0)
349  create(size_.height, size_.width, type_);
350  }
351 
352  inline GpuMat::GpuMat(int rows_, int cols_, int type_, Scalar s_)
353  : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0)
354  {
355  if (rows_ > 0 && cols_ > 0)
356  {
357  create(rows_, cols_, type_);
358  setTo(s_);
359  }
360  }
361 
362  inline GpuMat::GpuMat(Size size_, int type_, Scalar s_)
363  : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0)
364  {
365  if (size_.height > 0 && size_.width > 0)
366  {
367  create(size_.height, size_.width, type_);
368  setTo(s_);
369  }
370  }
371 
373  {
374  release();
375  }
376 
377  inline GpuMat GpuMat::clone() const
378  {
379  GpuMat m;
380  copyTo(m);
381  return m;
382  }
383 
384  inline void GpuMat::assignTo(GpuMat& m, int _type) const
385  {
386  if (_type < 0)
387  m = *this;
388  else
389  convertTo(m, _type);
390  }
391 
392  inline size_t GpuMat::step1() const
393  {
394  return step / elemSize1();
395  }
396 
397  inline bool GpuMat::empty() const
398  {
399  return data == 0;
400  }
401 
402  template<typename _Tp> inline _Tp* GpuMat::ptr(int y)
403  {
404  return (_Tp*)ptr(y);
405  }
406 
407  template<typename _Tp> inline const _Tp* GpuMat::ptr(int y) const
408  {
409  return (const _Tp*)ptr(y);
410  }
411 
412  inline void swap(GpuMat& a, GpuMat& b)
413  {
414  a.swap(b);
415  }
416 
417  inline GpuMat GpuMat::row(int y) const
418  {
419  return GpuMat(*this, Range(y, y+1), Range::all());
420  }
421 
422  inline GpuMat GpuMat::col(int x) const
423  {
424  return GpuMat(*this, Range::all(), Range(x, x+1));
425  }
426 
427  inline GpuMat GpuMat::rowRange(int startrow, int endrow) const
428  {
429  return GpuMat(*this, Range(startrow, endrow), Range::all());
430  }
431 
433  {
434  return GpuMat(*this, r, Range::all());
435  }
436 
437  inline GpuMat GpuMat::colRange(int startcol, int endcol) const
438  {
439  return GpuMat(*this, Range::all(), Range(startcol, endcol));
440  }
441 
443  {
444  return GpuMat(*this, Range::all(), r);
445  }
446 
447  inline void GpuMat::create(Size size_, int type_)
448  {
449  create(size_.height, size_.width, type_);
450  }
451 
452  inline GpuMat GpuMat::operator()(Range _rowRange, Range _colRange) const
453  {
454  return GpuMat(*this, _rowRange, _colRange);
455  }
456 
458  {
459  return GpuMat(*this, roi);
460  }
461 
462  inline bool GpuMat::isContinuous() const
463  {
464  return (flags & Mat::CONTINUOUS_FLAG) != 0;
465  }
466 
467  inline size_t GpuMat::elemSize() const
468  {
469  return CV_ELEM_SIZE(flags);
470  }
471 
472  inline size_t GpuMat::elemSize1() const
473  {
474  return CV_ELEM_SIZE1(flags);
475  }
476 
477  inline int GpuMat::type() const
478  {
479  return CV_MAT_TYPE(flags);
480  }
481 
482  inline int GpuMat::depth() const
483  {
484  return CV_MAT_DEPTH(flags);
485  }
486 
487  inline int GpuMat::channels() const
488  {
489  return CV_MAT_CN(flags);
490  }
491 
492  inline Size GpuMat::size() const
493  {
494  return Size(cols, rows);
495  }
496 
497  inline uchar* GpuMat::ptr(int y)
498  {
499  CV_DbgAssert((unsigned)y < (unsigned)rows);
500  return data + step * y;
501  }
502 
503  inline const uchar* GpuMat::ptr(int y) const
504  {
505  CV_DbgAssert((unsigned)y < (unsigned)rows);
506  return data + step * y;
507  }
508 
510  {
511  setTo(s);
512  return *this;
513  }
514 
515  template <class T> inline GpuMat::operator PtrStepSz<T>() const
516  {
517  return PtrStepSz<T>(rows, cols, (T*)data, step);
518  }
519 
520  template <class T> inline GpuMat::operator PtrStep<T>() const
521  {
522  return PtrStep<T>((T*)data, step);
523  }
524 
525  template <class T> inline GpuMat::operator DevMem2D_<T>() const
526  {
527  return DevMem2D_<T>(rows, cols, (T*)data, step);
528  }
529 
530  template <class T> inline GpuMat::operator PtrStep_<T>() const
531  {
532  return PtrStep_<T>(static_cast< DevMem2D_<T> >(*this));
533  }
534 
535  inline GpuMat createContinuous(int rows, int cols, int type)
536  {
537  GpuMat m;
538  createContinuous(rows, cols, type, m);
539  return m;
540  }
541 
542  inline void createContinuous(Size size, int type, GpuMat& m)
543  {
544  createContinuous(size.height, size.width, type, m);
545  }
546 
548  {
549  GpuMat m;
550  createContinuous(size, type, m);
551  return m;
552  }
553 
554  inline void ensureSizeIsEnough(Size size, int type, GpuMat& m)
555  {
556  ensureSizeIsEnough(size.height, size.width, type, m);
557  }
558 }}
559 
560 #endif // __cplusplus
561 
562 #endif // __OPENCV_GPUMAT_HPP__
GLdouble GLdouble GLdouble r
Definition: core.hpp:1950
Definition: gpumat.hpp:76
GLenum GLint GLint y
Definition: core_c.h:613
IplImage CvRect * roi
Definition: legacy.hpp:234
CV_EXPORTS void createContinuous(int rows, int cols, int type, GpuMat &m)
Creates continuous GPU matrix.
uchar * dataend
Definition: gpumat.hpp:308
int depth
Definition: core_c.h:73
Definition: gpumat.hpp:78
CV_EXPORTS GpuMat allocMatFromBuf(int rows, int cols, int type, GpuMat &mat)
void copyTo(GpuMat &m) const
copies the GpuMatrix content to "m".
void swap(GpuMat &a, GpuMat &b)
Definition: gpumat.hpp:412
size_t elemSize() const
returns element size in bytes,
Definition: gpumat.hpp:467
Definition: gpumat.hpp:69
Size2i Size
Definition: core.hpp:896
int channels() const
returns element type, similar to CV_MAT_CN(cvMat->type)
Definition: gpumat.hpp:487
int cols
Definition: gpumat.hpp:294
CV_EXPORTS void printCudaDeviceInfo(int device)
CV_EXPORTS void printShortCudaDeviceInfo(int device)
CvCmpFunc func
Definition: core_c.h:1072
GpuMat rowRange(int startrow, int endrow) const
... for the specified row span
Definition: gpumat.hpp:427
GpuMat col(int x) const
returns a new GpuMatrix header for the specified column
Definition: gpumat.hpp:422
static Range all()
Definition: operations.hpp:2219
CV_EXPORTS int getCudaEnabledDeviceCount()
This is the only function that do not throw exceptions if the library is compiled without Cuda...
CvSize size
Definition: calib3d.hpp:212
The 2D range class.
Definition: core.hpp:979
void release()
decreases reference counter;
int deviceID() const
Definition: gpumat.hpp:135
int int int flags
Definition: highgui_c.h:186
GpuMat()
default constructor
Definition: gpumat.hpp:333
GLsizei GLsizei GLenum GLenum const GLvoid * data
Definition: core_c.h:403
Definition: gpumat.hpp:73
const CvMat const CvMat const CvMat CvMat CvMat CvMat CvMat CvSize CvMat CvMat * T
Definition: calib3d.hpp:270
int majorVersion() const
Definition: gpumat.hpp:118
CV_EXPORTS void resetDevice()
Definition: gpumat.hpp:75
internal::ParamGenerator< T > Range(T start, T end, IncrementT step)
Definition: ts_gtest.h:15869
cv::gpu::DevMem2D_ __CV_GPU_DEPR_AFTER__
void int step
Definition: core_c.h:403
void assignTo(GpuMat &m, int type=-1) const
Definition: gpumat.hpp:384
Definition: gpumat.hpp:79
std::string name() const
Definition: gpumat.hpp:115
The 2D size class.
Definition: core.hpp:81
Definition: cuda_devptrs.hpp:104
GpuMat & operator=(const GpuMat &m)
assignment operators
GpuMat clone() const
returns deep copy of the GpuMatrix, i.e. the data is copied
Definition: gpumat.hpp:377
Definition: gpumat.hpp:80
GLenum GLenum GLvoid * row
FeatureSet
Definition: gpumat.hpp:67
Definition: gpumat.hpp:106
int type() const
returns element type, similar to CV_MAT_TYPE(cvMat->type)
Definition: gpumat.hpp:477
CV_EXPORTS void error(const char *error_string, const char *file, const int line, const char *func="")
CV_EXPORTS int getDevice()
CvArr const CvMat * mat
Definition: core_c.h:700
int flags
includes several bit-fields:
Definition: gpumat.hpp:291
GLclampf GLclampf GLclampf alpha
Definition: core_c.h:687
int depth() const
returns element type, similar to CV_MAT_DEPTH(cvMat->type)
Definition: gpumat.hpp:482
Definition: gpumat.hpp:82
Definition: cuda_devptrs.hpp:138
Definition: gpumat.hpp:72
DeviceInfo(int device_id)
Definition: gpumat.hpp:113
size_t elemSize1() const
returns the size of element channel in bytes.
Definition: gpumat.hpp:472
void create(int rows, int cols, int type)
allocates new GpuMatrix data unless the GpuMatrix already has specified size and type.
Smart pointer for GPU memory with reference counting. Its interface is mostly similar with cv::Mat...
Definition: gpumat.hpp:154
GLenum GLint x
Definition: core_c.h:632
int * refcount
pointer to the reference counter;
Definition: gpumat.hpp:304
Definition: gpumat.hpp:71
Definition: cuda_devptrs.hpp:89
bool empty() const
returns true if GpuMatrix data is NULL
Definition: gpumat.hpp:397
bool isContinuous() const
returns true iff the GpuMatrix data is continuous
Definition: gpumat.hpp:462
uchar * ptr(int y=0)
returns pointer to y-th row
Definition: gpumat.hpp:497
double const CvArr double beta
Definition: core_c.h:523
The Core Functionality.
GLboolean GLboolean GLboolean b
Definition: legacy.hpp:633
size_t step
a distance between successive rows in bytes; includes the gap if any
Definition: gpumat.hpp:297
The n-dimensional matrix class.
Definition: core.hpp:1688
GpuMat row(int y) const
returns a new GpuMatrix header for the specified row
Definition: gpumat.hpp:417
int rows
Definition: core_c.h:114
int int y
Definition: highgui_c.h:186
Size size() const
returns GpuMatrix size:
Definition: gpumat.hpp:492
int cols
Definition: core_c.h:109
GLuint GLuint GLsizei GLenum type
Definition: core_c.h:114
void convertTo(GpuMat &m, int rtype, double alpha=1, double beta=0) const
converts GpuMatrix to another datatype with optional scalng. See cvConvertScale.
int int type
Definition: core_c.h:109
int int channels
Definition: core_c.h:73
GpuMat & setTo(Scalar s, const GpuMat &mask=GpuMat())
sets some of the GpuMatrix elements to s, according to the mask
Definition: gpumat.hpp:81
Definition: gpumat.hpp:70
size_t step1() const
returns step/elemSize1()
Definition: gpumat.hpp:392
template 2D point class.
Definition: core.hpp:82
_Tp width
Definition: core.hpp:840
~GpuMat()
destructor - calls release()
Definition: gpumat.hpp:372
const char * ptr
Definition: core_c.h:942
GLboolean GLboolean GLboolean GLboolean a
Definition: legacy.hpp:633
Definition: cuda_devptrs.hpp:167
uchar * datastart
helper fields used in locateROI and adjustROI
Definition: gpumat.hpp:307
Definition: gpumat.hpp:74
unsigned char uchar
Definition: types_c.h:170
GpuMat colRange(int startcol, int endcol) const
... for the specified column span
Definition: gpumat.hpp:437
Definition: core.hpp:1950
uchar * data
pointer to the data
Definition: gpumat.hpp:300
DeviceInfo()
Definition: gpumat.hpp:110
CV_EXPORTS void ensureSizeIsEnough(int rows, int cols, int type, GpuMat &m)
const GLfloat * m
_Tp height
Definition: core.hpp:840
GpuMat operator()(Range rowRange, Range colRange) const
extracts a rectangular sub-GpuMatrix
Definition: gpumat.hpp:452
CV_EXPORTS bool deviceSupports(FeatureSet feature_set)
GLdouble s
GLenum GLint GLuint mask
Definition: tracking.hpp:132
void swap(GpuMat &mat)
swaps with other smart pointer
int rows
the number of rows and columns
Definition: gpumat.hpp:294
GLsizeiptr size
Definition: core_c.h:939
int minorVersion() const
Definition: gpumat.hpp:119
int multiProcessorCount() const
Definition: gpumat.hpp:121
Definition: gpumat.hpp:90
CV_EXPORTS void setDevice(int device)
Functions below throw cv::Expception if the library is compiled without Cuda.
CV_EXPORTS_W void line(CV_IN_OUT Mat &img, Point pt1, Point pt2, const Scalar &color, int thickness=1, int lineType=8, int shift=0)
draws the line segment (pt1, pt2) in the image