void Mat2Array(float3 * &dst, Mat & src, const int W, const int H)
{
for (int row = 0; row < H; row++) {
float* data = src.ptr<float>(row);//获取行首指针
for (int col = 0; col < W; col++) {
int index = row*W + col;
float b = data[col* 3],g=data[col *3+1],r=data[col *3+2];//Mat中BGR三通道顺序排列
dst[index].x =b;
dst[index].y =g;
dst[index].z =r;
}
}
}
void Mat2Array2(Mat src, float * dst)
{
const int W = src.cols, H = src.rows;
float *data =(float *) src.data;//注意类型转换,data是一维数组!
for (int i = 0; i < H; i++) {
for (int j = 0; j < W*3; j++) {
int index = i*W*3 + j;
dst[index] = data[index];
dst[index] = data[index];
dst[index] = data[index];
}
}
}
//错误写法
Rect area(10, 10, 1000, 500);
Mat image = src(area);
图像裁剪后的data指针与原来指针为同一个,即Mat image和Mat src公用一个data指针。这样会出现一个问题,裁剪后的图像每行的步长没变,这样在后续利用指针操作图像时会导致问题,访问到了错误的位置。
//正确写法
Rect area(10, 10, 1000, 500);
Mat image = src(area).clone();
原文:https://www.cnblogs.com/JustNo/p/14018346.html