Ask Your Question
6

Matrix depth equals 0

asked 2012-07-24 21:52:29 -0600

Pat gravatar image

updated 2012-07-24 21:53:25 -0600

If I do the following :

cv::Mat image = imread("someImage.bmp");

cv::Mat dst;

image.copyTo(dst);

int depth = dst.depth()//dst has a depth of 0

I'm not sure I follow why the depth is zero, according to the doc copyTo does(Before the copy):

dst.create(this->size(), this->type);

Doesn't the type tell the depth since it's likely CV_8UC3?

Otherwise how should I make dst have the same properties as 'image' after a copyTo? I'm asking because I've hard-coded the depth to 8 in my code and it works fine, so I just need to match the settings of the first mat to the second one. The create function only takes rows, cols and type. I assumed giving it the type would be enough for the mat to figure out it's depth. The channels() is set properly to 3.

edit retag flag offensive close merge delete

1 answer

Sort by ยป oldest newest most voted
9

answered 2012-07-25 02:33:57 -0600

Michael Burdinov gravatar image

I think you are missing the difference between dst.depth() and dst.type().

Function depth() returns information about type of data stored in image. It can be unsigned char (CV_8U), signed char (CV_8S), unsigned short (CV_16U), and so on... In your case (unsigned char) depth is CV_8U. Also don't forget that CV_8U is something created for user convenience only (through macro). Its actual value is 0. Value of CV_8S is 1 ... value of CV_64F is 6. And that is exactly what you got by calling dst.depth().

Function type() returns information combined from 2 values: image depth + number of channels. It can be for example CV_8UC1 (which is equal to CV_8U), CV_8UC2, CV_8UC3, CV_8SC1 (which is equal to CV_8S), and so on... If you used dst.type() you would get value different from 0.

Anyway don't use those integers values directly. This is inconvenient, creates unreadable code, and may lead to bugs if some future version of OpenCV will change the way it stores this information. Instead use functions and typedefs of OpenCV. Here some examples:

if (dst.depth() == CV_8U)
      cout << "Unsigned char image" << endl;
cout << "Number of channels is: " << dst.channels() << endl;
Mat tmp(dst.size(),dst.type());
edit flag offensive delete link more

Comments

1
Kirill Kornyakov gravatar imageKirill Kornyakov ( 2012-07-25 02:46:04 -0600 )edit

Question Tools

Stats

Asked: 2012-07-24 21:52:29 -0600

Seen: 16,583 times

Last updated: Jul 25 '12