ViewFrogBoth
VTKEx/Python/Visualization/ViewFrogBoth
Other languages
See (Cxx)
Question
If you have a simple question about this example contact us at VTKExProject If your question is more complex and may require extended discussion, please use the VTK Discourse Forum
Code¶
ViewFrogBoth.py
#!/usr/bin/env python
"""
"""
import vtk
def main():
colors = vtk.vtkNamedColors()
fileName, tissue = get_program_parameters()
tissueMap = CreateTissueMap()
colorLut = CreateFrogLut()
# Setup render window, renderer, and interactor.
rendererLeft = vtk.vtkRenderer()
rendererLeft.SetViewport(0, 0, .5, 1)
rendererRight = vtk.vtkRenderer()
rendererRight.SetViewport(.5, 0, 1, 1)
renderWindow = vtk.vtkRenderWindow()
renderWindow.AddRenderer(rendererLeft)
renderWindow.AddRenderer(rendererRight)
renderWindowInteractor = vtk.vtkRenderWindowInteractor()
renderWindowInteractor.SetRenderWindow(renderWindow)
actor = CreateFrogActor(fileName, tissueMap[tissue])
actor.GetProperty().SetDiffuseColor(colorLut.GetTableValue(tissueMap[tissue])[:3])
rendererLeft.AddActor(actor)
actorSmooth = CreateSmoothFrogActor(fileName, tissueMap[tissue])
actorSmooth.GetProperty().SetDiffuseColor(colorLut.GetTableValue(tissueMap[tissue])[:3])
actorSmooth.GetProperty().SetDiffuse(1.0)
actorSmooth.GetProperty().SetSpecular(.5)
actorSmooth.GetProperty().SetSpecularPower(100)
rendererRight.AddActor(actorSmooth)
rendererLeft.ResetCamera()
rendererLeft.GetActiveCamera().SetViewUp(-1, 0, 0)
rendererLeft.GetActiveCamera().Azimuth(180)
rendererLeft.ResetCameraClippingRange()
rendererLeft.SetBackground(colors.GetColor3d("SlateGray"))
rendererRight.SetBackground(colors.GetColor3d("SlateGray"))
rendererRight.SetActiveCamera(rendererLeft.GetActiveCamera())
renderWindow.SetSize(640, 480)
renderWindow.Render()
renderWindowInteractor.Start()
def get_program_parameters():
import argparse
description = 'The frog’s brain.'
epilogue = '''
Model extracted without smoothing (left) and with smoothing (right).
'''
parser = argparse.ArgumentParser(description=description, epilog=epilogue,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('filename', help='frog.mhd.')
parser.add_argument('tissue', default='brain', nargs='?', help='The tissue to use.')
args = parser.parse_args()
return args.filename, args.tissue
def CreateFrogLut():
colors = vtk.vtkNamedColors()
colorLut = vtk.vtkLookupTable()
colorLut.SetNumberOfColors(17)
colorLut.SetTableRange(0, 16)
colorLut.Build()
colorLut.SetTableValue(0, 0, 0, 0, 0)
colorLut.SetTableValue(1, colors.GetColor4d("salmon")) # blood
colorLut.SetTableValue(2, colors.GetColor4d("beige")) # brain
colorLut.SetTableValue(3, colors.GetColor4d("orange")) # duodenum
colorLut.SetTableValue(4, colors.GetColor4d("misty_rose")) # eye_retina
colorLut.SetTableValue(5, colors.GetColor4d("white")) # eye_white
colorLut.SetTableValue(6, colors.GetColor4d("tomato")) # heart
colorLut.SetTableValue(7, colors.GetColor4d("raspberry")) # ileum
colorLut.SetTableValue(8, colors.GetColor4d("banana")) # kidney
colorLut.SetTableValue(9, colors.GetColor4d("peru")) # l_intestine
colorLut.SetTableValue(10, colors.GetColor4d("pink")) # liver
colorLut.SetTableValue(11, colors.GetColor4d("powder_blue")) # lung
colorLut.SetTableValue(12, colors.GetColor4d("carrot")) # nerve
colorLut.SetTableValue(13, colors.GetColor4d("wheat")) # skeleton
colorLut.SetTableValue(14, colors.GetColor4d("violet")) # spleen
colorLut.SetTableValue(15, colors.GetColor4d("plum")) # stomach
return colorLut
def CreateTissueMap():
tissueMap = dict()
tissueMap["blood"] = 1
tissueMap["brain"] = 2
tissueMap["duodenum"] = 3
tissueMap["eyeRetina"] = 4
tissueMap["eyeWhite"] = 5
tissueMap["heart"] = 6
tissueMap["ileum"] = 7
tissueMap["kidney"] = 8
tissueMap["intestine"] = 9
tissueMap["liver"] = 10
tissueMap["lung"] = 11
tissueMap["nerve"] = 12
tissueMap["skeleton"] = 13
tissueMap["spleen"] = 14
tissueMap["stomach"] = 15
return tissueMap
def CreateSmoothFrogActor(fileName, tissue):
reader = vtk.vtkMetaImageReader()
reader.SetFileName(fileName)
reader.Update()
selectTissue = vtk.vtkImageThreshold()
selectTissue.ThresholdBetween(tissue, tissue)
selectTissue.SetInValue(255)
selectTissue.SetOutValue(0)
selectTissue.SetInputConnection(reader.GetOutputPort())
gaussianRadius = 1
gaussianStandardDeviation = 2.0
gaussian = vtk.vtkImageGaussianSmooth()
gaussian.SetStandardDeviations(gaussianStandardDeviation, gaussianStandardDeviation, gaussianStandardDeviation)
gaussian.SetRadiusFactors(gaussianRadius, gaussianRadius, gaussianRadius)
gaussian.SetInputConnection(selectTissue.GetOutputPort())
isoValue = 127.5
mcubes = vtk.vtkMarchingCubes()
mcubes.SetInputConnection(gaussian.GetOutputPort())
mcubes.ComputeScalarsOff()
mcubes.ComputeGradientsOff()
mcubes.ComputeNormalsOff()
mcubes.SetValue(0, isoValue)
smoothingIterations = 0
passBand = 0.001
featureAngle = 60.0
smoother = vtk.vtkWindowedSincPolyDataFilter()
smoother.SetInputConnection(mcubes.GetOutputPort())
smoother.SetNumberOfIterations(smoothingIterations)
smoother.BoundarySmoothingOff()
smoother.FeatureEdgeSmoothingOff()
smoother.SetFeatureAngle(featureAngle)
smoother.SetPassBand(passBand)
smoother.NonManifoldSmoothingOn()
smoother.NormalizeCoordinatesOn()
smoother.Update()
normals = vtk.vtkPolyDataNormals()
normals.SetInputConnection(smoother.GetOutputPort())
normals.SetFeatureAngle(featureAngle)
stripper = vtk.vtkStripper()
stripper.SetInputConnection(normals.GetOutputPort())
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(stripper.GetOutputPort())
actor = vtk.vtkActor()
actor.SetMapper(mapper)
return actor
def CreateFrogActor(fileName, tissue):
reader = vtk.vtkMetaImageReader()
reader.SetFileName(fileName)
reader.Update()
selectTissue = vtk.vtkImageThreshold()
selectTissue.ThresholdBetween(tissue, tissue)
selectTissue.SetInValue(255)
selectTissue.SetOutValue(0)
selectTissue.SetInputConnection(reader.GetOutputPort())
isoValue = 63.5
mcubes = vtk.vtkMarchingCubes()
mcubes.SetInputConnection(selectTissue.GetOutputPort())
mcubes.ComputeScalarsOff()
mcubes.ComputeGradientsOff()
mcubes.ComputeNormalsOn()
mcubes.SetValue(0, isoValue)
stripper = vtk.vtkStripper()
stripper.SetInputConnection(mcubes.GetOutputPort())
mapper = vtk.vtkPolyDataMapper()
mapper.SetInputConnection(stripper.GetOutputPort())
actor = vtk.vtkActor()
actor.SetMapper(mapper)
return actor
if __name__ == '__main__':
main()