Use Numpy to convert rgb pixel array to grayscale

What is the best way to use Numpy to convert an rgb size array of size (x, y, 3) to an array of sizes (x, y, 1) of grayscale values?

I have a function rgbToGrey (rgbArray) that can take an array [r, g, b] and return a gray scale value. I would like to use it together with Numpy to compress the 3rd dimension of my array from size 3 to size 1.

How can i do this?

Note. It would be very easy if I had the original image, and I could use Pillow in grayscale at first, but I don’t have it.

UPDATE:

The function I was looking for was np.dot().

From the answer to this question :

Assuming we convert rgb to gray scale through the formula:

.3r * .6g * .1b = gray,

we can do np.dot(rgb[...,:3], [.3, .6, .1])to get what I'm looking for, a 2d array of gray-only values.

+4
source share
1 answer

See answers in another thread. How do I convert an RGB image to grayscale in Python? `Essentially gray = 0.2989 * r + 0.5870 * g + 0.1140 * b np.dot (rgb [...,: 3], [0.299, 0.587, 0.114])

+4
source

Source: https://habr.com/ru/post/1668477/


All Articles