ITK对应python中SimpleITK库的常用函数举例

在医学图像上经常使用的一个库-SimpleITK,很多处理都只懂个大概或者只会简单的读取,保存图像,这里对常用的一些操作进行总结:

import  SimpleITK as sitk 

1、sitk.Cast(sitk.ReadImage(),sitk.sitkFloat32) 图像转换数据类型

2、sitk.ReadImage(图像路径)读取图像,如.mhd .nii .nrrd等

3、sitk.WriteImage(image,图像路径)保存图像

4、sitk.GetArrayFromImage(sitk.ReadImage()) 将sitk图像转换成数组

5、sitk.GetImageFromArray() 将数组转换成sitk图像

6、rescalFilt = sitk.RescaleIntensityImageFilter() 改变图像的像素值,转换为[0,255]

      rescalFilt.SetOutputMaximum(255)

      rescalFilt.SetOutputMinimum(0)

      itkimage = rescalFilt.Execute(sitk.ReadImage())

7、resampler = sitk.ResampleImageFilter() 图像重采样

     resampler.SetReferenceImage(itkimage)#需要重新采样的目标图像

     resampler.SetSize(newSize.tolist())

     resampler.SetTransform(sitk.Transform(3, sitk.sitkIdentity))

     resampler.SetInterpolator(sitk.sitkNearestNeighbor)

     itkimgResampled = resampler.Execute(itkimage)#得到重新采样后的图像

8、sitk.Threshold(sitk.ReadImage(), 0, 1.0, 255) 将图像中像素值<0和>1.0的像素值改为255,否则保持不变 

9、sitk.BinaryThreshold(sitk_src, lowerThreshold=lowervalue, upperThreshold=uppervalue, insideValue=255, outsideValue=0) 图像二值化

10、image1 = sitk.ReadImage() 对sitk的image1处理完后恢复到世界坐标系

      image2 = sitk.GetArrayFromImage(image1)

      origin =  image1.GetOrigin()

      spacing = image1.GetSpacing()

      direction = image1.GetDirection()

      image2 = sitk.GetImageFromArray(image2)

      image2.SetOrigin(origin)

      image2.SetSpacing(spacing)

      image2.SetDirection(direction)

11、sitk_src_gaus = sitk.DiscreteGaussianImageFilter() 图像高斯滤波

        sitk_src_gaus.SetVariance(3)

        sitk_src_gaus.SetMaximumError(0.2)

        sitk_src_gaus = sitk_src_gaus.Execute(sitk.ReadImage())

12、sitk.BinaryMorphologicalOpening(sitk.ReadImage() != 0, kernelsize) 图像的形态学操作:开、闭、膨胀、腐蚀

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

        sitk.BinaryDilate(sitk.ReadImage() != 0, kernelsize)

        sitk.BinaryErode(sitk.ReadImage() != 0, kernelsize)

13、sitk_xorop = sitk.XorImageFilter() 图像的逻辑运算:异或、非

        sitk_mask1 = sitk_xorop.Execute(imag1, imag2) image1和image2是sitk图像的二值化结果

        sitk_notop = sitk.NotImageFilter()

        sitk_mask2 = sitk_notop.Execute(image) image是sitk图像的二值化结果

14、sitk.BinaryFillhole(sitk的二值化图像)去除二值化图像内的孔洞

15、去除小的目标:

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

16、取最大连通域

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())

17、获取读取图像的数据类型

image = sitk.ReadImage(image_path)

image_type = image.GetPixelID()

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

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())

19、快速建立一个与当前图像相同大小的全零图像(SimpleITK类型的图像)

Image_new = sitk.Image(img.GetSize()[0],img.GetSize()[1],img.GetSize()[2],sitk.sitkFloat32)#生成新图像的spacing为[1,1,1]

你可能感兴趣的:(计算机视觉)