include/opencv2/core/gpumat.hpp
Go to the documentation of this file.
00001 /*M///////////////////////////////////////////////////////////////////////////////////////
00002 //
00003 //  IMPORTANT: READ BEFORE DOWNLOADING, COPYING, INSTALLING OR USING.
00004 //
00005 //  By downloading, copying, installing or using the software you agree to this license.
00006 //  If you do not agree to this license, do not download, install,
00007 //  copy or use the software.
00008 //
00009 //
00010 //                           License Agreement
00011 //                For Open Source Computer Vision Library
00012 //
00013 // Copyright (C) 2000-2008, Intel Corporation, all rights reserved.
00014 // Copyright (C) 2009, Willow Garage Inc., all rights reserved.
00015 // Third party copyrights are property of their respective owners.
00016 //
00017 // Redistribution and use in source and binary forms, with or without modification,
00018 // are permitted provided that the following conditions are met:
00019 //
00020 //   * Redistribution's of source code must retain the above copyright notice,
00021 //     this list of conditions and the following disclaimer.
00022 //
00023 //   * Redistribution's in binary form must reproduce the above copyright notice,
00024 //     this list of conditions and the following disclaimer in the documentation
00025 //     and/or other GpuMaterials provided with the distribution.
00026 //
00027 //   * The name of the copyright holders may not be used to endorse or promote products
00028 //     derived from this software without specific prior written permission.
00029 //
00030 // This software is provided by the copyright holders and contributors "as is" and
00031 // any express or implied warranties, including, but not limited to, the implied
00032 // warranties of merchantability and fitness for a particular purpose are disclaimed.
00033 // In no event shall the Intel Corporation or contributors be liable for any direct,
00034 // indirect, incidental, special, exemplary, or consequential damages
00035 // (including, but not limited to, procurement of substitute goods or services;
00036 // loss of use, data, or profits; or business interruption) however caused
00037 // and on any theory of liability, whether in contract, strict liability,
00038 // or tort (including negligence or otherwise) arising in any way out of
00039 // the use of this software, even if advised of the possibility of such damage.
00040 //
00041 //M*/
00042 
00043 #ifndef __OPENCV_GPUMAT_HPP__
00044 #define __OPENCV_GPUMAT_HPP__
00045 
00046 #ifdef __cplusplus
00047 
00048 #include "opencv2/core/core.hpp"
00049 #include "opencv2/core/cuda_devptrs.hpp"
00050 
00051 namespace cv { namespace gpu
00052 {
00054 
00056     CV_EXPORTS int getCudaEnabledDeviceCount();
00057 
00059 
00060     CV_EXPORTS void setDevice(int device);
00061     CV_EXPORTS int getDevice();
00062 
00065     CV_EXPORTS void resetDevice();
00066 
00067     enum FeatureSet
00068     {
00069         FEATURE_SET_COMPUTE_10 = 10,
00070         FEATURE_SET_COMPUTE_11 = 11,
00071         FEATURE_SET_COMPUTE_12 = 12,
00072         FEATURE_SET_COMPUTE_13 = 13,
00073         FEATURE_SET_COMPUTE_20 = 20,
00074         FEATURE_SET_COMPUTE_21 = 21,
00075         FEATURE_SET_COMPUTE_30 = 30,
00076         GLOBAL_ATOMICS = FEATURE_SET_COMPUTE_11,
00077         SHARED_ATOMICS = FEATURE_SET_COMPUTE_12,
00078         NATIVE_DOUBLE = FEATURE_SET_COMPUTE_13,
00079         WARP_SHUFFLE_FUNCTIONS = FEATURE_SET_COMPUTE_30
00080     };
00081 
00082     // Gives information about what GPU archs this OpenCV GPU module was
00083     // compiled for
00084     class CV_EXPORTS TargetArchs
00085     {
00086     public:
00087         static bool builtWith(FeatureSet feature_set);
00088         static bool has(int major, int minor);
00089         static bool hasPtx(int major, int minor);
00090         static bool hasBin(int major, int minor);
00091         static bool hasEqualOrLessPtx(int major, int minor);
00092         static bool hasEqualOrGreater(int major, int minor);
00093         static bool hasEqualOrGreaterPtx(int major, int minor);
00094         static bool hasEqualOrGreaterBin(int major, int minor);
00095     private:
00096         TargetArchs();
00097     };
00098 
00099     // Gives information about the given GPU
00100     class CV_EXPORTS DeviceInfo
00101     {
00102     public:
00103         // Creates DeviceInfo object for the current GPU
00104         DeviceInfo() : device_id_(getDevice()) { query(); }
00105 
00106         // Creates DeviceInfo object for the given GPU
00107         DeviceInfo(int device_id) : device_id_(device_id) { query(); }
00108 
00109         std::string name() const { return name_; }
00110 
00111         // Return compute capability versions
00112         int majorVersion() const { return majorVersion_; }
00113         int minorVersion() const { return minorVersion_; }
00114 
00115         int multiProcessorCount() const { return multi_processor_count_; }
00116 
00117         size_t freeMemory() const;
00118         size_t totalMemory() const;
00119 
00120         // Checks whether device supports the given feature
00121         bool supports(FeatureSet feature_set) const;
00122 
00123         // Checks whether the GPU module can be run on the given device
00124         bool isCompatible() const;
00125 
00126         int deviceID() const { return device_id_; }
00127 
00128     private:
00129         void query();
00130         void queryMemory(size_t& free_memory, size_t& total_memory) const;
00131 
00132         int device_id_;
00133 
00134         std::string name_;
00135         int multi_processor_count_;
00136         int majorVersion_;
00137         int minorVersion_;
00138     };
00139 
00140     CV_EXPORTS void printCudaDeviceInfo(int device);
00141     CV_EXPORTS void printShortCudaDeviceInfo(int device);
00142 
00144 
00146     class CV_EXPORTS GpuMat
00147     {
00148     public:
00150         GpuMat();
00151 
00153         GpuMat(int rows, int cols, int type);
00154         GpuMat(Size size, int type);
00155 
00157         GpuMat(int rows, int cols, int type, Scalar s);
00158         GpuMat(Size size, int type, Scalar s);
00159 
00161         GpuMat(const GpuMat& m);
00162 
00164         GpuMat(int rows, int cols, int type, void* data, size_t step = Mat::AUTO_STEP);
00165         GpuMat(Size size, int type, void* data, size_t step = Mat::AUTO_STEP);
00166 
00168         GpuMat(const GpuMat& m, Range rowRange, Range colRange);
00169         GpuMat(const GpuMat& m, Rect roi);
00170 
00172         explicit GpuMat(const Mat& m);
00173 
00175         ~GpuMat();
00176 
00178         GpuMat& operator = (const GpuMat& m);
00179 
00181         void upload(const Mat& m);
00182 
00184         void download(Mat& m) const;
00185 
00187         GpuMat row(int y) const;
00189         GpuMat col(int x) const;
00191         GpuMat rowRange(int startrow, int endrow) const;
00192         GpuMat rowRange(Range r) const;
00194         GpuMat colRange(int startcol, int endcol) const;
00195         GpuMat colRange(Range r) const;
00196 
00198         GpuMat clone() const;
00200         // It calls m.create(this->size(), this->type()).
00201         void copyTo(GpuMat& m) const;
00203         void copyTo(GpuMat& m, const GpuMat& mask) const;
00205         void convertTo(GpuMat& m, int rtype, double alpha = 1, double beta = 0) const;
00206 
00207         void assignTo(GpuMat& m, int type=-1) const;
00208 
00210         GpuMat& operator = (Scalar s);
00212         GpuMat& setTo(Scalar s, const GpuMat& mask = GpuMat());
00214         // number of channels and/or different number of rows. see cvReshape.
00215         GpuMat reshape(int cn, int rows = 0) const;
00216 
00218         // previous data is unreferenced if needed.
00219         void create(int rows, int cols, int type);
00220         void create(Size size, int type);
00222         // deallocate the data when reference counter reaches 0.
00223         void release();
00224 
00226         void swap(GpuMat& mat);
00227 
00229         void locateROI(Size& wholeSize, Point& ofs) const;
00231         GpuMat& adjustROI(int dtop, int dbottom, int dleft, int dright);
00233         // (this is a generalized form of row, rowRange etc.)
00234         GpuMat operator()(Range rowRange, Range colRange) const;
00235         GpuMat operator()(Rect roi) const;
00236 
00238         // (i.e. when there are no gaps between successive rows).
00239         // similar to CV_IS_GpuMat_CONT(cvGpuMat->type)
00240         bool isContinuous() const;
00242         // similar to CV_ELEM_SIZE(cvMat->type)
00243         size_t elemSize() const;
00245         size_t elemSize1() const;
00247         int type() const;
00249         int depth() const;
00251         int channels() const;
00253         size_t step1() const;
00255         // width == number of columns, height == number of rows
00256         Size size() const;
00258         bool empty() const;
00259 
00261         uchar* ptr(int y = 0);
00262         const uchar* ptr(int y = 0) const;
00263 
00265         template<typename _Tp> _Tp* ptr(int y = 0);
00266         template<typename _Tp> const _Tp* ptr(int y = 0) const;
00267 
00268         template <typename _Tp> operator PtrStepSz<_Tp>() const;
00269         template <typename _Tp> operator PtrStep<_Tp>() const;
00270 
00271         // Deprecated function
00272         __CV_GPU_DEPR_BEFORE__ template <typename _Tp> operator DevMem2D_<_Tp>() const __CV_GPU_DEPR_AFTER__;
00273         __CV_GPU_DEPR_BEFORE__ template <typename _Tp> operator PtrStep_<_Tp>() const __CV_GPU_DEPR_AFTER__;
00274         #undef __CV_GPU_DEPR_BEFORE__
00275         #undef __CV_GPU_DEPR_AFTER__
00276 
00283         int flags;
00284 
00286         int rows, cols;
00287 
00289         size_t step;
00290 
00292         uchar* data;
00293 
00295         // when GpuMatrix points to user-allocated data, the pointer is NULL
00296         int* refcount;
00297 
00299         uchar* datastart;
00300         uchar* dataend;
00301     };
00302 
00304     CV_EXPORTS void createContinuous(int rows, int cols, int type, GpuMat& m);
00305     CV_EXPORTS GpuMat createContinuous(int rows, int cols, int type);
00306     CV_EXPORTS void createContinuous(Size size, int type, GpuMat& m);
00307     CV_EXPORTS GpuMat createContinuous(Size size, int type);
00308 
00311     CV_EXPORTS void ensureSizeIsEnough(int rows, int cols, int type, GpuMat& m);
00312     CV_EXPORTS void ensureSizeIsEnough(Size size, int type, GpuMat& m);
00313 
00314     CV_EXPORTS GpuMat allocMatFromBuf(int rows, int cols, int type, GpuMat &mat);
00315 
00317     // Error handling
00318 
00319     CV_EXPORTS void error(const char* error_string, const char* file, const int line, const char* func = "");
00320 
00324 
00325     inline GpuMat::GpuMat()
00326         : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0)
00327     {
00328     }
00329 
00330     inline GpuMat::GpuMat(int rows_, int cols_, int type_)
00331         : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0)
00332     {
00333         if (rows_ > 0 && cols_ > 0)
00334             create(rows_, cols_, type_);
00335     }
00336 
00337     inline GpuMat::GpuMat(Size size_, int type_)
00338         : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0)
00339     {
00340         if (size_.height > 0 && size_.width > 0)
00341             create(size_.height, size_.width, type_);
00342     }
00343 
00344     inline GpuMat::GpuMat(int rows_, int cols_, int type_, Scalar s_)
00345         : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0)
00346     {
00347         if (rows_ > 0 && cols_ > 0)
00348         {
00349             create(rows_, cols_, type_);
00350             setTo(s_);
00351         }
00352     }
00353 
00354     inline GpuMat::GpuMat(Size size_, int type_, Scalar s_)
00355         : flags(0), rows(0), cols(0), step(0), data(0), refcount(0), datastart(0), dataend(0)
00356     {
00357         if (size_.height > 0 && size_.width > 0)
00358         {
00359             create(size_.height, size_.width, type_);
00360             setTo(s_);
00361         }
00362     }
00363 
00364     inline GpuMat::~GpuMat()
00365     {
00366         release();
00367     }
00368 
00369     inline GpuMat GpuMat::clone() const
00370     {
00371         GpuMat m;
00372         copyTo(m);
00373         return m;
00374     }
00375 
00376     inline void GpuMat::assignTo(GpuMat& m, int _type) const
00377     {
00378         if (_type < 0)
00379             m = *this;
00380         else
00381             convertTo(m, _type);
00382     }
00383 
00384     inline size_t GpuMat::step1() const
00385     {
00386         return step / elemSize1();
00387     }
00388 
00389     inline bool GpuMat::empty() const
00390     {
00391         return data == 0;
00392     }
00393 
00394     template<typename _Tp> inline _Tp* GpuMat::ptr(int y)
00395     {
00396         return (_Tp*)ptr(y);
00397     }
00398 
00399     template<typename _Tp> inline const _Tp* GpuMat::ptr(int y) const
00400     {
00401         return (const _Tp*)ptr(y);
00402     }
00403 
00404     inline void swap(GpuMat& a, GpuMat& b)
00405     {
00406         a.swap(b);
00407     }
00408 
00409     inline GpuMat GpuMat::row(int y) const
00410     {
00411         return GpuMat(*this, Range(y, y+1), Range::all());
00412     }
00413 
00414     inline GpuMat GpuMat::col(int x) const
00415     {
00416         return GpuMat(*this, Range::all(), Range(x, x+1));
00417     }
00418 
00419     inline GpuMat GpuMat::rowRange(int startrow, int endrow) const
00420     {
00421         return GpuMat(*this, Range(startrow, endrow), Range::all());
00422     }
00423 
00424     inline GpuMat GpuMat::rowRange(Range r) const
00425     {
00426         return GpuMat(*this, r, Range::all());
00427     }
00428 
00429     inline GpuMat GpuMat::colRange(int startcol, int endcol) const
00430     {
00431         return GpuMat(*this, Range::all(), Range(startcol, endcol));
00432     }
00433 
00434     inline GpuMat GpuMat::colRange(Range r) const
00435     {
00436         return GpuMat(*this, Range::all(), r);
00437     }
00438 
00439     inline void GpuMat::create(Size size_, int type_)
00440     {
00441         create(size_.height, size_.width, type_);
00442     }
00443 
00444     inline GpuMat GpuMat::operator()(Range _rowRange, Range _colRange) const
00445     {
00446         return GpuMat(*this, _rowRange, _colRange);
00447     }
00448 
00449     inline GpuMat GpuMat::operator()(Rect roi) const
00450     {
00451         return GpuMat(*this, roi);
00452     }
00453 
00454     inline bool GpuMat::isContinuous() const
00455     {
00456         return (flags & Mat::CONTINUOUS_FLAG) != 0;
00457     }
00458 
00459     inline size_t GpuMat::elemSize() const
00460     {
00461         return CV_ELEM_SIZE(flags);
00462     }
00463 
00464     inline size_t GpuMat::elemSize1() const
00465     {
00466         return CV_ELEM_SIZE1(flags);
00467     }
00468 
00469     inline int GpuMat::type() const
00470     {
00471         return CV_MAT_TYPE(flags);
00472     }
00473 
00474     inline int GpuMat::depth() const
00475     {
00476         return CV_MAT_DEPTH(flags);
00477     }
00478 
00479     inline int GpuMat::channels() const
00480     {
00481         return CV_MAT_CN(flags);
00482     }
00483 
00484     inline Size GpuMat::size() const
00485     {
00486         return Size(cols, rows);
00487     }
00488 
00489     inline uchar* GpuMat::ptr(int y)
00490     {
00491         CV_DbgAssert((unsigned)y < (unsigned)rows);
00492         return data + step * y;
00493     }
00494 
00495     inline const uchar* GpuMat::ptr(int y) const
00496     {
00497         CV_DbgAssert((unsigned)y < (unsigned)rows);
00498         return data + step * y;
00499     }
00500 
00501     inline GpuMat& GpuMat::operator = (Scalar s)
00502     {
00503         setTo(s);
00504         return *this;
00505     }
00506 
00507     template <class T> inline GpuMat::operator PtrStepSz<T>() const
00508     {
00509         return PtrStepSz<T>(rows, cols, (T*)data, step);
00510     }
00511 
00512     template <class T> inline GpuMat::operator PtrStep<T>() const
00513     {
00514         return PtrStep<T>((T*)data, step);
00515     }
00516 
00517     template <class T> inline GpuMat::operator DevMem2D_<T>() const
00518     {
00519         return DevMem2D_<T>(rows, cols, (T*)data, step);
00520     }
00521 
00522     template <class T> inline GpuMat::operator PtrStep_<T>() const
00523     {
00524         return PtrStep_<T>(static_cast< DevMem2D_<T> >(*this));
00525     }
00526 
00527     inline GpuMat createContinuous(int rows, int cols, int type)
00528     {
00529         GpuMat m;
00530         createContinuous(rows, cols, type, m);
00531         return m;
00532     }
00533 
00534     inline void createContinuous(Size size, int type, GpuMat& m)
00535     {
00536         createContinuous(size.height, size.width, type, m);
00537     }
00538 
00539     inline GpuMat createContinuous(Size size, int type)
00540     {
00541         GpuMat m;
00542         createContinuous(size, type, m);
00543         return m;
00544     }
00545 
00546     inline void ensureSizeIsEnough(Size size, int type, GpuMat& m)
00547     {
00548         ensureSizeIsEnough(size.height, size.width, type, m);
00549     }
00550 
00551     inline void createContinuous(int rows, int cols, int type, GpuMat& m)
00552     {
00553         int area = rows * cols;
00554         if (!m.isContinuous() || m.type() != type || m.size().area() != area)
00555             ensureSizeIsEnough(1, area, type, m);
00556         m = m.reshape(0, rows);
00557     }
00558 
00559     inline void ensureSizeIsEnough(int rows, int cols, int type, GpuMat& m)
00560     {
00561         if (m.type() == type && m.rows >= rows && m.cols >= cols)
00562             m = m(Rect(0, 0, cols, rows));
00563         else
00564             m.create(rows, cols, type);
00565     }
00566 
00567     inline GpuMat allocMatFromBuf(int rows, int cols, int type, GpuMat &mat)
00568     {
00569         if (!mat.empty() && mat.type() == type && mat.rows >= rows && mat.cols >= cols)
00570             return mat(Rect(0, 0, cols, rows));
00571         return mat = GpuMat(rows, cols, type);
00572     }
00573 }}
00574 
00575 #endif // __cplusplus
00576 
00577 #endif // __OPENCV_GPUMAT_HPP__