SimpleITK 和 Nibabel 的区别:

SimpleITK 加载数据是channel_first,即(155,240,240);

Nibabel 是 channel_last,即(240,240,155),其中155是图像通道数,也就是155张图像,可以把nii看成二维图像,也可以看成三维。

import SimpleITK as sitk
from matplotlib import pyplot as plt

def showNii(img):
    for i in range(img.shape[1]):
        plt.imshow(img[i,:,:], cmap="gray")
        plt.show()
# itk_img = sitk.ReadImage('./Brats18_2013_2_1_flair.nii.gz')
# img = sitk.GetArrayFromImage(itk_img)
# print(img.shape)    # (155, 240, 240) 155表示个数,240,240表示图片长度和宽度
# showNii(img)

itk_img = sitk.ReadImage('./id001-128x128x64.nii.gz')
img = sitk.GetArrayFromImage(itk_img)
showNii(img)

3D Plot:

import scipy.ndimage
import matplotlib.pyplot as plt
import numpy as np
from skimage import measure, morphology
from mpl_toolkits.mplot3d.art3d import Poly3DCollection


def plot_3d(image, threshold=-300):
    
    # Position the scan upright, 
    # so the head of the patient would be at the top facing the camera
    image = image.astype(np.int16)
    p = image.transpose(2,1,0)
#     p = p[:,:,::-1]
    
    print(p.shape)
    verts, faces, _, x = measure.marching_cubes_lewiner(p, threshold) #marching_cubes_classic measure.marching_cubes

    fig = plt.figure(figsize=(10, 10))
    ax = fig.add_subplot(111, projection='3d')

    # Fancy indexing: `verts[faces]` to generate a collection of triangles
    mesh = Poly3DCollection(verts[faces], alpha=0.1)
    face_color = [0.5, 0.5, 1]
    mesh.set_facecolor(face_color)
    ax.add_collection3d(mesh)

    ax.set_xlim(0, p.shape[0])
    ax.set_ylim(0, p.shape[1])
    ax.set_zlim(0, p.shape[2])

    plt.show()
plot_3d(img, 100)

 

参考文献:

1. SimpleITK

posted on 2020-05-07 00:08  Google-boy  阅读(508)  评论(0编辑  收藏  举报