医学图像处理SimpleITK库的一些常用操作解释_jancis的博客-CSDN博客
在医学图像上经常使用的一个库-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 = 0maxsize = 0for l in stats.GetLabels():size = stats.GetPhysicalSize(l)if maxsize < size:maxlabel = lmaxsize = sizenot_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] = 0for i in range(len(not_remove)):outmask[labelmaskimage == not_remove[i]] = 255outmask_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