0

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.

8
  • Your mask is smaller than the original data, and the ratios between sides are different. Please, explain what do you mean by 'extend'. Is a rescaling or something else? Commented Feb 4, 2019 at 14:18
  • By extend I mean that in reshaping the mask file to match the shape of my original data. So that overlaying of mask file on original data becomes easier. Sorry If i mixed up keywords as newbie. I dnt know what do you mean by rescale though. Commented Feb 4, 2019 at 14:22
  • @Valentino, My be i should more clarify. the mask is a nfiti file. and my original data is mri images. the nifti file has bounding box coordinates of some tumours.. Commented Feb 4, 2019 at 14:26
  • Sorry, but I still don't understand. If you call it "mask" I expect that each pixel of the mask can be opaque or transparent. So you have an image of the mask made by the transparent pixels. Do you want just to add opaque pixels around the image mask to fit the bigger shape, or do you want to stretch the mask image to fit the bigger shape? The former is much more easier to do than the latter. Commented Feb 4, 2019 at 14:42
  • Perhaps you are looking for something like scipy.ndimage.zoom(mask3d, (512/200, 512/130, 485/131))? Commented Feb 4, 2019 at 15:03

1 Answer 1

1

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.

Sign up to request clarification or add additional context in comments.

2 Comments

Thanks a lot for your approach. I think your idea resonates what I am trying to do. I will try and update what happened. Also i think i should invest some time before I ask question. As it seems people are not understanding.
It seems best way to extend this task is Interpolation + resampling. As in interpolation there is a chance to add extra values in mask image. Nearest neighbor interpolation is safe , i think. What i did is used Simple ITK , ResampleImageFilter(), to do that. Although , I am not sure if I am doing it correctly. In different case may be interpolation would not be necessary.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.