如何在 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
类型的 var 你可以这样调用它:
If M
is a var of type Mat
you can call it like so:
string ty = type2str( M.type() );
printf("Matrix: %s %dx%d
", ty.c_str(), M.cols, M.rows );
会输出数据如:
Matrix: 8UC3 640x480
Matrix: 64FC1 3x2
值得注意的是,还有矩阵方法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屋!