mfc cimage加载显示图片_OpenCV加载图片显示对应类型(位深度)方法

45243044c5689e6692b7c038c9fafbce.gif

  对于部分初学者,偶尔会想在代码中查看图片的位深度,一般我们会用Mat.type()来获得类型,但是默认输出的是整型数字,不便于理解,可能还需要自己对照宏定义去查看,这里有一段代码可以实现将整型转为宏定义位深度

string Type2String(int type){  string strType;  uchar depth = type & CV_MAT_DEPTH_MASK;  uchar chans = 1 + (type >> CV_CN_SHIFT);  switch (depth)   {    case CV_8U:        strType = "CV_8U"; break;    case CV_8S:        strType = "CV_8S"; break;    case CV_16U:       strType = "CV_16U"; break;    case CV_16S:       strType = "CV_16S"; break;    case CV_32S:       strType = "CV_32S"; break;    case CV_32F:       strType = "CV_32F"; break;    case CV_64F:       strType = "CV_64F"; break;    default:        strType = "UNKNOWN_TYPE"; break;  }  strType += "C";  strType += (chans + '0');  return strType;}

6ea4b66b646700537845de4a848e0518.png  比如,使用imread读取图片,如果flags参数不填,默认以彩色模式读取并转为8位,那么类型就是CV_8UC3, 如果flags设置位0,则以灰度模式读取,类型位CV_8UC1。但是,有时候我们会读取一些16位或32位的图片,这时候就需要设置flags参数为 IMREAD_UNCHANGED,这样读取的图片位深度才会被正确显示,如下:

Mat img = imread("1.tif", IMREAD_UNCHANGED);cout <endl;cout <endl;

ba1016373c485758011fea245a22bfb9.png

c8865e1d346d38388c7daa44a739a6c6.png

   flags参数其他含义可以参考定义说明查看:

92727a0a86ce72fd9a2d0b4ba9f9a0cb.png

    完整代码:

#include#includeusing namespace std;using namespace cv;string Type2String(int type){  string strType;  uchar depth = type & CV_MAT_DEPTH_MASK;  uchar chans = 1 + (type >> CV_CN_SHIFT);  switch (depth)   {    case CV_8U:        strType = "CV_8U"; break;    case CV_8S:        strType = "CV_8S"; break;    case CV_16U:       strType = "CV_16U"; break;    case CV_16S:       strType = "CV_16S"; break;    case CV_32S:       strType = "CV_32S"; break;    case CV_32F:       strType = "CV_32F"; break;    case CV_64F:       strType = "CV_64F"; break;    default:        strType = "UNKNOWN_TYPE"; break;  }  strType += "C";  strType += (chans + '0');  return strType;}void main(){  Mat img = imread("1.tif", IMREAD_UNCHANGED);  cout << img.type() << endl;  cout << Type2String(img.type()) << endl;}

fb49ae5a84d3f0642471dc6da472d626.png

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值