[Insight-users] itk to vtk connection problems

Luis Ibanez luis . ibanez at kitware . com
Sat, 31 May 2003 01:13:46 -0400


Hi Sven,

Are you actually returning the pointer of the
output from a function ?

Keep in mind that ITK uses SmartPointers.

When you return from the function, all the
ITK pipeline will be destroyed, so by the time
you try to use the VTK image in a visualization
pipeline most of the ITK pointers will be invalid.
An update call in the combined VTK+ITK pipeline
will certainly lead to a crash.

You may want to create a class for implementing
the whole process, and make "Foo" a member
method of this class, as well as making the
ITK and VTK filter member variables.

In that way the ITK pipeline will stay alive
when it comes the time of requesting updates
in the pipeline.

----

Also, for convinience you may want to try the
pseudo-filter:

         itk::ImageToVTKImageFiter

available in

         Insight/Auxiliary/vtk

It encapsulates an itk::VTKImageExporter
and a vtkImporter. It will also connects
both pipelines for you.

---


   Luis



---------------------------
Sven Prevrhal wrote:
> I religiously followed the examples and still get an error ☺! The 
> relevant code is
> 
>  
> 
> #include <itkVTKImageImport.h>
> 
> #include <itkVTKImageExport.h>
> 
>  
> 
> /**
> 
>  * This function will connect the given itk::VTKImageExport filter to
> 
>  * the given vtkImageImport filter.
> 
>  */
> 
> 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());
> 
> }
> 
>  
> 
> vtkImageData *Foo() {
> 
>       typedef itk::Image<unsigned short , 3>  OutputImageType;
> 
>  
> 
>       typedef itk::ImageFileReader< OutputImageType >  ReaderType;
> 
>       ReaderType::Pointer reader = ReaderType::New();
> 
>  
> 
>       reader->SetFileName("C:\\data\\headsq\\quarter.mhd"); //Thanks Luis!
> 
>       reader->Update();
> 
>  
> 
>       OutputImageType::Pointer data = reader->GetOutput();
> 
>  
> 
>  
> 
>       typedef 
> itk::BinaryThresholdImageFilter<OutputImageType,OutputImageType>  ThresholdingFilterType;
> 
>       ThresholdingFilterType::Pointer thresholder = 
> ThresholdingFilterType::New();
> 
> //    thresholder->SetInput(data);
> 
>       thresholder->SetInput(reader->GetOutput());
> 
>       thresholder->SetLowerThreshold(1000);
> 
>       thresholder->SetUpperThreshold(3000);
> 
>       thresholder->SetOutsideValue(0);
> 
>       thresholder->SetInsideValue(3000);
> 
>       thresholder->Update();
> 
>       //
> 
>       //pipe itk data back into vtk
> 
>       //
> 
>       typedef itk::VTKImageExport<OutputImageType>  ImageExportType;
> 
>     ImageExportType::Pointer itkExporter = ImageExportType::New();
> 
>       itkExporter->SetInput(thresholder->GetOutput());
> 
>       itkExporter->Update();
> 
>       vtkImageImport *vtkImporter = vtkImageImport::New();
> 
>       ConnectPipelines(itkExporter, vtkImporter);
> 
>       vtkImporter->Update();
> 
>       return vtkImporter->GetOutput();
> 
>  
> 
> I threw Update()s in to check where the error occurs. It does not occur 
> here but when I connect the vtkImporter output to the VTK imaging 
> pipeline, and the camera->ResetCamera() triggers an UpdateInformation(). Everything seems to be 
> connected correctly, because execution makes it down to 
> itkVTKImageExportBase.cxx
> 
>  
> 
> void VTKImageExportBase::UpdateInformationCallbackFunction(void* userData)
> 
> {
> 
>   static_cast<VTKImageExportBase*>(userData)->
> 
>     UpdateInformationCallback();
> 
> }
> 
>  
> 
> and at calling UpdateInformationCallback() it fails with a unhandled 
> exception! Seems to me the cast goes wrong because some information on 
> the image type is missing. But what?
> 
>  
> 
> :-) sven
> 
> --
> 
>  
>