这里可能有两个问题:
问题 1:
看起来您的颜色通道(红色、绿色、蓝色)是混合的。这可以解释为什么颜色如此奇怪。如果是这种情况,您将需要交换阵列中的颜色通道,如下所示。
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.cbook import get_sample_data
rgb_image = plt.imread(get_sample_data("grace_hopper.png", asfileobj=False))
# correct color channels (R, G, B)
plt.figure()
plt.imshow(rgb_image)
plt.axis('off')
# swapped color channels (R, B, G)
rgb_image = rgb_image[:, :, [0, 2, 1]]
plt.figure()
plt.imshow(rgb_image)
plt.axis('off')
问题 2:
Matplotlib 的 plt.imshow 有一个关键字参数 interpolation,如果未指定,则默认为 None。 Matplotlib 然后参考您的本地样式表来确定默认的插值行为。根据您的样式表,这可能会导致应用插值,从而导致图像失真。请参阅documentation for imshow for more details。
如果你想保证 Matplotlib 不会对你的图像进行插值,你应该在 plt.imshow 中指定 interpolation="none"。这是令人困惑的,因为None 的默认 NoneType 值与"none" 的字符串值产生不同的行为。
red = np.zeros((100, 100, 3), dtype=np.uint8)
red[:, :, 0] = 255
red[40:60, 40:60, :] = 255
# with interpolation
plt.figure()
plt.imshow(red, interpolation='bicubic')
plt.axis('off')
# without interpolation
plt.figure()
plt.imshow(red, interpolation='none')
plt.axis('off')