Mat_类模板小计

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指向同一块地址。&M1 == &2


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

猜你喜欢

转载自blog.csdn.net/vict_wang/article/details/81210342