存储在OpenCV的Mat结构中的元素的类型

skf*_*eng 0 c++ opencv types mat

是否存在一些方法可用于确定OpenCV中Mat结构中存储的元素的数据类型(如uchar,cv :: Vec3b ...)?

sga*_*zvi 5

您可以使用cv::Mat::type()来确定存储在中的像素的数据类型cv::Mat.

类型可以确定如下:

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");
Run Code Online (Sandbox Code Playgroud)