[Insight-users] Itk Segmentation Fault
akbarjan bayiz
abayiz at yahoo.com
Fri Mar 25 16:06:49 EDT 2011
Hello,
Thank you for your reply again.
I am using ITK-3.20.0 version.
The source code is as follows, in it, the "ABC Dicom Loader" is a VTK based data loader. Hope it could be helpful to figure out its error.:
******************************************************************************************
#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif
#ifdef __BORLANDC__
#define ITK_LEAN_AND_MEAN
#endif
//for DCMTK
#define HAVE_CONFIG_H 1
#include <osconfig.h>
#include <iostream>
#include <fstream>
#include <sstream>
#include <cstring>
#include <string>
#include <stdio.h>
#include <stdlib.h>
#include "BaseData.h"
//for casting to float and converting to ITK image
#include "vtkImageCast.h"
#include "./Headerfiles/itkVTKImageToImageFilter.h"
#include "itkCastImageFilter.h"
//for geodesic active contor segmentation
#include "itkGradientAnisotropicDiffusionImageFilter.h"
#include "itkScalarImageToHistogramGenerator.h"
//for itk images
#include "itkImage.h"
#include "itkImageRegionIterator.h"
#include "itkImageRegionConstIterator.h"
#include "itkCovariantVector.h"
#include "itkImageRegionConstIterator.h"
#include "itkImageRegionIterator.h"
//for itk vector
#include "itkVector.h"
//modified ITK classes
#include "./Headerfiles/VavGradientVectorFlowImageFilter.h"
#include "./Headerfiles/VavEdgeEnhancingDiffusionImageFilter.h"
//header files for reading/writing dicom series
#include "itkImage.h"
#include "itkGDCMImageIO.h"
#include "itkOrientedImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkImageSeriesReader.h"
#include "itkGDCMSeriesFileNames.h"
//header files for Demons algorithm registration
#include "itkCastImageFilter.h"
#include "itkWarpImageFilter.h"
#include "itkHistogramMatchingImageFilter.h"
#include "itkLinearInterpolateImageFunction.h"
#include "itkSymmetricForcesDemonsRegistrationFilter.h"
#include "itkDemonsRegistrationFilter.h"
//for reading images
#include <QtGui>
#include "DICOMLoader.h"
//#include "dicomviewer2d.h"
#include "ContentTree.h"
#include "ContentItem.h"
#include <QtUiTools>
#include "/usr/local/include/vtk-5.2/QVTKWidget.h"
#include <QMainWindow>
#include <QVBoxLayout>
#include <QSlider>
#include <QtGui>
//for converting itk result back to vtk
#include "itkImageToVTKImageFilter.h"
//for interpolator
#include "itkContinuousIndex.h"
#include "itkLinearInterpolateImageFunction.h"
// image types
const unsigned int Dimension = 3;
typedef unsigned short PixelType;
typedef float InternalPixelType;
typedef itk::Image< InternalPixelType, Dimension > InternalImageType;
typedef itk::Image< float, 3 > ImageType;
typedef itk::Image< float, 3 > ShapeImageType;
typedef itk::Image< int, 3 > IntegerImageType;
typedef itk::VTKImageToImageFilter<ImageType> VTKImageToImageType;
typedef itk::Image< unsigned char, 3 > OutputImageType;// 2D output image for .png writers
typedef itk::CovariantVector<float, 3> GradientPixelType;
typedef itk::Image<GradientPixelType,3> GradientImageType;
typedef itk::Image< PixelType, Dimension > FixedImageType;
typedef itk::Image< PixelType, Dimension > MovingImageType;
// region iterators
typedef itk::ImageRegionConstIterator<ImageType> ImageConstIterator;
typedef itk::ImageRegionConstIterator<OutputImageType> ImageConstIteratorChar;
typedef itk::ImageRegionIterator<ImageType> ImageIterator;
typedef itk::ImageRegionIterator<GradientImageType> DerivativeImageIterator;
//define filters
typedef itk::VTKImageToImageFilter < InternalImageType > VTK2ITKConnectorFilterType;
//matchingfilter type
typedef itk::HistogramMatchingImageFilter<InternalImageType, InternalImageType > MatchingFilterType;
//deformation field type
typedef itk::Vector< float, Dimension > VectorPixelType;
typedef itk::Image< VectorPixelType, Dimension > DeformationFieldType;
// writers
typedef itk::ImageFileWriter< OutputImageType > WriterType;
typedef itk::ImageFileWriter< ImageType > WriterTypeDouble;
typedef itk::ImageFileWriter< GradientImageType > WriterTypeGradient;
typedef itk::ImageFileWriter< DeformationFieldType > FieldWriterType;
// reader
typedef itk::ImageFileReader< ImageType > ReaderType;
typedef itk::ImageFileReader< GradientImageType > ReaderTypeGradient;
//registration filter type
typedef itk::SymmetricForcesDemonsRegistrationFilter
<InternalImageType,InternalImageType,DeformationFieldType> RegistrationFilterType;
//warper type
typedef itk::WarpImageFilter<
ImageType,
ImageType,
DeformationFieldType> WarperType;
//interpolator type
typedef itk::LinearInterpolateImageFunction<
ImageType, double> InterpolatorType;
//output image type
typedef unsigned char OutputPixelType;
typedef itk::Image< OutputPixelType, Dimension > OutputImageType;
//castfilter type
typedef itk::CastImageFilter<
ImageType,
OutputImageType > CastFilterType;
//itk to vtk connector
typedef itk::ImageToVTKImageFilter< ImageType > ConnectorType;
using namespace std;
//implements a command observer
class CommandIterationUpdate : public itk::Command
{
public:
typedef CommandIterationUpdate Self;
typedef itk::Command Superclass;
typedef itk::SmartPointer<CommandIterationUpdate> Pointer;
itkNewMacro( CommandIterationUpdate );
protected:
CommandIterationUpdate() {};
// typedef itk::Image< float, 3 > InternalImageType;
typedef itk::Image< InternalPixelType, Dimension > InternalImageType;
typedef itk::Vector< float, 3 > VectorPixelType;
typedef itk::Image< VectorPixelType, 3 > DeformationFieldType;
public:
void Execute(itk::Object *caller, const itk::EventObject & event)
{
Execute( (const itk::Object *)caller, event);
}
void Execute(const itk::Object * object, const itk::EventObject & event)
{
const RegistrationFilterType * filter =
dynamic_cast< const RegistrationFilterType * >( object );
if( !(itk::IterationEvent().CheckEvent( &event )) )
{
return;
}
cout << filter->GetMetric() <<endl;
}
};
int main( int argc, char * argv[] )
{
//////////////////////////////////////////////
//////////// READING IMAGES /////////////////
////////////////////////////////////////////
/////////////////////////////////////
/// Reading the fixed image ////////
///////////////////////////////////
ABC::Loader::DICOMLoader* fixedImageLoader = new ABC::Loader::DICOMLoader();
ABC::Data::BaseData* fixedImageData= new ABC::Data::BaseData();
ContentTree *fixedImageTree;
ContentItem* fixedImageItem;
fixedImageTree=NULL;
fixedImageTree = fixedImageLoader->LoadDataContent(argv[1]);
fixedImageItem = fixedImageTree->GetFistItem()->GetFirstItem(); // first study first series
cout << " 1.2" << fixedImageItem->GetDescription() << endl;
cout << " 1.2" << fixedImageItem->GetID() << endl;
cout << " 1.2" << fixedImageItem->GetFSPath() << endl;
/// LOAD DATA !!!
fixedImageData = fixedImageLoader->LoadData(fixedImageItem);
vector<int> dimensionsFixed = fixedImageData->GetScalar()->GetDimensions();
printf("Dimensions %d %d %d \n", dimensionsFixed[0], dimensionsFixed[1], dimensionsFixed[2]);
vector<double> SpacingFixed = fixedImageData->GetScalar()->GetSpacing();
printf("Spacing %f %f %f \n", SpacingFixed[0], SpacingFixed[1], SpacingFixed[2]);
cout<<"The end! "<<endl<<endl<<endl;
double* bndFixed = fixedImageData->GetScalar()->GetImageData()->GetBounds();
printf("Bounds1 %lf %lf %lf %lf %lf %lf \n", bndFixed[0], bndFixed[1], bndFixed[2], bndFixed[3], bndFixed[4], bndFixed[5]);
vtkImageCast* VtkCasterFixed = vtkImageCast::New(); // Casting to float in VTK is needed
VtkCasterFixed->SetInput(fixedImageData -> GetScalar() -> GetImageData());
VtkCasterFixed->SetOutputScalarTypeToFloat();
VTK2ITKConnectorFilterType::Pointer VTK2ITKconnectorFixed = VTKImageToImageType::New();
VTK2ITKconnectorFixed->SetInput( VtkCasterFixed->GetOutput());
VTK2ITKconnectorFixed->GetImporter()->Update();
/////////////////////////////////////
/// Reading the moving image ////////
///////////////////////////////////
ABC::Loader::DICOMLoader* movingImageLoader = new ABC::Loader::DICOMLoader();
ABC::Data::BaseData* movingImageData= new ABC::Data::BaseData();
ContentTree *movingImageTree;
ContentItem* movingImageItem;
movingImageTree = movingImageLoader->LoadDataContent(argv[2]);
movingImageItem = movingImageTree->GetFistItem()->GetFirstItem(); // first study first series
cout << " 1.2" << movingImageItem->GetDescription() << endl;
cout << " 1.2" << movingImageItem->GetID() << endl;
cout << " 1.2" << movingImageItem->GetFSPath() << endl;
/// LOAD DATA !!!
movingImageData = movingImageLoader->LoadData(fixedImageItem);
vector<int> dimensionsMoving = movingImageData->GetScalar()->GetDimensions();
printf("Dimensions %d %d %d \n", dimensionsMoving[0], dimensionsMoving[1], dimensionsMoving[2]);
vector<double> SpacingMoving = movingImageData->GetScalar()->GetSpacing();
printf("Spacing %f %f %f \n", SpacingMoving[0], SpacingMoving[1], SpacingMoving[2]);
double* bndMoving = movingImageData->GetScalar()->GetImageData()->GetBounds();
printf("Bounds1 %lf %lf %lf %lf %lf %lf \n", bndMoving[0], bndMoving[1], bndMoving[2], bndMoving[3], bndMoving[4], bndMoving[5]);
//
vtkImageCast* VtkCasterMoving = vtkImageCast::New(); // Casting to float in VTK is needed
VtkCasterMoving->SetInput(movingImageData -> GetScalar() -> GetImageData());
VtkCasterMoving->SetOutputScalarTypeToFloat();
VTK2ITKConnectorFilterType::Pointer VTK2ITKconnectorMoving = VTKImageToImageType::New();
VTK2ITKconnectorMoving->SetInput( VtkCasterMoving->GetOutput());
VTK2ITKconnectorMoving->GetImporter()->Update();
/////////////////////////////////////////////
// Demons registration starts from here ////
///////////////////////////////////////////
// The basic idea is to match the histograms of the two images at a user-specified number of quantile values.
MatchingFilterType::Pointer matcher = MatchingFilterType::New();
matcher->SetInput( VTK2ITKconnectorMoving->GetOutput() );
matcher->SetReferenceImage( VTK2ITKconnectorFixed->GetOutput() );
//select the number of bins to represent the histograms and the
// number of points or quantile values where the histogram is to be
// matched.
matcher->SetNumberOfHistogramLevels( 1024 );
matcher->SetNumberOfMatchPoints( 7 );
// Simple background extraction is done by thresholding at the mean intensity.
matcher->ThresholdAtMeanIntensityOn();
//the deformation field is represented as an image whose pixels are floating point vectors.
RegistrationFilterType::Pointer filter = RegistrationFilterType::New();
//Create the Command observer and register it with the registration filter.
CommandIterationUpdate::Pointer observer = CommandIterationUpdate::New();
filter->AddObserver( itk::IterationEvent(), observer );
filter->SetFixedImage( VTK2ITKconnectorFixed->GetOutput() );
filter->SetMovingImage( matcher->GetOutput() );
filter->SetNumberOfIterations( 1.0 );
filter->SetStandardDeviations( 1.0 );
filter->Update(); //the filter output is the computed deformation field
//warp the moving image with the output deformation field
WarperType::Pointer warper = WarperType::New();
InterpolatorType::Pointer interpolator = InterpolatorType::New();
typedef itk::CastImageFilter< ImageType, InternalImageType > FixedImageCasterType;
typedef itk::CastImageFilter< ImageType, InternalImageType > MovingImageCasterType;
FixedImageCasterType::Pointer fixedImageCaster = FixedImageCasterType::New();
MovingImageCasterType::Pointer movingImageCaster = MovingImageCasterType::New();
fixedImageCaster->SetInput( VTK2ITKconnectorFixed->GetOutput() );
movingImageCaster->SetInput( VTK2ITKconnectorMoving->GetOutput() );
ImageType::Pointer fixedImage = fixedImageCaster->GetOutput();
ImageType::Pointer movingImage = movingImageCaster->GetOutput();
warper->SetInput( movingImage );
warper->SetInterpolator( interpolator );
warper->SetOutputSpacing( fixedImage->GetSpacing() );
warper->SetOutputOrigin( fixedImage->GetOrigin() );
warper->SetOutputDirection( fixedImage->GetDirection() );
warper->SetDeformationField( filter->GetOutput() );
// Write warped image out to file
WriterType::Pointer writer = WriterType::New();
CastFilterType::Pointer caster = CastFilterType::New();
writer->SetFileName( argv[3] );
caster->SetInput( warper->GetOutput() );
writer->SetInput( caster->GetOutput() );
writer->Update();
if( argc > 4 )
{
FieldWriterType::Pointer fieldWriter = FieldWriterType::New();
fieldWriter->SetFileName( argv[6] );
fieldWriter->SetInput( filter->GetOutput() );
fieldWriter->Update();
}
return EXIT_SUCCESS;
}
--- On Fri, 3/25/11, Ariel Hernán Curiale <curiale at gmail.com> wrote:
From: Ariel Hernán Curiale <curiale at gmail.com>
Subject: Re: [Insight-users] Itk Segmentation Fault
To: "akbarjan bayiz" <abayiz at yahoo.com>
Cc: insight-users at itk.org
Date: Friday, March 25, 2011, 7:21 AM
What ITK version are running ?
Another things is that "DeformationPixelType" need to be a float or double.
Please post the source code.
SaludosAriel Hernán Curialewww.curiale.com.ar
El 25/03/2011, a las 12:54, akbarjan bayiz escribió:
Hello!
Thank you for your reply.
I've done it as you said, changed the data types, but unfortunately it gives out a full screen of error now. I am trying to read two dicom series from two different directories, and perform Demons registration, then save the output as a new dicom file. Maybe this is different from what you are doing, and that is why your data type does not work for my code I think.
Do you have any other good suggestions?
Best Regards,
--- On Fri, 3/25/11, Ariel Hernán Curiale <curiale at gmail.com> wrote:
From: Ariel Hernán Curiale <curiale at gmail.com>
Subject: Re: [Insight-users] Itk Segmentation Fault
To: "akbarjan bayiz" <abayiz at yahoo.com>
Cc:
insight-users at itk.org
Date: Friday, March 25, 2011, 5:34 AM
Hi, Im recently used this class and I had no errors. This is my code:
typedef itk::Image<PixelType,3> ImageType; typedef itk::Image< itk::Vector<DeformationPixelType, 3>, 3> DeformationFieldType; typedef
itk::DiffeomorphicDemonsRegistrationFilter <ImageType, ImageType, DeformationFieldType> FilterType;
typename FilterType::Pointer filter = FilterType::New();
filter->SetFixedImage(img_diastole->GetOutput()); filter->SetMovingImage(img_sistole->GetOutput()); filter->Update();
typename DeformationFieldType::Pointer deformacion = filter->GetDeformationField();
SaludosAriel Hernán Curialewww.curiale.com.ar
El 25/03/2011, a las 10:00, akbarjan bayiz escribió:
Hello all!
I am using Demons algorithm to perform registration of two DICOM series. My code compiles without any error. And when I run it, at the final step, when it updates the filter, there is a "0" on the screen, and gives error message saying "Segmentation Fault".
What should I do for it?
Thank you in advance!
_____________________________________
Powered by www.kitware.com
Visit other Kitware open-source projects at
http://www.kitware.com/opensource/opensource.html
Kitware offers ITK Training Courses, for more information
visit:
http://www.kitware.com/products/protraining.html
Please keep messages on-topic and check the ITK FAQ at:
http://www.itk.org/Wiki/ITK_FAQ
Follow this link to subscribe/unsubscribe:
http://www.itk.org/mailman/listinfo/insight-users
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://www.itk.org/pipermail/insight-users/attachments/20110325/38acd881/attachment.htm>
More information about the Insight-users
mailing list