1 // This example illustrates how the vtkGdcmReader vtk class can be
3 // * produce a simple (vtk based) Dicom image STACK VIEWER.
4 // * dump the stack considered as a volume in a vtkStructuredPoints
5 // vtk file: the vtk gdcm wrappers can be seen as a simple way to convert
6 // a stack of Dicom images into a native vtk volume.
9 // * the filenames of the Dicom images constituting the stack should be
10 // given as command line arguments,
11 // * you can navigate through the stack by hitting any character key,
12 // * the produced vtk file is named "foo.vtk" (in the invocation directory).
14 //----------------------------------------------------------------------------
17 #include <vtkRenderWindowInteractor.h>
18 #include <vtkImageViewer2.h>
19 #include <vtkStructuredPoints.h>
20 #include <vtkStructuredPointsWriter.h>
21 #include <vtkCommand.h>
22 #include <vtkRenderer.h>
23 #include <vtkImageMapToColors.h>
24 #include <vtkLookupTable.h>
26 #include "vtkGdcmReader.h"
28 #ifndef vtkFloatingPointType
29 #define vtkFloatingPointType float
32 //----------------------------------------------------------------------------
33 // Callback for the interaction
34 class vtkgdcmObserver : public vtkCommand
37 virtual char const *GetClassName() const
39 return "vtkgdcmObserver";
41 static vtkgdcmObserver *New()
43 return new vtkgdcmObserver;
47 this->ImageViewer = NULL;
49 virtual void Execute(vtkObject *, unsigned long event, void* )
51 if ( this->ImageViewer )
53 if ( event == vtkCommand::CharEvent )
55 int max = ImageViewer->GetWholeZMax();
56 int slice = (ImageViewer->GetZSlice() + 1 ) % ++max;
57 ImageViewer->SetZSlice( slice );
58 ImageViewer->GetRenderer()->ResetCameraClippingRange();
59 ImageViewer->Render();
63 vtkImageViewer2 *ImageViewer;
67 int main(int argc, char *argv[])
72 vtkGdcmReader *reader = vtkGdcmReader::New();
73 reader->AllowLookupTableOff();
76 reader->SetFileName( argv[1] );
78 for(int i=1; i< argc; i++)
79 reader->AddFileName( argv[i] );
84 reader->GetOutput()->Print( cout );
86 vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
88 vtkImageViewer2 *viewer = vtkImageViewer2::New();
90 if( reader->GetLookupTable() )
93 vtkImageMapToColors *map = vtkImageMapToColors::New ();
94 map->SetInput (reader->GetOutput());
95 map->SetLookupTable (reader->GetLookupTable());
96 map->SetOutputFormatToRGB();
97 viewer->SetInput ( map->GetOutput() );
102 double *range = reader->GetOutput()->GetScalarRange();
103 viewer->SetColorLevel (0.5 * (range[1] + range[0]));
104 viewer->SetColorWindow (range[1] - range[0]);
106 viewer->SetInput ( reader->GetOutput() );
108 viewer->SetupInteractor (iren);
110 //vtkFloatingPointType *range = reader->GetOutput()->GetScalarRange();
111 //viewer->SetColorWindow (range[1] - range[0]);
112 //viewer->SetColorLevel (0.5 * (range[1] + range[0]));
114 // Here is where we setup the observer,
115 vtkgdcmObserver *obs = vtkgdcmObserver::New();
116 obs->ImageViewer = viewer;
117 iren->AddObserver(vtkCommand::CharEvent,obs);
123 //if you wish you can export dicom to a vtk file
124 vtkStructuredPointsWriter *writer = vtkStructuredPointsWriter::New();
125 writer->SetInput( reader->GetOutput());
126 writer->SetFileName( "foo.vtk" );
127 writer->SetFileTypeToBinary();