Does there exist some methods that I can use to determine the data type(such as uchar,cv::Vec3b...)of the element stored in Mat structure in OpenCV?

有帮助吗?

解决方案

You can use cv::Mat::type() to determine the data type of pixels stored in the cv::Mat.

The type can be determined as follows:

int type = mat.type();
if(type == CV_8UC1)
    unsigned char* ptr = mat.ptr<unsigned char>();
else if(type == CV_8UC3)
    cv::Vec3b* ptr = mat.ptr<cv::Vec3b>();
else if(type == CV_16UC3)
    unsigned short* ptr = mat.ptr<unsigned short>();
else if(type == CV_16UC3)
    cv::Vec3w* ptr = mat.ptr<cv::Vec3w>();
else if(type == CV_32FC1)
    float* ptr = mat.ptr<float>();
else if(type == CV_32FC3)
    cv::Vec3f* ptr = mat.ptr<cv::Vec3f>();
else
    printf("Unknown type\n");
许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top