Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Setting up depth for a cv::Mat

Tags:

c++

opencv

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

It 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 with a made-up mat, to avoid using 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 have experimented with all kinds of combinations, but if I print, I get 0 depth.

I have also tried to load a png or a jpg file - with same result (I prefer not to use outside files... but I see no reason why this doesn't 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 );

They all seem to have depth = 0 ?

Is there something else I have to do ? I can't see anything in documentation.

like image 628
Thalia Avatar asked May 29 '13 16:05

Thalia


People also ask

What is a CV mat?

The Mat class of OpenCV library is used to store the values of an image. It represents an n-dimensional array and is used to store image data of grayscale or color images, voxel volumes, vector fields, point clouds, tensors, histograms, etc.

What is CV_64F?

That is, image of type CV_64FC1 is simple grayscale image and has only 1 channel: image[i, j] = 0.5. while image of type CV_64FC3 is colored image with 3 channels: image[i, j] = (0.5, 0.3, 0.7) (in C++ you can check individual pixels as image.at<double>(i, j) ) CV_64F is the same as CV_64FC1 .

What is CV_8UC3?

CV_8UC3 - 3 channel array with 8 bit unsigned integers. CV_8UC4 - 4 channel array with 8 bit unsigned integers. CV_8UC(n) - n channel array with 8 bit unsigned integers (n can be from 1 to 512) )

What is scalar OpenCV?

ScalarRepresents a 4-element vector. The type Scalar is widely used in OpenCV for passing pixel values. In this tutorial, we will use it extensively to represent BGR color values (3 parameters). It is not necessary to define the last argument if it is not going to be used.


1 Answers

When you call depth() on Mat, it returns depth values as defined below instead of 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 cv::DataDepth::value to figure out which one is which. For example,

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

So you should get 0 on all CV_8UCX matrix, and when you load an image, it is usually loaded as CV_8UC3, so you will get 0 as well. But I am not sure why you got 0 on cv::Mat M(255, 255, CV_32FC3), I tested it on my computer, it returned 5.

like image 66
cxyzs7 Avatar answered Oct 15 '22 18:10

cxyzs7