[Insight-users] Problem with itkImageToVTKImageFilter.h

kdsfinger at gmail.com kdsfinger at gmail.com
Wed Jun 21 20:41:53 EDT 2006


hi, all

I am importing a series of images using itk into vtk by
itkImageToVTKImageFilter.h and displayed using vtkImagePlaneWidget.
The code works fine except one thing: My input image is of unsigned
char, but the image displayed looks like binary (after thresholding).
Also, the vtkImageData imported from itk won't display correct data
dimension (in my case, it should be 3 but displayed as 0). In order to
test what I said, you only need to find 2 png file (unsigned char
type) with same size and named them as abc1.png and abc2.png. They can
be found in the vtk data package.

The CMakeLists.txt:

PROJECT(QtSimpleView)

FIND_PACKAGE(VTK)
IF(NOT VTK_DIR)
  MESSAGE(FATAL_ERROR "Please set VTK_DIR.")
ENDIF(NOT VTK_DIR)
INCLUDE(${VTK_USE_FILE})

FIND_PACKAGE(ITK)
IF(NOT ITK_DIR)
  MESSAGE(FATAL_ERROR "Please set ITK_DIR.")
ENDIF(NOT ITK_DIR)
INCLUDE(${ITK_USE_FILE})


ADD_EXECUTABLE( testview image.cxx)

TARGET_LINK_LIBRARIES( testview
  vtkRendering
  vtkGraphics
  vtkIO
  vtkCommon

  ITKBasicFilters
  ITKCommon
  ITKIO
  vtkHybrid
  vtkImaging
  vtkFiltering
  vtkWidgets
)
#--------------------------------------over


//The cxx code:
//-------image.cxx -------
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "vtkImageViewer.h"
#include "vtkRenderWindowInteractor.h"
#include "itkImageToVTKImageFilter.h"

#include "itkImageSeriesReader.h"
#include "itkImageFileWriter.h"
#include "itkNumericSeriesFileNames.h"
#include "itkPNGImageIO.h"

#include "vtkActor.h"
#include "vtkCamera.h"
#include "vtkCellPicker.h"
#include "vtkCommand.h"
#include "vtkImageActor.h"
#include "vtkImageMapToColors.h"
#include "vtkImagePlaneWidget.h"
#include "vtkImageReader.h"
#include "vtkInteractorEventRecorder.h"
#include "vtkLookupTable.h"
#include "vtkOutlineFilter.h"
#include "vtkPolyDataMapper.h"
#include "vtkProperty.h"
#include "vtkRenderWindow.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkRenderer.h"
#include "vtkVolume16Reader.h"
#include "vtkImageData.h"

#include "vtkTestUtilities.h"
#include "vtkRegressionTestImage.h"
#include "vtkDebugLeaks.h"



int main()
{
	//The following is mainly copyed from the demo of itkImageToVTKImageFilter
	typedef unsigned char      PixelType;
  const   unsigned int        Dimension = 3;

  typedef itk::Image< PixelType, Dimension >   ImageType;
  typedef itk::ImageSeriesReader< ImageType >  ReaderType;

  ReaderType::Pointer reader = ReaderType::New();

  typedef itk::NumericSeriesFileNames    NameGeneratorType;

  NameGeneratorType::Pointer nameGenerator = NameGeneratorType::New();

  nameGenerator->SetSeriesFormat("abc%03d.png"); //!!!! specify the format !!!!
  //if the series are abc001.png, abc002.png, then "abc%03d.png"
  //if the series are abc1.png, abc2.png, ...abc99.png, then "abc%d.png"
  //if the series under a subdir named data, then "data/abc%d.png"

  nameGenerator->SetStartIndex( 1 );//the start number of the series
!!!specify the start index !!!
  nameGenerator->SetEndIndex( 2 );//the end number of the series !!!
specify the end index !!!
  nameGenerator->SetIncrementIndex( 1 );

  reader->SetImageIO( itk::PNGImageIO::New() );

  reader->SetFileNames( nameGenerator->GetFileNames()  );


  typedef itk::ImageToVTKImageFilter< ImageType > ConnectorType;

  ConnectorType::Pointer connector = ConnectorType::New();

  connector->SetInput(reader->GetOutput());
	vtkImageData * data = connector->GetOutput();
	int dim = data->GetDataDimension();
	cout<<dim<<endl;
	cout<<data->GetClassName()<<endl;
	cout<<data->GetEstimatedMemorySize()<<endl;
	cout<<data->GetActualMemorySize()<<endl;
	
	/*
  viewer->SetColorWindow(255);
  viewer->SetColorLevel(128);
  renderWindowInteractor->Start();*/

  //the following portion is mainly copyed from the vtkImageWedget test example
  vtkRenderer* ren1 = vtkRenderer::New();
  vtkRenderer* ren2 = vtkRenderer::New();

  vtkRenderWindow* renWin = vtkRenderWindow::New();
    renWin->AddRenderer(ren2);
    renWin->AddRenderer(ren1);

  vtkRenderWindowInteractor* iren = vtkRenderWindowInteractor::New();
    iren->SetRenderWindow(renWin);
	
  vtkCellPicker* picker = vtkCellPicker::New();
    picker->SetTolerance(0.005);

  vtkProperty* ipwProp = vtkProperty::New();
   //assign default props to the ipw's texture plane actor
  vtkImagePlaneWidget* planeWidgetX = vtkImagePlaneWidget::New();
    planeWidgetX->SetInteractor( iren);
    planeWidgetX->SetKeyPressActivationValue('x');
    planeWidgetX->SetPicker(picker);
    planeWidgetX->RestrictPlaneToVolumeOn();
    planeWidgetX->GetPlaneProperty()->SetColor(1,0,0);
    planeWidgetX->SetTexturePlaneProperty(ipwProp);
    planeWidgetX->TextureInterpolateOff();
    planeWidgetX->SetResliceInterpolateToNearestNeighbour();
    planeWidgetX->SetInput(data);
    planeWidgetX->SetPlaneOrientationToXAxes();
    planeWidgetX->SetSliceIndex(32);
    planeWidgetX->DisplayTextOn();
    planeWidgetX->On();
    planeWidgetX->InteractionOff();
    planeWidgetX->InteractionOn();

  vtkImagePlaneWidget* planeWidgetY = vtkImagePlaneWidget::New();
    planeWidgetY->SetInteractor( iren);
    planeWidgetY->SetKeyPressActivationValue('y');
    planeWidgetY->SetPicker(picker);
    planeWidgetY->GetPlaneProperty()->SetColor(1,1,0);
    planeWidgetY->SetTexturePlaneProperty(ipwProp);
    planeWidgetY->TextureInterpolateOn();
    planeWidgetY->SetResliceInterpolateToLinear();
    planeWidgetY->SetInput(data);
    planeWidgetY->SetPlaneOrientationToYAxes();
    planeWidgetY->SetSlicePosition(102.4);
    planeWidgetY->SetLookupTable( planeWidgetX->GetLookupTable());
    planeWidgetY->DisplayTextOff();
    planeWidgetY->UpdatePlacement();
    planeWidgetY->On();

  vtkImagePlaneWidget* planeWidgetZ = vtkImagePlaneWidget::New();
    planeWidgetZ->SetInteractor( iren);
    planeWidgetZ->SetKeyPressActivationValue('z');
    planeWidgetZ->SetPicker(picker);
    planeWidgetZ->GetPlaneProperty()->SetColor(0,0,1);
    planeWidgetZ->SetTexturePlaneProperty(ipwProp);
    planeWidgetZ->TextureInterpolateOn();
    planeWidgetZ->SetResliceInterpolateToCubic();
    planeWidgetZ->SetInput(data);
    planeWidgetZ->SetPlaneOrientationToZAxes();
    planeWidgetZ->SetSliceIndex(25);
    planeWidgetZ->SetLookupTable( planeWidgetX->GetLookupTable());
    planeWidgetZ->DisplayTextOn();
    planeWidgetZ->On();

  double wl[2];
  planeWidgetZ->GetWindowLevel(wl);

  // Add a 2D image to test the GetReslice method
  //

  vtkImageMapToColors* colorMap = vtkImageMapToColors::New();
    colorMap->PassAlphaToOutputOff();
    colorMap->SetActiveComponent(0);
    colorMap->SetOutputFormatToLuminance();
    colorMap->SetInput(planeWidgetZ->GetResliceOutput());
    colorMap->SetLookupTable(planeWidgetX->GetLookupTable());

  vtkImageActor* imageActor = vtkImageActor::New();
    imageActor->PickableOff();
    imageActor->SetInput(colorMap->GetOutput());
	
  // Add the actors
  //
  ren2->AddActor( imageActor);

  ren1->SetBackground( 0.1, 0.1, 0.3);
  ren2->SetBackground( 0.2, 0.1, 0.2);

  renWin->SetSize( 300, 300);

  ren1->SetViewport(0,0,0.65,1);
  ren2->SetViewport(0.65,0,1,1);

  // Set the actors' postions
  //

  renWin->Render();

  iren->SetEventPosition( 475,175);
  iren->SetKeyCode('r');
  iren->InvokeEvent(vtkCommand::CharEvent,NULL);
  renWin->Render();
	
	
  ren1->GetActiveCamera()->Elevation(210);
  ren1->GetActiveCamera()->SetViewUp(0, 0, -1);
  ren1->GetActiveCamera()->Azimuth(45);
  ren1->GetActiveCamera()->Dolly(1);
  ren1->ResetCameraClippingRange();

  iren->Initialize();
  renWin->Render();

  // Test SetKeyPressActivationValue for one of the widgets
  //

  iren->SetKeyCode('z');
  iren->InvokeEvent(vtkCommand::CharEvent,NULL);

  iren->SetKeyCode('z');
  iren->InvokeEvent(vtkCommand::CharEvent,NULL);
	
	iren->Start();

  planeWidgetX->Delete();
  planeWidgetY->Delete();
  planeWidgetZ->Delete();
  colorMap->Delete();
  imageActor->Delete();
  picker->Delete();
  iren->Delete();
  renWin->Delete();
  ren1->Delete();
  ren2->Delete();

  return 0;
}
//----------------------------------------------------over

Thanks for help.

zl2k


More information about the Insight-users mailing list