<li> by using MATLAB-style matrix initializers, cv::Mat::zeros(), cv::Mat::ones(), cv::Mat::eye(), e.g.:
\code
// create a double-precision identity martix and add it to M.
M += Mat::eye(M.rows, M.cols, CV_64F);
\endcode
<li> by using comma-separated initializer:
\code
// create 3x3 double-precision identity matrix
Mat M = (Mat_<double>(3,3) << 1, 0, 0, 0, 1, 0, 0, 0, 1);
\endcode
here we first call constructor of cv::Mat_ class (that we describe further) with the proper matrix,
and then we just put "<<" operator followed by comma-separated values that can be constants,
variables, expressions etc. Also, note the extra parentheses that are needed to avoid compiler errors.
</ul>
Once matrix is created, it will be automatically managed by using reference-counting mechanism
(unless the matrix header is built on top of user-allocated data,
in which case you should handle the data by yourself).
The matrix data will be deallocated when no one points to it;
if you want to release the data pointed by a matrix header before the matrix destructor is called,
use cv::Mat::release().
The next important thing to learn about the matrix class is element access. Here is how the matrix is stored.
The elements are stored in row-major order (row by row). The cv::Mat::data member points to the first element of the first row,
cv::Mat::rows contains the number of matrix rows and cv::Mat::cols - the number of matrix columns. There is yet another member,
cv::Mat::step that is used to actually compute address of a matrix element. cv::Mat::step is needed because the matrix can be
a part of another matrix or because there can some padding space in the end of each row for a proper alignment.
\image html roi.png
Given these parameters, address of the matrix element M_{ij} is computed as following:
addr(M_{ij})=M.data + M.step*i + j*M.elemSize()
if you know the matrix element type, e.g. it is float, then you can use cv::Mat::at() method:
addr(M_{ij})=&M.at<float>(i,j)
(where & is used to convert the reference returned by cv::Mat::at() to a pointer).
if you need to process a whole row of matrix, the most efficient way is to get the pointer to the row first, and then just use plain C operator \texttt{[]}:
\code
// compute sum of positive matrix elements
// (assuming that M is double-precision matrix)
double sum=0;
for(int i = 0; i < M.rows; i++)
{
const double* Mi = M.ptr<double>(i);
for(int j = 0; j < M.cols; j++)
sum += std::max(Mi[j], 0.);
}
\endcode
Some operations, like the above one, do not actually depend on the matrix shape,
they just process elements of a matrix one by one (or elements from multiple matrices
that are sitting in the same place, e.g. matrix addition). Such operations are called
element-wise and it makes sense to check whether all the input/output matrices are continuous,
i.e. have no gaps in the end of each row, and if yes, process them as a single long row:
\code
// compute sum of positive matrix elements, optimized variant
double sum=0;
int cols = M.cols, rows = M.rows;
if(M.isContinuous())
{
cols *= rows;
rows = 1;
}
for(int i = 0; i < rows; i++)
{
const double* Mi = M.ptr<double>(i);
for(int j = 0; j < cols; j++)
sum += std::max(Mi[j], 0.);
}
\endcode
in the case of continuous matrix the outer loop body will be executed just once,
so the overhead will be smaller, which will be especially noticeable in the case of small matrices.
Finally, there are STL-style iterators that are smart enough to skip gaps between successive rows:
\code
// compute sum of positive matrix elements, iterator-based variant
double sum=0;
MatConstIterator_<double> it = M.begin<double>(), it_end = M.end<double>();
for(; it != it_end; ++it)
sum += std::max(*it, 0.);
\endcode
The matrix iterators are random-access iterators, so they can be passed to any STL algorithm, including \texttt{std::sort()}.
*/
classCV_EXPORTSMat
{
public:
// constructors
//! default constructor
Mat();
// constructs matrix of the specified size and type
//! constructs matrix of the specified size and type
// (_type is CV_8UC1, CV_64FC3, CV_32SC(12) etc.)
Mat(int_rows,int_cols,int_type);
Mat(Size_size,int_type);
// constucts matrix and fills it with the specified value _s.
//! constucts matrix and fills it with the specified value _s.
Mat(int_rows,int_cols,int_type,constScalar&_s);
Mat(Size_size,int_type,constScalar&_s);
// copy constructor
//! copy constructor
Mat(constMat&m);
// constructor for matrix headers pointing to user-allocated data
//! constructor for matrix headers pointing to user-allocated data
//! computes norm of selected part of the difference between two arrays
CV_EXPORTSdoublenorm(constMat&a,constMat&b,
intnormType,constMat&mask);
//! scales and shifts array elements so that either the specified norm (alpha) or the minimum (alpha) and maximum (beta) array values get the specified values