cv2 imread()函数

Reason

        这两天学习OpenCV-Python时,也就是cv2库,读取图像时不时出现和预料之外的结果。于是作者从源头来考究一下cv2.imread(filename, flags)

Result

这里参考文章cv2.imread(filename, flags)
cv2.imread(filename, flags)
参数:
filepath:读入imge的完整路径
flags:标志位,{cv2.IMREAD_COLOR,cv2.IMREAD_GRAYSCALE,cv2.IMREAD_UNCHANGED}
cv2.IMREAD_COLOR:默认参数,读入一副彩色图片,忽略alpha通道,可用1作为实参替代
cv2.IMREAD_GRAYSCALE:读入灰度图片,可用0作为实参替代
cv2.IMREAD_UNCHANGED:顾名思义,读入完整图片,包括alpha通道,可用-1作为实参替代
PS:alpha通道,又称A通道,是一个8位的灰度通道,该通道用256级灰度来记录图像中的透明度复信息,定义透明、不透明和半透明区域,其中黑表示全透明,白表示不透明,灰表示半透明

Experiemt

    这里用到的两个img:MyPic.png,MyPicGray.png

MyPic.png:500x500, 24bpp
MyPicGray.png:500x500, 8bpp
        当然需要先导入cv2,并接下来会使用的一个自定义fun()
import cv2
 # ouput img properties 
def funOutputImgProperties(img):
    print("properties:shape:{},size:{},dtype:{}".format(img.shape,img.size,img.dtype))

        首先对彩色图MyPic测试:imread分别以三种flag读取MyPic,并输出图像参数:
代码如下:

# 3 channels img loads
# 读入完整图片,含alpha通道
img3ChaCom = cv2.imread('MyPic.png', cv2.IMREAD_UNCHANGED)
cv2.imshow('IMREAD_UNCHANGED+Color',img3ChaCom)
cv2.waitKey()
funOutputImgProperties(img3ChaCom)

# 读入彩色图片,忽略alpha通道
img3Cha=cv2.imread('MyPic.png',cv2.IMREAD_COLOR)
cv2.imshow('IMREAD_COLOR+Color', img3Cha)
cv2.waitKey()
funOutputImgProperties(img3Cha)

#彩色图片按,灰度图片读入
img3ChaGray=cv2.imread('MyPic.png',cv2.IMREAD_GRAYSCALE)
cv2.imshow('IMREAD_GRAYSCALE+Color', img3ChaGray)
cv2.waitKey()
funOutputImgProperties(img3ChaGray)

输出信息:
    窗口图像

    控制台输出:
properties:shape:(500, 500, 3),size:750000,dtype:uint8
properties:shape:(500, 500, 3),size:750000,dtype:uint8
properties:shape:(500, 500),size:250000,dtype:uint8

        和作者预想的一样,IMREAD_UNCHANGED和IMREAD_COLOR标志位的img读取自然是3 channels彩图,而使用IMREAD_GRAYSCALE标志位时,imread将彩色图像直接读取为灰色,B\G\R的三通道变成了单通道。图片像素改变:500X500X3->500X500X1
        接下来,考察灰度图MyPicGray:imread分别以三种flag读取MyPic,并输出图像参数:
代码如下:

# 1 channel img loads
# 读入完整图片,含alpha通道
img1ChaCom = cv2.imread('MyPicGray.png', cv2.IMREAD_UNCHANGED)
cv2.imshow('IMREAD_UNCHANGED+Gray', img1ChaCom)
cv2.waitKey()
funOutputImgProperties(img1ChaCom)

# 读入彩色图片,忽略alpha通道
img1ChaColor = cv2.imread('MyPicGray.png', cv2.IMREAD_COLOR)
cv2.imshow('IMREAD_COLOR+Gray', img1ChaColor)
cv2.waitKey()
funOutputImgProperties(img1ChaColor)

# 彩色图片按,灰度图片读入
img1Cha = cv2.imread('MyPicGray.png', cv2.IMREAD_GRAYSCALE)
cv2.imshow('IMREAD_GRAYSCALE+Gray', img1Cha)
cv2.waitKey()
funOutputImgProperties(img1Cha)

输出信息:
    窗口图像

    控制台输出:
properties:shape:(500, 500),size:250000,dtype:uint8
properties:shape:(500, 500, 3),size:750000,dtype:uint8
properties:shape:(500, 500),size:250000,dtype:uint8

        结合输出图像和控制台输出我们可以看到,三个imgshow的输出都是展现的灰度图,但是imread的flag为IMREAD_COLOR时,图像实际上时三通道的彩色图。
        进一步的,我们直接输出IMREAD_COLOR+Gray和IMREAD_GRAYSCALE+Gray的像素矩阵。可以看到IMREAD_COLOR+Gray确实时三通道的[ [ [ ] ] ]像素矩阵,IMREAD_GRAYSCALE+Gray是[ [ ] ]。

[[[246 246 246]
  [246 246 246]
  [246 246 246]
  ...
  [246 246 246]
  [246 246 246]
  [246 246 246]]

 [[246 246 246]
  [246 246 246]
  [246 246 246]
  ...
  [246 246 246]
  [246 246 246]
  [246 246 246]]

 [[246 246 246]
  [246 246 246]
  [246 246 246]
  ...
  [246 246 246]
  [246 246 246]
  [246 246 246]]

 ...

 [[246 246 246]
  [246 246 246]
  [246 246 246]
  ...
  [246 246 246]
  [246 246 246]
  [246 246 246]]

 [[246 246 246]
  [246 246 246]
  [246 246 246]
  ...
  [246 246 246]
  [246 246 246]
  [246 246 246]]

 [[246 246 246]
  [246 246 246]
  [246 246 246]
  ...
  [246 246 246]
  [246 246 246]
  [246 246 246]]]
  
[[246 246 246 ... 246 246 246]
 [246 246 246 ... 246 246 246]
 [246 246 246 ... 246 246 246]
 ...
 [246 246 246 ... 246 246 246]
 [246 246 246 ... 246 246 246]
 [246 246 246 ... 246 246 246]]

Conclusion

        收获:除了深入了解imread函数的参数,更多的是知道3 channels colored img可以直接读取为1 channel grayscale img;而1 channel grayscale img也可以读取为3 channels colored img。

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值