VtkGdmReader examples

Below you will find, by order of complexity, two examples that illustate how the vtkGdcmReader vtk class can be used. Basically, vtkGdcmReader behaves like any other derived class of vtkImageReader...
// $Header: /cvs/public/gdcm/vtk/vtkGdcmDemo.cxx,v 1.2 2004/11/09 11:21:33 regrain Exp $

//----------------------------------------------------------------------------
// A simple straightfoward example of vtkGdcmReader vtk class usage.
//
// The vtkGdcmReader vtk class behaves like any other derived class of
// vtkImageReader. It's usage within a vtk pipeline hence follows the
// classical vtk pattern.
// This example is a really simple Dicom image viewer demo.
// It builds the minimal vtk rendering pipeline in order to display
// (with the native vtk classes) a single Dicom image parsed with gdcm.
//
// Usage: the filename of the Dicom image to display should be given as
//        command line arguments,
//----------------------------------------------------------------------------

#include <vtkRenderer.h>
#include <vtkRenderWindow.h>
#include <vtkRenderWindowInteractor.h>
#include <vtkPolyDataMapper.h>
#include <vtkActor.h>
#include <vtkImageMapper.h>
#include <vtkImageData.h>
#include <vtkImageViewer.h>
#include <vtkMatrix4x4.h>
#include <vtkLookupTable.h>
#include <vtkMatrixToLinearTransform.h>
#include <vtkTexture.h>
#include <vtkPlaneSource.h>
#include <vtkTextureMapToPlane.h>
#include <vtkDataSetMapper.h>
#include <vtkActor.h>
#include <vtkImageCast.h>
#include <vtkPNGWriter.h>
#include <vtkTexture.h>

#include "vtkGdcmReader.h"

  
int main( int argc, char *argv[] )
{
   vtkGdcmReader *reader = vtkGdcmReader::New();

   if (argc < 2)
   {
      cerr << "Usage: " << argv[0] << " image.dcm\n";
      return 0;
   }

   reader->SetFileName( argv[1] );

   reader->UpdateWholeExtent();
   vtkImageData* ima = reader->GetOutput();

   int* Size = ima->GetDimensions();
   cout << "Dimensions of the picture as read with gdcm: "
        << Size[0] << " x " << Size[1] << endl;

   // 
   vtkLookupTable* VTKtable = vtkLookupTable::New();
   VTKtable->SetNumberOfColors(1000);
   VTKtable->SetTableRange(0,1000);
   VTKtable->SetSaturationRange(0,0);
   VTKtable->SetHueRange(0,1);
   VTKtable->SetValueRange(0,1);
   VTKtable->SetAlphaRange(1,1);
   VTKtable->Build();

   vtkTexture* VTKtexture = vtkTexture::New();
   VTKtexture->SetInput(ima);
   VTKtexture->InterpolateOn();
   VTKtexture->SetLookupTable(VTKtable);

   vtkPlaneSource* VTKplane = vtkPlaneSource::New();
   VTKplane->SetOrigin( -0.5, -0.5, 0.0);
   VTKplane->SetPoint1(  0.5, -0.5, 0.0);
   VTKplane->SetPoint2( -0.5,  0.5, 0.0);

   vtkPolyDataMapper *VTKplaneMapper = vtkPolyDataMapper::New();
   VTKplaneMapper->SetInput(VTKplane->GetOutput());

   vtkActor* VTKplaneActor = vtkActor::New();
   VTKplaneActor->SetTexture(VTKtexture);
   VTKplaneActor->SetMapper(VTKplaneMapper);
   VTKplaneActor->PickableOn();

   vtkRenderer        *ren = vtkRenderer::New();
   vtkRenderWindow *renwin = vtkRenderWindow::New();
   renwin->AddRenderer(ren);
   vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
   iren->SetRenderWindow(renwin);
   ren->AddActor(VTKplaneActor);
   ren->SetBackground(0,0,0.5);
   renwin->Render();
   iren->Start();

   reader->Delete();
   VTKtable->Delete();
   VTKtexture->Delete();
   VTKplane->Delete();
   VTKplaneMapper->Delete();
   VTKplaneActor->Delete();
   ren->Delete();
   renwin->Delete();
   iren->Delete();

   return(0);
}
/*=========================================================================
                                                                                
  Program:   gdcm
  Module:    $RCSfile: vtkgdcmViewer.cxx,v $
  Language:  C++
  Date:      $Date: 2007/06/19 13:09:45 $
  Version:   $Revision: 1.29 $
                                                                                
  Copyright (c) CREATIS (Centre de Recherche et d'Applications en Traitement de
  l'Image). All rights reserved. See Doc/License.txt or
  http://www.creatis.insa-lyon.fr/Public/Gdcm/License.html for details.
                                                                                
     This software is distributed WITHOUT ANY WARRANTY; without even
     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
     PURPOSE.  See the above copyright notices for more information.
                                                                                
=========================================================================*/
// This example illustrates how the vtkGdcmReader vtk class can be
// used in order to:
//  * produce a simple (vtk based) Dicom image STACK VIEWER.
//  * dump the stack considered as a volume in a vtkStructuredPoints
//    vtk file: the vtk gdcm wrappers can be seen as a simple way to convert
//    a stack of Dicom images into a native vtk volume.
//
// Usage:
//  * the filenames of the Dicom images constituting the stack should be
//    given as command line arguments,
//  * you can navigate through the stack by hitting any character key,
//  * the produced vtk file is named "foo.vtk" (in the invocation directory).
// 
//----------------------------------------------------------------------------
#include <vtkRenderWindowInteractor.h>
#include <vtkImageViewer.h>
#include <vtkStructuredPoints.h>
#include <vtkStructuredPointsWriter.h>
#include <vtkCommand.h>
#include <vtkRenderer.h>
#include <vtkImageMapToColors.h>
#include <vtkLookupTable.h>

#include "vtkGdcmReader.h"
#include "gdcmDocument.h"  // for NO_SHADOWSEQ
#ifndef vtkFloatingPointType
#define vtkFloatingPointType float
#endif

//----------------------------------------------------------------------------
// Callback for the interaction
class vtkgdcmObserver : public vtkCommand
{
public:
   virtual char const *GetClassName() const 
   { 
      return "vtkgdcmObserver";
   }
   static vtkgdcmObserver *New() 
   { 
      return new vtkgdcmObserver; 
   }
   vtkgdcmObserver()
   {
      this->ImageViewer = NULL;
   }
   virtual void Execute(vtkObject *, unsigned long event, void* )
   {
      if ( this->ImageViewer )
      {
         if ( event == vtkCommand::CharEvent )
         {
            int max = ImageViewer->GetWholeZMax();
            int slice = (ImageViewer->GetZSlice() + 1 ) % ++max;
            ImageViewer->SetZSlice( slice );
            ImageViewer->GetRenderer()->ResetCameraClippingRange();
            ImageViewer->Render();
         }
      }
   }
   vtkImageViewer *ImageViewer;
};


int main(int argc, char *argv[])
{
   if( argc < 2 )
      return 0;
  
   vtkGdcmReader *reader = vtkGdcmReader::New();
   reader->AllowLookupTableOff();

   if( argc == 2 )
      reader->SetFileName( argv[1] );
   else
      for(int i=1; i< argc; i++)
         reader->AddFileName( argv[i] );

// TODO : allow user to choose Load Mode
   reader->SetLoadMode(GDCM_NAME_SPACE::LD_NOSHADOWSEQ);  
   reader->Update();

   //print debug info:
   reader->GetOutput()->Print( cout );

   vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();

   vtkImageViewer *viewer = vtkImageViewer::New();

   if( reader->GetLookupTable() )
   {
      //convert to color:
      vtkImageMapToColors *map = vtkImageMapToColors::New ();
      map->SetInput (reader->GetOutput());
      map->SetLookupTable (reader->GetLookupTable());
      map->SetOutputFormatToRGB();
      viewer->SetInput ( map->GetOutput() );
      map->Delete();
   }
   else
   {
      vtkFloatingPointType *range = reader->GetOutput()->GetScalarRange();
      viewer->SetColorLevel (0.5 * (range[1] + range[0]));
      viewer->SetColorWindow (range[1] - range[0]);

      viewer->SetInput ( reader->GetOutput() );
   }
   viewer->SetupInteractor (iren);
  
   //vtkFloatingPointType *range = reader->GetOutput()->GetScalarRange();
   //viewer->SetColorWindow (range[1] - range[0]);
   //viewer->SetColorLevel (0.5 * (range[1] + range[0]));

   // Here is where we setup the observer, 
   vtkgdcmObserver *obs = vtkgdcmObserver::New();
   obs->ImageViewer = viewer;
   iren->AddObserver(vtkCommand::CharEvent,obs);
   obs->Delete();

   //viewer->Render();
   iren->Initialize();
   iren->Start();

   //if you wish you can export dicom to a vtk file  
   vtkStructuredPointsWriter *writer = vtkStructuredPointsWriter::New();
   writer->SetInput( reader->GetOutput());
   writer->SetFileName( "foo.vtk" );
   writer->SetFileTypeToBinary();
   //writer->Write();

   reader->Delete();
   iren->Delete();
   viewer->Delete();
   writer->Delete();

   return 0;
}

Generated on Fri Aug 24 13:01:20 2007 for gdcm by  doxygen 1.4.6