对于一个Mat,有时需要将其转为二维指针传递
int** mat2ptrarray(Mat& pic)
{
int** data;
data = new int* [pic.rows];
for (int i = 0; i < pic.rows; i++)
{
data[i] = new int[pic.cols];
for (int j = 0; j < pic.cols; j++)
{
data[i][j] = pic.at<int>(i, j);
}
}
return data;
}
释放指针指向的内存空间
for (int i = 0; i < pic.rows; i++)
{
delete[] data[i];
}
delete[] data;
将二维指针的数据再拼回到Mat中(这个是按单通道的例子)
Mat ImgData(int** pImgdata, int width, int height)
{
Mat Img;
Img.create(height, width, CV_8U);//这里按照uchar类型
for (int i = 0; i < height; i++) //行数--高度
{
for (int j = 0; j < width; j++) //列数 -- 宽度
{
Img.at<uchar>(i, j) = pImgdata[i][j];
}
}
return Img;
}
|