Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to convert black and white image to array with 3 dimensions in python?

I have image in either RGB format or grayscale format (I converted it through Gimp, let's say), now everytime I load the image in grayscale, or just transform it to grayscale format, the shape always says [height, width] without the third dimension (number of color channels).

I know that usually b/w images are stored in such format, but I specifically need the [height, width, 1] image shape, the one you would get with, let's say:

numpy.zeros(shape=[400, 400, 1])
like image 382
danchy Avatar asked May 08 '17 17:05

danchy


People also ask

Can a grayscale image have 3 channels?

A grayscale image has just one channel.

How RGB and grayscale images are represented in NumPy arrays?

A single RGB image can be represented using a three-dimensional (3D) NumPy array or a tensor. Since there are three color channels in the RGB image, we need an extra dimension for the color channel. A batch of 3 RGB images can be represented using a four-dimensional (4D) NumPy array or a tensor.


1 Answers

You can always add "empty" dimensions using np.expand_dims:

>>> a2d = np.ones((100, 200))
>>> a3d = np.expand_dims(a2d, axis=2)
>>> a3d.shape
(100, 200, 1)

or by slicing with None or np.newaxis:

>>> a2d[..., None].shape  # instead of "..." (Ellipsis) you could also use `[:, :, None]`
(100, 200, 1)

I prefer np.expand_dims because it's a bit more explicit about what happens than slicing.


If you need it conditionally, check for arr.ndim first:

if arr.ndim == 2:
    arr = np.expand_dims(arr, axis=2)
like image 96
MSeifert Avatar answered Sep 19 '22 22:09

MSeifert