Reputation: 71
I have a 3D array of shape 200, 130, 131 (x,y,z). Which is basically mask file. lets say it is called "maskCoords". I want to extend this to my original data shape of 512*512*485 (x,y,z). But keeping the mask unaltered and filling rest of the indices as zeros.
So, I created a empty 3D array like:
mask3d = np.zeros_like(MainData3d)
But Now I am unable to understand how to fill this mask3d array with the values saved in my masked file. I tried to do like
mask3d[maskCoords]=1
But this does not work.When I overlap Maindata3d and mask3d masked areas are not visible. Any help or idea would be appreciated.
Upvotes: 0
Views: 212
Reputation: 1890
I'm assuming your maskCoords are 3d coordinates into your 3d array with my answer since like the comments say in your question it's hard to tell exactly what you mean.
Here is the approach.
mask3d = np.zeros((512, 512, 485))
# Create random coordinates
maskCoords = (np.random.random((200, 130, 131)) * 400).astype('int')
# Convert the coordinates to be converted by numpy.ravel_multi_index
maskCoords2 = np.hstack((maskCoords[:, :, 0].flatten()[:, None], maskCoords[:, :, 1].flatten()[:, None], maskCoords[:, :, 2].flatten()[:, None])).T
# Convert the coordinates into indices so we can access the locations in the original array
inds = np.ravel_multi_index(maskCoords2, [512, 512, 485])
# Set the array values at the locations of the indices to 1
mask3d.flat[inds] = 1
Maybe not be what you're looking for but I'm trying something anyway.
Upvotes: 1