1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152
|
#!/usr/bin/env python
import vtk
from vtk import *
import sys, os
#import Tkinter
#from Tkinter import *
if sys.hexversion < 0x03000000:
# for Python2
import Tkinter as tkinter
from Tkinter import *
else:
# for Python3
import tkinter
from tkinter import *
import vtk.tk
import vtk.tk.vtkLoadPythonTkWidgets
import vtk.tk.vtkTkImageViewerWidget
from vtk.tk.vtkTkPhotoImage import *
from vtk.util.misc import *
class SampleViewer:
def __init__ ( self ):
self.Tk = Tk = tkinter.Tk();
Tk.title ( 'Python Version of vtkImageDataToTkPhoto' );
# Image pipeline
reader = vtkVolume16Reader ()
reader.SetDataDimensions ( 64, 64 )
reader.SetDataByteOrderToLittleEndian ( )
reader.SetFilePrefix ( vtkGetDataRoot() + '/Data/headsq/quarter' )
reader.SetImageRange ( 1, 93 )
reader.SetDataSpacing ( 3.2, 3.2, 1.5 )
reader.Update ()
self.cast = cast = vtkImageCast()
cast.SetInputConnection( reader.GetOutputPort() )
cast.SetOutputScalarType ( reader.GetOutput().GetScalarType() )
cast.ClampOverflowOn()
# Make the image a little bigger
self.resample = resample = vtkImageResample ()
resample.SetInputConnection( cast.GetOutputPort() )
resample.SetAxisMagnificationFactor ( 0, 2 )
resample.SetAxisMagnificationFactor ( 1, 2 )
resample.SetAxisMagnificationFactor ( 2, 1 )
l,h = reader.GetOutput().GetScalarRange()
# Create the three orthogonal views
tphoto = self.tphoto = self.tphoto = vtkTkPhotoImage ();
cphoto = self.cphoto = vtkTkPhotoImage ();
sphoto = self.sphoto = vtkTkPhotoImage ();
reader.Update()
d = reader.GetOutput().GetDimensions()
self.Position = [ int(d[0]/2.0), int(d[0]/2.0), int(d[0]/2.0) ]
# Create a popup menu
v = IntVar()
self.popup = popup = Menu ( Tk, tearoff=0 )
popup.add_radiobutton ( label='unsigned char', command=self.CastToUnsignedChar, variable=v, value=-1 )
popup.add_radiobutton ( label='unsigned short', command=self.CastToUnsignedShort, variable=v, value=0 )
popup.add_radiobutton ( label='unsigned int', command=self.CastToFloat, variable=v, value=1 )
popup.add_radiobutton ( label='float', command=self.CastToFloat, variable=v, value=2 )
v.set ( 0 )
w = self.TransverseLabelWidget = Label ( Tk, image = tphoto )
w.grid ( row = 0, column = 0 )
w.bind ( "<Button1-Motion>", lambda e, i=tphoto, o='transverse', s=self: s.Motion ( e, i, o ) )
w.bind ( "<Button-3>", self.DoPopup )
w = Label ( Tk, image = cphoto )
w.grid ( row = 1, column = 0 )
w.bind ( "<Button1-Motion>", lambda e, i=cphoto, o='coronal', s=self: s.Motion ( e, i, o ) )
w.bind ( "<Button-3>", self.DoPopup )
w = Label ( Tk, image = sphoto )
w.grid ( row = 0, column = 1 )
w.bind ( "<Button1-Motion>", lambda e, i=sphoto, o='sagittal', s=self: s.Motion ( e, i, o ) )
w.bind ( "<Button-3>", self.DoPopup )
w = self.WindowWidget = Scale ( Tk, label='Window', orient='horizontal', from_=1, to=(h-l)/2, command = self.SetWindowLevel )
w = self.LevelWidget = Scale ( Tk, label='Level', orient='horizontal', from_=l, to=h, command=self.SetWindowLevel )
self.WindowWidget.grid ( row=2, columnspan=2, sticky='ew' )
self.LevelWidget.grid ( row=3, columnspan=2, sticky='ew' );
self.WindowWidget.set ( 1370 );
self.LevelWidget.set ( 1268 );
w = self.LabelWidget = Label ( Tk, bd=2, relief='raised' )
w.grid ( row=4, columnspan=2, sticky='ew' )
w.configure ( text = "Use the right mouse button to change data type" )
def DoPopup ( self, event ):
self.popup.post ( event.x_root, event.y_root )
def CastToUnsignedChar ( self ):
self.cast.SetOutputScalarTypeToUnsignedChar()
self.SetImages()
def CastToUnsignedShort ( self ):
self.cast.SetOutputScalarTypeToUnsignedShort()
self.SetImages()
def CastToUnsignedInt ( self ):
self.cast.SetOutputScalarTypeToUnsignedInt()
self.SetImages()
def CastToFloat ( self ):
self.cast.SetOutputScalarTypeToFloat()
self.SetImages()
def Motion ( self, event, image, orientation ):
w = image.width();
h = image.height()
if orientation == 'transverse':
self.Position[0] = event.x
self.Position[1] = h - event.y - 1
if orientation == 'coronal':
self.Position[0] = event.x;
self.Position[2] = event.y
if orientation == 'sagittal':
self.Position[1] = w - event.x - 1
self.Position[2] = event.y
self.LabelWidget.configure ( text = "Position: %d, %d, %d" % tuple ( self.Position ) )
self.SetImages()
def SetWindowLevel ( self, event ):
self.SetImages()
def SetImages ( self ):
Window = self.WindowWidget.get()
Level = self.LevelWidget.get()
image = self.resample.GetOutput()
self.tphoto.PutImageSlice ( self.resample.GetOutputPort(),
self.Position[2],
'transverse',
Window,
Level )
self.sphoto.PutImageSlice ( self.resample.GetOutputPort(),
self.Position[0],
'sagittal',
Window,
Level )
self.cphoto.PutImageSlice ( self.resample.GetOutputPort(),
self.Position[1],
'coronal',
Window,
Level )
if __name__ == '__main__':
S = SampleViewer()
S.Tk.mainloop()
|