2016-05-18 68 views

回答

5

最簡單的方法是使用SimpleITK(MedPy使用ITK的.mhd/.RAW文件也是如此)。命令

pip install SimpleITK 

適用於許多python版本。對於閱讀.mhd/.RAW您可以使用此代碼from kaggle

import SimpleITK as sitk 
import numpy as np 
''' 
This funciton reads a '.mhd' file using SimpleITK and return the image array, origin and spacing of the image. 
''' 

def load_itk(filename): 
    # Reads the image using SimpleITK 
    itkimage = sitk.ReadImage(filename) 

    # Convert the image to a numpy array first and then shuffle the dimensions to get axis in the order z,y,x 
    ct_scan = sitk.GetArrayFromImage(itkimage) 

    # Read the origin of the ct_scan, will be used to convert the coordinates from world to voxel and vice versa. 
    origin = np.array(list(reversed(itkimage.GetOrigin()))) 

    # Read the spacing along each dimension 
    spacing = np.array(list(reversed(itkimage.GetSpacing()))) 

    return ct_scan, origin, spacing 
+0

順便說一句,[this](http://stackoverflow.com/questions/37989196/how-do-i-open-mhd-file-corresponding-to-a-raw-file-in-blender-3d-tool )問題是一樣的。有人可以合併它們嗎? – savfod

3

使用skimage安裝SimpleITK

import skimage.io as io 
img = io.imread('file.mhd', plugin='simpleitk') 

後,這會給你與Z A numpy的陣列可能會更容易,Y, x排序。