[vtkusers] How to draw histograms?

dean.inglis at camris.ca dean.inglis at camris.ca
Sun Nov 13 10:21:31 EST 2005


Hi liuyang

see tcl script below...
(you may have to change the path to VTK_DATA_ROOT)

Dean
//////////////////////////////////////

package require vtk
package require vtkinteraction

  vtkVolume16Reader v16
  v16 SetDataDimensions 64 64
  v16 SetDataByteOrderToLittleEndian
  v16 SetImageRange 1 93
  v16 SetDataSpacing 3.2 3.2 1.5
  v16 SetFilePrefix "$VTK_DATA_ROOT/Data/headsq/quarter"
  v16 ReleaseDataFlagOff
  v16 SetDataMask 0x7fff

  vtkImageClip clip
  clip SetOutputWholeExtent 31 31 0 63 0 92
  clip SetInput [v16 GetOutput]
  clip ClipDataOn
  clip ReleaseDataFlagOff
  clip Update

  set range [[clip GetOutput] GetScalarRange]
  set min [lindex $range 0]
  set max [lindex $range 1]
  set diff [expr $max - $min]
  set avg [expr 0.5*($max + $min)]
  set bounds [[clip GetOutput] GetBounds]


  vtkImageMapToWindowLevelColors imageMapper
  imageMapper SetOutputFormatToLuminance
  imageMapper SetInput [clip GetOutput]
  imageMapper SetLevel $avg
  imageMapper SetWindow $diff

  vtkImageActor imageActor
  imageActor SetInput [imageMapper GetOutput]
  imageActor SetDisplayExtent 31 31 0 63 0 92
  imageActor InterpolateOff

  vtkImageAccumulate accum
  accum SetInput [clip GetOutput]
  accum SetComponentSpacing 1 0 0
  accum SetComponentExtent 0 $diff 0 0 0 0
  accum SetComponentOrigin $min  0 0
  accum ReleaseDataFlagOff
  accum Update

  set aN [accum GetVoxelCount]
  set amin [accum GetMin]
  set amax [accum GetMax]
  set amean [accum GetMean]
  set astd  [accum GetStandardDeviation]

  set atext {}
  append atext "N: "
  append atext [string toupper $aN]
  append atext ", \["
  append atext [string toupper [lindex $amin 0] ]
  append atext " -> "
  append atext [string toupper [lindex $amax 0] ]
  append atext "\] "
  append atext [string toupper [lindex $amean 0] ]
  append atext " +- "
  append atext [string toupper [lindex $astd 0] ]
  append atext " HU"

  vtkTextActor textActor
  textActor ScaledTextOff
  textActor SetInput $atext
  [textActor GetPositionCoordinate] SetCoordinateSystemToNormalizedDisplay
  [textActor GetPositionCoordinate] SetValue 0.01 0.99

  set tprop [textActor GetTextProperty]
  $tprop SetFontSize 16
  $tprop SetFontFamilyToArial
  $tprop BoldOff
  $tprop ItalicOff
  $tprop ShadowOff
  $tprop SetLineSpacing 0.9
  $tprop SetJustificationToLeft
  $tprop SetVerticalJustificationToTop
  $tprop SetColor 1 1 0

vtkRenderer ren1
   ren1 SetBackground 0.4 0.4 0.5
vtkRenderer ren2
   ren2 SetBackground 0.8 0.1 0.1

vtkRenderWindow renWin
  renWin AddRenderer ren1
  renWin AddRenderer ren2
  renWin SetSize 800 400

ren1 SetViewport 0 0 0.5 1
ren2 SetViewport 0.5 0 1 1

  vtkInteractorStyleImage interactor

  vtkRenderWindowInteractor iren
  iren SetInteractorStyle interactor
  iren SetRenderWindow renWin

  ren1 AddViewProp imageActor
  ren1 AddViewProp textActor


  vtkXYPlotActor plot
  [ plot GetPositionCoordinate ] SetValue 0.05 0.05 0
  [ plot GetPosition2Coordinate ] SetValue 0.95 0.95 0
  plot SetXValuesToValue
  plot SetXRange [lindex $amin 0]  [lindex $amax 0]
  plot AddInput [ accum GetOutput ]  
  plot SetLabelFormat "%g"

  ren2 AddViewProp  plot

  iren AddObserver UserEvent {wm deiconify .vtkInteract}
  renWin Render

  set cam [ren1 GetActiveCamera]
  $cam SetViewUp 0 1 0
  $cam Azimuth 270
  $cam Roll 270
  $cam Dolly 1.7
  ren1 ResetCamera

  renWin Render

# Prevent the tk window from showing up then start the event loop.
  wm withdraw .




More information about the vtkusers mailing list