Skip to content Skip to sidebar Skip to footer

How To Stack Multiple Numpy 2d Arrays Into One 3d Array?

Here's my code: img = imread('lena.jpg') for channel in range(3): res = filter(img[:,:,channel], filter) # todo: stack to 3d here As you can see, I'm applying some filter

Solution 1:

You could use np.dstack:

import numpy as np

image = np.random.randint(100, size=(100, 100, 3))

r, g, b = image[:, :, 0], image[:, :, 1], image[:, :, 2]

result = np.dstack((r, g, b))

print("image shape", image.shape)
print("result shape", result.shape)

Output

image shape(100, 100, 3)
result shape(100, 100, 3)

Solution 2:

I'd initialize a variable with the needed shape before:

img = imread("lena.jpg")
res = np.zeros_like(img)     # or simply np.copy(img)for channel inrange(3):
    res[:, :, channel] = filter(img[:,:,channel], filter)

Post a Comment for "How To Stack Multiple Numpy 2d Arrays Into One 3d Array?"