Ask Your Question

Revision history [back]

Accessing each pixel of a Mat .at vs pointer arithmetic gives differnet result

Today I stumbled across this (for me) weird thing, namely when I run this code

m.convertTo(tmp_mat, CV_32FC3, 1./255);
cvtColor(tmp_mat, tmp_mat, CV_RGB2XYZ, 3);
split(tmp_mat, xyz_channels);

y_channel = xyz_channels[1].clone();

int i,j;
uint8_t* p = y_channel.data;
for( i = 0; i < y_channel.rows; ++i)
{
    for ( j = 0; j < y_channel.cols; ++j)
    {
       std::cout << y_channel.at<double>(i,j) << std::endl;
       std::cout << saturate_cast<double>(p[i*y_channel.cols + j]) << std::endl;
    }
}

this is the output I get

0.00683212
123
0.00683212
63
0.00683212
254
0.00683212
251
0.00683212
123
0.00683212
63

So now I am confused - shouldn't it be same?

Accessing each pixel of a Mat .at vs pointer arithmetic gives differnet result

Today I stumbled across this (for me) weird thing, namely when I run this code

m.convertTo(tmp_mat, CV_32FC3, 1./255);
cvtColor(tmp_mat, tmp_mat, CV_RGB2XYZ, 3);
split(tmp_mat, xyz_channels);

y_channel = xyz_channels[1].clone();

int i,j;
uint8_t* p = y_channel.data;
for( i = 0; i < y_channel.rows; ++i)
{
    for ( j = 0; j < y_channel.cols; ++j)
    {
       std::cout << y_channel.at<double>(i,j) << std::endl;
       std::cout << saturate_cast<double>(p[i*y_channel.cols + j]) << std::endl;
    }
}

this is the output I get

0.00683212
123
0.00683212
63
0.00683212
254
0.00683212
251
0.00683212
123
0.00683212
63
...

So now I am confused - shouldn't it be same?

Accessing each pixel of a Mat .at vs pointer arithmetic gives differnet result

Today I stumbled across this (for me) weird thing, namely when I run this code

m.convertTo(tmp_mat, CV_32FC3, 1./255);
cvtColor(tmp_mat, tmp_mat, CV_RGB2XYZ, 3);
split(tmp_mat, xyz_channels);

y_channel = xyz_channels[1].clone();

int i,j;
uint8_t* p = y_channel.data;
for( i = 0; i < y_channel.rows; ++i)
{
    for ( j = 0; j < y_channel.cols; ++j)
    {
       std::cout << y_channel.at<double>(i,j) y_channel.at<float>(i,j) << std::endl;
       std::cout << saturate_cast<double>(p[i*y_channel.cols saturate_cast<float>(p[i*y_channel.cols + j]) << std::endl;
    }
}

this is the output I get

0.00683212
0.984314
123
0.00683212
0.984314
63
0.00683212
0.984314
254
0.00683212
0.984314
251
0.00683212
0.984314
123
0.00683212
0.984314
63
0.984314
254
0.984314
251
0.984314
123
0.984314
63
...

So now I am confused - shouldn't it be same?