Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

copy/reshape high dimensional numpy array

Tags:

python

numpy

I have a numpy array with a shape like this

x.shape
(100, 1, 300, 300)

Think of this as 100 observations of grayscale images of size 300x300. Grayscale images have only 1 channel, hence the second 1 in the shape.

I want to convert this to an array of RGB images, with 3 channels. I want to just copy the grayscale image to the two other channels.

So the final shape would be (100, 3, 300, 300)

How can I do that?

like image 926
spore234 Avatar asked Feb 17 '26 21:02

spore234


1 Answers

Use np.repeat -

np.repeat(x,3,axis=1)

Sample run -

In [8]: x = np.random.randint(11,99,(2,1,3,4))

In [9]: np.repeat(x,3,axis=1).shape
Out[9]: (2, 3, 3, 4)
like image 131
Divakar Avatar answered Feb 19 '26 10:02

Divakar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!