简体   繁体   English

我想使用Python将32 * 32 * 3 rgb图像转换为32 * 32 * 1维度的灰度

[英]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.

相关问题 python从3个float32 numpy数组中生成RGB图像 - python make RGB image from 3 float32 numpy arrays 如何显示尺寸为 [10000, 32, 32, 3] 的 numpy 数组中的图像? - How do I display an image from numpy array of dimension [10000, 32, 32, 3]? Python:将2个int转换为32个float - Python: convert 2 ints to 32 float 使用 Python。 如何将 4 维数组 (21,32,1024,1024) 保存为 tif 图像。 生物医学 - Using Python. How to save a 4 dimension array (21,32,1024,1024) as a tif image. biomedicine 如何将图像从(32,32,3)转换为(1,3072)? - How to convert image from (32,32,3) to (1,3072)? 如何在python中将数组str 32转换为数组int 32 - How to convert array str 32 to array int 32 in python 如何在 Python 中将任何图像转换为 CV_32FC1 - How to convert any image to CV_32FC1 in Python 将3x32x32大小的numpy图像数组转换为32x32x3并保存为Python中的实际图像 - Converting 3x32x32 size numpy image array to 32x32x3 and saving to actual image in Python 想要使用Python Wincom32从工作簿中读取工作表 - Want to read Worksheets from a workbook using Python Wincom32 将数据集的维度从 (32,32,3,10000) 更改为 (10000,32,32,3) 以训练 CNN - Change the dimension of data set from (32,32,3,10000) to (10000,32,32,3) for training the CNN
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM