Python中SimpleITK库的常用函数

在医学图像上经常使用的一个Python库SimpleITK,下面介绍其常用的函数:

import  SimpleITK as sitk 

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

# 2.保存图像
sitk.WriteImage(image,save_path)

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

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

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

# 6.改变图像的像素值,转换为[0,255]
rescalFilt = sitk.RescaleIntensityImageFilter() 
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.将图像中像素值裁剪,小于0和大于1.0的像素值改为255,否则保持不变 
sitk.Threshold(sitk.ReadImage(), 0, 1.0, 255) 

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

# 10.对sitk的image1处理完后恢复到物理坐标系
image1 = sitk.ReadImage() 
array= sitk.GetArrayFromImage(image1)
origin =  array.GetOrigin()
spacing = array.GetSpacing()
direction = array.GetDirection()
image2 = sitk.GetImageFromArray(array)
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 = [123,456,789]
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]

你可能感兴趣的:(Python,医学影像处理,python,计算机视觉,opencv)