Mat类型其本质只是一片数据域的指针,当进行复制操作时,并不会新复制一片数据,而只是创建一个新的指针指向原数据域。
CV_{8U,16S,16U,32S,32F,64F}C{1,2,3}:数据类型和通道数
Constructor | Description |
---|---|
cv::Mat; | 默认构造器 |
cv::Mat( int rows, int cols, int type ); | 指定行列和类型 |
cv::Mat( int rows, int cols, int type, const Scalar& s ); |
指定行列和类型并初始化数值 |
cv::Mat( int rows, int cols, int type, void* data, size_t step=AUTO_STEP ); |
指定行列和类型并加载数据初始化 |
cv::Mat( cv::Size sz, int type ); | 使用Size指定行列和类型 |
cv::Mat( cv::Size sz, int type, const Scalar& s ); |
使用Size指定行列和类型并初始化数值 |
cv::Mat( cv::Size sz, int type, void* data, size_t step=AUTO_STEP ); |
使用Size指定行列和类型并加载数据初始化 |
cv::Mat( int ndims, const int* sizes, int type ); |
指定多维矩阵 |
cv::Mat( int ndims, const int* sizes, int type, const Scalar& s ); |
指定多维矩阵并初始化 |
cv::Mat( int ndims, const int* sizes, int type, void* data, size_t step=AUTO_STEP ); |
指定多维矩阵并加载数据初始化 |
cv::Mat m;
// Create data area for 3 rows and 10 columns of 3-channel 32-bit floats
m.create( 3, 10, CV_32FC3 );
// Set the values in the 1st channel to 1.0, the 2nd to 0.0, and the 3rd to 1.0
m.setTo( cv::Scalar( 1.0f, 0.0f, 1.0f ) );
cv::Mat m( 3, 10, CV_32FC3, cv::Scalar( 1.0f, 0.0f, 1.0f ) );
通过另一个矩阵来构造
Constructor | Description |
---|---|
cv::Mat( const Mat& mat ); | Copy constructor |
cv::Mat( const Mat& mat, const cv::Range& rows, const cv::Range& cols ); |
Copy constructor that copies only a subset of rows and columns |
cv::Mat( const Mat& mat, const cv::Rect& roi ); |
Copy constructor that copies only a subset of rows and columns specified by a region of interest |
cv::Mat( const Mat& mat, const cv::Range* ranges ); |
Generalized region of interest copy constructor that uses an array of ranges to select from an n-dimensional array |
cv::Mat( const cv::MatExpr& expr ); | Copy constructor that initializes m with the result of an algebraic expression of other matrices |
cv::Mat( const CvMat* old, bool copyData=false ); |
Constructor for a new object m that creates m from an oldstyle CvMat, with optional data copy |
cv::Mat( const IplImage* old, bool copyData=false ); |
Constructor for a new object m that creates m from an oldstyle IplImage, with optional data copy |
Constructor | Description |
---|---|
cv::Mat( const cv::Vec bool copyData=true ); |
Construct a one-dimensional array of type T and size n from a cv::Vec of the same type |
cv::Mat( const cv::Matx bool copyData=true ); |
Construct a two-dimensional array of type T and size m × n from a cv::Matx of the same type |
cv::Mat( const std::vector bool copyData=true ); |
Construct a one-dimensional array of type T from an STL vector containing elements of the same type |
Function | Description |
---|---|
cv::Mat::zeros( rows, cols, type ); | Create a cv::Mat of size rows × cols, which is full of zeros, with type type (CV_32F, etc.) |
cv::Mat::ones( rows, cols, type ); | Create a cv::Mat of size rows × cols, which is full of ones, with type type (CV_32F, etc.) |
cv::Mat::eye( rows, cols, type ); | Create a cv::Mat of size rows × cols, which is an identity matrix, with type type (CV_32F, etc.) |
Example | Description |
---|---|
M.at |
Element i from integer array M |
M.at |
Element ( i, j ) from float array M |
M.at |
Element at location (pt.x, pt.y) in integer matrix M |
M.at |
Element at location ( i, j, k ) in three-dimensional float array M |
M.at |
Element at n-dimensional location indicated by idx[] in array M of unsigned characters |
int sz[3] = { 4, 4, 4 };
cv::Mat m( 3, sz, CV_32FC3 ); // A three-dimensional array of size 4-by-4-by-4
cv::randu( m, -1.0f, 1.0f ); // fill with random numbers from -1.0 to 1.0
float max = 0.0f; // minimum possible value of L2 norm
cv::MatConstIterator it = m.begin();
while( it != m.end() ) {
len2 = (*it)[0]*(*it)[0]+(*it)[1]*(*it)[1]+(*it)[2]*(*it)[2];
if( len2 > max ) max = len2;
it++;
}
const int n_mat_size = 5;
const int n_mat_sz[] = { n_mat_size, n_mat_size, n_mat_size };
cv::Mat n_mat0( 3, n_mat_sz, CV_32FC1 );
cv::Mat n_mat1( 3, n_mat_sz, CV_32FC1 );
cv::RNG rng;
rng.fill( n_mat0, cv::RNG::UNIFORM, 0.f, 1.f );
rng.fill( n_mat1, cv::RNG::UNIFORM, 0.f, 1.f );
const cv::Mat* arrays[] = { &n_mat0, &n_mat1, 0 };
cv::Mat my_planes[2];
cv::NAryMatIterator it( arrays, my_planes );
float s = 0.f; // Total sum over all planes in both arrays
int n = 0; // Total number of planes
for( int p = 0; p < it.nplanes; p++, ++it ) {
s += cv::sum(it.planes[0])[0];
s += cv::sum(it.planes[1])[0];
n++;
}
/////////// compute dst[*] = pow(src1[*], src2[*]) //////////////
const Mat* arrays[] = { src1, src2, dst, 0 };
float* ptrs[3];
NAryMatIterator it(arrays, (uchar**)ptrs);
for( size_t i = 0; i < it.nplanes; i++, ++it )
{
for( size_t j = 0; j < it.size; j++ )
{
ptrs[2][j] = std::pow(ptrs[0][j], ptrs[1][j]);
}
}
Example | Description |
---|---|
m.row( i ); | Array corresponding to row i of m |
m.col( j ); | Array corresponding to column j of m |
m.rowRange( i0, i1 ); | Array corresponding to rows i0 through i1-1 of matrix m |
m.rowRange( cv::Range( i0, i1 ) ); | Array corresponding to rows i0 through i1-1 of matrix m |
m.colRange( j0, j1 ); | Array corresponding to columns j0 through j1-1 of matrix m |
m.colRange( cv::Range( j0, j1 ) ); | Array corresponding to columns j0 through j1-1 of matrix m |
m.diag( d ); | Array corresponding to the d-offset diagonal of matrix m |
m( cv::Range(i0,i1), cv::Range(j0,j1) ); | Array corresponding to the subrectangle of matrix m with one corner at i0, j0 and the opposite corner at (i1-1, j1-1) |
m( cv::Rect(i0,i1,w,h) ); | Array corresponding to the subrectangle of matrix m with one corner at i0, j0 and the opposite corner at (i0+w-1, j0+h-1) |
m( ranges ); | Array extracted from m corresponding to the subvolume that is the intersection of the ranges given by ranges[0]- ranges[ndim-1] |
Example | Description |
---|---|
m0 + m1, m0 – m1; | Addition or subtraction of matrices |
m0 + s; m0 – s; s + m0, s – m1; | Addition or subtraction between a matrix and a singleton |
-m0; | Negation of a matrix |
s * m0; m0 * s; | Scaling of a matrix by a singleton |
m0.mul( m1 ); m0/m1; | Per element multiplication of m0 and m1, per-element division of m0 by m1 |
m0 * m1; | Matrix multiplication of m0 and m1 |
m0.inv( method ); | Matrix inversion of m0 (default value of method is DECOMP_LU) |
m0.t(); | Matrix transpose of m0 (no copy is done) |
m0>m1; m0>=m1; m0==m1; m0<=m1; m0Per element comparison, returns uchar matrix with elements 0 |
or 255 |
m0&m1; m0|m1; m0^m1; ~m0; m0&s; s&m0; m0|s; s|m0; m0^s; s^m0; |
Bitwise logical operators between matrices or matrix and a singleton |
min(m0,m1); max(m0,m1); min(m0,s); min(s,m0); max(m0,s); max(s,m0); |
Per element minimum and maximum between two matrices or a matrix and a singleton |
cv::abs( m0 ); | Per element absolute value of m0 |
m0.cross( m1 ); m0.dot( m1 ); | Vector cross and dot product (vector cross product is defined only for 3 × 1 matrices) |
cv::Mat::eye( Nr, Nc, type ); cv::Mat::zeros( Nr, Nc, type ); cv::Mat::ones( Nr, Nc, type ); |
Class static matrix initializers that return fixed Nr × Nc matrices of type type |
Example | Description |
---|---|
m1 = m0.clone(); | Make a complete copy of m0, copying all data elements as well; cloned array will be continuous |
m0.copyTo( m1 ); | Copy contents of m0 onto m1, reallocating m1 if necessary (equivalent to m1=m0.clone()) |
m0.copyTo( m1, mask ); | Same as m0.copyTo(m1), except only entries indicated in the array mask are copied |
m0.convertTo( m1, type, scale, offset ); |
Convert elements of m0 to type (e.g., CV_32F) and write to m1 after scaling by scale (default 1.0) and adding offset (default 0.0) |
m0.assignTo( m1, type ); | Internal use only (resembles convertTo) |
m0.setTo( s, mask ); | Set all entries in m0 to singleton value s; if mask is present, set only those values corresponding to nonzero elements in mask |
m0.reshape( chan, rows ); | Changes effective shape of a two-dimensional matrix; chan or rows may be zero, which implies “no change”; data is not copied |
m0.push_back( s ); | Extend an m × 1 matrix and insert the singleton s at the end |
m0.push_back( m1 ); | Extend an m × n by k rows and copy m1 into those rows; m1 must be k × n |
m0.pop_back( n ); | Remove n rows from the end of an m × n (default value of n is 1) |
m0.locateROI( size, offset ); | Write whole size of m0 to cv::Size size; if m0 is a “view” of a larger matrix, write location of starting corner to Point& offset |
m0.adjustROI( t, b, l, r ); | Increase the size of a view by t pixels above, b pixels below, l pixels to the left, and r pixels to the right |
m0.total(); | Compute the total number of array elements (does not include channels) |
m0.isContinuous(); | Return true only if the rows in m0 are packed without space between them in memory |
m0.elemSize(); | Return the size of the elements of m0 in bytes (e.g., a three-channel float matrix would return 12 bytes) |
m0.elemSize1(); | Return the size of the subelements of m0 in bytes (e.g., a three-channel float matrix would return 4 bytes) |
m0.type(); | Return a valid type identifier for the elements of m0 (e.g., CV_32FC3) |
m0.depth(); | Return a valid type identifier for the individial channels of m0 (e.g., CV_32F) |
m0.channels(); | Return the number of channels in the elements of m0 |
m0.size(); | Return the size of the m0 as a cv::Size object |
m0.empty(); | Return true only if the array has no elements (i.e., m0.total==0 or m0.data==NULL |