簡體   English   中英

在python中讀取* .mhd / *。raw格式

[英]Reading *.mhd/*.raw format in python

任何人都可以告訴我如何在python中讀取包含.mhd / .raw文件的數據集?

最簡單的方法是使用SimpleITK (MedPy也使用ITK作為.mhd / .raw文件)。 命令

pip install SimpleITK

適用於許多python版本。 對於閱讀.mhd / .raw,您可以使用來自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

安裝SimpleITK后,使用skimage可能會更容易

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

這將為您提供z,y,x排序的numpy數組。

添加上述帖子,您可以從此處下載的CT-Scan .mhd文件開始,並使用以下代碼顯示/保存29個圖像(假設您在當前目錄中同時下載了標題和原始文件):

import SimpleITK as sitk
import matplotlib.pylab as plt
ct_scans = sitk.GetArrayFromImage(sitk.ReadImage("training_001_ct.mhd", sitk.sitkFloat32))
plt.figure(figsize=(20,16))
plt.gray()
plt.subplots_adjust(0,0,1,1,0.01,0.01)
for i in range(ct_scans.shape[0]):
    plt.subplot(5,6,i+1), plt.imshow(ct_scans[i]), plt.axis('off')
    # use plt.savefig(...) here if you want to save the images as .jpg, e.g.,
plt.show()

在此輸入圖像描述

這是使用SimpleITK和動畫讀取的相同CT掃描.mhd文件: 在此輸入圖像描述

您可以嘗試使用MedPy或此mhd_utils腳本

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM