Mat Datatypes in OpenCV
I spend most of my OpenCV debugging time on falsy datatypes causing all kinds of different errors, from segmentation faults to very strange image outputs. I searched the internet for a complete list of OpenCV datatypes that works as a quick reference. The documentation I found was poor, so I decided to write a short overview myself.
The datatypes are named by the following structure:
16UC3= 16-bit unsigned short, 3 channels32FC1= 32-bit float, 1 channel
OpenCV Mat datatype reference
-
Unsigned 8-bit (
uchar)0~255
CV_8UC1, CV_8UC2, CV_8UC3, CV_8UC4 -
Signed 8-bit (
char)-128~127
CV_8SC1, CV_8SC2, CV_8SC3, CV_8SC4 -
Unsigned 16-bit (
ushort)0~65535
CV_16UC1, CV_16UC2, CV_16UC3, CV_16UC4 -
Signed 16-bit (
short)-32768~32767
CV_16SC1, CV_16SC2, CV_16SC3, CV_16SC4 -
Signed 32-bit (
int)-2147483648~2147483647
CV_32SC1, CV_32SC2, CV_32SC3, CV_32SC4 -
32-bit float (
float)-1.18*10^-38 ~ 3.40*10^38
CV_32FC1, CV_32FC2, CV_32FC3, CV_32FC4 -
64-bit float (
double)
CV_64FC1, CV_64FC2, CV_64FC3, CV_64FC4
Initialize a Mat object
// Mat myMat(rows, cols, Datatype);
Mat myMat(2, 2, CV_32FC3);
Access a pixel in a Mat
// myMat.at(row, col);
myMat.at<int>(y, x);
myMat.at<float>(y, x);
myMat.at<double>(y, x);