python - 如何加载图像并使用keras显示图像?

标签 python numpy matplotlib tensorflow keras

%matplotlib inline
from keras.preprocessing import image

import matplotlib.pyplot as plt
import numpy as np
img = np.random.rand(224,224,3)
plt.imshow(img)
plt.show()

img_path = "image.jpeg"
img = image.load_img(img_path, target_size=(224, 224))
print(type(img))

x = image.img_to_array(img)
print(type(x))
print(x.shape)
plt.imshow(x)

我有一些这样的代码可以打印图像。但它在错误的 channel 中显示图像。我在这里缺少什么?

最佳答案

这是图像缩放问题。 imshow() 的输入期望它在 0-1 范围内,而您向它传递 [0-255] 范围输入。尝试将其视为:

plt.imshow(x/255.)

关于python - 如何加载图像并使用keras显示图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44841869/

相关文章:

python - numpy.delete 将 float 组更改为字符串

python - 绘制时间戳数据帧的图表

python - 基于行的图表绘制(Seaborn 或 Matplotlib)

python - replace() 方法不适用于 Pandas DataFrame

python : How to return most occurrent value on each row depend on fix columns?

python - 尝试滚动浏览 CT 切片

python - 使用 `sklearn.svm` 回归量的定制内核存在歧义

Python + Cassandra 1.2 自动建表

python - Scapy 发送/接收的定义是什么?

python - 如何在特定范围内增加列表中的值