Skip to content

Instantly share code, notes, and snippets.

Created January 7, 2014 17:10
Show Gist options
  • Save anonymous/8302651 to your computer and use it in GitHub Desktop.
Save anonymous/8302651 to your computer and use it in GitHub Desktop.
Deformation Field Inversion program - error caused when building in MS Visual C++ 2010 Express: C:/ITKrap/InsightToolkit-4.2.0/Modules/Core/Common/include\itkFixedPointInverseDeformationFieldImageFilter.txx(109): error C2664: 'itk::ImageRegionIterator<TImage>::Set' : cannot convert parameter 1 from 'itk::Vector<T,NVectorDimension>' to 'const itk…
#include "itkWin32Header.h"
#include <iostream>
#include <fstream>
#include "itkNumericTraits.h"
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkFixedPointInverseDeformationFieldImageFilter.h"
int main(int argc, char **argv)
{
if( argc < 3 )
{
std::cerr << "usage: " << argv[0] << " input_filename output_filename" << std::endl;
return 1;
}
const unsigned int Dimension = 3;
typedef itk::Vector< float, Dimension > VectorPixelType;
typedef itk::Image< VectorPixelType, Dimension > InputDFType;
typedef itk::Image< VectorPixelType, Dimension > OutputDFType;
typedef itk::ImageFileReader< InputDFType > ReaderType;
typedef itk::ImageFileWriter< OutputDFType > WriterType;
typedef itk::FixedPointInverseDeformationFieldImageFilter<InputDFType, OutputDFType> FPInverseType;
// read the file
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(argv[1]);
try
{
reader->UpdateLargestPossibleRegion();
}
catch (itk::ExceptionObject& e)
{
std::cerr << "Exception detected while reading " << argv[1];
std::cerr << " : " << e.GetDescription();
return 1;
}
// invert the deformationfield
InputDFType::Pointer inputDf = reader->GetOutput();
FPInverseType::Pointer inverter = FPInverseType::New();
inverter->SetInput(inputDf);
inverter->SetOutputOrigin(inputDf->GetOrigin());
inverter->SetSize(inputDf->GetLargestPossibleRegion().GetSize());
inverter->SetOutputSpacing(inputDf->GetSpacing());
inverter->SetNumberOfIterations(20);
inverter->Update();
OutputDFType::Pointer outputDf = inverter->GetOutput();
// write the file
WriterType::Pointer writer = WriterType::New();
writer->SetInput(inverter->GetOutput());
try
{
writer->SetFileName( argv[2] );
writer->Update();
}
catch (...)
{
std::cerr << "Error during write of " << argv[2] << std::endl;
return 1;
}
return 0;
}
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: itkFixedPointInverseDeformationFieldImageFilter.h,v $
Language: C++
Copyright: University of Basel, All rights reserved
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#ifndef __itkFixedPointInverseDeformationFieldImageFilter_h
#define __itkFixedPointInverseDeformationFieldImageFilter_h
#include "itkImageToImageFilter.h"
#include "itkWarpVectorImageFilter.h"
#include "itkVectorLinearInterpolateImageFunction.h"
#include "itkImageRegionIterator.h"
#include "itkTimeProbe.h"
namespace itk
{
/** \class FixedPointInverseDeformationFieldImageFilter
* \brief Computes the inverse of a deformation field using a fixed point iteration scheme.
*
* FixedPointInverseDeformationFieldImageFilter takes a deformation field as input and
* computes the deformation field that is its inverse. If the input deformation
* field was mapping coordinates from a space A into a space B, the output of
* this filter will map coordinates from the space B into the space A.
*
* To compute the inverse of the given deformation field, the fixed point algorithm by
* Mingli Chen, Weiguo Lu, Quan Chen, Knneth J. Ruchala and Gusavo H. Olivera
* described in the paper
* "A simple fixed-point approach to invert a deformation field",
* Medical Physics, vol. 35, issue 1, p. 81,
* is applied.
*
* \author Marcel Lüthi, Computer Science Department, University of Basel
*/
template < class TInputImage, class TOutputImage >
class ITK_EXPORT FixedPointInverseDeformationFieldImageFilter :
public ImageToImageFilter<TInputImage,TOutputImage>
{
public:
/** Standard class typedefs. */
typedef FixedPointInverseDeformationFieldImageFilter Self;
typedef ImageToImageFilter<TInputImage,TOutputImage> Superclass;
typedef SmartPointer<Self> Pointer;
typedef SmartPointer<const Self> ConstPointer;
/** Method for creation through the object factory. */
itkNewMacro(Self);
/** Run-time type information (and related methods). */
itkTypeMacro(FixedPointInverseDeformationFieldImageFilter, ImageToImageFilter);
/** Some typedefs. */
typedef TInputImage InputImageType;
typedef typename InputImageType::ConstPointer InputImageConstPointer;
typedef typename InputImageType::Pointer InputImagePointer;
typedef typename InputImageType::PointType InputImagePointType;
typedef typename InputImageType::RegionType InputImageRegionType;
typedef typename InputImageType::SpacingType InputImageSpacingType;
typedef typename InputImageType::IndexType InputImageIndexType;
typedef TOutputImage OutputImageType;
typedef typename OutputImageType::Pointer OutputImagePointer;
typedef typename OutputImageType::PixelType OutputImagePixelType;
typedef typename OutputImageType::PointType OutputImagePointType;
typedef typename OutputImageType::IndexType OutputImageIndexType;
typedef typename OutputImagePixelType::ValueType OutputImageValueType;
typedef typename OutputImageType::SizeType OutputImageSizeType;
typedef typename OutputImageType::SpacingType OutputImageSpacingType;
typedef typename TOutputImage::PointType OutputImageOriginPointType;
typedef TimeProbe TimeType;
/** Number of dimensions. */
itkStaticConstMacro(ImageDimension, unsigned int,
TOutputImage::ImageDimension);
typedef ImageRegionConstIterator<InputImageType> InputConstIterator;
typedef ImageRegionIterator<InputImageType> InputIterator;
typedef ImageRegionConstIterator<OutputImageType> OutputConstIterator;
typedef ImageRegionIterator<OutputImageType> OutputIterator;
typedef WarpVectorImageFilter<TOutputImage,TInputImage,TOutputImage> VectorWarperType;
typedef VectorLinearInterpolateImageFunction<TInputImage,double> FieldInterpolatorType;
typedef typename FieldInterpolatorType::Pointer FieldInterpolatorPointer;
typedef typename FieldInterpolatorType::OutputType FieldInterpolatorOutputType;
itkSetMacro(NumberOfIterations, unsigned int);
itkGetConstMacro(NumberOfIterations, unsigned int);
/** Set the size of the output image. */
itkSetMacro( Size, OutputImageSizeType );
/** Get the size of the output image. */
itkGetConstReferenceMacro( Size, OutputImageSizeType );
/** Set the output image spacing. */
itkSetMacro(OutputSpacing, OutputImageSpacingType);
virtual void SetOutputSpacing(const double* values);
/** Get the output image spacing. */
itkGetConstReferenceMacro( OutputSpacing, OutputImageSpacingType );
/** Set the output image origin. */
itkSetMacro(OutputOrigin, OutputImageOriginPointType);
virtual void SetOutputOrigin( const double* values);
#ifdef ITK_USE_CONCEPT_CHECKING
/** Begin concept checking */
itkConceptMacro(OutputHasNumericTraitsCheck,
(Concept::HasNumericTraits<OutputImageValueType>));
/** End concept checking */
#endif
protected:
FixedPointInverseDeformationFieldImageFilter();
~FixedPointInverseDeformationFieldImageFilter() {}
void PrintSelf(std::ostream& os, Indent indent) const;
void GenerateData( );
void GenerateOutputInformation();
unsigned int m_NumberOfIterations;
private:
FixedPointInverseDeformationFieldImageFilter(const Self&); //purposely not implemented
void operator=(const Self&); //purposely not implemented
OutputImageSizeType m_Size; // Size of the output image
OutputImageSpacingType m_OutputSpacing; // output image spacing
OutputImageOriginPointType m_OutputOrigin; // output image origin
};
} // end namespace itk
#include "itkFixedPointInverseDeformationFieldImageFilter.txx"
#endif
/*=========================================================================
Program: Insight Segmentation & Registration Toolkit
Module: $RCSfile: itkFixedPointInverseDeformationFieldImageFilter.txx,v $
Language: C++
Copyright: University of Basel, All rights reserved
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#ifndef __itkFixedPointInverseDeformationFieldImageFilter_txx
#define __itkFixedPointInverseDeformationFieldImageFilter_txx
#include "itkFixedPointInverseDeformationFieldImageFilter.h"
#include <iostream>
namespace itk {
//----------------------------------------------------------------------------
// Constructor
template<class TInputImage, class TOutputImage>
FixedPointInverseDeformationFieldImageFilter<TInputImage, TOutputImage>::FixedPointInverseDeformationFieldImageFilter() :
m_NumberOfIterations(5) {
m_OutputSpacing.Fill(1.0);
m_OutputOrigin.Fill(0.0);
for (unsigned int i = 0; i < ImageDimension; i++)
{
m_Size[i] = 0;
}
}
/**
* Set the output image spacing.
*/
template <class TInputImage, class TOutputImage>
void
FixedPointInverseDeformationFieldImageFilter<TInputImage,TOutputImage>
::SetOutputSpacing(const double* spacing)
{
OutputImageSpacingType s(spacing);
this->SetOutputSpacing( s );
}
/**
* Set the output image origin.
*/
template <class TInputImage, class TOutputImage>
void
FixedPointInverseDeformationFieldImageFilter<TInputImage,TOutputImage>
::SetOutputOrigin(const double* origin)
{
OutputImageOriginPointType p(origin);
this->SetOutputOrigin( p );
}
//----------------------------------------------------------------------------
template<class TInputImage, class TOutputImage>
void FixedPointInverseDeformationFieldImageFilter<TInputImage, TOutputImage>::GenerateData() {
const unsigned int ImageDimension = InputImageType::ImageDimension;
InputImageConstPointer inputPtr = this->GetInput(0);
OutputImagePointer outputPtr = this->GetOutput(0);
// some checks
if (inputPtr.IsNull()) {
itkExceptionMacro("\n Input is missing.");
}
if (!TInputImage::ImageDimension == TOutputImage::ImageDimension) {
itkExceptionMacro("\n Image Dimensions must be the same.");
}
// The initial deformation field is simply the negative input deformation field.
// Here we create this deformation field.
InputImagePointer negField = InputImageType::New();
negField->SetRegions(inputPtr->GetLargestPossibleRegion());
negField->SetOrigin(inputPtr->GetOrigin());
negField->SetSpacing(inputPtr->GetSpacing());
negField->SetDirection(inputPtr->GetDirection());
negField->Allocate();
InputConstIterator InputIt = InputConstIterator(inputPtr,
inputPtr->GetRequestedRegion());
InputIterator negImageIt = InputIterator(negField,
negField->GetRequestedRegion());
for (negImageIt.GoToBegin(), InputIt.GoToBegin(); !negImageIt.IsAtEnd(); ++negImageIt) {
negImageIt.Set(-InputIt.Get());
++InputIt;
}
// We allocate a deformation field that holds the output
// and initialize it to 0
outputPtr->SetBufferedRegion( outputPtr->GetRequestedRegion() );
outputPtr->Allocate();
OutputIterator outputIt = OutputIterator(outputPtr,
outputPtr->GetRequestedRegion());
OutputImagePixelType zero_pt;
zero_pt.Fill(0);
for (outputIt.GoToBegin(); !outputIt.IsAtEnd(); ++outputIt) {
outputIt.Set(zero_pt);
}
// In the fixed point iteration, we will need to access non-grid points.
// Currently, the best interpolator that is supported by itk for vector
// images is the linear interpolater.
typename FieldInterpolatorType::Pointer vectorInterpolator =
FieldInterpolatorType::New();
vectorInterpolator->SetInputImage(negField);
// Finally, perform the fixed point iteration.
InputImagePointType mappedPt;
OutputImagePointType pt;
OutputImageIndexType index;
OutputImagePixelType displacement;
for (unsigned int i = 0; i <= m_NumberOfIterations; i++) {
for (outputIt.GoToBegin(); !outputIt.IsAtEnd(); ++outputIt) {
index = outputIt.GetIndex();
outputPtr->TransformIndexToPhysicalPoint(index, pt);
displacement = outputIt.Get();
for (unsigned int j = 0; j < ImageDimension; j++) {
mappedPt[j] = pt[j] + displacement[j];
}
if (vectorInterpolator->IsInsideBuffer(mappedPt)) {
outputIt.Set(vectorInterpolator->Evaluate(mappedPt));
}
}
}
}
/**
* Inform pipeline of required output region
*/
template <class TInputImage, class TOutputImage>
void
FixedPointInverseDeformationFieldImageFilter<TInputImage,TOutputImage>
::GenerateOutputInformation()
{
// call the superclass' implementation of this method
Superclass::GenerateOutputInformation();
// get pointers to the input and output
OutputImagePointer outputPtr = this->GetOutput();
if ( !outputPtr )
{
return;
}
// Set the size of the output region
typename TOutputImage::RegionType outputLargestPossibleRegion;
outputLargestPossibleRegion.SetSize( m_Size );
outputPtr->SetLargestPossibleRegion( outputLargestPossibleRegion );
// Set spacing and origin
outputPtr->SetSpacing( m_OutputSpacing );
outputPtr->SetOrigin( m_OutputOrigin );
return;
}
//----------------------------------------------------------------------------
template<class TInputImage, class TOutputImage>
void FixedPointInverseDeformationFieldImageFilter<TInputImage, TOutputImage>::PrintSelf(
std::ostream& os, Indent indent) const {
Superclass::PrintSelf(os, indent);
os << indent << "Number of iterations: " << m_NumberOfIterations
<< std::endl;
os << std::endl;
}
} // end namespace itk
#endif
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment