# Large Integer in cv::Mat

Hey, I'm a beginner in OpenCv, and i use Opencv2 API in C++. I want to create the distance map from a segmentation image. I use the Chamfer method described by Gunilla Borgefors in 'Distance Transformation In Digital Images' I have this in input And this in output As you can see, the distance map is smaller than the input image. I wanted normalize this image but it doesn't work because almost all the data is equal to 255. it's because processing wants put integer > 255 in the data of the cv::Mat, but this is not allowed, the integer is truncated and lot of my data become 255. How can i put long integer in the matrix, a normalize will rescale the data in the range 0-255 ? Sorry for my english, i hope you understand what i want mean. Thank you for your answers, Psykomusic

edit retag close merge delete

Sort by ยป oldest newest most voted

Truncating will occur if you use a cv::Mat object of type CV_8U (or its variants CV_8UC1, etc...) and try to write an value larger than 255. Create a cv::Mat object of a larger signed type, for example CV_32S or CV_32U instead CV_8U. CV_32S will use 32bit integer type. The assignment of values can be done in the following way:

cv::Mat my_matrix_of_integers(200,200,CV_32S); //creating a 200x200 matrix of 32 bit signed values
my_matrix_of_integers.at<int32_t>(30,40) = 1034; //assignent of a signed 32 bit value


It is VERY important to you know the type of your matrix before assigning a value. If you try to assign a large integer value using "at.< int32_t >" in a matrix of type CV_8U, your program is very likely to crash at certain point, since OpenCV don't verify this and will write outside the allocated memory boundaries...or worse, invading a memory block of another allocated data structure of your software. ALWAYS check the type of your matrix with the cv::Mat::type() function.

Another thing: cv::imshow seems to not like any matrices that arent of type CV_8U and CV_32F. Use cv::Mat::convertTo to convert the matrix to a compatible type for displaying and scale its values.

more

Thank you very much !

( 2015-06-16 14:51:33 -0500 )edit

Official site

GitHub

Wiki

Documentation