均值滤波vtkImageConvolve
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import vtk
reader = vtk.vtkJPEGReader() # 读入灰度图
reader.SetFileName(r'E:\lena-gray.jpg')
reader.Update()
originalCastFilter = vtk.vtkImageCast() # 将图像像素类型由unsigned char转换成float数据类型
originalCastFilter.SetInputConnection(reader.GetOutputPort())
originalCastFilter.SetOutputScalarTypeToFloat()
originalCastFilter.Update()
convolveFilter = vtk.vtkImageConvolve()
'''
实现图像的卷积运算,需要两个输入,一个是需要进行卷积的图像,这里为vtkJPEGReader读取的图像数据
另一个是卷积模板数据
'''
convolveFilter.SetInputConnection(originalCastFilter.GetOutputPort())
kernel = [0.04,0.04,0.04,0.04,0.04,
0.04,0.04,0.04,0.04,0.04,
0.04,0.04,0.04,0.04,0.04,
0.04,0.04,0.04,0.04,0.04,
0.04,0.04,0.04,0.04,0.04]
convolveFilter.SetKernel5x5(kernel) # 接收一个5*5的卷积模板数组
convolveFilter.Update()
convCastFilter = vtk.vtkImageCast() # 卷积运算完成后,在此将float数据转换为unsigned char数据类型进行图像显示
convCastFilter.SetInputData(convolveFilter.GetOutput())
convCastFilter.Update()
originalActor = vtk.vtkImageActor()
originalActor.SetInputData(reader.GetOutput())
convolvedActor = vtk.vtkImageActor()
convolvedActor.SetInputData(convCastFilter.GetOutput())
leftViewport = [0.0, 0.0, 0.5, 1.0]
rightViewport = [0.5, 0.0, 1.0, 1.0]
originalRenderer = vtk.vtkRenderer()
originalRenderer.SetViewport(leftViewport)
originalRenderer.AddActor(originalActor)
originalRenderer.ResetCamera()
originalRenderer.SetBackground(1.0, 1.0, 1.0)
convolvedRenderer = vtk.vtkRenderer()
convolvedRenderer.SetViewport(rightViewport)
convolvedRenderer.AddActor(convolvedActor)
convolvedRenderer.ResetCamera()
convolvedRenderer.SetBackground(1.0, 1.0, 1.0)
renderWindow = vtk.vtkRenderWindow()
renderWindow.AddRenderer(originalRenderer)
renderWindow.AddRenderer(convolvedRenderer)
renderWindow.Render()
renderWindow.SetSize(900, 300)
renderWindow.SetWindowName('MeanFilterExample')
renderWindowInteractor = vtk.vtkRenderWindowInteractor()
style = vtk.vtkInteractorStyleImage()
renderWindowInteractor.SetInteractorStyle(style)
renderWindowInteractor.SetRenderWindow(renderWindow)
renderWindowInteractor.Initialize()
renderWindowInteractor.Start()
高斯平滑vtkImageGaussianSmooth
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import vtk
reader = vtk.vtkJPEGReader() # 读入灰度图
reader.SetFileName(r'E:\lena-gray.jpg')
reader.Update()
gaussianSmoothFilter = vtk.vtkImageGaussianSmooth() # 默认是执行三维高斯滤波
gaussianSmoothFilter.SetInputConnection(reader.GetOutputPort())
gaussianSmoothFilter.SetDimensionality(2) # 根据需要设置相应的维数
gaussianSmoothFilter.SetRadiusFactor(5) # 设置高斯模板的大小,当超出该模板的范围时,系数取0
gaussianSmoothFilter.SetStandardDeviation(3) # 用于设置高斯分布函数的标准差
gaussianSmoothFilter.Update()
originalActor = vtk.vtkImageActor()
originalActor.SetInputData(reader.GetOutput())
smoothedActor = vtk.vtkImageActor()
smoothedActor.SetInputData(gaussianSmoothFilter.GetOutput())
originalViewport = [0.0, 0.0, 0.5, 1.0]
smoothedViewport = [0.5, 0.0, 1.0, 1.0]
originalRenderer = vtk.vtkRenderer()
originalRenderer.SetViewport(originalViewport)
originalRenderer.AddActor(originalActor)
originalRenderer.ResetCamera()
originalRenderer.SetBackground(1.0, 1.0, 1.0)
smoothedRenderer = vtk.vtkRenderer()
smoothedRenderer.SetViewport(smoothedViewport)
smoothedRenderer.AddActor(smoothedActor)
smoothedRenderer.ResetCamera()
smoothedRenderer.SetBackground(1.0, 1.0, 1.0)
renderWindow = vtk.vtkRenderWindow()
renderWindow.AddRenderer(originalRenderer)
renderWindow.AddRenderer(smoothedRenderer)
renderWindow.Render()
renderWindow.SetSize(900, 300)
renderWindow.SetWindowName('GaussianFilterExample')
renderWindowInteractor = vtk.vtkRenderWindowInteractor()
style = vtk.vtkInteractorStyleImage()
renderWindowInteractor.SetInteractorStyle(style)
renderWindowInteractor.SetRenderWindow(renderWindow)
renderWindowInteractor.Initialize()
renderWindowInteractor.Start()
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import vtk
reader = vtk.vtkJPEGReader() # 读入灰度图
reader.SetFileName(r'E:\lena-noise.jpg')
reader.Update()
hybridMedian = vtk.vtkImageHybridMedian2D() #不需要用户设置任何参数,该方法能够有效保持图像边缘,并对椒盐噪声有较好的抑制作用
hybridMedian.SetInputData(reader.GetOutput())
hybridMedian.Update()
originalActor = vtk.vtkImageActor()
originalActor.SetInputData(reader.GetOutput())
hybridMedianActor = vtk.vtkImageActor()
hybridMedianActor.SetInputData(hybridMedian.GetOutput())
originalViewport = [0.0, 0.0, 0.5, 1.0]
hybridMedianViewport = [0.5, 0.0, 1.0, 1.0]
originalRenderer = vtk.vtkRenderer()
originalRenderer.SetViewport(originalViewport)
originalRenderer.AddActor(originalActor)
originalRenderer.ResetCamera()
originalRenderer.SetBackground(1.0, 1.0, 1.0)
hybridMedianRenderer = vtk.vtkRenderer()
hybridMedianRenderer.SetViewport(hybridMedianViewport)
hybridMedianRenderer.AddActor(hybridMedianActor)
hybridMedianRenderer.ResetCamera()
hybridMedianRenderer.SetBackground(1.0, 1.0, 1.0)
renderWindow = vtk.vtkRenderWindow()
renderWindow.AddRenderer(originalRenderer)
renderWindow.AddRenderer(hybridMedianRenderer)
renderWindow.Render()
renderWindow.SetSize(900, 300)
renderWindow.SetWindowName('MedianFilterExample')
renderWindowInteractor = vtk.vtkRenderWindowInteractor()
style = vtk.vtkInteractorStyleImage()
renderWindowInteractor.SetInteractorStyle(style)
renderWindowInteractor.SetRenderWindow(renderWindow)
renderWindowInteractor.Initialize()
renderWindowInteractor.Start()
各向异性滤波vtkImageAnisotropicDiffusion2D
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import vtk
reader = vtk.vtkJPEGReader() # 读入灰度图
reader.SetFileName(r'E:\lena-gray.jpg')
reader.Update()
diffusion = vtk.vtkImageAnisotropicDiffusion2D()
diffusion.SetInputConnection(reader.GetOutputPort())
diffusion.SetNumberOfIterations(10) # 设置迭代的次数
'''
各向异性扩散过滤原理是在梯度较小的像素处进行较大幅度扩散,而在大梯度处则只进行细微的扩散
'''
diffusion.SetDiffusionThreshold(20) # 只有当像素梯度小于该阈值20时才会进行扩散
diffusion.Update()
originalActor = vtk.vtkImageActor()
originalActor.SetInputData(reader.GetOutput())
diffusionActor = vtk.vtkImageActor()
diffusionActor.SetInputData(diffusion.GetOutput())
originalViewport = [0.0, 0.0, 0.5, 1.0]
diffusionViewport = [0.5, 0.0, 1.0, 1.0]
originalRenderer = vtk.vtkRenderer()
originalRenderer.SetViewport(originalViewport)
originalRenderer.AddActor(originalActor)
originalRenderer.ResetCamera()
originalRenderer.SetBackground(1.0, 1.0, 1.0)
diffusionRenderer = vtk.vtkRenderer()
diffusionRenderer.SetViewport(diffusionViewport)
diffusionRenderer.AddActor(diffusionActor)
diffusionRenderer.ResetCamera()
diffusionRenderer.SetBackground(1.0, 1.0, 1.0)
renderWindow = vtk.vtkRenderWindow()
renderWindow.AddRenderer(originalRenderer)
renderWindow.AddRenderer(diffusionRenderer)
renderWindow.Render()
renderWindow.SetSize(900, 300)
renderWindow.SetWindowName('AnistropicFilteringExample')
renderWindowInteractor = vtk.vtkRenderWindowInteractor()
style = vtk.vtkInteractorStyleImage()
renderWindowInteractor.SetInteractorStyle(style)
renderWindowInteractor.SetRenderWindow(renderWindow)
renderWindowInteractor.Initialize()
renderWindowInteractor.Start()