[vtkusers] new path tracing classes in VTK
    Dean Inglis 
    dean.inglis at camris.ca
       
    Sat Aug 23 23:25:49 EDT 2008
    
    
  
Hi,
There are two new classes in VTK that enable one to interactively
trace out paths on images, similar to what can be done with livewire.
There is now vtkDijkstraImageGeodesicPath.cxx in VTK/Graphics
which inherits from vtkDijkstraGeodesicPath and a 
line interpolator for vtkContourRepresentation:
vtkDijkstraImageContourLineInterpolator, so that vertices can
be placed and interacted with.  I modified vtkDijkstraGeodesicPath 
to use vtkDataSet in some method signatures instead of vtkPolyData 
to re-use most of the core dijkstra algorithm.  Because
vtkDijkstraGeodesicPath indirectly inherits from vtkPolyDataAlgorithm
I had to mandate a second input port for the cost image, which
is what is actually used to build the adjacency list and calculate
edge costs.  So, the first input port which takes vtkPolyData is
currently not used (although maybe could be used for path cooling etc.)
and perhaps a better class hierarchy needs to be considered.
I will submit a test for these classes in VTK/Widgets shortly,
but for now, enclosed is a tcl script that demonstrates use.
enjoy,
Dean
package require vtk
package require vtkinteraction
  vtkPNGReader reader
  reader SetFileName "$VTK_DATA_ROOT/Data/fullhead15.png"
 # size the image in half
 #
  vtkImageResample resample
  resample SetInputConnection [reader GetOutputPort]
  resample SetDimensionality 2
  resample SetAxisMagnificationFactor 0 0.5
  resample SetAxisMagnificationFactor 1 0.5
# smooth the image
#
  vtkImageAnisotropicDiffusion2D diffusion
  diffusion SetInputConnection [resample GetOutputPort]
  diffusion SetDiffusionFactor 1.0
  diffusion SetDiffusionThreshold 200.0
  diffusion SetNumberOfIterations 5
 
# gradient magnitude to detect edges
# 
  vtkImageGradientMagnitude grad
  grad SetDimensionality 2
  grad HandleBoundariesOn
  grad SetInputConnection [diffusion GetOutputPort]
  grad Update
  
  set grange [[grad GetOutput] GetScalarRange]
  
# invert so that edges are low intensity and normalized to 0 <> 1
#
  vtkImageShiftScale gradInvert
  gradInvert SetShift [ expr -1.0*[lindex $grange 1] ]
  gradInvert SetScale [ expr 1.0 /( [lindex $grange 0] - [lindex $grange 1]
) ]
  gradInvert SetOutputScalarTypeToFloat
  gradInvert SetInputConnection [ grad GetOutputPort ]
  gradInvert ReleaseDataFlagOn
  gradInvert Update
 
# vtkImageActor needs unsigned char
# 
  vtkImageShiftScale shifter
  shifter SetShift [ expr -1.0*[lindex $grange 1] ]
  shifter SetScale [ expr 255.0 /( [lindex $grange 0] - [lindex $grange 1] )
]
  shifter SetOutputScalarTypeToUnsignedChar
  shifter SetInputConnection [ grad GetOutputPort ]
  shifter ReleaseDataFlagOn
  shifter Update  
  
  vtkImageActor imageActor
  imageActor SetInput [shifter GetOutput]
  imageActor VisibilityOn
  imageActor SetDisplayExtent 0 127 0 127 0 0
  imageActor InterpolateOff 
  
  #  Create the RenderWindow,  Renderer and the image actor
 
  vtkRenderer ren1
  vtkRenderWindow renWin
  renWin AddRenderer ren1
  vtkRenderWindowInteractor iren
  iren SetRenderWindow renWin
  vtkInteractorStyleImage style
  iren SetInteractorStyle style
  ren1 SetBackground 0.1 0.2 0.4
  #  Add the actors to the renderer  set the background and size
  ren1 AddActor imageActor
  ren1 ResetCameraClippingRange
  #  Here comes the contour widget stuff.....
  
  vtkContourWidget contourWidget
  contourWidget DebugOff
  contourWidget SetInteractor iren
  
  vtkOrientedGlyphContourRepresentation rep
  [rep GetLinesProperty ] SetColor 1 0.2 0
  [rep GetProperty ] SetColor 0 0.2 1
  [rep GetLinesProperty ] SetLineWidth 3.0  
  contourWidget SetRepresentation rep
  vtkImageActorPointPlacer placer
  placer SetImageActor imageActor  
  rep SetPointPlacer  placer 
 vtkDijkstraImageContourLineInterpolator interpolator
 interpolator SetCostImage [ gradInvert GetOutput ]  
 rep SetLineInterpolator interpolator
  contourWidget On
# render the image
#
iren AddObserver UserEvent {wm deiconify .vtkInteract}
iren Initialize
# prevent the tk window from showing up then start the event loop
wm withdraw .
_______________________________________________
vtk-developers mailing list
vtk-developers at vtk.org
http://www.vtk.org/mailman/listinfo/vtk-developers
    
    
More information about the vtkusers
mailing list