I'm trying to mask a 3D array (RGB image) with numpy.
However, my current approach is reshaping the masked array (output below). I have tried to follow the approach described on the SciKit-Image crash course. Crash Course
I have looked in the Stackoverflow and a similar question has been asked, but with no accepted answer (similar question here)
What is the best way to accomplish masking like this?
Here is my attempt:
# create some random numbers to fill array
tmp = np.random.random((10, 10))
# create a 3D array to be masked
a = np.dstack((tmp, tmp, tmp))
# create a boolean mask of zeros
mask = np.zeros_like(a, bool)
# set a few values in the mask to true
mask[1:5,0,0] = 1
mask[1:5,0,1] = 1
# Try to mask the original array
masked_array = a[:,:,:][mask == 1]
# Check that masked array is still 3D for plotting with imshow
print(a.shape)
(10, 10, 3)
print(mask.shape)
(10, 10, 3)
print(masked_array.shape)
(8,)
# plot original array and masked array, for comparison
plt.imshow(a)
plt.imshow(masked_array)
plt.show()