【问题标题】:Image transformation with Keras and Numpy使用 Keras 和 Numpy 进行图像转换
【发布时间】:2017-08-06 11:46:51
【问题描述】:

我想使用 Keras 为网络提供图像。我正在从互联网下载图像并将它们存储到一个 numpy 数组中。当我绘制单个图像时,它会正确显示。 作为下一步,我将创建一个新的 numpy 数组,在其中存储单个图像。但是,在该步骤中,图像仅显示为黑色图像。我想知道为什么会这样?

这是我的代码:

import numpy as np
import urllib.request
import cv2
from matplotlib import pyplot as plt
from keras import backend as K
%matplotlib inline

file = "http://farm2.static.flickr.com/1353/1230897342_2bd7c7569f.jpg"

# Read web file
images_or = np.ndarray((1,128, 128,3), dtype=np.uint8)
req = urllib.request.urlopen(file)
arr = np.asarray(bytearray(req.read()), dtype=np.uint8)
img = cv2.imdecode(arr,-1) # 'load it as it is'
images_or[0] = cv2.resize(img,(128,128))  

# Display image
plt.imshow(images_or[0])
plt.show()

# Format image
images_or = images_or.astype(K.floatx())
images_or *= 0.96/255
images_or += 0.02

# Display image
plt.imshow(images_or[0])
plt.show()

# Reshape image
images_or = images_or.reshape(images_or.shape[0], 3, 128, 128)

# Copy image in another np.array
A_train_test = np.ndarray((1, 3, 128, 128), dtype=np.uint8)
A_train_test[0] = images_or[0]  

# Format image
A_train_test = A_train_test.astype(K.floatx())
A_train_test *= 0.96/255
A_train_test += 0.02

# Reshape image
A_train_test = A_train_test.reshape(A_train_test.shape[0], 128, 128, 3)
image_xxx = A_train_test[0]

plt.imshow(image_xxx)
plt.show()

非常感谢您, 安迪

【问题讨论】:

    标签: python numpy keras


    【解决方案1】:

    我需要更改命令的顺序:

    # Copy image in another np.array
    A_train_test = np.ndarray((1, 3, 128, 128), dtype=np.uint8)
    A_train_test = A_train_test.astype(K.floatx())
    A_train_test[0][:] = images_or[0][:]  
    

    问题解决了。

    【讨论】:

      猜你喜欢
      • 2018-02-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-26
      • 1970-01-01
      • 2016-11-14
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多