[Neuroimaging] Slicing images on different planes

Buist, Carl R. cbuist at ou.edu
Fri Mar 19 15:30:21 EDT 2021


Hello all,

I would like to preference this question by saying that I am new to the world of python in general and apologize if this is a silly question and if my code is suboptimal. I am also not using medical data, but microCT scanned images of fossils, but I think that the principles for slicing the images are pretty similar.

I have recently written a very basic code to slice a NIfTI (.nii) file along the major axes or some variation of those axes. Now I would like to slice the file along a plane that is not along one of these major axes, for example maybe a plane that is rotated 45 degrees off the x axis in any direction or something like that. My initial thought was to define a plane and use that in place of one of the planes I am currently displaying but am running into trouble.

I tried to look through the archive to see if this was a problem for others but didn't see anything. I would really appreciate any help or if someone could point me in the correct direction. Below is the simple code that I made and attached is an image of the output.

Thank you so much for your help,
Carl

def display_views(file_path):
    image_axis = 2
    medical_image = nib.load(file_path)
    image = medical_image.get_fdata()
    image = np.squeeze(image)
    print(image.shape)

    # Infer dimensions on each Cartesian axis
    x_dim = len(image[:,0,0])
    y_dim = len(image[0,:,0])
    z_dim = len(image[0,0,:])

    # Midpoint layers, as integers
    x_midpoint = int(x_dim/2)
    y_midpoint = int(y_dim/2)
    z_midpoint = int(z_dim/2)

    sagital_image = image[x_midpoint, :, :]
    coronal_image = image[:, y_midpoint, :]
    axial_image = image[:, :, z_midpoint]
    axial_imagep10 = image[:, :, int(z_midpoint + 100)]

    plt.figure(figsize=(20, 10))
    plt.style.use('grayscale')

    plt.subplot(141)
    plt.imshow(np.rot90(sagital_image))
    plt.title('Sagital Plane')
    plt.axis('off')

    plt.subplot(142)
    plt.imshow(np.rot90(coronal_image))
    plt.title('Coronal Plane')
    plt.axis('off')

    plt.subplot(143)
    plt.imshow(np.rot90(axial_image))
    plt.title('Axial Plane')
    plt.axis('off')

    plt.subplot(144)
    plt.imshow(np.rot90(axial_imagep10))
    plt.title('Axial Plane moved')
    plt.axis('off')

    plt.show()

#fpath = sys.argv[1]
fpath = '/content/drive/MyDrive/3D_Forams/Foram_NH1108sit_gdr_7_1__IR_rec480-1180_testcrop.nii'

display_views(fpath)

-------------- next part --------------
An HTML attachment was scrubbed...
URL: <https://mail.python.org/pipermail/neuroimaging/attachments/20210319/04ab8fb6/attachment-0001.html>
-------------- next part --------------
A non-text attachment was scrubbed...
Name: Fossil_Images.png
Type: image/png
Size: 408563 bytes
Desc: Fossil_Images.png
URL: <https://mail.python.org/pipermail/neuroimaging/attachments/20210319/04ab8fb6/attachment-0001.png>


More information about the Neuroimaging mailing list