ITK入门教程(17)nii,mhd,stl之间的相互转换

ITK入门教程(17)nii,mhd,stl之间的相互转换,第1张

ITK入门教程(17)nii,mhd,stl之间的相互转换

本文细说一下医学图像中常见的文件格式之间的转换。

1.如何将raw/mhd转换为Nifti/nii

您应该能够使用 SimpleITK 来完成。你会做这样的事情:

import SimpleITK as sitk

img = sitk.ReadImage("input.mhd")
sitk.WriteImage(img, "output.nii")

如果python中没有SimpleITK,安装如下:

pip install SimpleITK

SimpleITK 尽最大努力保留所有标头信息,尽管它并不完美。体素尺寸将被保留。

2.如何将单标签的Nifti/nii转换为stl

使用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")
3.如何将多标签的Nifti/nii转换为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}_

.stl') writer.Write()

4.图像读写 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)

欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5436591.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-11
下一篇 2022-12-11

发表评论
请登录后评论...
登录
后才能评论 提交

评论列表(0条)
保存
{label}