简体   繁体   English

如何将 3d Nifti(.nii) 图像切片为具有特定平面的 2d 图像?

[英]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.我正在编写一个脚本,可以估计 3d Nifti(.nii) 图像的清晰度。 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.在我的方法中,我需要取出 3d 图像的 xz(axis) 切片,然后将其保存为 .png 文件。(为了效率,只取一个)然后,估计图像的清晰度。 However, I don't know how to slice a 3d image with particular plane(slice).但是,我不知道如何使用特定平面(切片)对 3d 图像进行切片。 Is there any existing code or library that can do the slicing?是否有任何现有的代码或库可以进行切片?

I am using Python 3.7 (ran in Window 10).我正在使用 Python 3.7(在 Window 10 中运行)。

I tried this library : https://pypi.org/project/nii2png/ However, it can't output a single silice.我试过这个库: https : //pypi.org/project/nii2png/但是,它不能输出一个硅片。

I also tried this pages' method https://nipy.org/nibabel/coordinate_systems.html#introducing-someone However, it doesn't work.我也试过这个页面的方法https://nipy.org/nibabel/coordinate_systems.html#introducing-someone但是,它不起作用。 The reason maybe the Nifti file is different.原因可能是 Nifti 文件不同。 I think there are two kinds of nifti format file.我认为有两种 nifti 格式的文件。

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)您的错误在于选择切片,请注意,如果您正在处理4D图像[x,y,z_Slice,time_Frame]并且您想选择不同的切片,您应该保留您的空间信息x 和 y ,并选择一个切片和一个框架(如果您的图像是 4D 而不是 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]

选择正确切片后的代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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