SimpleITK常用指令

说明文档:SimpleITK Sphinx Documentation — SimpleITK 2.0rc2 documentation

import SimpleITK as sitk

1、 数据类型

        1)查看

img_itk.GetPixelIDTypeAsString()
img_itk.GetPixelID()

        2)修改

sitk.Cast(img_itk, sitk.sitkInt16)

2、sitk中的一些基本概念

        1)redirection/resample,不改变origin和物理坐标

        2)

                                SimpleITK常用指令_第1张图片

 3、导入/写入数据

sitk.ReadImage(path)
sitk.WriteImage(img_itk, 'name.nii.gz')

4、与numpy转换(size保持一致)

sitk.GetImageFromArray(img_arr)
sitk.GetArrayFromImage(img_itk)

5、物理坐标和index转换

img_itk.TransformContinuousIndexToPhysicalPoint(point[::-1])
img_itk.TransformPhysicalPointToContinuousIndex(point)[::-1]

6、复制图片(version >= 2.0)

img_itk_cp = img_itk.__deepcopy__(None)

7、复制meta信息(direction、spacing、origin)

img_itk.CopyInformation(source_itk)

8、通过构建filter来实现的一些功能

        1)像素值缩放到区间(示例为[0, 255])

Filter = sitk.RescaleIntensityImageFilter() 

Filter.SetOutputMaximum(255)

Filter.SetOutputMinimum(0)

output_itk = Filter.Execute(img_itk)

        2)图像高斯滤波

Filter = sitk.DiscreteGaussianImageFilter()

Filter.SetVariance(3)

Filter.SetMaximumError(0.2)

output_itk = Filter.Execute(img_itk)

         3)图像逻辑运算(输入图像需要进行二值化)

# 异或
Filter = sitk.XorImageFilter()

output_itk = Filter.Execute(image_itk_1, image_itk_2) 

# 非

Filter = sitk.NotImageFilter()

output_itk = Filter.Execute(image_itk) 

        4)计算dice系数

Filter = sitk.LabelOverlapMeasuresImageFilter()
Filter.Execute(itkLabel == label, itkPred == label)
dice = Filter.GetDiceCoefficient()

9、特定像素值改变?

sitk.Threshold(img_itk, 0, 1.0, 255) 

10、图像二值化?

sitk.BinaryThreshold(img_itk, lowerThreshold=lowervalue, upperThreshold=uppervalue, insideValue=255, outsideValue=0)

11、图像形态学操作

sitk.BinaryMorphologicalOpening(sitk.ReadImage() != 0, kernelsize) # 开

sitk.BinaryMorphologicalClosing(sitk.ReadImage() != 0, kernelsize) # 闭

sitk.BinaryDilate(sitk.ReadImage() != 0, kernelsize) # 膨胀

sitk.BinaryErode(sitk.ReadImage() != 0, kernelsize) # 腐蚀

12、去除二值化图像内的孔洞(输入图像需要进行二值化)

sitk.BinaryFillhole(image_itk)

13、去除小目标

def RemoveSmallConnectedCompont(sitk_maskimg, rate=0.5):
    """
    remove small object
    :param sitk_maskimg:input binary image
    :param rate:size rate
    :return:binary image
    """
    cc = sitk.ConnectedComponent(sitk_maskimg)
    stats = sitk.LabelIntensityStatisticsImageFilter()
    stats.SetGlobalDefaultNumberOfThreads(8)
    stats.Execute(cc, sitk_maskimg)
    maxlabel = 0
    maxsize = 0
    for l in stats.GetLabels():
        size = stats.GetPhysicalSize(l)
        if maxsize < size:
            maxlabel = l
            maxsize = size
    not_remove = []
    for l in stats.GetLabels():
        size = stats.GetPhysicalSize(l)
        if size > maxsize * rate:
            not_remove.append(l)
    labelmaskimage = sitk.GetArrayFromImage(cc)
    outmask = labelmaskimage.copy()
    outmask[labelmaskimage != maxlabel] = 0
    for i in range(len(not_remove)):
        outmask[labelmaskimage == not_remove[i]] = 255
    outmask_sitk = sitk.GetImageFromArray(outmask)
    outmask_sitk.SetDirection(sitk_maskimg.GetDirection())
    outmask_sitk.SetSpacing(sitk_maskimg.GetSpacing())
    outmask_sitk.SetOrigin(sitk_maskimg.GetOrigin())
    return outmask_sitk

14、取最大连通域

cc = sitk.ConnectedComponent(binarysitk_image)
stats = sitk.LabelIntensityStatisticsImageFilter()
stats.SetGlobalDefaultNumberOfThreads(8)
stats.Execute(cc, binarysitk_image)
maxlabel = 0
maxsize = 0
for l in stats.GetLabels():
    size = stats.GetPhysicalSize(l)
    if maxsize < size:
        maxlabel = l
        maxsize = size
labelmaskimage = sitk.GetArrayFromImage(cc)
outmask = labelmaskimage.copy()
outmask[labelmaskimage == maxlabel] = 1
outmask[labelmaskimage != maxlabel] = 0
outmasksitk = sitk.GetImageFromArray(outmask)
outmasksitk.SetSpacing(binarysitk_image.GetSpacing())
outmasksitk.SetOrigin(binarysitk_image.GetOrigin())
outmasksitk.SetDirection(binarysitk_image.GetDirection())

15、获取所在坐标点处的连通域

label_image = SimpleITK.ConnectedComponent(image)#image必须是二值图像
label_image_array = SimpleITK.GetArrayFromImage(label_image)
output = np.zeros_like(label_image_array)
point = [122,235,456]
value = label_image.GetPixel(point)
if value != 0:
     output[label_image_array == value] = 1
output = SimpleITK.GetImageFromArray(output)
output.SetOrigin(image.GetOrigin())
output.SetSpacing(image.GetSpacing())
output.SetDirection(image.GetDirection())

16、快速建立一个与当前图像相同大小的全零图像(SimpleITK类型的图像, spacing=[1, 1, 1])

Image_new = sitk.Image(img.GetSize()[0],img.GetSize()[1],img.GetSize()[2],sitk.sitkFloat32)

17、np.clip操作的等价

sitk.Clamp(img_itk, upperBound=200, lowerBound=100)

你可能感兴趣的:(医学图像,python)