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通道
cv2.IMREAD_GRAYSCALE:读入灰度图片
cv2.IMREAD_UNCHANGED:顾名思义,读入完整图片,包括alpha通道
PS:alpha通道,又称A通道,是一个8位的灰度通道,该通道用256级灰度来记录图像中的透明度复信息,定义透明、不透明和半透明区域,其中黑表示全透明,白表示不透明,灰表示半透明
Experiemt
这里用到的两个img:MyPic.png,MyPicGray.png
MyPic.png:500x500, 24bpp |
MyPicGray.png:500x500, 8bpp |
当然需要先导入cv2,并接下来会使用的一个自定义fun()
1 2 3 4 | import cv2 # ouput img properties def funOutputImgProperties(img): print("properties:shape:{},size:{},dtype:{}".format(img.shape,img.size,img.dtype)) |
首先对彩色图MyPic测试:imread分别以三种flag读取MyPic,并输出图像参数:
代码如下:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | # 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) |
输出信息:
窗口图像
![]() |
![]() |
![]() |
控制台输出:
1 2 3 | 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 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 | # 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) |
输出信息:
窗口图像
![]() |
![]() |
![]() |
控制台输出:
1 2 3 | 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是[[ ]]。
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 | [[[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。
MyPic.png:500x500, 24bpp
MyPicGray.png:500x500, 8bpp




