【问题标题】:Converting RGB to Grayscale manually tensorflow手动将RGB转换为灰度
【发布时间】:2016-10-18 00:38:21
【问题描述】:

我想手动将 RGB 图像转换为灰度图像,而无需在 tensorflow 中使用库。所以我写了以下...

import tensorflow as tf
import matplotlib.image as mpimg
import matplotlib.pyplot as plt

# First, load the image again
filename = "MarshOrchid.jpg"
raw_image_data = mpimg.imread(filename)

image = tf.placeholder("float", [None, None, 3])
slice = tf.slice(image,[0,0,0],[-1,-1,1])

with tf.Session() as session:
    result = session.run(slice, feed_dict={image: raw_image_data})
    plt.imshow(result)
    plt.show()

我提取了图像的第一个通道进行转换。 但这会在使用 imread 语句时产生错误

TypeError: Invalid dimensions for image data

我该怎么办?

【问题讨论】:

  • 哪一行报错?
  • plt.imshow(result)
  • Shubham,你找到解决办法了吗?我在使用 imshow 显示图像时遇到了类似的问题
  • @Fiona 解决方案中的 np.squeeze() 帮助了我。

标签: python numpy rgb tensorflow grayscale


【解决方案1】:

来自plt.imshow(X)的doc

X : array_like, shape (n, m) or (n, m, 3) or (n, m, 4)

这里有一个形状为 [None, None, 1] 的输入。您只需要像这样删除最后一个维度:

result = np.squeeze(result, 2)

【讨论】:

  • 我的灰度转换方法是正确的(消除 2 个通道)还是应该对 3 个通道进行平均
  • 平均!好多了
猜你喜欢
  • 2021-04-02
  • 2011-12-03
  • 2020-04-29
  • 1970-01-01
  • 2010-10-15
  • 2013-09-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多