参考文档:
matplotlib.pyplot.imshow 官方文档
plt.imshow()将灰度图像进行彩色映射 CSDN
以下三张图(都是通过 matplotlib.pyplot.imshow 进行显示!!!)
original 是原图(通过python的matplotlib.image.imread读取图像)
RGB2BGR 是转换后的图(opencv图像操作需要),opencv使用BGR,matplotlib使用RGB,混用时显示会有差别
transforms_image 是初始化之后的图
初始化的函数:
transform = T.Compose(
[
T.ToPILImage(),
Resize(min_size, max_size),#调整图像尺寸[800~1333]
T.ToTensor(),#转tensor归一化到[0~1]
to_bgr_transform,
normalize_transform,#归一化到[-1~1]
]
)
这里主要讲一下transforms_image的图像的颜色问题,一个字:怪!
'''
transforms_image 的打印结果
像素值有负数,我以为会显示不了,没想到也能显示,就离谱!
'''
tensor([[[ -61.9801, -61.9801, -62.9801, ..., 121.0199, 105.0199,
92.0199],
[ -62.9801, -62.9801, -63.9801, ..., 128.0199, 116.0199,
105.0199],
[ -62.9801, -63.9801, -63.9801, ..., 138.0199, 130.0199,
123.0199],
...,
[ -50.9801, -52.9801, -54.9801, ..., -32.9801, -27.9801,
-31.9801],
[ -55.9801, -58.9801, -62.9801, ..., -37.9801, -31.9801,
-33.9801],
[ -64.9801, -66.9801, -69.9801, ..., -42.9801, -34.9801,
-34.9801]],
[[ -90.9465, -90.9465, -91.9465, ..., 131.0535, 119.0535,
111.0535],
[ -91.9465, -91.9465, -92.9465, ..., 136.0535, 126.0535,
119.0535],
[ -91.9465, -92.9465, -92.9465, ..., 139.0535, 136.0535,
130.0535],
...,
[ -66.9465, -68.9465, -70.9465, ..., -24.9465, -17.9465,
-21.9465],
[ -71.9465, -74.9465, -78.9465, ..., -29.9465, -21.9465,
-23.9465],
[ -80.9465, -82.9465, -85.9465, ..., -34.9465, -24.9465,
-24.9465]],
[[ -96.7717, -96.7717, -97.7717, ..., 120.2283, 108.2283,
99.2283],
[ -97.7717, -97.7717, -98.7717, ..., 126.2283, 115.2283,
108.2283],
[ -97.7717, -98.7717, -98.7717, ..., 130.2283, 125.2283,
120.2283],
...,
[ -81.7717, -83.7717, -85.7717, ..., -39.7717, -32.7717,
-36.7717],
[ -86.7717, -89.7717, -93.7717, ..., -44.7717, -36.7717,
-38.7717],
[ -95.7717, -97.7717, -100.7717, ..., -49.7717, -39.7717,
-39.7717]]])
Clipping input data to the valid range for imshow with RGB data ([0..1] for floats or [0..255] for integers).
于是我想着能不能把图像的颜色范围显示看一下,使用了plt.colorbar()
函数,然后发现显示了个寂寞,图像中明明有黑色,在右边的 colorbar 上只有黄色到紫色
后来看官方文档才了解到 matplotlib 库 默认 的颜色范围就是黄到紫(也就是下面第二张图中的viridis颜色序列),这也就是问什么plt.imshow
显示的二值图不是黑白两色而是是黄色和紫色两色
设置了cmap参数:cmap='Greys'
的结果(真正的黑白二值图)
如果想要使用其他的颜色序列可以使用cmap
参数进行设置(下面有例子),最关键的是这个cmap
参数只能对单通道图像有效,对多通道图像(RGB、RGBA)等无效,所以colorbar
才会图像的颜色对不上
plt.title("transforms_image")
after_transforms = np.transpose(image, (1,2,0))#转换通道数
plt.imshow(after_transforms,cmap=plt.get_cmap('gist_gray'))#cmap='gist_gray'
plt.colorbar()
plt.show()
下图是只显示单通道图像,设置了cmap=plt.get_cmap('gist_gray')
之后的显示结果,这佐证了cmap
参数只对单通道图像有效
最后,还是回到图像的像素值出现负数,plt.imshow
怎么进行图像的显示的问题,其实官方文档已经进行了说明:只接受 [0~1] 、[0~255] 范围内的像素值,超过得部分(包括负数)全部舍弃掉,从最后显示的结果来看,舍弃掉也就是用0像素值进行填充了,所以就出现了图像中的大部分的黑色区域,其他像素值正常的区域正常显示
注意:(对超出范围的像素值进行舍弃的操作)只针对多通道图像(RGB、RGBA),单通道图像有负数也不会舍弃,它会按照数值的大小映射到颜色序列上(参考下面的例子像素值-100对应紫色,100对应黄色)
'''多通道图像'''
ttt=np.zeros(shape=(100,100,3))
ttt[:,:,0]=np.linspace(-100,100,10000).reshape([100,100])
ttt[:,:,1]=np.linspace(-100,100,10000).reshape([100,100])
ttt[:,:,2]=np.linspace(-100,100,10000).reshape([100,100])
plt.figure(figsize=[10,8])
plt.title("ttt")
plt.imshow(ttt)
'''单通道图像'''
plt.figure(figsize=[10,8])
plt.title("ttt[:,:,0]")
plt.imshow(ttt[:,:,0])
plt.show()
ttt:
[[[-100. -100. -100. ]
[ -99.979998 -99.979998 -99.979998 ]
[ -99.959996 -99.959996 -99.959996 ]
...
[ -98.05980598 -98.05980598 -98.05980598]
[ -98.03980398 -98.03980398 -98.03980398]
[ -98.01980198 -98.01980198 -98.01980198]]
[[ -97.99979998 -97.99979998 -97.99979998]
[ -97.97979798 -97.97979798 -97.97979798]
[ -97.95979598 -97.95979598 -97.95979598]
...
[ -96.05960596 -96.05960596 -96.05960596]
[ -96.03960396 -96.03960396 -96.03960396]
[ -96.01960196 -96.01960196 -96.01960196]]
[[ -95.99959996 -95.99959996 -95.99959996]
[ -95.97959796 -95.97959796 -95.97959796]
[ -95.95959596 -95.95959596 -95.95959596]
...
[ -94.05940594 -94.05940594 -94.05940594]
[ -94.03940394 -94.03940394 -94.03940394]
[ -94.01940194 -94.01940194 -94.01940194]]
...
[[ 94.01940194 94.01940194 94.01940194]
[ 94.03940394 94.03940394 94.03940394]
[ 94.05940594 94.05940594 94.05940594]
...
[ 95.95959596 95.95959596 95.95959596]
[ 95.97959796 95.97959796 95.97959796]
[ 95.99959996 95.99959996 95.99959996]]
[[ 96.01960196 96.01960196 96.01960196]
[ 96.03960396 96.03960396 96.03960396]
[ 96.05960596 96.05960596 96.05960596]
...
[ 97.95979598 97.95979598 97.95979598]
[ 97.97979798 97.97979798 97.97979798]
[ 97.99979998 97.99979998 97.99979998]]
[[ 98.01980198 98.01980198 98.01980198]
[ 98.03980398 98.03980398 98.03980398]
[ 98.05980598 98.05980598 98.05980598]
...
[ 99.959996 99.959996 99.959996 ]
[ 99.979998 99.979998 99.979998 ]
[ 100. 100. 100. ]]]
ttt[:,:,0]:
[[-100. -99.979998 -99.959996 ... -98.05980598
-98.03980398 -98.01980198]
[ -97.99979998 -97.97979798 -97.95979598 ... -96.05960596
-96.03960396 -96.01960196]
[ -95.99959996 -95.97959796 -95.95959596 ... -94.05940594
-94.03940394 -94.01940194]
...
[ 94.01940194 94.03940394 94.05940594 ... 95.95959596
95.97959796 95.99959996]
[ 96.01960196 96.03960396 96.05960596 ... 97.95979598
97.97979798 97.99979998]
[ 98.01980198 98.03980398 98.05980598 ... 99.959996
99.979998 100. ]]