Hi Luis,<br><br>I made some small corrections in my email below <i style="color: rgb(255, 0, 0);"><b>in red</b></i>. Also this time I remembered to carbon copy the insight users mailing list.<br><br>thanks,<br>john<br><br>
<div class="gmail_quote">On Thu, Oct 15, 2009 at 10:52 AM, John Drozd <span dir="ltr"><<a href="mailto:john.drozd@gmail.com">john.drozd@gmail.com</a>></span> wrote:<br><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
Hello Luis,<br><br>I used itkInverseDeformationFieldImageFilter.h to calculate an inverse deformation field from the moving subject to the fixed subject. I tried to read in the deformation field in vtk format but reading the deformationField.vtk file took forever so I included calculating the inverse deformation field as the last part of the original deformable
registration code that first calculated the deformation field (from the
fixed subject to the moving subject). I was wondering about setting the index of the region. I arbitrarily set it to 0,0,0 as done in the testing code Insight/Testing/Code/BasicFilters/itkInverseDeformationFieldImageFilterTest.cxx<br>
Does the choice of the index make a difference? Also it took almost 1 hour to calculate the inverse deformation field. I was wondering if there is a way to optimize the code so that it can run on the 8 processors on my computer. I noticed that the registration procedure used the 8 processors.<br>
<br>To validate my code, I would like <i style="color: rgb(255, 0, 0);"><b>to use the inverse deformation field</b></i> <i style="color: rgb(255, 0, 0);"><b>that my code has calculated</b></i> to next translate physical coordinates of points from the moving subject to physical coordinates of points in the fixed subject and then back to the moving subject to see if I get back the original point (location). Is there any example code that would show me how to do this? I am currently looking over ThinPlateSplineWarp.cxx which transforms some points.<br>
<br>Eventually, I want to use the moving subject as an atlas and use the inverse deformation field to translate physical coordinates of points from ventricles in the moving subject atlas to physical coordinates of points of ventricles in the fixed subject, and then use these as seeds in the fixed subject in a region growing algorithm to segment the ventricles <i style="color: rgb(255, 0, 0);"><b>and calculate the volumes</b></i> <i style="color: rgb(255, 0, 0);"><b>of the ventricles</b></i> in the fixed subject. This way I can have an automated segmentation code.<br>
<br>thank you<br>john<br><br>Below is my code<br><br>#if defined(_MSC_VER)<br>#pragma warning ( disable : 4786 )<br>#endif<br><br><br>/*<br>./DeformableRegistration8andinverse correctedsubject1.dcm correctedsubject2.dcm outputImage.mhd differenceOutput.mhd differenceBeforeRegistration.mhd deformationField.vtk inversedeformationField.vtk<br>
*/<br><br><br>// Software Guide : BeginLatex<br>//<br>// This example illustrates the use of the \doxygen{BSplineDeformableTransform}<br>// class for performing registration of two $3D$ images and for the case of<br>// multi-modality images. The image metric of choice in this case is the<br>
// \doxygen{MattesMutualInformationImageToImageMetric}.<br>//<br>// \index{itk::BSplineDeformableTransform}<br>// \index{itk::BSplineDeformableTransform!DeformableRegistration}<br>// \index{itk::LBFGSBOptimizer}<br>//<br>
//<br>// Software Guide : EndLatex <br><br>#include "itkImageRegistrationMethod.h"<br>#include "itkMattesMutualInformationImageToImageMetric.h"<br>#include "itkLinearInterpolateImageFunction.h"<br>
#include "itkImage.h"<br><br>#include "itkTimeProbesCollectorBase.h"<br><br>//added per itkInverseDeformationFieldImageFilterTest.cxx<br>#include "itkVector.h"<br>#include "itkInverseDeformationFieldImageFilter.h"<br>
#include "itkImageRegionIteratorWithIndex.h"<br>//#include "itkImageFileWriter.h"<br>#include "itkFilterWatcher.h"<br>//end of added code<br><br>#ifdef ITK_USE_REVIEW<br>#include "itkMemoryProbesCollectorBase.h"<br>
#define itkProbesCreate() \<br> itk::TimeProbesCollectorBase chronometer; \<br> itk::MemoryProbesCollectorBase memorymeter<br>#define itkProbesStart( text ) memorymeter.Start( text ); chronometer.Start( text )<br>#define itkProbesStop( text ) chronometer.Stop( text ); memorymeter.Stop( text )<br>
#define itkProbesReport( stream ) chronometer.Report( stream ); memorymeter.Report( stream )<br>#else<br>#define itkProbesCreate() \<br> itk::TimeProbesCollectorBase chronometer<br>#define itkProbesStart( text ) chronometer.Start( text )<br>
#define itkProbesStop( text ) chronometer.Stop( text )<br>#define itkProbesReport( stream ) chronometer.Report( stream )<br>#endif<br><br><br>// Software Guide : BeginLatex<br>// <br>// The following are the most relevant headers to this example.<br>
//<br>// \index{itk::BSplineDeformableTransform!header}<br>// \index{itk::LBFGSBOptimizer!header}<br>// <br>// Software Guide : EndLatex <br><br>// Software Guide : BeginCodeSnippet<br>#include "itkBSplineDeformableTransform.h"<br>
#include "itkLBFGSBOptimizer.h"<br>// Software Guide : EndCodeSnippet<br><br>// Software Guide : BeginLatex<br>// <br>// The parameter space of the \code{BSplineDeformableTransform} is composed by<br>// the set of all the deformations associated with the nodes of the BSpline<br>
// grid. This large number of parameters makes possible to represent a wide<br>// variety of deformations, but it also has the price of requiring a<br>// significant amount of computation time.<br>//<br>// \index{itk::BSplineDeformableTransform!header}<br>
// <br>// Software Guide : EndLatex <br><br>#include "itkImageFileReader.h"<br>#include "itkImageFileWriter.h"<br><br>#include "itkResampleImageFilter.h"<br>#include "itkCastImageFilter.h"<br>
#include "itkSquaredDifferenceImageFilter.h"<br><br><br>#include "itkTransformFileReader.h"<br><br>//added from ReadAtlasAndSubject.cxx<br>#include "itkGDCMImageIO.h"<br>//end of added code<br>
<br>// The following section of code implements a Command observer<br>// used to monitor the evolution of the registration process.<br>//<br>#include "itkCommand.h"<br>class CommandIterationUpdate : public itk::Command <br>
{<br>public:<br> typedef CommandIterationUpdate Self;<br> typedef itk::Command Superclass;<br> typedef itk::SmartPointer<Self> Pointer;<br> itkNewMacro( Self );<br>protected:<br> CommandIterationUpdate() {};<br>
public:<br> typedef itk::LBFGSBOptimizer OptimizerType;<br> typedef const OptimizerType * OptimizerPointer;<br><br> void Execute(itk::Object *caller, const itk::EventObject & event)<br> {<br> Execute( (const itk::Object *)caller, event);<br>
}<br><br> void Execute(const itk::Object * object, const itk::EventObject & event)<br> {<br> OptimizerPointer optimizer = <br> dynamic_cast< OptimizerPointer >( object );<br> if( !(itk::IterationEvent().CheckEvent( &event )) )<br>
{<br> return;<br> }<br> std::cout << optimizer->GetCurrentIteration() << " ";<br> std::cout << optimizer->GetCachedValue() << " ";<br>
std::cout << optimizer->GetInfinityNormOfProjectedGradient() << std::endl;<br>
}<br>};<br><br><br>int main( int argc, char *argv[] )<br>{<br> if( argc < 4 )<br> {<br> std::cerr << "Missing Parameters " << std::endl;<br> std::cerr << "Usage: " << argv[0];<br>
std::cerr << " fixedImageFile movingImageFile outputImagefile ";<br> std::cerr << " [differenceOutputfile] [differenceBeforeRegistration] ";<br> <br> //std::cerr << " [deformationField] ";<br>
std::cerr << " [deformationField] " << " [inversedeformationField] ";<br> <br> std::cerr << " [useExplicitPDFderivatives ] [useCachingBSplineWeights ] ";<br> std::cerr << " [filenameForFinalTransformParameters] ";<br>
std::cerr << std::endl;<br> return EXIT_FAILURE;<br> }<br> <br> const unsigned int ImageDimension = 3;<br> typedef signed short PixelType;<br><br> typedef itk::Image< PixelType, ImageDimension > FixedImageType;<br>
typedef itk::Image< PixelType, ImageDimension > MovingImageType;<br><br><br> // Software Guide : BeginLatex<br> //<br> // We instantiate now the type of the \code{BSplineDeformableTransform} using<br> // as template parameters the type for coordinates representation, the<br>
// dimension of the space, and the order of the BSpline. <br> // <br> // \index{BSplineDeformableTransform!New}<br> // \index{BSplineDeformableTransform!Instantiation}<br> //<br> // Software Guide : EndLatex <br>
<br> // Software Guide : BeginCodeSnippet<br> const unsigned int SpaceDimension = ImageDimension;<br> const unsigned int SplineOrder = 3;<br> typedef double CoordinateRepType;<br><br> typedef itk::BSplineDeformableTransform<<br>
CoordinateRepType,<br> SpaceDimension,<br> SplineOrder > TransformType;<br> // Software Guide : EndCodeSnippet<br><br><br> typedef itk::LBFGSBOptimizer OptimizerType;<br>
<br><br> typedef itk::MattesMutualInformationImageToImageMetric< <br> FixedImageType, <br> MovingImageType > MetricType;<br><br> typedef itk:: LinearInterpolateImageFunction< <br>
MovingImageType,<br> double > InterpolatorType;<br><br> typedef itk::ImageRegistrationMethod< <br> FixedImageType, <br>
MovingImageType > RegistrationType;<br><br> MetricType::Pointer metric = MetricType::New();<br> OptimizerType::Pointer optimizer = OptimizerType::New();<br>
InterpolatorType::Pointer interpolator = InterpolatorType::New();<br> RegistrationType::Pointer registration = RegistrationType::New();<br> <br><br> registration->SetMetric( metric );<br> registration->SetOptimizer( optimizer );<br>
registration->SetInterpolator( interpolator );<br><br><br> // Software Guide : BeginLatex<br> //<br> // The transform object is constructed below and passed to the registration<br> // method.<br> // \index{itk::RegistrationMethod!SetTransform()}<br>
//<br> // Software Guide : EndLatex <br><br> // Software Guide : BeginCodeSnippet<br> TransformType::Pointer transform = TransformType::New();<br> registration->SetTransform( transform );<br> // Software Guide : EndCodeSnippet<br>
<br><br> //<br> // In general, you must first solve an Affine registration between<br> // the images before attempting to solve a deformable registration.<br> // If you have solve an affine transform, it can be loaded into the<br>
// BSplineDeformableTransform as a "bulk" transform that will be<br> // pre-composed with the deformation computed by the BSpline.<br> // The following code loads one of such initial transforms if they<br>
// are available.<br> // <br> typedef itk::TransformFileReader TransformReaderType;<br> typedef itk::AffineTransform<double, 3> AffineTransformType;<br><br> TransformReaderType::Pointer transformReader = TransformReaderType::New();<br>
<br> if( argc > 11 )<br> {<br> std::cout << "Loading Transform: " << argv[11] << std::endl;<br> transformReader->SetFileName( argv[11] );<br> transformReader->Update();<br>
<br> typedef TransformReaderType::TransformListType * TransformListType;<br> TransformListType transforms = transformReader->GetTransformList();<br> TransformReaderType::TransformListType::const_iterator tit = transforms->begin();<br>
if( !strcmp((*tit)->GetNameOfClass(),"AffineTransform") )<br> {<br> AffineTransformType::Pointer affine_read = <br> static_cast<AffineTransformType*>((*tit).GetPointer());<br>
AffineTransformType::Pointer affine_transform = <br> dynamic_cast< AffineTransformType * >( affine_read.GetPointer() );<br> <br> if( affine_transform )<br> {<br> transform->SetBulkTransform( affine_transform );<br>
}<br> } <br> else<br> {<br> std::cerr << "Bulk transform wasn't an affine transform." << std::endl;<br> return EXIT_FAILURE;<br> }<br> }<br><br><br> typedef itk::ImageFileReader< FixedImageType > FixedImageReaderType;<br>
typedef itk::ImageFileReader< MovingImageType > MovingImageReaderType;<br><br> FixedImageReaderType::Pointer fixedImageReader = FixedImageReaderType::New();<br> MovingImageReaderType::Pointer movingImageReader = MovingImageReaderType::New();<br>
<br> fixedImageReader->SetFileName( argv[1] );<br> movingImageReader->SetFileName( argv[2] );<br><br> //added from ReadAtlasAndSubject.cxx<br> typedef itk::GDCMImageIO ImageIOTypefixed;<br> ImageIOTypefixed::Pointer gdcmImageIOfixed = ImageIOTypefixed::New();<br>
fixedImageReader->SetImageIO( gdcmImageIOfixed );<br><br> typedef itk::GDCMImageIO ImageIOTypemoving;<br> ImageIOTypemoving::Pointer gdcmImageIOmoving = ImageIOTypemoving::New();<br> movingImageReader->SetImageIO( gdcmImageIOmoving );<br>
//end of added code<br><br> movingImageReader->Update();<br> fixedImageReader->Update();<br><br> FixedImageType::ConstPointer fixedImage = fixedImageReader->GetOutput();<br><br> //added<br> MovingImageType::ConstPointer movingImage = movingImageReader->GetOutput();<br>
<br> registration->SetFixedImage( fixedImage );<br> registration->SetMovingImage( movingImageReader->GetOutput() );<br><br> //fixedImageReader->Update();<br><br> FixedImageType::RegionType fixedRegion = fixedImage->GetBufferedRegion();<br>
<br> registration->SetFixedImageRegion( fixedRegion );<br><br> // Software Guide : BeginLatex<br> //<br> // Here we define the parameters of the BSplineDeformableTransform grid. We<br> // arbitrarily decide to use a grid with $5 \times 5$ nodes within the image. <br>
// The reader should note that the BSpline computation requires a<br> // finite support region ( 1 grid node at the lower borders and 2<br> // grid nodes at upper borders). Therefore in this example, we set<br> // the grid size to be $8 \times 8$ and place the grid origin such that<br>
// grid node (1,1) coincides with the first pixel in the fixed image.<br> // <br> // \index{BSplineDeformableTransform}<br> //<br> // Software Guide : EndLatex <br><br> unsigned int numberOfGridNodesInOneDimension = 5;<br>
<br> if( argc > 10 )<br> {<br> numberOfGridNodesInOneDimension = atoi( argv[10] );<br> }<br><br> // Software Guide : BeginCodeSnippet<br> typedef TransformType::RegionType RegionType;<br> RegionType bsplineRegion;<br>
RegionType::SizeType gridSizeOnImage;<br> RegionType::SizeType gridBorderSize;<br> RegionType::SizeType totalGridSize;<br><br> gridSizeOnImage.Fill( numberOfGridNodesInOneDimension );<br> gridBorderSize.Fill( SplineOrder ); // Border for spline order = 3 ( 1 lower, 2 upper )<br>
totalGridSize = gridSizeOnImage + gridBorderSize;<br><br> bsplineRegion.SetSize( totalGridSize );<br> // Software Guide : EndCodeSnippet<br><br><br> // Software Guide : BeginCodeSnippet<br> typedef TransformType::SpacingType SpacingType;<br>
SpacingType spacing = fixedImage->GetSpacing();<br><br> typedef TransformType::OriginType OriginType;<br> OriginType origin = fixedImage->GetOrigin();;<br><br> FixedImageType::SizeType fixedImageSize = fixedRegion.GetSize();<br>
<br> for(unsigned int r=0; r<ImageDimension; r++)<br> {<br> spacing[r] *= static_cast<double>(fixedImageSize[r] - 1) / <br> static_cast<double>(gridSizeOnImage[r] - 1);<br> }<br>
<br> FixedImageType::DirectionType gridDirection = fixedImage->GetDirection();<br> SpacingType gridOriginOffset = gridDirection * spacing;<br><br> OriginType gridOrigin = origin - gridOriginOffset; <br><br> transform->SetGridSpacing( spacing );<br>
transform->SetGridOrigin( gridOrigin );<br> transform->SetGridRegion( bsplineRegion );<br> transform->SetGridDirection( gridDirection );<br> <br><br> typedef TransformType::ParametersType ParametersType;<br>
<br> const unsigned int numberOfParameters =<br> transform->GetNumberOfParameters();<br> <br> ParametersType parameters( numberOfParameters );<br><br> parameters.Fill( 0.0 );<br><br> transform->SetParameters( parameters );<br>
// Software Guide : EndCodeSnippet<br><br><br><br> // Software Guide : BeginLatex<br> // <br> // We now pass the parameters of the current transform as the initial<br> // parameters to be used when the registration process starts. <br>
//<br> // Software Guide : EndLatex <br><br> // Software Guide : BeginCodeSnippet<br> registration->SetInitialTransformParameters( transform->GetParameters() );<br> // Software Guide : EndCodeSnippet<br><br>
<br>
// Software Guide : BeginLatex<br> // <br> // Next we set the parameters of the LBFGSB Optimizer. <br> //<br> // Software Guide : EndLatex <br><br><br> // Software Guide : BeginCodeSnippet<br> OptimizerType::BoundSelectionType boundSelect( transform->GetNumberOfParameters() );<br>
OptimizerType::BoundValueType upperBound( transform->GetNumberOfParameters() );<br> OptimizerType::BoundValueType lowerBound( transform->GetNumberOfParameters() );<br><br> boundSelect.Fill( 0 );<br> upperBound.Fill( 0.0 );<br>
lowerBound.Fill( 0.0 );<br><br> optimizer->SetBoundSelection( boundSelect );<br> optimizer->SetUpperBound( upperBound );<br> optimizer->SetLowerBound( lowerBound );<br><br> optimizer->SetCostFunctionConvergenceFactor( 1.e7 );<br>
optimizer->SetProjectedGradientTolerance( 1e-6 );<br> optimizer->SetMaximumNumberOfIterations( 200 );<br> optimizer->SetMaximumNumberOfEvaluations( 30 );<br> optimizer->SetMaximumNumberOfCorrections( 5 );<br>
// Software Guide : EndCodeSnippet<br><br> // Create the Command observer and register it with the optimizer.<br> //<br> CommandIterationUpdate::Pointer observer = CommandIterationUpdate::New();<br> optimizer->AddObserver( itk::IterationEvent(), observer );<br>
<br><br> // Software Guide : BeginLatex<br> // <br> // Next we set the parameters of the Mattes Mutual Information Metric. <br> //<br> // Software Guide : EndLatex <br><br> // Software Guide : BeginCodeSnippet<br>
metric->SetNumberOfHistogramBins( 50 );<br> <br> const unsigned int numberOfSamples = <br> static_cast<unsigned int>( fixedRegion.GetNumberOfPixels() * 20.0 / 100.0 );<br><br> metric->SetNumberOfSpatialSamples( numberOfSamples );<br>
// Software Guide : EndCodeSnippet<br> <br><br> // Software Guide : BeginLatex<br> // <br> // Given that the Mattes Mutual Information metric uses a random iterator in<br> // order to collect the samples from the images, it is usually convenient to<br>
// initialize the seed of the random number generator.<br> //<br> // \index{itk::Mattes\-Mutual\-Information\-Image\-To\-Image\-Metric!ReinitializeSeed()}<br> //<br> // Software Guide : EndLatex <br><br> // Software Guide : BeginCodeSnippet<br>
metric->ReinitializeSeed( 76926294 );<br> // Software Guide : EndCodeSnippet<br><br> if( argc > 8 )<br> {<br> // Define whether to calculate the metric derivative by explicitly<br> // computing the derivatives of the joint PDF with respect to the Transform<br>
// parameters, or doing it by progressively accumulating contributions from<br> // each bin in the joint PDF.<br> metric->SetUseExplicitPDFDerivatives( atoi( argv[8] ) );<br> }<br><br> if( argc > 9 )<br>
{<br> // Define whether to cache the BSpline weights and indexes corresponding to<br> // each one of the samples used to compute the metric. Enabling caching will<br> // make the algorithm run faster but it will have a cost on the amount of memory<br>
// that needs to be allocated. This option is only relevant when using the <br> // BSplineDeformableTransform.<br> metric->SetUseCachingOfBSplineWeights( atoi( argv[9] ) );<br> }<br><br><br> // Add time and memory probes<br>
itkProbesCreate();<br><br> std::cout << std::endl << "Starting Registration" << std::endl;<br><br> try <br> { <br> itkProbesStart( "Registration" );<br> registration->StartRegistration(); <br>
itkProbesStop( "Registration" );<br> } <br> catch( itk::ExceptionObject & err ) <br> { <br> std::cerr << "ExceptionObject caught !" << std::endl; <br> std::cerr << err << std::endl; <br>
return EXIT_FAILURE;<br> } <br> <br> OptimizerType::ParametersType finalParameters = <br> registration->GetLastTransformParameters();<br><br><br> // Report the time and memory taken by the registration<br>
itkProbesReport( std::cout );<br><br> // Software Guide : BeginCodeSnippet<br> transform->SetParameters( finalParameters );<br> // Software Guide : EndCodeSnippet<br><br><br> typedef itk::ResampleImageFilter< <br>
MovingImageType, <br> FixedImageType > ResampleFilterType;<br><br> ResampleFilterType::Pointer resample = ResampleFilterType::New();<br><br> resample->SetTransform( transform );<br>
resample->SetInput( movingImageReader->GetOutput() );<br><br> resample->SetSize( fixedImage->GetLargestPossibleRegion().GetSize() );<br> resample->SetOutputOrigin( fixedImage->GetOrigin() );<br>
resample->SetOutputSpacing( fixedImage->GetSpacing() );<br>
resample->SetOutputDirection( fixedImage->GetDirection() );<br><br> // This value is set to zero in order to make easier to perform<br> // regression testing in this example. However, for didactic <br> // exercise it will be better to set it to a medium gray value<br>
// such as 100 or 128.<br> //resample->SetDefaultPixelValue( 0 );<br> resample->SetDefaultPixelValue( 0 );<br><br> typedef signed short OutputPixelType;<br><br> typedef itk::Image< OutputPixelType, ImageDimension > OutputImageType;<br>
<br> typedef itk::CastImageFilter< <br> FixedImageType,<br> OutputImageType > CastFilterType;<br> <br> typedef itk::ImageFileWriter< OutputImageType > WriterType;<br>
<br><br> WriterType::Pointer writer = WriterType::New();<br> CastFilterType::Pointer caster = CastFilterType::New();<br><br> //added from ReadAtlasAndSubject.cxx<br> /*typedef itk::GDCMImageIO ImageIOType;<br>
ImageIOType::Pointer gdcmImageIO = ImageIOType::New();<br><br> writer->UseInputMetaDataDictionaryOff();<br> writer->SetImageIO( gdcmImageIO );*/<br> //end of added code<br><br> writer->SetFileName( argv[3] );<br>
<br> writer->UseInputMetaDataDictionaryOff();<br> //writer->SetImageIO( gdcmImageIOAtlas );<br><br> caster->SetInput( resample->GetOutput() );<br> writer->SetInput( caster->GetOutput() );<br><br><br>
try<br> {<br> writer->Update();<br> }<br> catch( itk::ExceptionObject & err ) <br> { <br> std::cerr << "ExceptionObject caught !" << std::endl; <br> std::cerr << err << std::endl; <br>
return EXIT_FAILURE;<br> } <br> <br><br><br> typedef itk::SquaredDifferenceImageFilter< <br> FixedImageType, <br> FixedImageType, <br> OutputImageType > DifferenceFilterType;<br>
<br> DifferenceFilterType::Pointer difference = DifferenceFilterType::New();<br><br> WriterType::Pointer writer2 = WriterType::New();<br><br> //added<br> writer2->UseInputMetaDataDictionaryOff();<br> //end of added code<br>
<br><br> writer2->SetInput( difference->GetOutput() ); <br> <br><br> // Compute the difference image between the <br> // fixed and resampled moving image.<br> if( argc > 4 )<br> {<br> difference->SetInput1( fixedImageReader->GetOutput() );<br>
difference->SetInput2( resample->GetOutput() );<br> writer2->SetFileName( argv[4] );<br> try<br> {<br> writer2->Update();<br> }<br> catch( itk::ExceptionObject & err ) <br> { <br>
std::cerr << "ExceptionObject caught !" << std::endl; <br> std::cerr << err << std::endl; <br> return EXIT_FAILURE;<br> } <br> }<br><br><br> // Compute the difference image between the <br>
// fixed and moving image before registration.<br> if( argc > 5 )<br> {<br> writer2->SetFileName( argv[5] );<br> difference->SetInput1( fixedImageReader->GetOutput() );<br> difference->SetInput2( movingImageReader->GetOutput() );<br>
try<br> {<br> writer2->Update();<br> }<br> catch( itk::ExceptionObject & err ) <br> { <br> std::cerr << "ExceptionObject caught !" << std::endl; <br> std::cerr << err << std::endl; <br>
return EXIT_FAILURE;<br> } <br> }<br><br> // Generate the explicit deformation field and inverse deformation field resulting from <br> // the registration.<br> if( argc > 6 )<br> {<br><br> typedef itk::Vector< float, ImageDimension > VectorType;<br>
typedef itk::Image< VectorType, ImageDimension > DeformationFieldType;<br><br> typedef itk::InverseDeformationFieldImageFilter<<br> DeformationFieldType, <br> DeformationFieldType <br>
> FilterType;<br><br> FilterType::Pointer filter = FilterType::New();<br><br> DeformationFieldType::Pointer field = DeformationFieldType::New();<br> field->SetRegions( fixedRegion );<br>
field->SetOrigin( fixedImage->GetOrigin() );<br> field->SetSpacing( fixedImage->GetSpacing() );<br> field->SetDirection( fixedImage->GetDirection() );<br> field->Allocate();<br><br> typedef itk::ImageRegionIterator< DeformationFieldType > FieldIterator;<br>
FieldIterator fi( field, fixedRegion );<br><br> fi.GoToBegin();<br><br> TransformType::InputPointType fixedPoint;<br> TransformType::OutputPointType movingPoint;<br> DeformationFieldType::IndexType index;<br>
<br> VectorType displacement;<br><br> while( ! fi.IsAtEnd() )<br> {<br> index = fi.GetIndex();<br> field->TransformIndexToPhysicalPoint( index, fixedPoint );<br> movingPoint = transform->TransformPoint( fixedPoint );<br>
displacement = movingPoint - fixedPoint;<br> fi.Set( displacement );<br> ++fi;<br> }<br><br> typedef itk::ImageFileWriter< DeformationFieldType > FieldWriterType;<br> FieldWriterType::Pointer fieldWriter = FieldWriterType::New();<br>
<br> fieldWriter->SetInput( field );<br><br> fieldWriter->SetFileName( argv[6] );<br> try<br> {<br> fieldWriter->Update();<br> }<br> catch( itk::ExceptionObject & excp )<br> {<br>
std::cerr << "Exception thrown " << std::endl;<br> std::cerr << excp << std::endl;<br> return EXIT_FAILURE;<br> }<br><br><br> // Generate the explicit inverse deformation field resulting from <br>
// the registration.<br><br> DeformationFieldType::SpacingType spacing;<br> //spacing.Fill( 1.0 );<br><br> DeformationFieldType::PointType origin;<br> //origin.Fill( 0.0 );<br><br> DeformationFieldType::RegionType region;<br>
DeformationFieldType::SizeType size;<br> DeformationFieldType::IndexType start;<br><br> /*size[0] = movingImage->GetLargestPossibleRegion().GetSize()[0];<br> size[1] = movingImage->GetLargestPossibleRegion().GetSize()[1];<br>
size[2] = movingImage->GetLargestPossibleRegion().GetSize()[2];<br> */<br><br> size[0] = fixedImage->GetLargestPossibleRegion().GetSize()[0];<br> size[1] = fixedImage->GetLargestPossibleRegion().GetSize()[1];<br>
size[2] = fixedImage->GetLargestPossibleRegion().GetSize()[2];<br> <br><br> std::cout << "size[0] "<< size[0] << std::endl;<br> std::cout << "size[1] "<< size[1] << std::endl;<br>
std::cout << "size[2] "<< size[2] << std::endl;<br><br> region.SetSize( size );<br><br> start[0] = 0;<br> start[1] = 0;<br> start[2] = 0;<br><br> <br><br> region.SetIndex( start );<br>
<br>
field->SetOrigin( field->GetOrigin() );<br> field->SetSpacing( field->GetSpacing() );<br><br> field->SetRegions( region );<br> <br> <br> field->Allocate();<br><br> VectorType pixelValue;<br><br>
filter->SetOutputSpacing( field->GetSpacing() );<br>
<br> filter->SetOutputOrigin( field->GetOrigin() );<br><br> filter->SetSize( size );<br><br><br><br> filter->SetInput( field ); <br><br><br> filter->SetSubsamplingFactor( 16 );<br><br> try<br> {<br>
std::cout << "updating inverse deformation filter" << std::endl;<br> filter->UpdateLargestPossibleRegion();<br> }<br> catch( itk::ExceptionObject & excp )<br> {<br> std::cerr << "Exception thrown " << std::endl;<br>
std::cerr << excp << std::endl;<br> }<br><br> std::cout << "finished updating inverse deformation filter" << std::endl;<br><br><br> // Write an image for regression testing<br> typedef itk::ImageFileWriter< DeformationFieldType > InvWriterType;<br>
<br> InvWriterType::Pointer writerinv = InvWriterType::New();<br><br> writerinv->SetInput (filter->GetOutput());<br> writerinv->SetFileName( argv[7] );<br><br> try<br> {<br> std::cout << "writing inverse deformation filter" << std::endl;<br>
writerinv->Update();<br> }<br> catch( itk::ExceptionObject & excp )<br> {<br> std::cerr << "Exception thrown by writer" << std::endl;<br> std::cerr << excp << std::endl;<br>
return EXIT_FAILURE;<br> }<br><br> std::cout << "finished writing inverse deformation filter" << std::endl;<br><br> }<br><br> // Optionally, save the transform parameters in a file<br> if( argc > 10 )<br>
{<br> std::ofstream parametersFile;<br> parametersFile.open( argv[10] );<br> parametersFile << finalParameters << std::endl;<br> parametersFile.close();<br> }<br><br> return EXIT_SUCCESS;<br>
}<br><br>#undef itkProbesCreate<br>#undef itkProbesStart<br>#undef itkProbesStop<br>#undef itkProbesReport<div><div></div><div class="h5"><br><br><br><br><div class="gmail_quote">On Mon, Oct 12, 2009 at 12:29 PM, Luis Ibanez <span dir="ltr"><<a href="mailto:luis.ibanez@kitware.com" target="_blank">luis.ibanez@kitware.com</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">Hi John,<br>
<br>
1) The Deformation Field resulting from a Deformable Registration<br>
process in ITK, contains the vectors that point from the physical<br>
coordinates of a point in the Fixed image reference system to<br>
its corresponding point in the Moving image reference system.<br>
<br>
2) You could combine that deformation field with the WarpImageFilter<br>
in order to map intensities from the Moving image reference system<br>
into the Fixed Image reference system. That is: to resample the<br>
Moving image into the image grid of the Fixed image.<br>
<br>
3) If you want to map points (not pixel values) from the Moving image<br>
to the Fixed image, then you need to invert the deformation field.<br>
Simply negating (multiplying by -1 ) the initial deformation field<br>
will NOT do the trick.<br>
<br>
4) You may want to look at the following two options for inverting<br>
deformation fields:<br>
<br>
4.1) Insight/Code/BasicFilters/itkInverseDeformationFieldImageFilter.h<br>
<br>
4.2) Insight/Code/BasicFilters/itkIterativeInverseDeformationFieldImageFilter.h<br>
<br>
The first method uses a KernelTransform ( such as ThinPlateSplines)<br>
as an intermediate helper for computing the inverse field.<br>
<br>
The second method computes the inverse field iterative based<br>
on F * R = I (forward field composed with Reverse field should<br>
be equal to the Identity), by redistributing the residual errors of<br>
Error = F * R - I<br>
<br>
<br>
5) An alternative option can be found in<br>
<br>
InsightApplications/InverseConsistentLandmarkRegistration<br>
<br>
Based on the paper:<br>
<br>
Consistent Landmark and Intensity-Based Image Registration<br>
by H. J. Johnson* and G. E. Christensen<br>
IEEE TRANSACTIONS ON MEDICAL IMAGING, VOL. 21, NO. 5, MAY 2002<br>
<br>
<br>
<br>
Please give it a try and let us know if you find<br>
any problems.<br>
<br>
<br>
<br>
Thanks<br>
<br>
<br>
Luis<br>
<br>
<br>
--------------------------------------------------------<br>
<div><div></div><div>On Sat, Oct 10, 2009 at 4:12 AM, John Drozd <<a href="mailto:john.drozd@gmail.com" target="_blank">john.drozd@gmail.com</a>> wrote:<br>
> Hello,<br>
><br>
> I used deformable registration using DeformableRegistration8.cxx to register<br>
> one brain subject from one person with another brain subject from another<br>
> person. I now have a deformation field file that was outputted from<br>
> deformableregistration8.cxx. I was reading on the internet the following<br>
> link <a href="http://www.itk.org/pipermail/insight-users/2009-April/030004.html" target="_blank">http://www.itk.org/pipermail/insight-users/2009-April/030004.html</a> that<br>
> describes how to map points from the fixed image to the moving image using a<br>
> deformation field. If i want to map points from the moving image to the<br>
> fixed image, can I use the same procedure but take the negative of the<br>
> deformation field, and if so how can i negate the deformation field and use<br>
> it with ResampleImagefilter? My logic is that when you take the negative of<br>
> a vector, this reverses its direction.<br>
><br>
> Thank you,<br>
><br>
> john<br>
><br>
><br>
><br>
><br>
> --<br>
> John Drozd<br>
> Postdoctoral Fellow<br>
> Imaging Research Laboratories<br>
> Robarts Research Institute<br>
> Room 1256<br>
> 100 Perth Drive<br>
> London, Ontario, Canada<br>
> N6A 5K8<br>
> (519) 661-2111 ext. 25306<br>
><br>
</div></div></blockquote></div><br><br clear="all"><br></div></div>-- <br><div><div></div><div class="h5">John Drozd<br>Postdoctoral Fellow<br>Imaging Research Laboratories<br>Robarts Research Institute<br>Room 1256<br>100 Perth Drive<br>
London, Ontario, Canada<br>
N6A 5K8<br>(519) 661-2111 ext. 25306<br>
</div></div></blockquote></div><br><br clear="all"><br>-- <br>John Drozd<br>Postdoctoral Fellow<br>Imaging Research Laboratories<br>Robarts Research Institute<br>Room 1256<br>100 Perth Drive<br>London, Ontario, Canada<br>
N6A 5K8<br>(519) 661-2111 ext. 25306<br>