3

I am struggling with putting large integers, such as 2942584, in a cv Mat. The only type accepting it was CV_8UC1, but it changes the value from 2942584 to 120 (well in 8 bits obviously).

But is there anyway to have the original value in a cv Mat??

Here is the simple code if it helps:

Mat matrix(6,10,CV_8UC1);
matrix.at<char>(0,0) = 2942584;
cout << (int)matrix.at<char>(0,0);

output:

120
George
  • 769
  • 4
  • 11
  • 31

1 Answers1

7

When you define matrix as CV_8UC1 you define that every element must be 8 bit. That means that you can store only value from 0 to 255. If you want to use a big number you should define matrix as CV_32UC1 for unsigned integers or CV_32SC1 for signed integers. Also you should to store a value as int instead of char and read it in appropriate way.

More correct code is

Mat matrix(6,10,CV_32SC1); 
matrix.at<int>(0,0) = 2942584;
cout << (int)matrix.at<int>(0,0);

One more thing: format of the matrix element is the following

CV_<NUMBER_OF_BITS><SIGNED/UNSIGNED>C<NUMBER_OF_CHANNELS>
Alex
  • 9,891
  • 11
  • 53
  • 87
  • Okay, I had tried CV_32SC1 but indeed char was the problem. Thank you so much – George Aug 26 '12 at 19:15
  • @George if you use an incorrect type for matrix allocation in memory, you will have a memory leak problem in future, that will be really hard to debug. – Alex Aug 26 '12 at 19:18
  • Okay, I will make sure to watch for this in the future. Those details are so obvious that unfortunately there is not a lot written in the documentation, and I didn't manage to figure it out by myself. Thanks again for the help! – George Aug 26 '12 at 19:29