Reputation: 3593
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?
Upvotes: 4
Views: 2318
Reputation: 5729
Alternatively, you can use np.split
:
R, G, B = np.split(img, img.shape[-1], axis=-1)
If your array is of shape (height, width, channel), you can use np.dsplit
to split along the depth dimension:
R, G, B = np.dsplit(img, img.shape[-1])
Upvotes: 1
Reputation: 221584
Alternatively you can use np.rollaxis
-
R,G,B = np.rollaxis(img,2)
Upvotes: 4
Reputation: 131600
You can transpose the length-3 dimension to the front and then unpack it:
R, G, B = img.transpose((2, 0, 1))
Upvotes: 1