【问题标题】:Image blurs after assignment分配后图像模糊
【发布时间】:2017-06-09 06:09:09
【问题描述】:

我刚开始学习 Tensorflow 和 Numpy 概念。我正在使用 Tensorflow 将不同形状的图像重塑为我正在使用循环的一个固定形状。在循环结束时,我将这些重新整形的图像累积到一个数组中。现在,如果我从这个数组中绘制图像,我会得到模糊的图像。但是,如果我使用 Tensorflow 绘制重塑图像的实例,我会得到正确的图像。请问有人能解释一下我哪里出了问题吗?

代码:

fixedW = 227.0
fixedH = 227.0
X_data = np.zeros((3, fixedW, fixedH, 3), dtype = np.float32)  # Only 3 images in this example
tf.reset_default_graph()
with tf.Session() as sess:
    sess.run(tf.global_variables_initializer())
    for i in range(3):
        img = matplotlib.image.imread(image_file_name[i])
        preshape = img.shape
        img = np.reshape(img, (1, preshape[0], preshape[1], preshape[2]))  #Make it single batched image
        tf_img = tf.image.resize_images(img, (fixedW, fixedH), tf.image.ResizeMethod.NEAREST_NEIGHBOR)
        resized_img = sess.run(tf_img)[0]
        print(resized_img.shape)  # Prints correctly
        X_data[i, :, :, :] = resized_img[:, :, :]  # Something is wrong here

# This plots correctly
plt.imshow(resized_img)
plt.show()

# This plots some blurred image
plt.imshow(X_data[2])
plt.show()

请任何人解释我在这方面哪里出了问题,以及在我对这项任务的理解中我在这里遗漏了什么概念。

【问题讨论】:

    标签: python numpy matplotlib tensorflow


    【解决方案1】:

    我已经找到了解决方案。问题出在 X_data 类型上。函数imshow 只接受 uint8 或 float32 类型的值,而且这些值必须在 0.0 到 1.0 的范围内。

    我的变量 X_data 接受 float32 类型,但值大于 1。因此将 X_data 类型转换为 uint8 解决了问题。

    解决办法如下:

    X_data = np.zeros((3, fixedW, fixedH, 3), dtype = np.uint8)
    

    【讨论】:

      【解决方案2】:

      如果将数据转换为uint8 可以解决问题,以下方法也可以解决问题(无需将数组声明为uint8):

      plt.imshow(X_data[2], vmin=0, vmax=255)
      

      从您的问题来看,我确实了解您的浮动图像实际上在[0, 255] 的范围内(这就是转换为 uint 的最终结果)。

      那么真正的问题似乎是matplotlib的imshow自动将图像的vminvmax调整为X_data[2].min()X_data[2].max(),如果没有提供的话。

      如果您提供 vmin=0vmax=255,则绘图应该与将数据四舍五入为无符号字节相同(并且可能更充分)。

      但是,情况可能并非如此,因为代码无法复制,因此我无法对其进行测试:P.

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-22
        • 2012-12-09
        • 2019-02-21
        • 2017-12-21
        • 2012-12-12
        相关资源
        最近更新 更多