Home > other >  Mat data types and access
Mat data types and access

Time:03-04

1. The opencv data type and common type correspondence
Want to accurate element values in the access to the Mat, in the understanding of opencv data type is important, he decided to the size of the memory of each element in the Mat,
Opencv in various type definition:

# define corresponding uchar CV_8U 0//
1//# define CV_8S corresponding char
2//# define CV_16U corresponding ushort
3//# define CV_16S corresponding short
4//# define CV_32S corresponding int
5//# define CV_32F corresponding float
6//# define CV_64F corresponding double
# define CV_USRTYPE1 7
.
# define CV_8UC1 CV_MAKETYPE (CV_8U, 1)//0
# define CV_8UC2 CV_MAKETYPE (CV_8U, 2)//8
# define CV_8UC3 CV_MAKETYPE (CV_8U, 3)//16
# define CV_8UC4 CV_MAKETYPE (CV_8U, 4)//32
# define CV_8UC (n) CV_MAKETYPE (CV_8U, (n))

# define CV_8SC1 CV_MAKETYPE (CV_8S, 1)//1
# define CV_8SC2 CV_MAKETYPE (CV_8S, 2)//9
# define CV_8SC3 CV_MAKETYPE (CV_8S, 3)//17
# define CV_8SC4 CV_MAKETYPE (CV_8S, 4)//33
# define CV_8SC (n) CV_MAKETYPE (CV_8S, (n))
.
# define CV_64FC1 CV_MAKETYPE (CV_64F, 1)//6
# define CV_64FC2 CV_MAKETYPE (CV_64F, 2)//14
# define CV_64FC3 CV_MAKETYPE (CV_64F, 3)//22
# define CV_64FC4 CV_MAKETYPE (CV_64F, 4)//38
# define CV_64FC (n) CV_MAKETYPE (CV_64F, (n))

Note: CV_MAKETYPE (the depth, cn)=(cn - 1) * 8 + the depth//define type, type is not conflict
Supplement: the change of the data types in opencv
Pay attention to the Mat matrix applicable to the various functions in the opencv data type, when the Mat matrix data type is improper, after the function call is likely to cause the change of the data type, if change, which would affect the Mat matrix data access,

C + + code of the case: (VS)

CamMatrix=CV: : Mat: : zeros (3, 3, CV_32FC1);//float
CV: : calibrateCamera (ptWs m_pts, image, the size (), camMatrix, distCoeffs, rotations, tranlation, CV_CALIB_FIX_K3);
If the code above, the new internal matrix camMatrix are float type (32-bit), in the call calibrateCamera procedures for calibration, internal matrix camMatrix data types from the initial 32-bit float into 64 for a double type, resulting in the data access errors, so need to initialize camMatrix CV_64FC1 here,
In uncertain type, by looking at the source code or debug view type transformation can be variable types, such as the actual view can see calibrateCamera opencv source code source code in the world coordinate (ptWs) and image point coordinate (m_pts) for float type (32), and the internal matrix (camMatrix) in source, distortion matrix (distCoeffs), rotating vector matrix (rotations - (1) X3 (rows) Xn (column)), translation vector matrix transformation to CV_64F type,

2. The Mat data access
2.1 the at (I, j)
Int ROWS=100;
Int COLS=200;
Mat img (ROWS, COLS, CV_32FC1);

for (int i=0; i{
for (int j=0; j{
Img. At (I, j)=3.2 f;
}
}
2.2 PTR (I) [j]
Int ROWS=100;
Int COLS=200;
Mat img (ROWS, COLS, CV_32FC1);

for (int i=0; i{
Float * PTR=img. Ptr (I);
for (int j=0; j{
PTR [j]=3.2 f;
}
}
Cover: access 8-bit image, type=uchar;
24 images, type=vec3b;
  • Related