[Insight-users] VTK Display Error- Need Help

firat.sarialtun at boun.edu.tr firat.sarialtun at boun.edu.tr
Sat Apr 4 17:41:26 EDT 2009




Hi Everyone . I have been trying to implement
GeodesicActiveContourLevelSetImageFilter in ITK for Liver Segmentation. I
modified the code in order to take a VTK image as input, convert it into ITK
image, process it with the filter,then reconvert the segmented image into VTK
image again. my code can display the image in VTK in all steps (ITK to VTK
conversion works well in these steps) but it gives error for the output of Fast
marching algorithm which is needed for the initial level set for the algorithm.
As a result when I want to display the final segmented image, it gives
Segmentation fault. the error for the fast marching output and my code is
below. I'd appreciate your help.

Thanks
Firat

Error: 

ERROR: In /usr/local/VTK/VTK/Filtering/vtkStreamingDemandDrivenPipeline.cxx,
line 698
vtkStreamingDemandDrivenPipeline (0x91a8a88): The update extent specified in the
information for output port 0 on algorithm vtkImageMapToColors(0x91a85c8) is 0
511 0 511 0 0, which is outside the whole extent 0 -1 0 -1 0 0.

ERROR: In /usr/local/VTK/VTK/Filtering/vtkImageData.cxx, line 1473
vtkImageData (0x91a6c18): GetScalarPointer: Pixel (0, 256, 0) not in memory.
 Current extent= (0, -1, 0, -1, 0, 0)

ERROR: In /usr/local/VTK/VTK/Filtering/vtkImageData.cxx, line 1473
vtkImageData (0x91a6c18): GetScalarPointer: Pixel (0, 0, 0) not in memory.
 Current extent= (0, -1, 0, -1, 0, 0)

Segmentation fault




Code: 

#include <iostream>
#include <fstream>

#include "vtkImageActor.h"
#include "vtkImageMapToColors.h"
#include "vtkColorTransferFunction.h"
#include "vtkRenderWindow.h"
#include "vtkRenderer.h"
#include "vtkRenderWindowInteractor.h"
#include "/usr/local/VTK/VTK/Imaging/vtkImageCast.h"

#include "/usr/local/ITK/ITK/Code/BasicFilters/itkCastImageFilter.h"

#include
"/usr/local/ITK/ITK/Code/BasicFilters/itkCurvatureAnisotropicDiffusionImageFilter.h"
#include "itkGradientMagnitudeRecursiveGaussianImageFilter.h"
#include "itkSigmoidImageFilter.h"
#include "itkFastMarchingImageFilter.h"
#include "itkRescaleIntensityImageFilter.h"
#include "itkBinaryThresholdImageFilter.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkImage.h"
#include "itkGeodesicActiveContourLevelSetImageFilter.h"

#include
"/home/frat/Desktop/liverdata/svn/src/VavLoader/itkImageToVTKImageFilter.h"
#include
"/home/frat/Desktop/liverdata/svn/src/VavLoader/itkVTKImageToImageFilter.h"
#include "/home/frat/Desktop/liverdata/VavDataLoader/VavDataLoader.h"
#include "/home/frat/Desktop/liverdata/VavDICOMLoader/VavDICOMLoader.h"

#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif

#ifdef __BORLANDC__
#define ITK_LEAN_AND_MEAN
#endif

using namespace std;

void render(vtkImageData *data, int viewSlice, int width, int height){

	vtkImageActor* axial;

	vtkImageMapToColors *orthogonalimageColors1 = vtkImageMapToColors::New();
	orthogonalimageColors1->SetInput ( data );
	orthogonalimageColors1->SetOutputFormatToRGBA();

	vtkColorTransferFunction *colorfunction =  vtkColorTransferFunction::New();
	colorfunction->SetColorSpaceToRGB();
	colorfunction->RemoveAllPoints();

	double *ranj = data->GetScalarRange();
	double window = ranj[1] - ranj[0];
	colorfunction->AddRGBPoint ( 0,	0, 0, 0 );
	colorfunction->AddRGBPoint ( window / 2, 0.5, 0.5, 0.5 );
	colorfunction->AddRGBPoint ( window, 1, 1, 1 );

	colorfunction->SetAlpha ( 0.8 );
	colorfunction->Build();

	orthogonalimageColors1->SetLookupTable ( colorfunction );

	axial = vtkImageActor::New();
	axial->SetDisplayExtent ( 0, width - 1, 0, height - 1, 0,0 );
	axial->SetInput ( orthogonalimageColors1->GetOutput() );

	// a renderer and render window
	vtkRenderer *ren1 = vtkRenderer::New();
	vtkRenderWindow *renWin = vtkRenderWindow::New();
	renWin->AddRenderer(ren1);

	// an interactor
	vtkRenderWindowInteractor *iren = vtkRenderWindowInteractor::New();
	iren->SetRenderWindow(renWin);

	// add the actor to the scene
	ren1->AddActor(axial);
	ren1->SetBackground(1,1,1); // Background color white

	// render an image (lights and cameras are created automatically)
	renWin->Render();

	// begin mouse interaction
	iren->Start();
}
int main( int argc, char * argv[] )
{

//************************************** GLOBAL DEFINITIONS
***********************************************//
	

	typedef itk::Image< float, 2 >  InternalImageType;
	typedef itk::Image< unsigned char, 2 >  OutputImageType;
	typedef itk::Image< double, 2 >  ExternalImageType;

//*************************************** MISSING PARAMETERS HANDLING
******************************************//

  if( argc < 12 )
    {
    std::cerr << "Missing Parameters " << std::endl;
    std::cerr << "Usage: " << argv[0];
    std::cerr << " inputImage  outputImage";
    std::cerr << " seedX seedY InitialDistance";
    std::cerr << " Sigma SigmoidAlpha SigmoidBeta";
    std::cerr << " PropagationScaling"  << std::endl;
    return 1;
    }

//************************************ VTK2ITK CONVERSION &
READING***************************************//

	// argv[1] = first file

	VavDataLoader *loader;
	loader = new VavDataLoader( );

	int modality = atoi( argv[2] );
	int slices = atoi( argv[3] );

	VavScalar* data_scalar;
	data_scalar = new VavScalar();
	
	VavCT* CT = new VavCT();
	
	loader -> load( argv[1], CT, NULL, modality, slices );

	vtkImageCast* VtkCaster = vtkImageCast::New(); 
	VtkCaster->SetInput(CT -> dataScalar -> getImageArray());
	VtkCaster->SetOutputScalarTypeToFloat();

	//VTK -> ITK Conversion
	typedef itk::VTKImageToImageFilter< InternalImageType > 
VTK2ITKConnectorFilterType;
	VTK2ITKConnectorFilterType::Pointer VTK2ITKconnector =
VTK2ITKConnectorFilterType::New();
	VTK2ITKconnector->SetInput( VtkCaster->GetOutput() );

//********************************************* CASTERS
******************************************//

	typedef itk::CastImageFilter<
		   ExternalImageType, InternalImageType > D2FCastFilterType;

	D2FCastFilterType::Pointer D2Fcaster1 = D2FCastFilterType::New();

	typedef itk::CastImageFilter<
		   OutputImageType, InternalImageType > U2FCastFilterType;

	U2FCastFilterType::Pointer U2Fcaster1 = U2FCastFilterType::New();


  //  The RescaleIntensityImageFilter type is declared below. This filter will
  //  renormalize image before sending them to writers.

	typedef itk::RescaleIntensityImageFilter< 
                               InternalImageType, 
                               OutputImageType >   CastFilterType;



//************************************** THRESHOLDER
*************************************************//

  typedef itk::BinaryThresholdImageFilter< 
                        InternalImageType, 
                        OutputImageType    >    ThresholdingFilterType;
  
  ThresholdingFilterType::Pointer thresholder = ThresholdingFilterType::New();

  thresholder->SetLowerThreshold( -1000.0 );
  thresholder->SetUpperThreshold(     0.0 );

  thresholder->SetOutsideValue(  0  );
  thresholder->SetInsideValue(  255 );



//************************************  READER & WRITER 
*********************************************//

  typedef  itk::ImageFileReader< InternalImageType > ReaderType;
  typedef  itk::ImageFileWriter<  OutputImageType  > WriterType;
  typedef  itk::ImageFileWriter<  InternalImageType  > InternalWriterType;

  ReaderType::Pointer reader = ReaderType::New();
  WriterType::Pointer writer = WriterType::New();
  InternalWriterType::Pointer Internalwriter = InternalWriterType::New();

  reader->SetFileName( argv[1] );
  writer->SetFileName( argv[4] );



//******************************* SMOOTHING (ANISOTROPIC
DIFFUSION)******************************//

  typedef   itk::CurvatureAnisotropicDiffusionImageFilter< 
                               InternalImageType, 
                               InternalImageType >  SmoothingFilterType;

  SmoothingFilterType::Pointer smoothing = SmoothingFilterType::New();

  smoothing->SetTimeStep( 0.125 );
  smoothing->SetNumberOfIterations(  5 );
  smoothing->SetConductanceParameter( 9.0 );



//************************************* GRADIENT MAGNITUDE FILTER
***************************************//

  typedef   itk::GradientMagnitudeRecursiveGaussianImageFilter< 
                               InternalImageType, 
                               InternalImageType >  GradientFilterType;

  GradientFilterType::Pointer  gradientMagnitude = GradientFilterType::New();

  //  The GradientMagnitudeRecursiveGaussianImageFilter performs the
  //  equivalent of a convolution with a Gaussian kernel, followed by a
  //  derivative operator. The sigma of this Gaussian can be used to control
  //  the range of influence of the image edges. This filter has been discussed
  //  in Section~\ref{sec:GradientMagnitudeRecursiveGaussianImageFilter}

  const double sigma = atof( argv[8] );
  gradientMagnitude->SetSigma(  sigma  );

//****************************************** SIGMOID FILTER
***************************************//

  typedef   itk::SigmoidImageFilter<
                               InternalImageType, 
                               InternalImageType >  SigmoidFilterType;

  SigmoidFilterType::Pointer sigmoid = SigmoidFilterType::New();

  sigmoid->SetOutputMinimum(  0.0  );
  sigmoid->SetOutputMaximum(  1.0  );

  //  The SigmoidImageFilter requires two parameters that define the linear
  //  transformation to be applied to the sigmoid argument. This parameters
  //  have been discussed in Sections~\ref{sec:IntensityNonLinearMapping} and
  //  \ref{sec:FastMarchingImageFilter}.

  const double alpha =  atof( argv[9] );
  const double beta  =  atof( argv[10] );

  sigmoid->SetAlpha( alpha );
  sigmoid->SetBeta(  beta  );

//****************************************** FAST MARCHING
***************************************//

  typedef  itk::FastMarchingImageFilter< 
                              InternalImageType, 
                              InternalImageType >    FastMarchingFilterType;

  FastMarchingFilterType::Pointer  fastMarching =
FastMarchingFilterType::New();

  //  The FastMarchingImageFilter requires the user to provide a seed
  //  point from which the level set will be generated. The user can actually
  //  pass not only one seed point but a set of them. Note the the
  //  FastMarchingImageFilter is used here only as a helper in the
  //  determination of an initial level set. We could have used the
  //  \doxygen{DanielssonDistanceMapImageFilter} in the same way.
  //
  //  The seeds are passed stored in a container. The type of this
  //  container is defined as \code{NodeContainer} among the
  //  FastMarchingImageFilter traits.
  //

  typedef FastMarchingFilterType::NodeContainer  NodeContainer;
  typedef FastMarchingFilterType::NodeType       NodeType;

  NodeContainer::Pointer seeds = NodeContainer::New();

  InternalImageType::IndexType  seedPosition;
  
  seedPosition[0] = atoi( argv[5] );
  seedPosition[1] = atoi( argv[6] );

  //  Nodes are created as stack variables and initialized with a value and an
  //  \doxygen{Index} position. Note that here we assign the value of minus the
  //  user-provided distance to the unique node of the seeds passed to the
  //  FastMarchingImageFilter. In this way, the value will increment
  //  as the front is propagated, until it reaches the zero value corresponding
  //  to the contour. After this, the front will continue propagating until it
  //  fills up the entire image. The initial distance is taken here from the
  //  command line arguments. The rule of thumb for the user is to select this
  //  value as the distance from the seed points at which she want the initial
  //  contour to be.

  const double initialDistance = atof( argv[7] );

  NodeType node;

  const double seedValue = - initialDistance;
  
  node.SetValue( seedValue );
  node.SetIndex( seedPosition );


  //  The list of nodes is initialized and then every node is inserted using
  //  the \code{InsertElement()}.

  seeds->Initialize();
  seeds->InsertElement( 0, node );

	//  For More Seed Points
	// 
	//   NodeType node2;
	// 
	//   seedPosition[0] = atoi( argv[13] );;
	//   seedPosition[1] = atoi( argv[14] );;
	// 
	//   node2.SetValue( seedValue );
	//   node2.SetIndex( seedPosition );
	// 
	//   seeds->InsertElement( 1, node2 );

  fastMarching->SetTrialPoints(  seeds  );

  //  Since the FastMarchingImageFilter is used here just as a
  //  Distance Map generator. It does not require a speed image as input.
  //  Instead the constant value $1.0$ is passed using the
  //  \code{SetSpeedConstant()} method.
  //
  fastMarching->SetSpeedConstant( 1.0 );

  //  The FastMarchingImageFilter requires the user to specify the
  //  size of the image to be produced as output. This is done using the
  //  \code{SetOutputSize()}. Note that the size is obtained here from the
  //  output image of the smoothing filter. The size of this image is valid
  //  only after the \code{Update()} methods of this filter has been called
  //  directly or indirectly.
  //
  fastMarching->SetOutputSize( 
           reader->GetOutput()->GetBufferedRegion().GetSize() );


//************************************* GEODESIC ACTIVE CONTOURS
*************************************//


  typedef  itk::GeodesicActiveContourLevelSetImageFilter< InternalImageType, 
                InternalImageType >    GeodesicActiveContourFilterType;
  GeodesicActiveContourFilterType::Pointer geodesicActiveContour = 
                                     GeodesicActiveContourFilterType::New();
  

  const double propagationScaling = atof( argv[11] );

  geodesicActiveContour->SetPropagationScaling( propagationScaling );
  geodesicActiveContour->SetCurvatureScaling( 1.0 );
  geodesicActiveContour->SetAdvectionScaling( 1.0 );


  //  Once activiated the level set evolution will stop if the convergence
  //  criteria or if the maximum number of iterations is reached.  The
  //  convergence criteria is defined in terms of the root mean squared (RMS)
  //  change in the level set function. The evolution is said to have
  //  converged if the RMS change is below a user specified threshold.  In a
  //  real application is desirable to couple the evolution of the zero set
  //  to a visualization module allowing the user to follow the evolution of
  //  the zero set. With this feedback, the user may decide when to stop the
  //  algorithm before the zero set leaks through the regions of low gradient
  //  in the contour of the anatomical structure to be segmented.

  geodesicActiveContour->SetMaximumRMSError( 0.02 );
  geodesicActiveContour->SetNumberOfIterations( 800 );



//******************************************** PIPELINE
*******************************************//
  
  smoothing->SetInput( VTK2ITKconnector->GetOutput() );
  gradientMagnitude->SetInput( smoothing->GetOutput() );
  sigmoid->SetInput( gradientMagnitude->GetOutput() );
// 
//   geodesicActiveContour->SetInput(  fastMarching->GetOutput() );
//   geodesicActiveContour->SetFeatureImage( sigmoid->GetOutput() );
// 
//   thresholder->SetInput( geodesicActiveContour->GetOutput() );
//   writer->SetInput( thresholder->GetOutput() );

//****************************************  INTERMEDIATE STEPS 
*************************************// 

//   //  Here we configure all the writers required to see the intermediate
//   //  outputs of the pipeline. This is added here only for
//   //  pedagogical/debugging purposes. These intermediate output are normaly
not
//   //  required. Only the output of the final thresholding filter should be
//   //  relevant.  Observing intermediate output is helpful in the process of
//   //  fine tuning the parameters of filters in the pipeline. 

 
//   CastFilterType::Pointer caster1 = CastFilterType::New();
//   CastFilterType::Pointer caster2 = CastFilterType::New();
//   CastFilterType::Pointer caster3 = CastFilterType::New();
//   CastFilterType::Pointer caster4 = CastFilterType::New();

// 
//   WriterType::Pointer writer1 = WriterType::New();
//   WriterType::Pointer writer2 = WriterType::New();
//   WriterType::Pointer writer3 = WriterType::New();
//   WriterType::Pointer writer4 = WriterType::New();


//   caster1->SetInput( smoothing->GetOutput() );
//   writer1->SetInput( caster1->GetOutput() );
//   writer1->SetFileName("GeodesicActiveContourImageFilterOutput1.png");
//   caster1->SetOutputMinimum(   0 );
//   caster1->SetOutputMaximum( 255 );
//   writer1->Update();

//   caster2->SetInput( gradientMagnitude->GetOutput() );
//   writer2->SetInput( caster2->GetOutput() );
//   writer2->SetFileName("GeodesicActiveContourImageFilterOutput2.png");
//   caster2->SetOutputMinimum(   0 );
//   caster2->SetOutputMaximum( 255 );
//   writer2->Update();
// 
//   caster3->SetInput( sigmoid->GetOutput() );
//   writer3->SetInput( caster3->GetOutput() );
//   writer3->SetFileName("GeodesicActiveContourImageFilterOutput3.png");
//   caster3->SetOutputMinimum(   0 );
//   caster3->SetOutputMaximum( 255 );
//   writer3->Update();
// 
//   caster4->SetInput( fastMarching->GetOutput() );
//   writer4->SetInput( caster4->GetOutput() );
//   writer4->SetFileName("GeodesicActiveContourImageFilterOutput4.png");
//   caster4->SetOutputMinimum(   0 );
//   caster4->SetOutputMaximum( 255 );


 
//   //  The invocation of the \code{Update()} method on the writer triggers
the
//   //  execution of the pipeline.  As usual, the call is placed in a
//   //  \code{try/catch} block should any errors occur or exceptions be
thrown.

//   try
//     {
//     writer->Update();
//     }
//   catch( itk::ExceptionObject & excep )
//     {
//     std::cerr << "Exception caught !" << std::endl;
//     std::cerr << excep << std::endl;
//     }

// 
//   // Print out some useful information 
//   std::cout << std::endl;
//   std::cout << "Max. no. iterations: " <<
geodesicActiveContour->GetNumberOfIterations() << std::endl;
//   std::cout << "Max. RMS error: " <<
geodesicActiveContour->GetMaximumRMSError() << std::endl;
//   std::cout << std::endl;
//   std::cout << "No. elpased iterations: " <<
geodesicActiveContour->GetElapsedIterations() << std::endl;
//   std::cout << "RMS change: " << geodesicActiveContour->GetRMSChange() <<
std::endl;
// 
//   writer4->Update();
// 
// 
//   // The following writer type is used to save the output of the
time-crossing
//   // map in a file with apropiate pixel representation. The advantage of
saving
//   // this image in native format is that it can be used with a viewer to
help
//   // determine an appropriate threshold to be used on the output of the
//   // fastmarching filter.
//   //
//   typedef itk::ImageFileWriter< InternalImageType > InternalWriterType;
// 
//   InternalWriterType::Pointer mapWriter = InternalWriterType::New();
//   mapWriter->SetInput( fastMarching->GetOutput() );
//   mapWriter->SetFileName("GeodesicActiveContourImageFilterOutput4.mha");
//   mapWriter->Update();
// 
//   InternalWriterType::Pointer speedWriter = InternalWriterType::New();
//   speedWriter->SetInput( sigmoid->GetOutput() );
//   speedWriter->SetFileName("GeodesicActiveContourImageFilterOutput3.mha");
//   speedWriter->Update();
// 
//   InternalWriterType::Pointer gradientWriter = InternalWriterType::New();
//   gradientWriter->SetInput( gradientMagnitude->GetOutput() );
//  
gradientWriter->SetFileName("GeodesicActiveContourImageFilterOutput2.mha");
//   gradientWriter->Update();
// 


//*********************************  BACKCONVERSION TO VTK & UPDATE 
********************************//
//   	Internalwriter->SetFileName( argv[4] );	
// 	Internalwriter->SetInput( fastMarching->GetOutput() );
// 	Internalwriter->Update();


         U2Fcaster1->SetInput (thresholder->GetOutput());

	//ITK -> VTK Conversion
	typedef itk::ImageToVTKImageFilter< InternalImageType >
ITK2VTKConnectorFilterType;
	ITK2VTKConnectorFilterType::Pointer ITK2VTKconnector =
ITK2VTKConnectorFilterType::New();
	ITK2VTKconnector->GetExporter()->SetInput(fastMarching->GetOutput()); // for
final output U2Fcaster1->GetOutput()
	ITK2VTKconnector->GetImporter()->Update();

	//display resulting image
	render(ITK2VTKconnector->GetImporter()->GetOutput(), 0, 512, 512); //1, -1, -1
	
	cout << "Done!" << endl;
  	return 0;
}






More information about the Insight-users mailing list