KWWidgets/Examples/Simple Medical Image Viewer

From KitwarePublic
Jump to: navigation, search

The following Tcl code is a complete example showing how to create a very simple image viewer supporting user-interaction like pan/zoom/window-level and a slice scale. The C++ version would be only slightly longer.

Simple Image Viewer
package require kwwidgets Load the KWWidgets package in Tcl

set app [vtkKWApplication New]
$app RestoreApplicationSettingsFromRegistry
$app SetHelpDialogStartingPage ""

Create an application object, restore previous application settings like the window position, set where the help page should point to

set win [vtkKWWindowBase New]
$app AddWindow $win
$win SupportHelpOn
$win Create

Create a window object, provide a Help menu, let the application know about our window

set rw [vtkKWRenderWidget New]
$rw SetParent [$win GetViewFrame]
$rw Create
$rw CornerAnnotationVisibilityOn
pack [$rw GetWidgetName] -side top -expand y -fill both

Create a render widget object (i.e. a 2D or 3D scene) inside the window, turn on its corner annotation, pack it inside the window

set reader [vtkXMLImageDataReader New]
$reader SetFileName "head100x100x47.vti"

Create a VTK file reader, and point it to our data file.

set viewer [vtkImageViewer2 New]
$viewer SetRenderWindow [$rw GetRenderWindow]
$viewer SetRenderer [$rw GetRenderer]
$viewer SetInput [$reader GetOutput]
$viewer SetupInteractor [[$rw GetRenderWindow] GetInteractor]
$rw ResetCamera

Create a VTK image viewer, set it up so that it uses our render widget, set its input to the output of the reader, reset the camera to center the whole slice

set ca [$rw GetCornerAnnotation]
$ca SetImageActor [$viewer GetImageActor]
$ca SetWindowLevel [$viewer GetWindowLevel]
$ca SetText 2 "<slice>"
$ca SetText 3 "<window>\n<level>"

Retrieve the corner annotation object from the render widget, point it to the viewer actor and window/level filter, and let it display both slice and window/level in different corners using tags

set slice_scale [vtkKWScale New]
$slice_scale SetParent [$win GetViewFrame]
$slice_scale Create
$slice_scale SetRange [$viewer GetSliceMin] [$viewer GetSliceMax]
$slice_scale SetCommand $viewer "SetSlice"
pack [$slice_scale GetWidgetName] -side top -expand n -fill x

Create a scale object in our window, set its range to match the number of slices, set its callback to use its value to set the slice in the viewer, pack the scale in our window below the render widget
$app Start Start the application; this pops up the window and start the event loop, until the user quits

$rw Delete
$reader Delete
$viewer Delete
$slice_scale Delete
$win Delete
$app Delete

Delete our objects
exit Exit Tcl/Tk

KWWidgets: [Welcome | Site Map]