opencv 3.0 Mat 数据类型转换

来源:互联网 发布:mysql 时间戳相减 编辑:程序博客网 时间:2024/06/05 19:17

opencv 3.0将 Mat 向 CvMat  CvMatND IplImage 的转化,都去除了

    // //! converts header to CvMat; no data is copied    // operator CvMat() const;    // //! converts header to CvMatND; no data is copied    // operator CvMatND() const;    // //! converts header to IplImage; no data is copied    // operator IplImage() const;


详见mat.hpp 的 line 798 - 803

 

关于 *buf -> Mat  转换

我们不禁想问,那如何实现从 缓存到矩阵的转换呢?

自己写咯。。。其实用不着

 

因为在mat.hpp 的 line 661 - 664

    //! constructor for matrix headers pointing to user-allocated data    Mat(int rows, int cols, int type, void* data, size_t step=AUTO_STEP);    Mat(Size size, int type, void* data, size_t step=AUTO_STEP);    Mat(int ndims, const int* sizes, int type, void* data, const size_t* steps=0);

可见,我们可以使用构造函数完成对外部的缓存的数据的Mat格式封装。

不多说,上代码。

注意:Mat只是数据头的记录,若要硬拷贝 需要使用clone()。

void MatTest(){    const char *imagename = "E:/1.jpg";       uchar *buf, *src;    Mat img = imread(imagename);         buf = new uchar[img.total()*img.elemSize()];    src = img.data;    //产生一个buffer,包含图像信息    for (int i=0; i< img.cols; i++ )    {        for (int j=0;j< img.rows; j++)        {            buf[3*(img.cols*j + i)]   = src[3*(img.cols*j + i)];            buf[3*(img.cols*j + i)+1] = src[3*(img.cols*j + i)+1];            buf[3*(img.cols*j + i)+2] = src[3*(img.cols*j + i)+2];        }    }    //对buffer软拷贝    Mat ans1(img.rows, img.cols, CV_8UC3, buf);    //对buffer硬拷贝    Mat ans2 = Mat(img.rows, img.cols, CV_8UC3, buf).clone();    imshow("image", img); //显示图像        imshow("ans1", ans1);    imshow("ans2", ans2);    waitKey();}




 

0 0
原创粉丝点击