OpenCV learning (3): Mat class usage

Mat class uses


structure

Basic parameters

The construction of the Mat object mainly needs to provide some parameters, of course, some of these parameters are default, and the user may not provide them. These parameters mainly include the number of channels, size, data, step size, etc. of the object, in which the data is stored separately, and the others are collectively referred to as the header (Header).
- The matrix size gives the size of the matrix (number of rows and columns). The usual methods are Enumeration form ([rows,cols]), vector form (vector& size), array pointer (int * size);
- the type is the previously defined Array Type;
- the matrix data value is usually a pointer to a matrix (int * data );
- the step size is also usually an array pointer (size_t * step).

type variable

In the constructor, you can usually see that you need to input an integer variable type. This parameter is defined in the relevant header file through the macro definition. Used to indicate the storage data type of the Mat object (mainly the data part). The meaning of each part can be as follows:
CV_[位数][带符号与否][类型前缀]C[通道数]: CV_8UC1 represents an 8-bit single-channel unsigned char array, and CV_32FC2 represents a 2-channel 23-bit floating-point array.
(depth: depth)
CV_8U: bool or uchar
CV_8S: schar or char
CV_16U: ushort
CV_16S: short
CV_32S: int or unsigned
CV_32F: float
CV_64F: double

//部分宏定义
#define CV_8UC1 CV_MAKETYPE(CV_8U,1)
#define CV_8UC2 CV_MAKETYPE(CV_8U,2)
#define CV_8UC3 CV_MAKETYPE(CV_8U,3)
#define CV_8UC4 CV_MAKETYPE(CV_8U,4)
#define CV_8UC(n) CV_MAKETYPE(CV_8U,(n))

//构造函数
Mat();
Mat(int rows, int cols, int type);

The type variable is an integer, which means that if you know the integer value corresponding to the macro definition, you can enter it directly. The following two statements are equivalent.

Mat matrix(3, 3, CV_8UC1);
Mat matrix(3, 3, 0);

Scalar type

The Scalar type defines a four-dimensional vector that can be converted to and from the type CvScalar. Each channel of the Mat object can be assigned a value using a Scalar object. The default value is 0. Usually three channels can be used to represent an RGB image.

class cv::Scalar_< _Tp >;//一个从`类Vec`派生过来的四维向量
typedef Scalar_<double> cv::Scalar;
//构造函数
Mat(int rows, int cols, int type, const Scalar& s);
Mat matrix(3, 3, CV_8UC1,Scalar(0));//为每个通道的对应元素都赋初值0

Size type

The data members of the Size class are width and height. Note that in Size width is the number of columns (cols) of the image in our usual sense, and height corresponds to the number of rows of the image (rows). That is to say, the two are opposite, which needs attention when initializing.

typedef Size_<int> Size2i;  
typedef Size2i Size;  
typedef Size_<float> Size2f;  
//构造函数
Mat(Size size, int type, const Scalar& s);

Dimensions and Channels

Dimension refers to the dimension of each channel, generally one or two dimensions; channel refers to how many values ​​each element has. It can be understood that each element is actually a multidimensional array, but the maximum dimension of this array is 4 (the same as the definition of the scalar type). When specifying the dimension parameter ndims, it is usually used with an array or vector specifying the size:

 Mat(int ndims, const int* sizes, int type, const Scalar& s);
 Mat(const std::vector<int>& sizes, int type, const Scalar& s);
 Mat(int ndims, const int* sizes, int type);
 //代码例子:
 int sizes[] = { 3,3 };
 Mat matrix(2, sizes, CV_64FC1, Scalar(1));//二维 3*3 单通道矩阵
 Mat matrix_(1, sizes, CV_64FC1,Scalar(1)); // 一维 3*1 单通道矩阵,虽然sizes是二维

Range type

Used to specify contiguous subsequences of a sequence. Has two public members: start and end. You can use these two members to represent the range of subsequences, left open and right closed. All can be represented using Range.all().

Range ();
Range (int _start, int _end);
//构造函数
Mat(const Mat& m, const Range& rowRange, const Range& colRange=Range::all());

Rect type

Creates a rectangular area that can be used to extract regions of interest. The first two digits are a coordinate, and the last two digits represent the offset.

Mat(const Mat& m, const Rect& roi);

Convert other types to matrices

Vectors, lists, arrays, two-dimensional points, and three-dimensional points can be explicitly converted to matrices, but not implicitly converted.

template<typename _Tp> explicit Mat(const std::vector<_Tp>& vec, bool copyData=false);
template<typename _Tp, typename = typename std::enable_if<std::is_arithmetic<_Tp>::value>::type>
    explicit Mat(const std::initializer_list<_Tp> list);
template<typename _Tp> explicit Mat(const std::initializer_list<int> sizes, const               std::initializer_list<_Tp> list);
template<typename _Tp, size_t _Nm> explicit Mat(const std::array<_Tp, _Nm>& arr, bool copyData=false);
template<typename _Tp> explicit Mat(const Point_<_Tp>& pt, bool copyData=true);
template<typename _Tp> explicit Mat(const Point3_<_Tp>& pt, bool copyData=true);

operation

add, subtract, multiply

Matrices overload many basic mathematical operators: A + B , A B , A B

assign

1. The assignment from the basic matrix is ​​as follows, this method does not reallocate memory, just copies the header and increments the reference count.

Mat& operator = (const Mat& m);
void assignTo( Mat& m, int type=-1 ) const;

2. Assignment from a matrix expression, if the left-hand matrix has the required size, reuse it; otherwise, reassign it.

Mat& operator = (const MatExpr& expr);

3. Sets the elements of the matrix to a scalar.

 Mat& operator = (const Scalar& s);
 Mat& setTo(InputArray value, InputArray mask=noArray());

dot product, cross product

Dot multiplication is to turn a matrix into a vector, and do a dot product of two vectors, and the final result is a real number.

Mat cross(InputArray m) const;
double dot(InputArray m) const;

element-wise multiplication and division

Multiply and divide the corresponding elements.

MatExpr mul(InputArray m, double scale=1) const;

special matrix

0 matrix

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);

full 1 matrix

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);

identity matrix

static MatExpr eye(int rows, int cols, int type);
static MatExpr eye(Size size, int type);

diagonal matrix

static Mat diag(const Mat& d);
Mat diag(int d=0) const;

copy

clone() will copy all the information of the matrix (including the header and data part) to the target matrix, which means that the data will not be shared.
coyTo() will copy the data part of the matrix to the destination matrix and call the creat() function before copying.

Mat clone() const;
void copyTo( OutputArray m ) const;
void copyTo( OutputArray m, InputArray mask) const;

special operation

deformation

    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;

Transpose

MatExpr t() const;

Invert

 MatExpr inv(int method=DECOMP_LU) const;

add element add element
at the end of the matrix

void push_back_(const void* elem);
template<typename _Tp> void push_back(const _Tp& elem);
template<typename _Tp> void push_back(const Mat_<_Tp>& elem);
template<typename _Tp> void push_back(const std::vector<_Tp>& elem);
void push_back(const Mat& m);
void pop_back(size_t nelems=1);

type conversion

Convert the data type of the elements in the matrix to the desired type. But this is just a conversion inside the matrix, returning void. If you need to convert from an array, vector or list to a matrix, you need to use the constructor explicitly. Note that this function changes the depth of the data without changing the channels.

 void convertTo( OutputArray m, int rtype, double alpha=1, double beta=0 ) const;

ROI extraction

void locateROI( Size& wholeSize, Point& ofs ) const;
Mat& adjustROI( int dtop, int dbottom, int dleft, int dright );
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;
template<typename _Tp> operator std::vector<_Tp>() const;
template<typename _Tp, int n> operator Vec<_Tp, n>() const;
template<typename _Tp, int m, int n> operator Matx<_Tp, m, n>() const;

element access

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;

iterator

template<typename _Tp> MatIterator_<_Tp> begin();
template<typename _Tp> MatConstIterator_<_Tp> begin() 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;

functions that return pointers

row pointer

const uchar* ptr(int i0=0) const;\\指针为什么是uchar??
uchar* ptr(int i0=0);
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;

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;

memory management

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);

void release();
void deallocate();
void copySize(const Mat& m);
void reserve(size_t sz);
void reserveBuffer(size_t sz);
void resize(size_t sz);
void resize(size_t sz, const Scalar& s);

size_t elemSize() const;//查看元素字节大小.
size_t elemSize1() const;//查询每个元素通道的字节大小.
int type() const;//查询元素的类型.

other functions


bool isContinuous() const;
bool isSubmatrix() //! 查看矩阵是否是子矩阵
void addref();//增加引用计数
int depth() const;\\查询矩阵元素的深度.enum{CV_8U=0,CV_8S=1,CV_16U=2,CV_16S=3,CV_32S=4,CV_32F=5,CV_64F=6}        
int channels() const;\\查询通道数
size_t step1(int i=0) const;\\查看标准化步长.
bool empty() const;\\查询是否空矩阵.
size_t total() const;\\查询元素的个数.
size_t total(int startDim, int endDim=INT_MAX) const;\\查询指定区域的元素数目.
int checkVector(int elemChannels, int depth=-1, bool requireContinuous=true) const;\\?

public variable

int flagsFlags with multiple bitfields:
- the magic signature\\??
- Consecutive flags
- Depth
- Number of channels

int dimsmatrix dimension, >= 2

int rows, colsWhen the matrix has multiple dimensions, the number of rows and columns or (-1, -1)

uchar* datapointer to data

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324702178&siteId=291194637