python VTK(十三) ----图像边缘检测 梯度算子 sobel算子

梯度算子

import vtk
reader = vtk.vtkJPEGReader()   # 读入灰度图
reader.SetFileName(r'E:\lena-gray.jpg')
reader.Update()

gradientFilter = vtk.vtkImageGradient()
'''
计算梯度时,采用的是中间差分法,即像素在每个方向的差分都是利用前后两个像素值之差
'''
gradientFilter.SetInputConnection(reader.GetOutputPort())
gradientFilter.SetDimensionality(2)   # 用于设置要计算的图像维数,默认为二维,此时梯度矢量也为二维

magnitudeFilter = vtk.vtkImageMagnitude()  # 计算梯度矢量的2-范数。即矢量的模
magnitudeFilter.SetInputConnection(gradientFilter.GetOutputPort())
magnitudeFilter.Update()

range = magnitudeFilter.GetOutput().GetScalarRange()
ShiftScale = vtk.vtkImageShiftScale()  # 将图像的数据范围调整为[0,255]然后显示。
ShiftScale.SetOutputScalarTypeToUnsignedChar()
ShiftScale.SetScale(255 / range[1])
ShiftScale.SetInputConnection(magnitudeFilter.GetOutputPort())
ShiftScale.Update()

originalActor = vtk.vtkImageActor()
originalActor.SetInputData(reader.GetOutput())
gradActor = vtk.vtkImageActor()
gradActor.SetInputData(ShiftScale.GetOutput())

originalViewport = [0.0, 0.0, 0.5, 1.0]
gradviewport = [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)

gradRenderer = vtk.vtkRenderer()
gradRenderer.SetViewport(gradviewport)
gradRenderer.AddActor(gradActor)
gradRenderer.ResetCamera()
gradRenderer.SetBackground(1.0, 1.0, 1.0)

renderWindow = vtk.vtkRenderWindow()
renderWindow.AddRenderer(originalRenderer)
renderWindow.AddRenderer(gradRenderer)
renderWindow.SetSize(640, 480)
renderWindow.Render()
renderWindow.SetWindowName('ImageGradientExample')

renderWindowInteractor = vtk.vtkRenderWindowInteractor()
style = vtk.vtkInteractorStyleImage()

renderWindowInteractor.SetInteractorStyle(style)
renderWindowInteractor.SetRenderWindow(renderWindow)
renderWindowInteractor.Initialize()
renderWindowInteractor.Start()

python VTK(十三) ----图像边缘检测 梯度算子 sobel算子_第1张图片

sobel算子

import vtk
reader = vtk.vtkJPEGReader()   # 读入灰度图
reader.SetFileName(r'E:\lena-gray.jpg')
reader.Update()
sobelFilter = vtk.vtkImageSobel2D()
sobelFilter.SetInputConnection(reader.GetOutputPort())  # 包含横向和竖向边缘
# 提取X向边缘成分
extractXFilter = vtk.vtkImageExtractComponents()
extractXFilter.SetComponents(0)   # 提取第一成分即x向梯度
extractXFilter.SetInputConnection(sobelFilter.GetOutputPort())
extractXFilter.Update()

xRange = extractXFilter.GetOutput().GetScalarRange()

xImageAbs = vtk.vtkImageMathematics()
xImageAbs.SetOperationToAbsoluteValue()    # 将属性设为绝对值模式
xImageAbs.SetInputConnection(extractXFilter.GetOutputPort())
xImageAbs.Update()

xShiftScale = vtk.vtkImageShiftScale()   # 将图像的数值范围调节到[0,255]
xShiftScale.SetOutputScalarTypeToUnsignedChar()
xShiftScale.SetScale(255 / xRange[1])
xShiftScale.SetInputConnection(xImageAbs.GetOutputPort())
xShiftScale.Update()
# 提取Y向边缘成分
extractYFilter = vtk.vtkImageExtractComponents()
extractYFilter.SetComponents(1)
extractYFilter.SetInputConnection(sobelFilter.GetOutputPort())
extractYFilter.Update()

yRange = extractYFilter.GetOutput().GetScalarRange()

yImageAbs = vtk.vtkImageMathematics()
yImageAbs.SetOperationToAbsoluteValue()
yImageAbs.SetInputConnection(extractYFilter.GetOutputPort())
yImageAbs.Update()

yShiftScale = vtk.vtkImageShiftScale()
yShiftScale.SetOutputScalarTypeToUnsignedChar()
yShiftScale.SetScale(255 / yRange[1])
yShiftScale.SetInputConnection(yImageAbs.GetOutputPort())
yShiftScale.Update()

originalActor = vtk.vtkImageActor()
originalActor.SetInputData(reader.GetOutput())

xActor = vtk.vtkImageActor()
xActor.SetInputData(xShiftScale.GetOutput())

yActor = vtk.vtkImageActor()
yActor.SetInputData(yShiftScale.GetOutput())

originalViewport = [0.0, 0.0, 0.33, 1.0]
xViewport = [0.33, 0.0, 0.66, 1.0]
yViewport = [0.66, 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)

xRenderer = vtk.vtkRenderer()
xRenderer.SetViewport(xViewport)
xRenderer.AddActor(xActor)
xRenderer.ResetCamera()
xRenderer.SetBackground(1.0, 1.0, 1.0)

yRenderer = vtk.vtkRenderer()
yRenderer.SetViewport(yViewport)
yRenderer.AddActor(yActor)
yRenderer.ResetCamera()
yRenderer.SetBackground(1.0, 1.0, 1.0)

renderWindow = vtk.vtkRenderWindow()
renderWindow.AddRenderer(originalRenderer)
renderWindow.AddRenderer(xRenderer)
renderWindow.AddRenderer(yRenderer)
renderWindow.SetSize(900, 300)
renderWindow.Render()
renderWindow.SetWindowName('ImageSobelExample')

renderWindowInteractor = vtk.vtkRenderWindowInteractor()
style = vtk.vtkInteractorStyleImage()

renderWindowInteractor.SetInteractorStyle(style)
renderWindowInteractor.SetRenderWindow(renderWindow)
renderWindowInteractor.Initialize()
renderWindowInteractor.Start()

python VTK(十三) ----图像边缘检测 梯度算子 sobel算子_第2张图片

你可能感兴趣的:(python,开发语言,后端)