Depth setting for cv :: Mat

I want to test a function that searches for a specific mat depth & number of channels

He has a test ...

if (image.channels() == 1 && image.depth() == 8) ... else if (image.channels() == 1 && image.depth() == 16) ... else if (image.channels() == 1 && image.depth() == 32) ... else { if ((image.channels() != 3) || (image.depth() != 8)) {printf("Expecting rgb24 input image"); return false;} ... } 

I prefer to test using an artificial mat so as not to use local resources:

 cv::Mat M(255, 255, CV_8UC3, cv::Scalar(0,0,255)); printf("M: %d %d \n", M.channels(), M.depth()); cv::Mat M1(255, 255, CV_32F, cv::Scalar(0,0,255)); cv::Mat M2(255, 255, CV_32FC3, cv::Scalar(0,0,255)); cv::Mat M2(255, 255, CV_8SC3, cv::Scalar(0,0,255)); 

I experimented with all kinds of combinations, but if I typed, I got 0 depths.

I also tried downloading a png or jpg file - with the same result (I prefer not to use external files ... but I see no reason why this does not work)

 cv::Mat M3 = cv::imread( "c:/my_image.png", CV_LOAD_IMAGE_COLOR ); cv::Mat M3 = cv::imread( "c:/my_image.jpg", CV_LOAD_IMAGE_COLOR ); 

It seems they all have depth = 0?

Is there anything else I should do? I do not see anything in the documentation.

+6
source share
1 answer

When you call depth () on Mat, it returns depth values, as defined below, instead of the number of bits:

 #define CV_8U 0 #define CV_8S 1 #define CV_16U 2 #define CV_16S 3 #define CV_32S 4 #define CV_32F 5 #define CV_64F 6 

And you can use the cv :: DataDepth :: value to find out which one is. For instance,

 cv::DataDepth<unsigned char>::value == CV_8U; cv::DataDepth<float>::value == CV_32F; 

So you should get 0 in the whole CV_8UCX matrix, and when you upload an image, it usually loads as CV_8UC3, so you will also get 0. But I'm not sure why you got 0 on cv :: Mat M (255, 255, CV_32FC3), I tested it on my computer, it returned 5.

+9
source

Source: https://habr.com/ru/post/946104/


All Articles