版权声明:转载请附链接 https://blog.csdn.net/isunLt/article/details/84863202
OpenCV Mat Channel的地址问题
最近因为做数字图像处理大实验的原因学习了一下warpPerspective函数的源码,在阅读源码时碰到了一些困难,其中一个就是关于Mat不同通道的数据是如何组织的。于是我做了一个小实验,下面是实验的代码:
#include <iostream>
#include <opencv2\core\core.hpp>
#include <opencv2\highgui\highgui.hpp>
#include <opencv2\imgproc\imgproc.hpp>
using namespace std;
using namespace cv;
int main()
{
int M[18];
for (int i = 0; i < 18; i++)
{
M[i] = i;
}
Mat matA(3, 3, CV_32SC2, M);// matA中数据的地址即是M中数据的地址,改变M中的数据就会改变matA中的数据
cout << "channel1: " << endl;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cout << matA.at<Vec2i>(i, j)[0] << ' ';
}
cout << endl;
}
cout << "channel2: " << endl;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cout << matA.at<Vec2i>(i, j)[1] << ' ';
}
cout << endl;
}
cout << "test3: " << endl;
for (int i = 0; i < 9; i++)
{
*(M + i) += 100;
}
cout << "channel1_after: " << endl;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cout << matA.at<Vec2i>(i, j)[0] << ' ';
}
cout << endl;
}
cout << "channel2_after: " << endl;
for (int i = 0; i < 3; i++)
{
for (int j = 0; j < 3; j++)
{
cout << matA.at<Vec2i>(i, j)[1] << ' ';
}
cout << endl;
}
return 0;
}
代码的输出结果是:
初始化一个数组M[18]使他的值依次为0~17,再用M初始化一个row=3,col=3,channel=2的矩阵,输出矩阵每一个通道里储存的9个数,结果一通道为{0,2,4,6,8,10,12,14,16},二通道为{1,3,5,7,9,11,13,15,17},把数组M前9个数每个加100,再打印矩阵的每个通道的值,发现通道一为{100,102,104,106,108,10,12,14,16},二通道为{101,103,105,107,9,11,13,15,17}
这说明Mat内部对待通道的存储方式是这样的
而不是这样的