VTK图像遮罩(vtkImageMask)

 

#!/usr/bin/env python
import vtk

reader = vtk.vtkJPEGReader()
reader.SetFileName("Pygoscelis adeliae.jpg")
reader.Update()
extent = reader.GetOutput().GetExtent()  

# Create a rectanglular mask
maskSource = vtk.vtkImageCanvasSource2D()
maskSource.SetScalarTypeToUnsignedChar()
maskSource.SetNumberOfScalarComponents(1)
maskSource.SetExtent(extent)
  
# Initialize the mask to black
maskSource.SetDrawColor(0,0,0);
maskSource.FillBox(extent[0],extent[1],extent[2],extent[3])

# Anything non-zero means "make the output pixel equal the input pixel." 
# If the mask is zero, the output pixel is set to MaskedValue
maskSource.SetDrawColor(255,255,255)
maskSource.FillBox(100,200,100,200)
maskSource.Update()
  
mask = vtk.vtkImageMask()
mask.SetImageInputData(reader.GetOutput())
mask.SetMaskInputData(maskSource.GetOutput())
# mask.SetMaskedOutputValue(100,128,200)
mask.NotMaskOn()
mask.Update()
  
imageActor = vtk.vtkImageActor()
imageActor.GetMapper().SetInputConnection(mask.GetOutputPort())
render = vtk.vtkRenderer()
render.AddActor(imageActor)
render.SetBackground(.4, .5, .6)
renderWindow = vtk.vtkRenderWindow()
renderWindow.SetSize(600, 400)
renderWindow.AddRenderer(render)
renderWindow.Render()
renderWindowInteractor = vtk.vtkRenderWindowInteractor()
style = vtk.vtkInteractorStyleImage()
renderWindowInteractor.SetInteractorStyle(style)
renderWindowInteractor.SetRenderWindow(renderWindow)
renderWindowInteractor.Initialize()
renderWindowInteractor.Start()

  

 

 

 

参考:

vtkImageMask Class Reference

VTK/Examples/Cxx/Images/ImageMask

Testing/Python/TestMask2.py

posted @ 2019-11-06 13:05  XXX已失联  阅读(1521)  评论(0编辑  收藏  举报