本文介紹了在 python 中讀取 *.mhd/*.raw 格式的處理方法,對大家解決問題具有一定的參考價值,需要的朋友們下面隨著小編來一起學習吧!
問題描述
誰能告訴我如何在 python 中讀取包含 .mhd/.raw 文件的數據集?
Can anyone please tell me the way I can read a dataset containing .mhd/.raw files in python?
推薦答案
最簡單的方法是使用 SimpleITK (MedPy 也將 ITK 用于 .mhd/.raw 文件).命令
The easiest way is to use SimpleITK (MedPy uses ITK for .mhd/.raw files too). Command
pip install SimpleITK
適用于許多 python 版本.要閱讀 .mhd/.raw,您可以使用此代碼 來自 kaggle
works for many python versions. For reading .mhd/.raw you can use this code 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
這篇關于在 python 中讀取 *.mhd/*.raw 格式的文章就介紹到這了,希望我們推薦的答案對大家有所幫助,也希望大家多多支持html5模板網!
【網站聲明】本站部分內容來源于互聯網,旨在幫助大家更快的解決問題,如果有圖片或者內容侵犯了您的權益,請聯系我們刪除處理,感謝您的支持!