1

I am trying to loop over a matrix and print its element, which should be a simple operation, but I experience some strange things...

I have a a null matrix : cv::Mat accum = cv::Mat::zeros(3,5,CV_8U);

Doing this:

for(int i=0;i<accum.rows;i++)
{
    for(int j=0;j<accum.cols;j++)
    {
        cout<<accum.at<int>(i,j) <<endl;
    }
}

I get the following elements:

0 0 0 0 0 0 0 0 0 -536870912 0 0 0 2027945984 587217671

Why is there some random number at places where zero should be?

If I initialize the value of matrix at i=1,j=1 with number 1, I get the following

0 0 256 0 0 0 1 0 0 587202560 0 0 0 1931673600 587257437

I just dont understand those random values, I might do something wrong, but cant figure out what. Could you please help?

Kristan
  • 387
  • 6
  • 19
  • 5
    `CV_8U` -- the elements of the matrix are bytes, i.e. **8bit unsigned integers**. `accum.at` -- yet you're reading them as **32bit signed integers** – Dan Mašek Aug 21 '17 at 10:46
  • @DanMašek Could you please, give an example how would it be correct? I tried to print accum.at as well but did not work out. And if I print the matrix itself, where I gave 1 to position (1,1), I got the following...:[ 0, 0, 0, 0, 0; 0, 0, 0, 0, 1; 0, 0, 0, 0, 0] – Kristan Aug 21 '17 at 11:02
  • 1
    An 8bit unsigned integer would be a `unsigned char` (or `uint8_t` if you prefer to use the aliases provided by ``). – Dan Mašek Aug 21 '17 at 11:20

0 Answers0