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

3

7

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])
Auspicate answered 8/5, 2017 at 17:12 Comment(2)
Did either of the posted solution work for you?Moneymaking
Yes, both solutions worked, I forgot to mark one of them as accpted, thanks for reminder!Auspicate
O
10

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)
Overgrow answered 8/5, 2017 at 17:24 Comment(0)
M
5

There's a built-in np.atleast_3d exactly for this purpose -

np.atleast_3d(img)

This built-in takes care of keeping the output shape to be 3D by appending one new axis as the last one for a 2D array and makes no change for a 3D input, all being taken care of under the hoods.

Sample run -

In [42]: img = np.random.randint(0,255,(800,600)) # grayscale img

In [43]: np.atleast_3d(img).shape
Out[43]: (800, 600, 1)

In [44]: img = np.random.randint(0,255,(800,600,3)) # RGB img

In [45]: np.atleast_3d(img).shape
Out[45]: (800, 600, 3)
Moneymaking answered 8/5, 2017 at 17:25 Comment(3)
I only tried np.array(img, ndmin=3, copy=False) but that just prepends dimensions. Good to know they have another function for appending dimensions. :)Overgrow
Another thing "that cares of it under the hoods" sounds wrong. Did you mean "that takes care of it ..."?Overgrow
@Overgrow Was running short of words there. Edited to make more sense.Moneymaking
S
0

I used np.reshape() to add another dimension into grayscale image

grayscale = cv2.cvtColor(raw_data, cv2.COLOR_BGR2GRAY)
print(grayscale.shape) # prints (800,600)

grayscale = grayscale.reshape(grayscale.shape[0], grayscale.shape[1], 1)
print(grayscale.shape) # prints (800,600,1)
Squid answered 29/10, 2021 at 11:59 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.