如何在OpenCV中使用Mat :: type()找出Mat对象的类型 [英] How to find out what type of a Mat object is with Mat::type() in OpenCV
问题描述
我对OpenCV中Mat
对象的type()
方法有点困惑.
如果我有以下几行:
I am kind of confused with type()
method of Mat
object in OpenCV.
If I have following lines:
mat = imread("C:\someimage.jpg");
type = mat.type();
和type = 16
.如何找出矩阵矩阵的类型?
我试图在手册中或徒劳的几本书中找到答案.
and type = 16
. How do I find out what type of mat matrix is?.
I tried to find the answer in its manual or in a couple of books in vain.
推荐答案
这是一个方便的函数,可用于帮助您在运行时识别opencv矩阵.我发现它至少对调试有用.
Here is a handy function you can use to help with identifying your opencv matrices at runtime. I find it 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;
}
如果M
是类型为Mat
的变量,则可以这样命名:
If M
is a var of type Mat
you can call it like so:
string ty = type2str( M.type() );
printf("Matrix: %s %dx%d \n", ty.c_str(), M.cols, M.rows );
将输出以下数据:
Matrix: 8UC3 640x480
Matrix: 64FC1 3x2
值得注意的是,还有Matrix方法Mat::depth()
和Mat::channels()
.此功能只是从这两个值的位组合都存储在同一值中的值中获取易于理解的解释的便捷方法.
Its worth noting that there are also Matrix methods Mat::depth()
and Mat::channels()
. This function is just a handy way of getting a human readable interpretation from the combination of those two values whose bits are all stored in the same value.
这篇关于如何在OpenCV中使用Mat :: type()找出Mat对象的类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!