Matplotlib offsetbox无法显示灰度图像

时间:2018-08-08 12:23:30

标签: python matplotlib scikit-image

正如标题所述,我试图在matplotlibs注解bbox中显示8位灰度的.bmp图像,但由于某种原因,结果是不希望的。我还使用skimage的函数resize调整了图像的大小,这也会影响结果。结果失败的原因是对带注释的图像进行了归一化处理,以使最暗的像素变为黑色,最亮的像素变为白色。

这是示例代码,原始图像和绘图结果。

import matplotlib.pyplot as plt
from matplotlib import offsetbox
import numpy as np
from PIL import Image
from skimage.transform import resize

image = np.array(Image.open('testimage.bmp'))

#Black color is converted to white color
for i in range(image.shape[0]):
    for j in range(image.shape[1]):
        image[i][j] = 255 - image[i][j]

fig, ax = plt.subplots(figsize=(300, 200),dpi=40)
shown_images = np.array([[1., 1.]])  # just something big
imagebox = offsetbox.AnnotationBbox(
        offsetbox.OffsetImage(resize(image, (540,940), order=1, preserve_range=True),cmap="Greys"),[0.5,0.5])
ax.add_artist(imagebox)
plt.xticks([]), plt.yticks([])
plt.show()

原始图片 enter image description here

带注释的图片

enter image description here

希望我只是缺少一些明显的东西...

1 个答案:

答案 0 :(得分:1)

您希望将图像的标准化设置为您期望的范围,在这种情况下,可能是0255

offsetbox.OffsetImage(image, norm=plt.Normalize(0,255), cmap="Greys")