header image
[ # ] OpenCV Mat and its Data Types
June 14th, 2010 under Programming

Since, OpenCV’s documentation is more than lacking a few sentences I’ll post some important facts that can easily be missed.

To access the elements of a

1
cv::Mat
cv::Mat
one can use the
1
at<T>(int y, int x)
at<T>(int y, int x)
method. Usually, the elements are stored as doubles ( ). So to get for example elements (1,1) the call is

1
mat.at<double>(1,1);
mat.at<double>(1,1);

but you’re not forbidden to call

1
mat.at<float>(1,1);
mat.at<float>(1,1);

OpenCV won’t complain that you’re reading the wrong data type. OpenCV also doesn’t perform a type conversion. Hence, you’ll end up with the wrong data.

In summary, make sure you’re reading the correct data type. You might use the

1
depth()
depth()
method to figure out the internal type of the matrix