Visulaizing 3D images

Hi,

I have a dataset of 3D medical images with .npy extensions. I have already downloaded the dataset using
np.load(filename.py). So, each image is a 4D NumPy array (channel, height, width, depth). I am interested to visualize some images to see how the augmentations would work on them or at least a 2D slice of them that I would be sure images are downloaded properly. Since the images are grayscales the color can be ignored and the arrays can be seen as 3D. So, how can I visualize the volumes.
Any help is really appreciated.

You could visualize a single slice from the depth dimension e.g. using matplotlib and indexing:

c, h, w, d
x = torch.randn(c, h, w, d)
plt.imshow(x[0, :, :, 0])

or you could also use torchvision.utils.make_grid to create a single image with all slices:

from torchvision.utils import make_grid

# permute to treat depth as "batch dim"
x_grid = make_grid(x.permute(1, 0, 2, 3)) 
# permute to create channels-last array (for matplotlib)
x_grid = x_grid.permute(1, 2, 0).numpy()
plt.imshow(x_grid)

Also, have a look at @MONAI’s plot_2d_or_3d_image, which can apparently plot these 3D images as GIFs into TensorBoard.

1 Like

Thank you so much @ptrblck.