Mat.at(row, col) , 雖然每次都得從頭算出位址, 是效率不高的存取方式,
但頗好用. 只是, 非單一 channel 的 matrix 要如何存取呢?
下文 testM 是有三個 channel 的 8-bits unsign char matrix.
可依舊以 (row, col) 存取, 但最小單位是 Vec3b
Mat testM(3,4,CV_8UC3);
但頗好用. 只是, 非單一 channel 的 matrix 要如何存取呢?
下文 testM 是有三個 channel 的 8-bits unsign char matrix.
可依舊以 (row, col) 存取, 但最小單位是 Vec3b
Mat testM(3,4,CV_8UC3);
for (int i = 0; i < testM.rows; i++)
{
for (int j = 0; j < testM.cols; j++)
{
Vec3b v = testM.at(i,j);
v[0] = i;
v[1] = i+10;
v[2] = i+20;
testM.at(i,j) = v;
}
}
當然也可直接存取 data, serial data 如下
row0= {ch0 ch1 ch2}, {ch0 ch1 ch2}, {ch0 ch1 c2}, {ch0 ch1 ch2}
row1= {ch0 ch1 ch2}, {ch0 ch1 ch2}, {ch0 ch1 c2}, {ch0 ch1 ch2}
row2= {ch0 ch1 ch2}, {ch0 ch1 ch2}, {ch0 ch1 c2}, {ch0 ch1 ch2}
當然也可直接存取 data, serial data 如下
row0= {ch0 ch1 ch2}, {ch0 ch1 ch2}, {ch0 ch1 c2}, {ch0 ch1 ch2}
row1= {ch0 ch1 ch2}, {ch0 ch1 ch2}, {ch0 ch1 c2}, {ch0 ch1 ch2}
row2= {ch0 ch1 ch2}, {ch0 ch1 ch2}, {ch0 ch1 c2}, {ch0 ch1 ch2}
for (int i = 0; i < testM.rows; i++)
{
uchar* pdata = testM.data + i*testM.step;
int k = 0;
for (int j = 0; j < testM.cols; j++, k+=3)
{
printf("r=%d,c=%d: %d,%d,%d\n", i, j, pdata[k],pdata[k+1],pdata[k+2]);
}
}
輸出:
r=0,c=0: 0,10,20
r=0,c=1: 0,10,20
r=0,c=2: 0,10,20
r=0,c=3: 0,10,20
r=1,c=0: 1,11,21
r=1,c=1: 1,11,21
r=1,c=2: 1,11,21
r=1,c=3: 1,11,21
r=2,c=0: 2,12,22
r=2,c=1: 2,12,22
r=2,c=2: 2,12,22
r=2,c=3: 2,12,22
沒有留言:
張貼留言