Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

My input image is 32*32*3 where as the output dimension looks like 32*32, But i am looking for 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])
like image 643
Viswa Anoop Nerella Avatar asked Dec 13 '25 13:12

Viswa Anoop Nerella


1 Answers

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

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!

like image 129
rmeertens Avatar answered Dec 15 '25 14:12

rmeertens