Python Matplotlib offsetbox无法显示灰度图像
正如标题所述,我试图在matplotlibs annotationbbox中显示8位灰度.bmp图像,但由于某些原因,结果并不理想。我还使用skimage的resize函数调整了图像的大小,该函数也会影响结果。失败结果的原因是注释图像被规格化,以便最暗的像素得到黑色,最亮的像素得到白色 下面是示例代码、原始图像和绘图结果Python Matplotlib offsetbox无法显示灰度图像,python,matplotlib,scikit-image,Python,Matplotlib,Scikit Image,正如标题所述,我试图在matplotlibs annotationbbox中显示8位灰度.bmp图像,但由于某些原因,结果并不理想。我还使用skimage的resize函数调整了图像的大小,该函数也会影响结果。失败结果的原因是注释图像被规格化,以便最暗的像素得到黑色,最亮的像素得到白色 下面是示例代码、原始图像和绘图结果 import matplotlib.pyplot as plt from matplotlib import offsetbox import numpy as np from
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()
原始图像
注释图像
希望我只是遗漏了一些明显的东西…您可能希望将图像的规格化设置为您期望的范围,在本例中可能是0到255
offsetbox.OffsetImage(image, norm=plt.Normalize(0,255), cmap="Greys")