[vtkusers] import data from itk

kdsfinger at gmail.com kdsfinger at gmail.com
Tue Sep 5 23:37:56 EDT 2006


hi, all
I want to read a stack of png (unsigned char) files int itk and then
export to vtk. Here is what I did but what I get is pure whit window.
The code is modified and simplified based on the Medical3.cxx. Can
someone point out what's wrong with my code? Thanks for help.
zl2k

template <typename ITK_Exporter, typename VTK_Importer>
void ConnectPipelines(ITK_Exporter exporter, VTK_Importer* importer)
{
  importer->SetUpdateInformationCallback(exporter->GetUpdateInformationCallback());
  importer->SetPipelineModifiedCallback(exporter->GetPipelineModifiedCallback());
  importer->SetWholeExtentCallback(exporter->GetWholeExtentCallback());
  importer->SetSpacingCallback(exporter->GetSpacingCallback());
  importer->SetOriginCallback(exporter->GetOriginCallback());
  importer->SetScalarTypeCallback(exporter->GetScalarTypeCallback());
  importer->SetNumberOfComponentsCallback(exporter->GetNumberOfComponentsCallback());
  importer->SetPropagateUpdateExtentCallback(exporter->GetPropagateUpdateExtentCallback());
  importer->SetUpdateDataCallback(exporter->GetUpdateDataCallback());
  importer->SetDataExtentCallback(exporter->GetDataExtentCallback());
  importer->SetBufferPointerCallback(exporter->GetBufferPointerCallback());
  importer->SetCallbackUserData(exporter->GetCallbackUserData());
}

int main (int argc, char **argv)
{
  vtkRenderer *aRenderer = vtkRenderer::New();
  vtkRenderWindow *renWin = vtkRenderWindow::New();
    renWin->AddRenderer(aRenderer);
  vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
    iren->SetRenderWindow(renWin);

  typedef unsigned char                       PixelType;
  const unsigned int Dimension = 3;
	
  typedef itk::Image< PixelType, Dimension >  ImageType_uc;
  typedef itk::ImageSeriesReader< ImageType_uc >  ReaderType_uc;
  ReaderType_uc::Pointer reader_uc = ReaderType_uc::New();

  typedef itk::NumericSeriesFileNames    NameGeneratorType;
  NameGeneratorType::Pointer nameGenerator = NameGeneratorType::New();
  nameGenerator->SetSeriesFormat( "../../braindata/a_%03d.png" );

  nameGenerator->SetStartIndex( 0 );
  nameGenerator->SetEndIndex( 126 );
  nameGenerator->SetIncrementIndex( 1 );

  reader_uc->SetImageIO( itk::PNGImageIO::New() );
  reader_uc->SetFileNames( nameGenerator->GetFileNames()  );

  reader_uc->Update();
  ImageType_uc::Pointer image = ImageType_uc::New();
  image = reader_uc->GetOutput();

  ImageType_uc::SpacingType spacing_uc;
	spacing_uc[0] = 0.5; // spacing along X
	spacing_uc[1] = 0.5; // spacing along Y
	spacing_uc[2] = 1; // spacing along Z
	image->SetSpacing( spacing_uc );
	ImageType_uc::SizeType size_uc;
	size_uc[0] = 256;
	size_uc[1] = 256;
	size_uc[2] = 127;
	image->SetRegions(size_uc);
	
  typedef itk::VTKImageExport< ImageType_uc > ExportFilterType;
  ExportFilterType::Pointer itkExporter = ExportFilterType::New();
  itkExporter->SetInput( image );
  // Create the vtkImageImport and connect it to the
  // itk::VTKImageExport instance.
  vtkImageImport* vtkImporter = vtkImageImport::New();
  ConnectPipelines(itkExporter, vtkImporter);
  vtkImporter->Update();

  // Now create a lookup table that consists of the full hue circle
  // (from HSV).
  vtkLookupTable *hueLut = vtkLookupTable::New();
    hueLut->SetTableRange (0, 255);
    hueLut->SetHueRange (0, 1);
    hueLut->SetSaturationRange (1, 1);
    hueLut->SetValueRange (1, 1);
    hueLut->Build(); //effective built
	
  // Create the second (axial) plane of the three planes. We use the
  // same approach as before except that the extent differs.
  vtkImageMapToColors *axialColors = vtkImageMapToColors::New();
    axialColors->SetInput(vtkImporter->GetOutput());
    axialColors->SetLookupTable(hueLut);
    axialColors->Update();
  vtkImageActor *axial = vtkImageActor::New();
    axial->SetInput(axialColors->GetOutput());
    axial->SetDisplayExtent(0,170, 0,170, 46,46);

  vtkCamera *aCamera = vtkCamera::New();
    aCamera->SetViewUp (0, 0, -1);
    aCamera->SetPosition (0, 1, 0);
    aCamera->SetFocalPoint (0, 0, 0);
    aCamera->ComputeViewPlaneNormal();
	
  aRenderer->AddActor(axial);
  aRenderer->SetActiveCamera(aCamera);
  aRenderer->Render();
  aRenderer->ResetCamera ();
  aCamera->Dolly(1.5);
  // Set a background color for the renderer and set the size of the
  // render window (expressed in pixels).
  aRenderer->SetBackground(1,1,1);
  renWin->SetSize(640, 480);

  aRenderer->ResetCameraClippingRange ();
  // interact with data
  iren->Initialize();
  iren->Start();

  reader_uc->Delete();
  nameGenerator->Delete();
  image->Delete();
  hueLut->Delete();
  axialColors->Delete();
  axial->Delete();
  aCamera->Delete();
  aRenderer->Delete();
  renWin->Delete();
  iren->Delete();

  return 0;
}



More information about the vtkusers mailing list