How to find out what type of Mat object is associated with Mat :: type () in OpenCV

I am a little confused with the type() method of the Mat object in OpenCV.
If I have the following lines:

 mat = imread("C:\someimage.jpg"); type = mat.type(); 

and type = 16 . How to find out what type of mat matrix ?.
I tried in vain to find the answer in my manual or in several books.

+82
opencv opencv-mat
Apr 16 2018-12-12T00:
source share
6 answers

Here is a handy feature that you can use to help identify your opencv matrices at runtime. I find this useful for debugging, at least.

 string type2str(int type) { string r; uchar depth = type & CV_MAT_DEPTH_MASK; uchar chans = 1 + (type >> CV_CN_SHIFT); switch ( depth ) { case CV_8U: r = "8U"; break; case CV_8S: r = "8S"; break; case CV_16U: r = "16U"; break; case CV_16S: r = "16S"; break; case CV_32S: r = "32S"; break; case CV_32F: r = "32F"; break; case CV_64F: r = "64F"; break; default: r = "User"; break; } r += "C"; r += (chans+'0'); return r; } 

If M is a var of type Mat , you can call it like this:

 string ty = type2str( M.type() ); printf("Matrix: %s %dx%d \n", ty.c_str(), M.cols, M.rows ); 

Print data such as:

 Matrix: 8UC3 640x480 Matrix: 64FC1 3x2 

It is worth noting that there are also Matrix methods Mat::depth() and Mat::channels() . This function is just a convenient way to get a human-readable interpretation from a combination of these two values, whose bits are stored in the same value.

+133
Jul 23 '13 at 20:40
source share

For debugging purposes, in case you want to find the raw type Mat :: in the debugger:

 +--------+----+----+----+----+------+------+------+------+ | | C1 | C2 | C3 | C4 | C(5) | C(6) | C(7) | C(8) | +--------+----+----+----+----+------+------+------+------+ | CV_8U | 0 | 8 | 16 | 24 | 32 | 40 | 48 | 56 | | CV_8S | 1 | 9 | 17 | 25 | 33 | 41 | 49 | 57 | | CV_16U | 2 | 10 | 18 | 26 | 34 | 42 | 50 | 58 | | CV_16S | 3 | 11 | 19 | 27 | 35 | 43 | 51 | 59 | | CV_32S | 4 | 12 | 20 | 28 | 36 | 44 | 52 | 60 | | CV_32F | 5 | 13 | 21 | 29 | 37 | 45 | 53 | 61 | | CV_64F | 6 | 14 | 22 | 30 | 38 | 46 | 54 | 62 | +--------+----+----+----+----+------+------+------+------+ 

So, for example, if type = 30, then the OpenCV data type is CV_64FC4. If type = 50, then the OpenCV data type is CV_16UC (7).

+71
Sep 29 '16 at 10:11
source share

There is a set of definitions in the OpenCV header "types_c.h" that generate them, format CV_bits{U|S|F}C<number_of_channels>
So, for example, CV_8UC3 means 8-bit unsigned characters, 3 color channels - each of these names is mapped to an arbitrary integer with macros in this file.

Edit: See "types_c.h" for example:

 #define CV_8UC3 CV_MAKETYPE(CV_8U,3) #define CV_MAKETYPE(depth,cn) (CV_MAT_DEPTH(depth) + (((cn)-1) << CV_CN_SHIFT)) eg. depth = CV_8U = 0 cn = 3 CV_CN_SHIFT = 3 CV_MAT_DEPTH(0) = 0 (((cn)-1) << CV_CN_SHIFT) = (3-1) << 3 = 2<<3 = 16 

So CV_8UC3 = 16 , but you shouldn't use this number, just check type() == CV_8UC3 if you need to know which type is an internal OpenCV array.
Remember that OpenCV converts jpeg to BGR (or grayscale if you pass "0" to imread ) - so it says nothing about the source file.

+32
Apr 16 '12 at 3:50
source share

I always use this link to find out what type I get with type() :
LIST OF TYPE MAT IN OPENCV
I hope this can help you.

+10
Apr 26 '16 at 23:46
source share

I added some usability of the function from @Octopus answer for debugging purposes.

 void MatType( Mat inputMat ) { int inttype = inputMat.type(); string r, a; uchar depth = inttype & CV_MAT_DEPTH_MASK; uchar chans = 1 + (inttype >> CV_CN_SHIFT); switch ( depth ) { case CV_8U: r = "8U"; a = "Mat.at<uchar>(y,x)"; break; case CV_8S: r = "8S"; a = "Mat.at<schar>(y,x)"; break; case CV_16U: r = "16U"; a = "Mat.at<ushort>(y,x)"; break; case CV_16S: r = "16S"; a = "Mat.at<short>(y,x)"; break; case CV_32S: r = "32S"; a = "Mat.at<int>(y,x)"; break; case CV_32F: r = "32F"; a = "Mat.at<float>(y,x)"; break; case CV_64F: r = "64F"; a = "Mat.at<double>(y,x)"; break; default: r = "User"; a = "Mat.at<UKNOWN>(y,x)"; break; } r += "C"; r += (chans+'0'); cout << "Mat is of type " << r << " and should be accessed with " << a << endl; } 
+6
Apr 04 '17 at 19:07 on
source share

A few others answered, but I found a solution that worked fine for me.

 System.out.println(CvType.typeToString(yourMat)); 
+2
Aug 15 '17 at 19:21
source share



All Articles