how to connect ITK to VTK with c++?

2019-02-25 10:58发布

问题:

I am a beginner in ITK, VTK and Qt. I use visual studio 9. I am trying to read a DICOM series with ITK and display with VTK in QVTKWidget (Qt). I based on this code http://www.itk.org/Wiki/VTK/Examples/Cxx/IO/ReadDICOMSeries and I modified according to my needs.

when I read the series DICOM with VTK and display it in QVTKWidget it works, but when I want to read this series with ITK and display with VTK in QVTKWidget, the program displays the first image of series and when I go to the next image with the mouse wheel, the program crashes.

when I debugged, I got this error:

void VTKImageExportBase::UpdateInformationCallbackFunction(void *userData)
{
static_cast< VTKImageExportBase * >
( userData )->UpdateInformationCallback();//the error is here
}

I tried ausssi ImageToVTKImageFilter class but with the same problem.

here is my code:

void essaieAppQtVTK::drawDCMSeries(std::string folderDCM)
{
typedef unsigned short    PixelType;
const unsigned int      Dimension = 3;

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

typedef itk::VTKImageExport<ImageType> ImageExportType; 
typedef itk::ImageSeriesReader< ImageType >        ReaderType;
ReaderType::Pointer reader = ReaderType::New();
typedef itk::GDCMImageIO       ImageIOType;
ImageIOType::Pointer dicomIO = ImageIOType::New(); 

reader->SetImageIO( dicomIO );
typedef itk::GDCMSeriesFileNames NamesGeneratorType;
NamesGeneratorType::Pointer nameGenerator = NamesGeneratorType::New();

nameGenerator->SetUseSeriesDetails( true );
nameGenerator->AddSeriesRestriction("0008|0021" );  
nameGenerator->SetDirectory( folderDCM);

typedef std::vector< std::string >    SeriesIdContainer;    
const SeriesIdContainer & seriesUID = nameGenerator->GetSeriesUIDs();   
std::cout << seriesUID.size() << std::endl;
SeriesIdContainer::const_iterator seriesItr = seriesUID.begin();
SeriesIdContainer::const_iterator seriesEnd = seriesUID.end();
while( seriesItr != seriesEnd )
{
std::cout << seriesItr->c_str() << std::endl;
seriesItr++;
}
std::string seriesIdentifier;
seriesIdentifier = seriesUID.begin()->c_str();
std::cout << seriesIdentifier.c_str() << std::endl;

typedef std::vector< std::string >   FileNamesContainer;
FileNamesContainer fileNames; 
fileNames = nameGenerator->GetFileNames( seriesIdentifier ); 

reader->SetFileNames( fileNames );
try
{
reader->Update();
}
catch (itk::ExceptionObject &ex)
{
std::cout << ex << std::endl;
}

//------------------------------------------------------------------------
// ITK to VTK pipeline connection.
//------------------------------------------------------------------------

// Create the itk::VTKImageExport instance and connect it to the
// itk::CurvatureFlowImageFilter.
ImageExportType::Pointer exporter = ImageExportType::New();
exporter->SetInput(reader->GetOutput());

// Create the vtkImageImport and connect it to the
// itk::VTKImageExport instance.
vtkImageImport* importer = vtkImageImport::New();  
ConnectPipelines(exporter, importer);

//------------------------------------------------------------------------
// VTK pipeline.
//------------------------------------------------------------------------

this->imageViewer= vtkImageViewer2::New();
imageViewer->SetInput(importer->GetOutput());


// slice status message
//******same code *****//

// usage hint message
//******same code *****//

// create an interactor with our own style (inherit from vtkInteractorStyleImage)
// in order to catch mousewheel and key events
//******same code *****//

// add slice status message and usage hint message to the renderer
//******same code *****//

//to display the result: 
ui.qvtkWidget->SetRenderWindow(imageViewer->GetRenderWindow()); 
ui.qvtkWidget->GetRenderWindow()->GetInteractor()-   >SetInteractorStyle(myInteractorStyle); 
imageViewer->Render(); 
ui.qvtkWidget->update();
}

Optional: My exporter and importer are as given below:

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());
}
/**
* This function will connect the given vtkImageExport filter to
* the given itk::VTKImageImport filter.
*/
template <typename VTK_Exporter, typename ITK_Importer>
void ConnectPipelines(VTK_Exporter* exporter, ITK_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());
}

Maybe there is a fault at the Pipline between ITK and VTK, please please can help me to find a solution for this problem, I spent two weeks looking for a solution but all methods have failed, maybe there has another method to bind ITK and VTK apart ImageToVTKImageFilter class. I count on your help. thank you in advance.

回答1:

I faced same error at the same point you showed inside void VTKImageExportBase::UpdateInformationCallbackFunction function I was using ImageToVTKImageFilter for conversion. What I was missing is update of ITK pipeline before connecting it to VTK viewer. In ITK to VTK pipeline connection part your code is also missing same thing. So use:

exporter->SetInput(reader->GetOutput());
exporter->Update();

then go for further.