Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
434 views
in Technique[技术] by (71.8m points)

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

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().

However, as you would expect, calling img.type() just gives an integer, an not an enum name (e.g. 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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

To my knowledge, such a function doesn't exist in 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();
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...