Skip to content
Snippets Groups Projects
Commit 22c8dd6e authored by Andrew Maclean's avatar Andrew Maclean
Browse files

Adding a Medical and Modelling example

parent e81e3247
No related branches found
No related tags found
1 merge request!327Add pythonic api examples 1
Pipeline #393827 canceled
......@@ -133,6 +133,11 @@ This Python script, [SelectExamples](../Python/Utilities/SelectExamples), will l
This section includes examples of manipulating meshes.
| Example Name | Description | Image |
| -------------- | ------------- | ------- |
[DelaunayMesh](/PythonicAPI/Modelling/DelaunayMesh) | Two-dimensional Delaunay triangulation of a random set of points. Points and edges are shown highlighted with sphere glyphs and tubes.
#### Clipping
| Example Name | Description | Image |
......@@ -147,7 +152,6 @@ This section includes examples of manipulating meshes.
| -------------- | ------------- | ------- |
[ImageWeightedSum](/PythonicAPI/ImageData/ImageWeightedSum) | Add two or more images.
#### ?vtkExplicitStructuredGrid?
| Example Name | Description | Image |
......@@ -171,6 +175,11 @@ This section includes ?vtkUnstructuredGrid?.
### Medical
| Example Name | Description | Image |
| -------------- | ------------- | ------- |
[MedicalDemo1](/PythonicAPI/Medical/MedicalDemo1) | Create a skin surface from volume data.
### Surface reconstruction
## Utilities
......
......@@ -210,7 +210,7 @@ def main():
mask_pts = vtkMaskPoints(random_mode=True, maximum_number_of_points=150)
arrow = vtkArrowSource(tip_resolution=16, tip_length=0.3, tip_radius=0.1)
glyph = vtkGlyph3D(source_connection=arrow.GetOutputPort(),
glyph = vtkGlyph3D(source_connection=arrow.output_port(),
vector_mode=glyph_vector_mode['use_normal'], orient=True,
scale_factor=get_maximum_length(bounds) / 10.0)
......
### Description
The skin extracted from a CT dataset of the head.
!!! example "usage"
MedicalDemo1 FullHead.mhd
!!! note
The skin color was selected from Table 7 in [Improvement of Haar Feature Based Face Detection in OpenCV Incorporating Human Skin Color Characteristic](https://www.researchgate.net/publication/310443424_Improvement_of_Haar_Feature_Based_Face_Detection_in_OpenCV_Incorporating_Human_Skin_Color_Characteristic)
!!! note
This original source code for this example is [here](https://gitlab.kitware.com/vtk/vtk/blob/395857190c8453508d283958383bc38c9c2999bf/Examples/Medical/Cxx/Medical1.cxx).
!!! info
See [Figure 12-2](../../../VTKBook/12Chapter12/#Figure%2012-2) in [Chapter 12](../../../VTKBook/12Chapter12) the [VTK Textbook](../../../VTKBook/01Chapter1).
!!! info
The example uses `src/Testing/Data/FullHead.mhd` which references `src/Testing/Data/FullHead.raw.gz`.
#!/usr/bin/env python3
# noinspection PyUnresolvedReferences
import vtkmodules.vtkInteractionStyle
# noinspection PyUnresolvedReferences
import vtkmodules.vtkRenderingOpenGL2
from vtkmodules.vtkCommonColor import vtkNamedColors
from vtkmodules.vtkFiltersCore import (
vtkFlyingEdges3D,
vtkMarchingCubes
)
from vtkmodules.vtkFiltersModeling import vtkOutlineFilter
from vtkmodules.vtkIOImage import vtkMetaImageReader
from vtkmodules.vtkRenderingCore import (
vtkActor,
vtkCamera,
vtkPolyDataMapper,
vtkProperty,
vtkRenderWindow,
vtkRenderWindowInteractor,
vtkRenderer
)
def main():
colors = vtkNamedColors()
file_name, use_flying_edges = get_program_parameters()
colors.SetColor('SkinColor', 240, 184, 160, 255)
colors.SetColor('BackfaceColor', 255, 229, 200, 255)
colors.SetColor('BkgColor', 51, 77, 102, 255)
# Create the renderer, the render window, and the interactor. The renderer
# draws into the render window, the interactor enables mouse- and
# keyboard-based interaction with the data within the render window.
#
# Set a background color for the renderer and set the name and
# size of the render window (expressed in pixels).
ren = vtkRenderer(background=colors.GetColor3d('BkgColor'))
ren_win = vtkRenderWindow(size=(640, 480), window_name='MedicalDemo1')
ren_win.AddRenderer(ren)
iren = vtkRenderWindowInteractor()
iren.SetRenderWindow(ren_win)
reader = vtkMetaImageReader(file_name=file_name)
# An isosurface, or contour value of 500 is known to correspond to the
# skin of the patient.
if use_flying_edges:
skin_extractor = vtkFlyingEdges3D(value=(0, 500))
else:
skin_extractor = vtkMarchingCubes(value=(0, 500))
skin_mapper = vtkPolyDataMapper(scalar_visibility=False)
reader >> skin_extractor >> skin_mapper
skin = vtkActor(mapper=skin_mapper)
skin.SetMapper(skin_mapper)
skin.property.diffuse_color = colors.GetColor3d('SkinColor')
back_prop = vtkProperty()
back_prop.diffuse_color = colors.GetColor3d('BackfaceColor')
skin.backface_property = back_prop
# An outline provides context around the data.
#
outline_data = vtkOutlineFilter()
map_outline = vtkPolyDataMapper()
reader >> outline_data >> map_outline
outline = vtkActor(mapper=map_outline)
outline.property.color = colors.GetColor3d('Black')
# It is convenient to create an initial view of the data. The FocalPoint
# and Position form a vector direction. Later on (ResetCamera() method)
# this vector is used to position the camera to look at the data in
# this direction.
camera = vtkCamera(view_up=(0, 0, -1), position=(0, -1, 0), focal_point=(0, 0, 0))
camera.ComputeViewPlaneNormal()
camera.Azimuth(30.0)
camera.Elevation(30.0)
# Actors are added to the renderer. An initial camera view is created.
# The Dolly() method moves the camera towards the FocalPoint,
# thereby enlarging the image.
ren.AddActor(outline)
ren.AddActor(skin)
ren.SetActiveCamera(camera)
ren.ResetCamera()
camera.Dolly(1.5)
# Note that when camera movement occurs (as it does in the Dolly()
# method), the clipping planes often need adjusting. Clipping planes
# consist of two planes: near and far along the view direction. The
# near plane clips out objects in front of the plane the far plane
# clips out objects behind the plane. This way only what is drawn
# between the planes is actually rendered.
ren.ResetCameraClippingRange()
# Initialize the event loop and then start it.
iren.Initialize()
iren.Start()
def get_program_parameters():
import argparse
description = 'The skin extracted from a CT dataset of the head.'
epilogue = '''
Derived from VTK/Examples/Cxx/Medical1.cxx
This example reads a volume dataset, extracts an isosurface that
represents the skin and displays it.
'''
parser = argparse.ArgumentParser(description=description, epilog=epilogue,
formatter_class=argparse.RawDescriptionHelpFormatter)
parser.add_argument('filename', help='FullHead.mhd.')
parser.add_argument('-m', '--marching_cubes', action='store_false',
help='Use Marching Cubes instead of Flying Edges.')
args = parser.parse_args()
return args.filename, args.marching_cubes
if __name__ == '__main__':
main()
### Description
This is two dimensional Delaunay triangulation of a random set of points. Points and edges are shown highlighted with spheres and tubes.
!!! info
See [Figure 9-54](../../../VTKBook/09Chapter9/#Figure%209-54) in [Chapter 9](../../../VTKBook/09Chapter9) The [VTK Textbook](../../../VTKBook/01Chapter1).
#!/usr/bin/env python3
"""
This code is based on the VTK file: Examples/Modelling/Tcl/DelMesh.py.
This example demonstrates how to use 2D Delaunay triangulation.
We create a fancy image of a 2D Delaunay triangulation. Points are
randomly generated.
"""
# noinspection PyUnresolvedReferences
import vtkmodules.vtkInteractionStyle
# noinspection PyUnresolvedReferences
import vtkmodules.vtkRenderingOpenGL2
from vtkmodules.vtkCommonColor import vtkNamedColors
from vtkmodules.vtkCommonCore import (
vtkMinimalStandardRandomSequence,
vtkPoints
)
from vtkmodules.vtkCommonDataModel import vtkPolyData
from vtkmodules.vtkFiltersCore import (
vtkDelaunay2D,
vtkGlyph3D,
vtkTubeFilter
)
# vtkExtractEdges moved from vtkFiltersExtraction to vtkFiltersCore in
# VTK commit d9981b9aeb93b42d1371c6e295d76bfdc18430bd
try:
from vtkmodules.vtkFiltersCore import vtkExtractEdges
except ImportError:
from vtkmodules.vtkFiltersExtraction import vtkExtractEdges
from vtkmodules.vtkFiltersSources import vtkSphereSource
from vtkmodules.vtkRenderingCore import (
vtkActor,
vtkPolyDataMapper,
vtkRenderWindow,
vtkRenderWindowInteractor,
vtkRenderer
)
def main():
colors = vtkNamedColors()
# Generate some "random" points.
points = vtkPoints()
random_sequence = vtkMinimalStandardRandomSequence(seed=1)
for i in range(0, 50):
p1 = random_sequence.GetValue()
random_sequence.Next()
p2 = random_sequence.GetValue()
random_sequence.Next()
points.InsertPoint(i, p1, p2, 0.0)
# Create a polydata with the points we just created.
profile = vtkPolyData(points=points)
# Perform a 2D Delaunay triangulation on them.
delny = vtkDelaunay2D(tolerance=0.001)
map_mesh = vtkPolyDataMapper()
profile >> delny >> map_mesh
mesh_actor = vtkActor(mapper=map_mesh)
mesh_actor.property.color = colors.GetColor3d('MidnightBlue')
# We will now create a nice looking mesh by wrapping the edges in tubes,
# and putting fat spheres at the points.
extract = vtkExtractEdges()
tubes = vtkTubeFilter(radius=0.01, number_of_sides=6)
tubes.SetInputConnection(extract.GetOutputPort())
map_edges = vtkPolyDataMapper()
map_edges.SetInputConnection(tubes.GetOutputPort())
delny >> extract >> tubes >> map_edges
edge_actor = vtkActor(mapper=map_edges)
edge_actor.property.color = colors.GetColor3d('peacock')
edge_actor.property.specular_color = (1, 1, 1)
edge_actor.property.specular = 0.3
edge_actor.property.specular_power = 20
edge_actor.property.ambient = 0.2
edge_actor.property.diffuse = 0.8
ball = vtkSphereSource(radius=0.025, theta_resolution=12, phi_resolution=12)
balls = vtkGlyph3D(source_connection=ball.output_port)
map_balls = vtkPolyDataMapper()
delny >> balls >> map_balls
ball_actor = vtkActor(mapper=map_balls)
ball_actor.property.color = colors.GetColor3d('hot_pink')
ball_actor.property.specular_color = (1, 1, 1)
ball_actor.property.specular = 0.3
ball_actor.property.specular_power = 20
ball_actor.property.ambient = 0.2
ball_actor.property.diffuse = 0.8
# Create the rendering window, renderer, and interactive renderer.
ren = vtkRenderer(background=colors.GetColor3d('AliceBlue'))
ren_win = vtkRenderWindow(size=(512, 512), window_name='DelaunayMesh')
ren_win.AddRenderer(ren)
iren = vtkRenderWindowInteractor()
iren.SetRenderWindow(ren_win)
# Add the actors to the renderer, set the background and size.
ren.AddActor(ball_actor)
ren.AddActor(edge_actor)
ren.ResetCamera()
ren.GetActiveCamera().Zoom(1.3)
# Interact with the data.
iren.Initialize()
ren_win.Render()
iren.Start()
if __name__ == '__main__':
main()
src/Testing/Baseline/PythonicAPI/Medical/TestMedicalDemo1.png

131 B

src/Testing/Baseline/PythonicAPI/Modelling/TestDelaunayMesh.png

131 B

0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment