template class cv::Mat_
Overview
Template matrix class derived from Mat. More…
#include <mat.hpp> template <typename _Tp> class Mat_: public cv::Mat { public: // typedefs typedef DataType<_Tp>::channel_type channel_type; typedef MatConstIterator_<_Tp> const_iterator; typedef MatIterator_<_Tp> iterator; typedef _Tp value_type; // construction Mat_(); Mat_( int _rows, int _cols ); Mat_( int _rows, int _cols, const _Tp& value ); Mat_(Size _size); Mat_( Size _size, const _Tp& value ); Mat_( int _ndims, const int* _sizes ); Mat_( int _ndims, const int* _sizes, const _Tp& value ); Mat_(const Mat& m); Mat_(const Mat_& m); Mat_( int _rows, int _cols, _Tp* _data, size_t _step = AUTO_STEP ); Mat_( int _ndims, const int* _sizes, _Tp* _data, const size_t* _steps = 0 ); Mat_( const Mat_& m, const Range& rowRange, const Range& colRange = Range::all() ); Mat_( const Mat_& m, const Rect& roi ); Mat_( const Mat_& m, const Range* ranges ); Mat_( const Mat_& m, const std::vector<Range>& ranges ); Mat_(const MatExpr& e); Mat_( const std::vector<_Tp>& vec, bool copyData = false ); template <int n> Mat_( const Vec<typename DataType<_Tp>::channel_type, n>& vec, bool copyData = true ); template < int m, int n > Mat_( const Matx<typename DataType<_Tp>::channel_type, m, n>& mtx, bool copyData = true ); Mat_( const Point_<typename DataType<_Tp>::channel_type>& pt, bool copyData = true ); Mat_( const Point3_<typename DataType<_Tp>::channel_type>& pt, bool copyData = true ); Mat_(const MatCommaInitializer_<_Tp>& commaInitializer); // methods Mat_& adjustROI( int dtop, int dbottom, int dleft, int dright ); iterator begin(); const_iterator begin() const; int channels() const; Mat_ clone() const; Mat_ col(int x) const; void create( int _rows, int _cols ); void create(Size _size); void create( int _ndims, const int* _sizes ); Mat_ cross(const Mat_& m) const; int depth() const; Mat_ diag(int d = 0) const; size_t elemSize() const; size_t elemSize1() const; iterator end(); const_iterator end() const; template <typename Functor> void forEach(const Functor& operation); template <typename Functor> void forEach(const Functor& operation) const; template <typename T2> operator Mat_< T2 >() const; template < int m, int n > operator Matx< typename DataType< _Tp >::channel_type, m, n >() const; operator std::vector< _Tp >() const; template <int n> operator Vec< typename DataType< _Tp >::channel_type, n >() const; Mat_ operator()( const Range& rowRange, const Range& colRange ) const; Mat_ operator()(const Rect& roi) const; Mat_ operator()(const Range* ranges) const; Mat_ operator()(const std::vector<Range>& ranges) const; _Tp& operator()(const int* idx); const _Tp& operator()(const int* idx) const; template <int n> _Tp& operator()(const Vec<int, n>& idx); template <int n> const _Tp& operator()(const Vec<int, n>& idx) const; _Tp& operator()(int idx0); const _Tp& operator()(int idx0) const; _Tp& operator()( int row, int col ); const _Tp& operator()( int row, int col ) const; _Tp& operator()( int idx0, int idx1, int idx2 ); const _Tp& operator()( int idx0, int idx1, int idx2 ) const; _Tp& operator()(Point pt); const _Tp& operator()(Point pt) const; Mat_& operator=(const Mat& m); Mat_& operator=(const Mat_& m); Mat_& operator=(const _Tp& s); Mat_& operator=(const MatExpr& e); _Tp* operator[](int y); const _Tp* operator[](int y) const; void release(); Mat_ row(int y) const; size_t step1(int i = 0) const; size_t stepT(int i = 0) const; int type() const; static MatExpr eye( int rows, int cols ); static MatExpr eye(Size size); static MatExpr ones( int rows, int cols ); static MatExpr ones(Size size); static MatExpr ones( int _ndims, const int* _sizes ); static MatExpr zeros( int rows, int cols ); static MatExpr zeros(Size size); static MatExpr zeros( int _ndims, const int* _sizes ); };
Inherited Members
public: // enums enum { MAGIC_VAL = 0x42FF0000, AUTO_STEP = 0, CONTINUOUS_FLAG = CV_MAT_CONT_FLAG, SUBMATRIX_FLAG = CV_SUBMAT_FLAG, }; enum { MAGIC_MASK = 0xFFFF0000, TYPE_MASK = 0x00000FFF, DEPTH_MASK = 7, }; // fields MatAllocator* allocator; int cols; uchar* data; const uchar* dataend; const uchar* datalimit; const uchar* datastart; int dims; int flags; int rows; MatSize size; MatStep step; UMatData* u; // methods void addref(); Mat& adjustROI( int dtop, int dbottom, int dleft, int dright ); void assignTo( Mat& m, int type = -1 ) const; template <typename _Tp> _Tp& at(int i0 = 0); template <typename _Tp> const _Tp& at(int i0 = 0) const; template <typename _Tp> _Tp& at( int row, int col ); template <typename _Tp> const _Tp& at( int row, int col ) const; template <typename _Tp> _Tp& at( int i0, int i1, int i2 ); template <typename _Tp> const _Tp& at( int i0, int i1, int i2 ) const; template <typename _Tp> _Tp& at(const int* idx); template <typename _Tp> const _Tp& at(const int* idx) const; template < typename _Tp, int n > _Tp& at(const Vec<int, n>& idx); template < typename _Tp, int n > const _Tp& at(const Vec<int, n>& idx) const; template <typename _Tp> _Tp& at(Point pt); template <typename _Tp> const _Tp& at(Point pt) const; template <typename _Tp> MatIterator_<_Tp> begin(); template <typename _Tp> MatConstIterator_<_Tp> begin() const; int channels() const; int checkVector( int elemChannels, int depth = -1, bool requireContinuous = true ) const; Mat clone() const; Mat col(int x) const; Mat colRange( int startcol, int endcol ) const; Mat colRange(const Range& r) const; void convertTo( OutputArray m, int rtype, double alpha = 1, double beta = 0 ) const; void copySize(const Mat& m); void copyTo(OutputArray m) const; void copyTo( OutputArray m, InputArray mask ) const; void create( int rows, int cols, int type ); void create( Size size, int type ); void create( int ndims, const int* sizes, int type ); void create( const std::vector<int>& sizes, int type ); Mat cross(InputArray m) const; void deallocate(); int depth() const; Mat diag(int d = 0) const; double dot(InputArray m) const; size_t elemSize() const; size_t elemSize1() const; bool empty() const; template <typename _Tp> MatIterator_<_Tp> end(); template <typename _Tp> MatConstIterator_<_Tp> end() const; template < typename _Tp, typename Functor > void forEach(const Functor& operation); template < typename _Tp, typename Functor > void forEach(const Functor& operation) const; UMat getUMat( int accessFlags, UMatUsageFlags usageFlags = USAGE_DEFAULT ) const; MatExpr inv(int method = DECOMP_LU) const; bool isContinuous() const; bool isSubmatrix() const; void locateROI( Size& wholeSize, Point& ofs ) const; MatExpr mul( InputArray m, double scale = 1 ) const; template < typename _Tp, int m, int n > operator Matx< _Tp, m, n >() const; template <typename _Tp> operator std::vector< _Tp >() const; template < typename _Tp, int n > operator Vec< _Tp, n >() const; Mat operator()( Range rowRange, Range colRange ) const; Mat operator()(const Rect& roi) const; Mat operator()(const Range* ranges) const; Mat operator()(const std::vector<Range>& ranges) const; Mat& operator=(const Mat& m); Mat& operator=(const MatExpr& expr); Mat& operator=(const Scalar& s); void pop_back(size_t nelems = 1); uchar* ptr(int i0 = 0); const uchar* ptr(int i0 = 0) const; uchar* ptr( int row, int col ); const uchar* ptr( int row, int col ) const; uchar* ptr( int i0, int i1, int i2 ); const uchar* ptr( int i0, int i1, int i2 ) const; uchar* ptr(const int* idx); const uchar* ptr(const int* idx) const; template <int n> uchar* ptr(const Vec<int, n>& idx); template <int n> const uchar* ptr(const Vec<int, n>& idx) const; template <typename _Tp> _Tp* ptr(int i0 = 0); template <typename _Tp> const _Tp* ptr(int i0 = 0) const; template <typename _Tp> _Tp* ptr( int row, int col ); template <typename _Tp> const _Tp* ptr( int row, int col ) const; template <typename _Tp> _Tp* ptr( int i0, int i1, int i2 ); template <typename _Tp> const _Tp* ptr( int i0, int i1, int i2 ) const; template <typename _Tp> _Tp* ptr(const int* idx); template <typename _Tp> const _Tp* ptr(const int* idx) const; template < typename _Tp, int n > _Tp* ptr(const Vec<int, n>& idx); template < typename _Tp, int n > const _Tp* ptr(const Vec<int, n>& idx) const; template <typename _Tp> void push_back(const _Tp& elem); template <typename _Tp> void push_back(const Mat_<_Tp>& elem); void push_back(const Mat& m); void push_back_(const void* elem); void release(); void reserve(size_t sz); void reserveBuffer(size_t sz); Mat reshape( int cn, int rows = 0 ) const; Mat reshape( int cn, int newndims, const int* newsz ) const; Mat reshape( int cn, const std::vector<int>& newshape ) const; void resize(size_t sz); void resize( size_t sz, const Scalar& s ); Mat row(int y) const; Mat rowRange( int startrow, int endrow ) const; Mat rowRange(const Range& r) const; Mat& setTo( InputArray value, InputArray mask = noArray() ); size_t step1(int i = 0) const; MatExpr t() const; size_t total() const; size_t total( int startDim, int endDim = INT_MAX ) const; int type() const; static Mat diag(const Mat& d); static MatExpr eye( int rows, int cols, int type ); static MatExpr eye( Size size, int type ); static MatAllocator* getDefaultAllocator(); static MatAllocator* getStdAllocator(); static MatExpr ones( int rows, int cols, int type ); static MatExpr ones( Size size, int type ); static MatExpr ones( int ndims, const int* sz, int type ); static void setDefaultAllocator(MatAllocator* allocator); static MatExpr zeros( int rows, int cols, int type ); static MatExpr zeros( Size size, int type ); static MatExpr zeros( int ndims, const int* sz, int type ); protected: // methods template < typename _Tp, typename Functor > void forEach_impl(const Functor& operation);
Detailed Documentation
Template matrix class derived from Mat.
template<typename _Tp> class Mat_ : public Mat { public: // ... some specific methods // and // no new extra fields };
The class Mat_<_Tp>
is a thin template wrapper on top of the Mat class. It does not have any extra data fields. Nor this class nor Mat has any virtual methods. Thus, references or pointers to these two classes can be freely but carefully converted one to another. For example:
// create a 100x100 8-bit matrix Mat M(100,100,CV_8U); // this will be compiled fine. no any data conversion will be done. Mat_<float>& M1 = (Mat_<float>&)M; // the program is likely to crash at the statement below M1(99,99) = 1.f;
While Mat is sufficient in most cases, Mat_ can be more convenient if you use a lot of element access operations and if you know matrix type at the compilation time. Note that Mat::at(int y,int x)
and Mat_::operator()(int y,int x)
do absolutely the same and run at the same speed, but the latter is certainly shorter:
Mat_<double> M(20,20); for(int i = 0; i < M.rows; i++) for(int j = 0; j < M.cols; j++) M(i,j) = 1./(i+j+1); Mat E, V; eigen(M,E,V); cout << E.at<double>(0,0)/E.at<double>(M.rows-1,0);
To use Mat_ for multi-channel images/matrices, pass Vec as a Mat_ parameter:
// allocate a 320x240 color image and fill it with green (in RGB space) Mat_<Vec3b> img(240, 320, Vec3b(0,255,0)); // now draw a diagonal white line for(int i = 0; i < 100; i++) img(i,i)=Vec3b(255,255,255); // and now scramble the 2nd (red) channel of each pixel for(int i = 0; i < img.rows; i++) for(int j = 0; j < img.cols; j++) img(i,j)[2] ^= (uchar)(i ^ j);
Construction
Mat_()
default constructor
Mat_( int _rows, int _cols )
equivalent to Mat(_rows, _cols, DataType<_Tp>::type)
Mat_( int _rows, int _cols, const _Tp& value )
constructor that sets each matrix element to specified value
Mat_(Size _size)
equivalent to Mat(_size, DataType<_Tp>::type)
Mat_( Size _size, const _Tp& value )
constructor that sets each matrix element to specified value
Mat_( int _ndims, const int* _sizes )
n-dim array constructor
Mat_( int _ndims, const int* _sizes, const _Tp& value )
n-dim array constructor that sets each matrix element to specified value
Mat_(const Mat& m)
copy/conversion contructor. If m is of different type, it’s converted
Mat_(const Mat_& m)
copy constructor
Mat_( int _rows, int _cols, _Tp* _data, size_t _step = AUTO_STEP )
constructs a matrix on top of user-allocated data. step is in bytes(!!!), regardless of the type
Mat_( int _ndims, const int* _sizes, _Tp* _data, const size_t* _steps = 0 )
constructs n-dim matrix on top of user-allocated data. steps are in bytes(!!!), regardless of the type
Mat_( const Mat_& m, const Range& rowRange, const Range& colRange = Range::all() )
selects a submatrix
Mat_( const Mat_& m, const Rect& roi )
selects a submatrix
Mat_( const Mat_& m, const Range* ranges )
selects a submatrix, n-dim version
Mat_( const Mat_& m, const std::vector<Range>& ranges )
selects a submatrix, n-dim version
Mat_(const MatExpr& e)
from a matrix expression
Mat_( const std::vector<_Tp>& vec, bool copyData = false )
makes a matrix out of Vec, std::vector, Point_ or Point3_. The matrix will have a single column
Methods
Mat_& adjustROI( int dtop, int dbottom, int dleft, int dright )
some more overriden methods
iterator begin()
iterators; they are smart enough to skip gaps in the end of rows
void create( int _rows, int _cols )
equivalent to Mat::create(_rows, _cols, DataType<_Tp>::type)
void create(Size _size)
equivalent to Mat::create(_size, DataType<_Tp>::type)
void create( int _ndims, const int* _sizes )
equivalent to Mat::create(_ndims, _sizes, DatType<_Tp>::type)
Mat_ cross(const Mat_& m) const
cross-product
size_t elemSize() const
overridden forms of Mat::elemSize() etc.
template <typename Functor> void forEach(const Functor& operation)
template methods for for operation over all matrix elements.
template <typename T2> operator Mat_< T2 >() const
data type conversion
template < int m, int n > operator Matx< typename DataType< _Tp >::channel_type, m, n >() const
conversion to Matx
operator std::vector< _Tp >() const
conversion to vector.
template <int n> operator Vec< typename DataType< _Tp >::channel_type, n >() const
conversion to Vec
_Tp& operator()(const int* idx)
returns reference to the specified element
const _Tp& operator()(const int* idx) const
returns read-only reference to the specified element
template <int n> _Tp& operator()(const Vec<int, n>& idx)
returns reference to the specified element
template <int n> const _Tp& operator()(const Vec<int, n>& idx) const
returns read-only reference to the specified element
_Tp& operator()(int idx0)
returns reference to the specified element (1D case)
const _Tp& operator()(int idx0) const
returns read-only reference to the specified element (1D case)
_Tp& operator()( int row, int col )
returns reference to the specified element (2D case)
const _Tp& operator()( int row, int col ) const
returns read-only reference to the specified element (2D case)
_Tp& operator()( int idx0, int idx1, int idx2 )
returns reference to the specified element (3D case)
const _Tp& operator()( int idx0, int idx1, int idx2 ) const
returns read-only reference to the specified element (3D case)
Mat_& operator=(const _Tp& s)
set all the elements to s.
Mat_& operator=(const MatExpr& e)
assign a matrix expression
_Tp* operator[](int y)
more convenient forms of row and element access operators
void release()
equivalent to Mat::release()
Mat_ row(int y) const
overridden forms of Mat::row() etc.
size_t stepT(int i = 0) const
returns step() /sizeof(_Tp)
static MatExpr zeros( int rows, int cols )
overridden forms of Mat::zeros() etc. Data type is omitted, of course