opencv中Mat与数组之间值传递的方法

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/liuxiangxxl/article/details/79090573

1.将数组内容传递给Mat

示例代码:

unsigned char cbuf[height][width];
cv::Mat img(height, width, CV_8UC1, (unsigned char*)cbuf);
  • 1
  • 2

2.将Mat中的内容传递给数组

如果Mat中的数据是连续的,那么对于传递到一维vector我们可以这样:

std::vector<uchar> array(mat.rows*mat.cols);
if (mat.isContinuous())
    array = mat.data;
  • 1
  • 2
  • 3
  • 4

同样的,传递到一维数组我们可以这样

unsigned char *array=new unsigned char[mat.rows*mat.cols];
if (mat.isContinuous())
    array = mat.data;
  • 1
  • 2
  • 3

对于二维vector的传值,我们可以这样处理

uchar **array = new uchar*[mat.rows];
for (int i=0; i<mat.rows; ++i)
    array[i] = new uchar[mat.cols];

for (int i=0; i<mat.rows; ++i)
    array[i] = mat.ptr<uchar>(i);
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7

参考链接 
http://stackoverflow.com/questions/26681713/convert-mat-to-array-vector-in-opencv

猜你喜欢

转载自blog.csdn.net/liuxiangxxl/article/details/79090573