Converting RGB to Grayscale manually tensorflow

二次信任 提交于 2019-12-24 00:59:26

问题


I wanted to convert an RGB image to grayscale manually without library usage in tensorflow. So I wrote the following...

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()

I extracted the first channel of the image for the conversion. But this generates error while using imread saying

TypeError: Invalid dimensions for image data

What should I do?


回答1:


From the doc of plt.imshow(X):

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

Here you have an input of shape [None, None, 1]. You just need to remove the last dimension like this:

result = np.squeeze(result, 2)


来源:https://stackoverflow.com/questions/37860136/converting-rgb-to-grayscale-manually-tensorflow

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!