Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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 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.

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.