简体   繁体   中英

Accessing each pixel of a Mat

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

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

the output I get looks like this:

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

can someone explain to me why is it so?

Shouldn't it be same?

Here's what it looks like the memory pointed at by p is:

// |251 |123 |63  |254 |...   uint8_t
// +----+----+----+----+----+
// | FB | 7B | 3F | FE |... | hex
// +----+----+----+----+----+
// |0.00683212          ...   double

when you do y.at<double>(i,j) what's essentially going on is:

p[i*y.cols + j]

you are accessing the array as a uint8_t and therefore get a uint8_t which converted to double keeps the same value. When you access it through .at<double>() , its accessing it as a double*

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM