danchy
danchy

Reputation: 447

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])

Upvotes: 7

Views: 11400

Answers (3)

Ming
Ming

Reputation: 775

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)

Upvotes: 0

MSeifert
MSeifert

Reputation: 152667

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)

Upvotes: 10

Divakar
Divakar

Reputation: 221574

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)

Upvotes: 5

Related Questions