以下代码可视化图像,以灰度显示每个像素的值。
from keras.datasets import mnist
(X_train, y_train), (X_test, y_test) = mnist.load_data()
def visualize_image(img):
fig, ax = plt.subplots(figsize=(12, 12))
ax.imshow(img, cmap='gray')
thresh = img.max()/2.5
width, height = img.shape
for x in range(width):
for y in range(height):
ax.annotate(str(round(img[x][y])), xy=(y, x),
horizontalalignment='center',
verticalalignment='center,',
color='white' if img[x][y]<thresh else 'black')
visualize_image(X_train[0])
我感到困惑的是它将(y, x)
而不是(x, y)
传递给ax.annotate
的原因?我知道传递(x, y)
作为参数会导致错误的图像,但为什么呢?该文件说
xy:iterable
长度2序列指定要注释的(x,y)点
答案 0 :(得分:1)
注释功能正常,文档也正确,xy = (x,y)
表示x
是水平组件,y
是垂直组件。这与全世界通常的绘图和图表命名一致。
此处,问题来自于您指定图像width
的高度和图像height
的宽度。这令人困惑。但当然,用户有责任跟踪变量名称。因此,如果您将宽度height
和y
命名为通常的x
坐标,则需要调用xy = (y,x)
。
我仍然建议坚持通常的命名约定,因此
def visualize_image(img):
fig, ax = plt.subplots(figsize=(12, 12))
ax.imshow(img, cmap='gray')
thresh = img.max()/2.5
height, width = img.shape
for x in range(width):
for y in range(height):
ax.annotate(str(round(img[y][x])), xy=(x, y),
horizontalalignment='center',
verticalalignment='center,',
color='white' if img[y][x]<thresh else 'black')