Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

numpy: unpack array along an axis

Tags:

numpy

Say that I have a RGB image:

from skimage import data
img = data.astronaut()
print(img.shape)  # (512, 512, 3)

Is there a succinct numpy command to unpack it along the color channels:

R, G, B = np.unpack(img, 2)  # ?

What I am doing is using comprehension:

R, G, B = (img[:, :, i] for i in range(3))

But is there no simpler command?

like image 876
Ricardo Magalhães Cruz Avatar asked Nov 01 '25 04:11

Ricardo Magalhães Cruz


1 Answers

Alternatively you can use np.rollaxis -

R,G,B = np.rollaxis(img,2)
like image 186
Divakar Avatar answered Nov 02 '25 23:11

Divakar