简体   繁体   中英

how to slice 3d Nifti(.nii) image to 2d image with particular plane?

I am writing a script that can estimate the sharpness of a 3d Nifti(.nii) image. In my method, I need to take out its xz(axis) slice of the 3d image and then save it as .png file first.(for efficiency, just take one) Then, estimate the image's sharpness. However, I don't know how to slice a 3d image with particular plane(slice). Is there any existing code or library that can do the slicing?

I am using Python 3.7 (ran in Window 10).

I tried this library : https://pypi.org/project/nii2png/ However, it can't output a single silice.

I also tried this pages' method https://nipy.org/nibabel/coordinate_systems.html#introducing-someone However, it doesn't work. The reason maybe the Nifti file is different. I think there are two kinds of nifti format file.

import nibabel as nib
import numpy as np
import matplotlib.pyplot as plt

def show_slices(slices):
    """ Function to display row of image slices """
    fig, axes = plt.subplots(1, len(slices))
    for i, slice in enumerate(slices):
        axes[i].imshow(slice.T, cmap="gray", origin="lower")

data = nib.load('D:\\Work\\Script\\__Project\\s\\ADNI\\good_img\\MPRAGE_SENSE2\\2012-11-08_07_32_36.0\\S174286\\ADNI_002_S_5018_MR_MPRAGE_SENSE2__br_raw_20121112145413785_28_S174286_I346236.nii')
data.get_fdata()
data = data.get_fdata()
data.shape
#d_data = np.delete(data, 3, 0) #doesn't work, because it's object of type 'Nifti1Image'
#matplotlib inline
#plt.imshow(data.get_data()[:,:,50])
plt.plot(np.mean(data,axis=(0,1,2)))
plt.show()

slice_0 = data[26, :, :, :]
slice_1 = data[:, 30, :, :]
slice_2 = data[:, :, 16, :]
slice_3 = data[:, :, :, 0]
show_slices([slice_0, slice_1, slice_2, slice_3])
plt.suptitle("Center slices for EPI image") 

Then I got error:

Warning (from warnings module):
  File "D:\Program_Files_2\Python\lib\site-packages\dicom\__init__.py", line 53
    warnings.warn(msg)
UserWarning: 
This code is using an older version of pydicom, which is no longer 
maintained as of Jan 2017.  You can access the new pydicom features and API 
by installing `pydicom` from PyPI.
See 'Transitioning to pydicom 1.x' section at pydicom.readthedocs.org 
for more information.

Traceback (most recent call last):
  File "D:\Work\Script\__Project\s\try_load_nii_image_and_view_slice_3.py", line 25, in <module>
    show_slices([slice_0, slice_1, slice_2, slice_3])
  File "D:\Work\Script\__Project\s\try_load_nii_image_and_view_slice_3.py", line 9, in show_slices
    axes[i].imshow(slice.T, cmap="gray", origin="lower")
  File "D:\Program_Files_2\Python\lib\site-packages\matplotlib\__init__.py", line 1601, in inner
    return func(ax, *map(sanitize_sequence, args), **kwargs)
  File "D:\Program_Files_2\Python\lib\site-packages\matplotlib\cbook\deprecation.py", line 369, in wrapper
    return func(*args, **kwargs)
  File "D:\Program_Files_2\Python\lib\site-packages\matplotlib\cbook\deprecation.py", line 369, in wrapper
    return func(*args, **kwargs)
  File "D:\Program_Files_2\Python\lib\site-packages\matplotlib\axes\_axes.py", line 5671, in imshow
    im.set_data(X)
  File "D:\Program_Files_2\Python\lib\site-packages\matplotlib\image.py", line 690, in set_data
    .format(self._A.shape))
TypeError: Invalid shape (1, 256, 256) for image data

your mistake is in selecting the slices, note that if you are dealing with 4D image [x,y,z_Slice,time_Frame] and you want to select different slices you should keep your spatial information x and y , and select a slice, and a frame (if your image 4D and not 3D)

# slices 0 --> 3 from frame 0   

slice_0 = data[:, :, 0, 0]
slice_1 = data[:, :, 1, 0]
slice_2 = data[:, :, 2, 0]
slice_3 = data[:, :, 3, 0]

选择正确切片后的代码

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM