1.Mat_类的特点、应用场合?
2.引用和指针的转化(引用的&和取址&的区别)?
3.怎么通过引用让两个变量指向同一块地址?
4.Mat_类访问矩阵元素的快捷方式?
Mat_类一般应用于矩阵(matrix)的运算。
Mat_类继承自Mat类,对数据类型更加灵活,可定义为Mat_<_Tp>的矩阵形式
template<typename _Tp> class Mat_ : public Mat //定义类模板的方式
{
public:
// ... some specific methods
// and
// no new extra fields
};
Mat类和Mat_类都没有虚方法,因此对这两个类的引用和指针可以转化(但是要小心)eg:
// 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;
其中Mat_<float>& M1 = (Mat_<float>&) M,引用M1和M指向同一块地址,这块地址存放的数据还是CV_8U类型。&M1 == &2(引用的类型必须和其绑定的变量类型一致,所以M1(99,99) = 1.f会崩溃),注意&不是取址运算符,而是起标志作用
(Mat_ can be more convenient if you use a lot of element
access operations and if you know matrix type at the compilation time. )如果在编译时使用了大量的元素访问操作,并且知道矩阵类型,MAT_可以更方便。直接用Mat_类型的变量M_(row,col)访问
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);//不使用at,直接用()索引,更方便
Mat E, V;
eigen(M,E,V);
cout << E.at<double>(0,0)/E.at<double>(M.rows-1,0);