保存图像和读取图像
import itk
import cv2
import matplotlib.pyplot as plt
import numpy as np
def saveImage(inImage,savePath):
writer = itk.ImageFileWriter[type(inImage)].New()
writer.SetFileName(savePath)
writer.SetInput(inImage)
writer.Update()
PixelType = itk.UC
Dimension = 2
ImageType = itk.Image[PixelType, Dimension]
imagePath1 = r'D:\svnproject\drrimage.tif'
reader1 = itk.ImageFileReader[ImageType].New()
reader1.SetFileName(imagePath1)
reader1.Update()
image1 = reader1.GetOutput()
#BinomialBlurImageFilter计算每个维度上的最近邻居平均值。根据用户的指定,该过程将重复多次。原则上,经过大量的迭代,结果将接近高斯卷积。
#https://examples.itk.org/src/filtering/smoothing/blurringanimageusingabinomialkernel/documentation
#number_of_repetitions(整型)值越大,图像越模糊
def SmoothingWithBinomialKernel(inputImage,number_of_repetitions):
ImageType = type(inputImage)
binomialFilter = itk.BinomialBlurImageFilter[ImageType,ImageType].New()
binomialFilter.SetInput(inputImage)
binomialFilter.SetRepetitions(number_of_repetitions)
rescaler = itk.RescaleIntensityImageFilter[ImageType, ImageType].New()
rescaler.SetInput(binomialFilter.GetOutput())
rescaler.SetOutputMinimum(0)
rescaler.SetOutputMaximum(255)
outputFileName = 'outImage_'+str(number_of_repetitions)+'.png'
saveImage(rescaler.GetOutput(),outputFileName)
return rescaler.GetOutput()
resImage1 = SmoothingWithBinomialKernel(image1,1)
resImage2 = SmoothingWithBinomialKernel(image1,10)
resImage3 = SmoothingWithBinomialKernel(image1,20)
resImage4 = SmoothingWithBinomialKernel(image1,50)
resImage5 = SmoothingWithBinomialKernel(image1,100)
resImage6 = SmoothingWithBinomialKernel(image1,150)
plt.subplot(231),plt.imshow(itk.GetArrayFromImage(resImage1),'gray'),plt.title('repetitions1')
plt.subplot(232),plt.imshow(itk.GetArrayFromImage(resImage2),'gray'),plt.title('repetitions10')
plt.subplot(233),plt.imshow(itk.GetArrayFromImage(resImage3),'gray'),plt.title('repetitions20')
plt.subplot(234),plt.imshow(itk.GetArrayFromImage(resImage4),'gray'),plt.title('repetitions50')
plt.subplot(235),plt.imshow(itk.GetArrayFromImage(resImage5),'gray'),plt.title('repetitions100')
plt.subplot(236),plt.imshow(itk.GetArrayFromImage(resImage6),'gray'),plt.title('repetitions150')
plt.show()
结果:
#通过高斯核卷积计算图像的平滑。 https://examples.itk.org/src/filtering/smoothing/computessmoothingwithgaussiankernel/documentation?highlight=smooth
# sigmaValue(浮点型,可以大于1.0)值越大,图像越模糊
def SmoothingWithGaussianKernel(inputImage,sigmaValue):
ImageType = type(inputImage)
smoothFilter = itk.SmoothingRecursiveGaussianImageFilter[ImageType, ImageType].New()
smoothFilter.SetInput(inputImage)
smoothFilter.SetSigma(sigmaValue)
outputFileName = 'outImage_'+str(sigmaValue)+'.png'
saveImage(smoothFilter.GetOutput(),outputFileName)
return smoothFilter.GetOutput()
#高斯平滑
resImage1 = SmoothingWithGaussianKernel(image1,0.1)
resImage2 = SmoothingWithGaussianKernel(image1,0.5)
resImage3 = SmoothingWithGaussianKernel(image1,0.9)
resImage4 = SmoothingWithGaussianKernel(image1,1.5)
resImage5 = SmoothingWithGaussianKernel(image1,2.5)
resImage6 = SmoothingWithGaussianKernel(image1,5.5)
plt.subplot(231),plt.imshow(itk.GetArrayFromImage(resImage1),'gray'),plt.title('sigmaValue0.1')
plt.subplot(232),plt.imshow(itk.GetArrayFromImage(resImage2),'gray'),plt.title('sigmaValue0.5')
plt.subplot(233),plt.imshow(itk.GetArrayFromImage(resImage3),'gray'),plt.title('sigmaValue0.9')
plt.subplot(234),plt.imshow(itk.GetArrayFromImage(resImage4),'gray'),plt.title('sigmaValue1.5')
plt.subplot(235),plt.imshow(itk.GetArrayFromImage(resImage5),'gray'),plt.title('sigmaValue2.5')
plt.subplot(236),plt.imshow(itk.GetArrayFromImage(resImage6),'gray'),plt.title('sigmaValue5.5')
plt.show()
结果:
#求图像的高阶导数。https://examples.itk.org/src/filtering/smoothing/findhigherderivativesofimage/documentation
#axisType--0表示x轴,1表示y轴 (突出边界)
def SmoothingWithHigherDerivatives(inputImage,axisType):
ImageType = type(inputImage)
gaussianFilter = itk.RecursiveGaussianImageFilter[ImageType, ImageType].New()
gaussianFilter.SetInput(inputImage)
gaussianFilter.SetDirection(axisType) #"x" axis
gaussianFilter.SetSecondOrder()
outputFileName = 'outImage_'+str(axisType)+'.png'
saveImage(gaussianFilter.GetOutput(),outputFileName)
return gaussianFilter.GetOutput()
#求图像的高阶导数
resImage1 = SmoothingWithHigherDerivatives(image1,0)
resImage2 = SmoothingWithHigherDerivatives(image1,1)
plt.subplot(121),plt.imshow(itk.GetArrayFromImage(resImage1),'gray'),plt.title('axisType X')
plt.subplot(122),plt.imshow(itk.GetArrayFromImage(resImage2),'gray'),plt.title('axisType Y')
plt.show()
结果:
#对图像应用均值滤波。https://examples.itk.org/src/filtering/smoothing/meanfilteringofanimage/documentation
# radius(整型)值越大,图像越模糊
def SmoothingWithMeanFiltering(inputImage,radius):
ImageType = type(inputImage)
meanFilter = itk.MeanImageFilter[ImageType, ImageType].New()
meanFilter.SetInput(inputImage)
meanFilter.SetRadius(radius)
outputFileName = 'outImage_'+str(radius)+'.png'
saveImage(meanFilter.GetOutput(),outputFileName)
return meanFilter.GetOutput()
#均值滤波
resImage1 = SmoothingWithMeanFiltering(image1,1)
resImage2 = SmoothingWithMeanFiltering(image1,5)
resImage3 = SmoothingWithMeanFiltering(image1,10)
resImage4 = SmoothingWithMeanFiltering(image1,20)
resImage5 = SmoothingWithMeanFiltering(image1,40)
resImage6 = SmoothingWithMeanFiltering(image1,80)
plt.subplot(231),plt.imshow(itk.GetArrayFromImage(resImage1),'gray'),plt.title('radius1')
plt.subplot(232),plt.imshow(itk.GetArrayFromImage(resImage2),'gray'),plt.title('radius5')
plt.subplot(233),plt.imshow(itk.GetArrayFromImage(resImage3),'gray'),plt.title('radius10')
plt.subplot(234),plt.imshow(itk.GetArrayFromImage(resImage4),'gray'),plt.title('radius20')
plt.subplot(235),plt.imshow(itk.GetArrayFromImage(resImage5),'gray'),plt.title('radius40')
plt.subplot(236),plt.imshow(itk.GetArrayFromImage(resImage6),'gray'),plt.title('radius80')
plt.show()
#在图像上应用中值滤波。https://examples.itk.org/src/filtering/smoothing/medianfilteringofanimage/documentation
#radius(整型)值越大,图像越模糊
def SmoothingWithMedianFiltering(inputImage,radius):
ImageType = type(inputImage)
medianFilter = itk.MedianImageFilter[ImageType, ImageType].New()
medianFilter.SetInput(inputImage)
medianFilter.SetRadius(radius)
outputFileName = 'outImage_'+str(radius)+'.png'
saveImage(medianFilter.GetOutput(),outputFileName)
return medianFilter.GetOutput()
resImage1 = SmoothingWithMedianFiltering(image1,1)
resImage2 = SmoothingWithMedianFiltering(image1,5)
resImage3 = SmoothingWithMedianFiltering(image1,10)
resImage4 = SmoothingWithMedianFiltering(image1,20)
resImage5 = SmoothingWithMedianFiltering(image1,40)
resImage6 = SmoothingWithMedianFiltering(image1,80)
plt.subplot(231),plt.imshow(itk.GetArrayFromImage(resImage1),'gray'),plt.title('radius1')
plt.subplot(232),plt.imshow(itk.GetArrayFromImage(resImage2),'gray'),plt.title('radius5')
plt.subplot(233),plt.imshow(itk.GetArrayFromImage(resImage3),'gray'),plt.title('radius10')
plt.subplot(234),plt.imshow(itk.GetArrayFromImage(resImage4),'gray'),plt.title('radius20')
plt.subplot(235),plt.imshow(itk.GetArrayFromImage(resImage5),'gray'),plt.title('radius40')
plt.subplot(236),plt.imshow(itk.GetArrayFromImage(resImage6),'gray'),plt.title('radius80')
plt.show()
#用离散高斯滤波器平滑图像。https://examples.itk.org/src/filtering/smoothing/smoothimagewithdiscretegaussianfilter/documentation
# variance(浮点型,可以大于1.0)值越大,图像越模糊
def SmoothWithDiscreteGaussianfilter(inputImage,variance):
ImageType = type(inputImage)
gaussianFilter = itk.DiscreteGaussianImageFilter[ImageType, ImageType].New()
gaussianFilter.SetInput(inputImage)
gaussianFilter.SetVariance(variance)
outputFileName = 'outImage_'+str(variance)+'.png'
saveImage(gaussianFilter.GetOutput(),outputFileName)
return gaussianFilter.GetOutput()
#离散高斯滤波器平滑图像
resImage1 = SmoothWithDiscreteGaussianfilter(image1,1.0)
resImage2 = SmoothWithDiscreteGaussianfilter(image1,4.0)
resImage3 = SmoothWithDiscreteGaussianfilter(image1,8.0)
resImage4 = SmoothWithDiscreteGaussianfilter(image1,15.0)
resImage5 = SmoothWithDiscreteGaussianfilter(image1,30.0)
resImage6 = SmoothWithDiscreteGaussianfilter(image1,50.0)
plt.subplot(231),plt.imshow(itk.GetArrayFromImage(resImage1),'gray'),plt.title('variance1')
plt.subplot(232),plt.imshow(itk.GetArrayFromImage(resImage2),'gray'),plt.title('variance4')
plt.subplot(233),plt.imshow(itk.GetArrayFromImage(resImage3),'gray'),plt.title('variance8')
plt.subplot(234),plt.imshow(itk.GetArrayFromImage(resImage4),'gray'),plt.title('variance15')
plt.subplot(235),plt.imshow(itk.GetArrayFromImage(resImage5),'gray'),plt.title('variance30')
plt.subplot(236),plt.imshow(itk.GetArrayFromImage(resImage6),'gray'),plt.title('variance50')
plt.show()
结果:
#使用曲率驱动流去噪图像。https://examples.itk.org/src/filtering/anisotropicsmoothing/computecurvatureflow/documentation?highlight=denois
#time_step越大,迭代次数越多,图像越暗
def DenoiseImageWithCurvatureDrivenFlow(inputImagePath,number_of_iterations,time_step):
PixelType = itk.F
Dimension = 2
ImageType = itk.Image[PixelType, Dimension]
reader = itk.ImageFileReader[ImageType].New()
reader.SetFileName(inputImagePath)
reader.Update()
ImageType = type(reader.GetOutput())
FilterType = itk.CurvatureFlowImageFilter[ImageType, ImageType]
curvatureFlowFilter = FilterType.New()
curvatureFlowFilter.SetInput(reader.GetOutput())
curvatureFlowFilter.SetNumberOfIterations(number_of_iterations)
curvatureFlowFilter.SetTimeStep(time_step)
OutputImageType = itk.Image[itk.UC,2]
RescaleFilterType = itk.RescaleIntensityImageFilter[ImageType, OutputImageType]
rescaler = RescaleFilterType.New()
rescaler.SetInput(curvatureFlowFilter.GetOutput())
outputPixelTypeMinimum = itk.NumericTraits[itk.UC].min()
outputPixelTypeMaximum = itk.NumericTraits[itk.UC].max()
rescaler.SetOutputMinimum(outputPixelTypeMinimum)
rescaler.SetOutputMaximum(outputPixelTypeMaximum)
outputFileName = 'outImage_'+str(number_of_iterations)+'_'+str(time_step)+'.png'
saveImage(rescaler.GetOutput(),outputFileName)
return rescaler.GetOutput()
#使用曲率驱动流去噪图像
resImage1 = DenoiseImageWithCurvatureDrivenFlow(imagePath1,1,0.5)
resImage2 = DenoiseImageWithCurvatureDrivenFlow(imagePath1,3,0.5)
resImage3 = DenoiseImageWithCurvatureDrivenFlow(imagePath1,6,0.5)
resImage4 = DenoiseImageWithCurvatureDrivenFlow(imagePath1,1,2.5)
resImage5 = DenoiseImageWithCurvatureDrivenFlow(imagePath1,3,2.5)
resImage6 = DenoiseImageWithCurvatureDrivenFlow(imagePath1,6,2.5)
plt.subplot(231),plt.imshow(itk.GetArrayFromImage(resImage1),'gray'),plt.title('iterations1,time_step 0.5')
plt.subplot(232),plt.imshow(itk.GetArrayFromImage(resImage2),'gray'),plt.title('iterations3,time_step 0.5')
plt.subplot(233),plt.imshow(itk.GetArrayFromImage(resImage3),'gray'),plt.title('iterations6,time_step 0.5')
plt.subplot(234),plt.imshow(itk.GetArrayFromImage(resImage4),'gray'),plt.title('iterations1,time_step 2.5')
plt.subplot(235),plt.imshow(itk.GetArrayFromImage(resImage5),'gray'),plt.title('iterations3,time_step 2.5')
plt.subplot(236),plt.imshow(itk.GetArrayFromImage(resImage6),'gray'),plt.title('iterations6,time_step 2.5')
plt.show()
结果:
# 应用由参数alpha和beta控制的幂律自适应直方图均衡化。
# https://examples.itk.org/src/filtering/imagestatistics/adaptivehistogramequalizationimagefilter/documentation?highlight=histogram
# 参数alpha控制了过滤器有多像经典直方图均衡化方法(alpha = 0),到过滤器有多像非锐化蒙版(alpha = 1)。
# 参数beta控制过滤器在多大程度上像非锐化蒙版(beta = 0)和过滤器在多大程度上像穿透(beta = 1,其中alpha = 1)。
# 参数窗口(或半径)控制计算局部统计信息的区域的大小。
# alpha:浮点型 beta:浮点型 radius:整型
def ImageHistogramEqualization(inputImage,alpha,beta,radius):
ImageType = type(inputImage)
histogramEqualization = itk.AdaptiveHistogramEqualizationImageFilter[ImageType].New()
histogramEqualization.SetInput(inputImage)
histogramEqualization.SetAlpha(alpha)
histogramEqualization.SetBeta(beta)
radiusArr = itk.Size[2]()
radiusArr.Fill(radius)
histogramEqualization.SetRadius(radiusArr)
outputFileName = 'outImage_'+str(alpha)+'_'+str(beta)+'_'+str(radius)+'.png'
itk.imwrite(histogramEqualization, outputFileName)
return histogramEqualization.GetOutput()
#自适应直方图均衡化
resImage1 = ImageHistogramEqualization(image1,0.0,1.0,3)
resImage2 = ImageHistogramEqualization(image1,1.0,0.0,3)
resImage3 = ImageHistogramEqualization(image1,0.5,0.5,3)
resImage4 = ImageHistogramEqualization(image1,0.0,1.0,7)
resImage5 = ImageHistogramEqualization(image1,1.0,0.0,7)
resImage6 = ImageHistogramEqualization(image1,0.5,0.5,7)
plt.subplot(231),plt.imshow(itk.GetArrayFromImage(resImage1),'gray'),plt.title('alpha0.0,beta1.0,radius3')
plt.subplot(232),plt.imshow(itk.GetArrayFromImage(resImage2),'gray'),plt.title('alpha1.0,beta0.0,radius3')
plt.subplot(233),plt.imshow(itk.GetArrayFromImage(resImage3),'gray'),plt.title('alpha0.5,beta0.5,radius3')
plt.subplot(234),plt.imshow(itk.GetArrayFromImage(resImage4),'gray'),plt.title('alpha0.0,beta1.0,radius7')
plt.subplot(235),plt.imshow(itk.GetArrayFromImage(resImage5),'gray'),plt.title('alpha1.0,beta0.0,radius7')
plt.subplot(236),plt.imshow(itk.GetArrayFromImage(resImage6),'gray'),plt.title('alpha0.5,beta0.5,radius7')
plt.show()
结果:
用之前的图像存在问题,以后有时间找原因
#使用 Canny 边缘检测过滤器检测边缘
#https://examples.itk.org/src/filtering/imagefeature/detectedgeswithcannyedgedetectionfilter/documentation?highlight=edge
def CannyEdgeDetectionImageFilter(inputImagePath,variance,lower_threshold,upper_threshold):
InputPixelType = itk.F
OutputPixelType = itk.UC
Dimension = 2
InputImageType = itk.Image[InputPixelType, Dimension]
OutputImageType = itk.Image[OutputPixelType, Dimension]
reader = itk.ImageFileReader[InputImageType].New()
reader.SetFileName(inputImagePath)
cannyFilter = itk.CannyEdgeDetectionImageFilter[InputImageType, InputImageType].New()
cannyFilter.SetInput(reader.GetOutput())
cannyFilter.SetVariance(variance)
cannyFilter.SetLowerThreshold(lower_threshold)
cannyFilter.SetUpperThreshold(upper_threshold)
rescaler = itk.RescaleIntensityImageFilter[InputImageType, OutputImageType].New()
rescaler.SetInput(cannyFilter.GetOutput())
rescaler.SetOutputMinimum(0)
rescaler.SetOutputMaximum(255)
outputFileName = 'outImage_'+str(variance)+'_'+str(lower_threshold)+'_'+str(upper_threshold)+'.png'
saveImage(rescaler.GetOutput(),outputFileName)
return rescaler.GetOutput()
#Canny 边缘检测
imagePath1 = r'D:\dell\picture\lena.png'
resImage1 = CannyEdgeDetectionImageFilter(imagePath1,0.1,0,255)
resImage2 = CannyEdgeDetectionImageFilter(imagePath1,0.5,0,255)
resImage3 = CannyEdgeDetectionImageFilter(imagePath1,1.0,0,255)
resImage4 = CannyEdgeDetectionImageFilter(imagePath1,5.0,0,255)
resImage5 = CannyEdgeDetectionImageFilter(imagePath1,10.0,0,255)
resImage6 = CannyEdgeDetectionImageFilter(imagePath1,50.0,0,255)
plt.subplot(231),plt.imshow(itk.GetArrayFromImage(resImage1),'gray'),plt.title('variance0.1')
plt.subplot(232),plt.imshow(itk.GetArrayFromImage(resImage2),'gray'),plt.title('variance0.5')
plt.subplot(233),plt.imshow(itk.GetArrayFromImage(resImage3),'gray'),plt.title('variance1.0')
plt.subplot(234),plt.imshow(itk.GetArrayFromImage(resImage4),'gray'),plt.title('variance5.0')
plt.subplot(235),plt.imshow(itk.GetArrayFromImage(resImage5),'gray'),plt.title('variance10')
plt.subplot(236),plt.imshow(itk.GetArrayFromImage(resImage6),'gray'),plt.title('variance50')
plt.show()
结果:
#将 SobelEdgeDetectionImageFilter 应用于图像
#https://examples.itk.org/src/filtering/imagefeature/sobeledgedetectionimagefilter/documentation?highlight=edge
def SobelEdgeDetectionImageFilter(inputImagePath):
input_image = itk.imread(inputImagePath, pixel_type=itk.F)
output_image = itk.sobel_edge_detection_image_filter(input_image)
rescaler = itk.RescaleIntensityImageFilter[type(output_image), itk.Image[itk.UC, 2]].New()
rescaler.SetInput(output_image)
rescaler.SetOutputMinimum(0)
rescaler.SetOutputMaximum(255)
outputFileName = 'outImage_obelEdgeDetection.png'
saveImage(rescaler.GetOutput(),outputFileName)
return rescaler.GetOutput()
#基于过零的边缘检测
#https://examples.itk.org/src/filtering/imagefeature/zerocrossingbasededgedecor/documentation?highlight=edge
def ZerocrossingEdgeDetecor(inputImagePath,variance=5.0):
FloatImageType = itk.Image[itk.F, 2]
FilterType = itk.ZeroCrossingBasedEdgeDetectionImageFilter[FloatImageType, FloatImageType]
input_image = itk.imread(inputImagePath, pixel_type=itk.F)
edgeDetector = FilterType.New()
edgeDetector.SetInput(input_image)
edgeDetector.SetVariance(variance)
rescaler = itk.RescaleIntensityImageFilter[type(edgeDetector.GetOutput()), itk.Image[itk.UC, 2]].New()
rescaler.SetInput(edgeDetector.GetOutput())
rescaler.SetOutputMinimum(0)
rescaler.SetOutputMaximum(255)
outputFileName = 'outImage_ZerocrossingEdgeDetection'+str(variance)+'.png'
saveImage(rescaler.GetOutput(),outputFileName)
return rescaler.GetOutput()
#Sobel边缘检测
resImage1 = SobelEdgeDetectionImageFilter(imagePath1)
# 基于过零的边缘检测
resImage2 = ZerocrossingEdgeDetecor(imagePath1,0.1)
resImage3 = ZerocrossingEdgeDetecor(imagePath1,0.5)
resImage4 = ZerocrossingEdgeDetecor(imagePath1,1.0)
resImage5 = ZerocrossingEdgeDetecor(imagePath1,5.0)
resImage6 = ZerocrossingEdgeDetecor(imagePath1,20.0)
plt.subplot(231),plt.imshow(itk.GetArrayFromImage(resImage1),'gray'),plt.title('Sobel')
plt.subplot(232),plt.imshow(itk.GetArrayFromImage(resImage2),'gray'),plt.title('variance0.1')
plt.subplot(233),plt.imshow(itk.GetArrayFromImage(resImage3),'gray'),plt.title('variance0.5')
plt.subplot(234),plt.imshow(itk.GetArrayFromImage(resImage4),'gray'),plt.title('variance1')
plt.subplot(235),plt.imshow(itk.GetArrayFromImage(resImage5),'gray'),plt.title('variance5')
plt.subplot(236),plt.imshow(itk.GetArrayFromImage(resImage6),'gray'),plt.title('variance20')
plt.show()
结果: