本文细说一下医学图像中常见的文件格式之间的转换。
您应该能够使用 SimpleITK 来完成。你会做这样的事情:
import SimpleITK as sitk
img = sitk.ReadImage("input.mhd")
sitk.WriteImage(img, "output.nii")
如果python中没有SimpleITK,安装如下:
pip install SimpleITK
SimpleITK 尽最大努力保留所有标头信息,尽管它并不完美。体素尺寸将被保留。
使用voxelfuse和nibabel来完成,你需要这样做:
from voxelfuse.voxel_model import VoxelModel
from voxelfuse.mesh import Mesh
from voxelfuse.primitives import generateMaterials
import nibabel
# Load nifti mask and convert to numpy
mask_dir = "Synthetic-04.nii" # Put the directory of the mask file here
mask_nifti = nibabel.load(mask_dir)
mask_npy = mask_nifti.get_fdata()
# Convert all nonzero labels (i.e. lesion labels) to 1
mask_npy[mask_npy != 0] = 1
# Convert to mesh and save
model = VoxelModel(mask_npy, generateMaterials(1))
mesh = Mesh.fromVoxelModel(model)
mesh.export("DSynthetic-04.stl")
简单的python代码,用于根据标签将一个分段的nii.gz文件转换为stl文件列表中。
分段的nii.gz文件包含一系列标签,例如1、2、4、5。然后代码可以使用simpleITK和vtk将这个单独的nii.gz文件转换成一系列的.stl文件来创建网格。
此代码可用于许多医学成像情况,作为处理客户端读取的输出的最后一步。标签不需要是连续的,但需要是数字(整数、浮点数…)
import vtk
import glob
import SimpleITK as sitk
import numpy as np
if __name__ == '__main__':
# can be done in a loop if you have multiple files to be processed, speed is guaranteed if GPU is used:)
filename_nii = 'verse096_seg.nii.gz'
filename = filename_nii.split(".")[0]
# read all the labels present in the file
multi_label_image=sitk.ReadImage(filename_nii)
img_npy = sitk.GetArrayFromImage(multi_label_image)
labels = np.unique(img_npy)
# read the file
reader = vtk.vtkNIFTIImageReader()
reader.SetFileName(filename_nii)
reader.Update()
# for all labels presented in the segmented file
for label in labels:
if int(label) != 0:
# apply marching cube surface generation
surf = vtk.vtkDiscreteMarchingCubes()
surf.SetInputConnection(reader.GetOutputPort())
surf.SetValue(0, int(label)) # use surf.GenerateValues function if more than one contour is available in the file
surf.Update()
#smoothing the mesh
smoother= vtk.vtkWindowedSincPolyDataFilter()
if vtk.VTK_MAJOR_VERSION <= 5:
smoother.SetInput(surf.GetOutput())
else:
smoother.SetInputConnection(surf.GetOutputPort())
# increase this integer set number of iterations if smoother surface wanted
smoother.SetNumberOfIterations(30)
smoother.NonManifoldSmoothingOn()
smoother.NormalizeCoordinatesOn() #The positions can be translated and scaled such that they fit within a range of [-1, 1] prior to the smoothing computation
smoother.GenerateErrorScalarsOn()
smoother.Update()
# save the output
writer = vtk.vtkSTLWriter()
writer.SetInputConnection(smoother.GetOutputPort())
writer.SetFileTypeToASCII()
# file name need to be changed
# save as the .stl file, can be changed to other surface mesh file
writer.SetFileName(f'{filename}_{label}.stl')
writer.Write()
SimpleITK
的图像读取器和写入器支持多种文件格式。对特定格式的支持由特定ITK ImageIO
类处理。默认情况下,根据文件名后缀和/或
文件头的内容自动确定特定文件的ImageIO
。高级SimpleITK
安装可以配置或扩展SimpleITK
支持的文件格式。可以使用GetRegisteredImageIOs()
方法找到已注册的ImageIO
的列表,内容如下:
BMPImageIO ( *.bmp, *.BMP )
BioRadImageIO ( *.PIC, *.pic )
Bruker2dseqImageIO
GDCMImageIO
GE4ImageIO
GE5ImageIO
GiplImageIO ( *.gipl *.gipl.gz)
HDF5ImageIO
JPEGImageIO ( *.jpg, *.JPG, *.jpeg, *.JPEG )
LSMImageIO ( *.tif, *.TIF, *.tiff, *.TIFF, *.lsm, *.LSM )
MINCImageIO ( *.mnc, *.MNC )
MRCImageIO ( *.mrc, *.rec )
MetaImageIO ( *.mha, *.mhd )
NiftiImageIO ( *.nia, *.nii, *.nii.gz, *.hdr, *.img, *.img.gz )
NrrdImageIO ( *.nrrd, *.nhdr )
PNGImageIO ( *.png, *.PNG )
StimulateImageIO
TIFFImageIO ( *.tif, *.TIF, *.tiff, *.TIFF )
VTKImageIO ( *.vtk )
使用 SimpleITK 的 ImageFileReader 和 ImageFileWriter 类的读写示例:
import SimpleITK as sitk
inputImageFileName = "3.mhd"
outputImageFileName = "3.tiff"
reader = sitk.ImageFileReader()
reader.SetImageIO("PNGImageIO")
reader.SetFileName(inputImageFileName)
image = reader.Execute();
writer = sitk.ImageFileWriter()
writer.SetFileName(outputImageFileName)
writer.Execute(image)
上面的示例指定使用PNGImageIO
读取文件。如果省略了这一行,SimpleITK
将根据文件名的后缀和/或文件头自动决定使用哪个IO。
使用SimpleITK
过程化接口的一个更紧凑的示例:
import SimpleITK as sitk
inputImageFileName = "3.mhd"
outputImageFileName = "3.tiff"
image = sitk.ReadImage(inputImageFileName, imageIO="PNGImageIO")
sitk.WriteImage(image, outputImageFileName)