[英]I want to convert 32*32*3 rgb image to gray scale with 32*32*1 dimension using Python
The following is my function to convert RGB to gray scale image. 以下是我将RGB转换为灰度图像的功能。
My input image is 32*32*3
where as the output dimension looks like 32*32
, But i am looking for 32*32*1
. 我的输入图像是
32*32*3
,其中输出尺寸看起来像32*32
,但我正在寻找32*32*1
。 Do I need to resize or re scale this image. 我是否需要调整大小或重新缩放此图像。
Any thoughts? 有什么想法吗?
def rgb2gray(rgb):
return np.dot(rgb[...,:3], [0.299, 0.587, 0.114])
If you want the conversion to happen in the Tensorflow graph itself you can use this function: https://www.tensorflow.org/api_docs/python/tf/image/rgb_to_grayscale 如果您希望在Tensorflow图表中进行转换,您可以使用此功能: https ://www.tensorflow.org/api_docs/python/tf/image/rgb_to_grayscale
tf.image.rgb_to_grayscale(input_images)
Also, looks like you are answering your own question. 此外,看起来你正在回答你自己的问题。 What is wrong with
出什么问题了
def rgb2gray(rgb):
return np.dot(rgb[...,:3], [0.299, 0.587, 0.114])
Good luck! 祝好运!
Many beginners have difficulty to understand this. 许多初学者很难理解这一点。 Simply I'll explain you.
我只会解释你。 If you have 20elements.
如果你有20个元素。 You can convert them into ndarray of dimension [20] or [10,2] or [5,2,2].
您可以将它们转换为维度[20]或[10,2]或[5,2,2]的ndarray。 So in your case, there are 32*32 = 1024 pixel data as your output.
因此,在您的情况下,输出有32 * 32 = 1024像素数据。 You can reshape them back using Numpy's reshape function.
你可以使用Numpy的重塑功能重新塑造它们。 It's pretty simple.
这很简单。
For example, 例如,
print output.shape
// This prints as (32,32) in your case
output = output.reshape(32,32,1)
This solves your problem. 这解决了您的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.