C++ 获取 OpenCV 图像类型的枚举名称(例如 CV_32FC1)?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/12335663/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 16:10:25  来源:igfitidea点击:

Getting enum names (e.g. CV_32FC1) of OpenCV image types?

c++image-processingopencvenumsimage-formats

提问by solvingPuzzles

In the C++ interface to OpenCV, it seems easy enough to check the type of an image. If you have an image cv::Mat img = cv::imread("someImage.xyz"), you just do int theType = img.type().

在 OpenCV 的 C++ 接口中,检查图像的类型似乎很容易。如果你有一个图像cv::Mat img = cv::imread("someImage.xyz"),你就做int theType = img.type()

However, as you would expect, calling img.type()just gives an integer, an not an enum name (e.g. CV_32FC1).

然而,正如您所期望的,调用img.type()只给出一个整数,而不是一个枚举名称(例如CV_32FC1)。

Is there an easy way to print out the enum name (e.g. CV_32FC1) if I know the integer value of the OpenCV enum?

CV_32FC1如果我知道 OpenCV 枚举的整数值,是否有一种简单的方法可以打印出枚举名称(例如)?

回答by Sassa

To my knowledge, such a function doesn't exist in OpenCV.

据我所知,OpenCV 中不存在这样的函数。

I think you would be better off writing your own function to get those. A lot of switch cases but I guess it does the job. The enumeration can be found here.

我认为你最好编写自己的函数来获得这些。很多开关盒,但我想它可以完成工作。枚举可以在这里找到。

EDIT:

编辑:

This is something you could use to extract the types. I am guessing there could be a more efficient method, but I can't wrap my head around it right now.

这是您可以用来提取类型的东西。我猜可能有更有效的方法,但我现在无法解决它。

std::string getImageType(int number)
{
    // find type
    int imgTypeInt = number%8;
    std::string imgTypeString;

    switch (imgTypeInt)
    {
        case 0:
            imgTypeString = "8U";
            break;
        case 1:
            imgTypeString = "8S";
            break;
        case 2:
            imgTypeString = "16U";
            break;
        case 3:
            imgTypeString = "16S";
            break;
        case 4:
            imgTypeString = "32S";
            break;
        case 5:
            imgTypeString = "32F";
            break;
        case 6:
            imgTypeString = "64F";
            break;
        default:
            break;
    }

    // find channel
    int channel = (number/8) + 1;

    std::stringstream type;
    type<<"CV_"<<imgTypeString<<"C"<<channel;

    return type.str();
}

回答by solvingPuzzles

Following @Bob's advice, I wrote my own function to solve this problem. Here it is:

按照@Bob 的建议,我编写了自己的函数来解决这个问题。这里是:

// take number image type number (from cv::Mat.type()), get OpenCV's enum string.
string getImgType(int imgTypeInt)
{
    int numImgTypes = 35; // 7 base types, with five channel options each (none or C1, ..., C4)

    int enum_ints[] =       {CV_8U,  CV_8UC1,  CV_8UC2,  CV_8UC3,  CV_8UC4,
                             CV_8S,  CV_8SC1,  CV_8SC2,  CV_8SC3,  CV_8SC4,
                             CV_16U, CV_16UC1, CV_16UC2, CV_16UC3, CV_16UC4,
                             CV_16S, CV_16SC1, CV_16SC2, CV_16SC3, CV_16SC4,
                             CV_32S, CV_32SC1, CV_32SC2, CV_32SC3, CV_32SC4,
                             CV_32F, CV_32FC1, CV_32FC2, CV_32FC3, CV_32FC4,
                             CV_64F, CV_64FC1, CV_64FC2, CV_64FC3, CV_64FC4};

    string enum_strings[] = {"CV_8U",  "CV_8UC1",  "CV_8UC2",  "CV_8UC3",  "CV_8UC4",
                             "CV_8S",  "CV_8SC1",  "CV_8SC2",  "CV_8SC3",  "CV_8SC4",
                             "CV_16U", "CV_16UC1", "CV_16UC2", "CV_16UC3", "CV_16UC4",
                             "CV_16S", "CV_16SC1", "CV_16SC2", "CV_16SC3", "CV_16SC4",
                             "CV_32S", "CV_32SC1", "CV_32SC2", "CV_32SC3", "CV_32SC4",
                             "CV_32F", "CV_32FC1", "CV_32FC2", "CV_32FC3", "CV_32FC4",
                             "CV_64F", "CV_64FC1", "CV_64FC2", "CV_64FC3", "CV_64FC4"};

    for(int i=0; i<numImgTypes; i++)
    {
        if(imgTypeInt == enum_ints[i]) return enum_strings[i];
    }
    return "unknown image type";
}

Did I forget to include any OpenCV image types in my lookup table?

我是否忘记在查找表中包含任何 OpenCV 图像类型?