#ifndef clitkCoeffsToDVF_h
#define clitkCoeffsToDVF_h
+#include "itkImageFileReader.h"
+#include "itkImageIOBase.h"
+
#include "clitkBSplineDeformableTransform.h"
+#include "clitkResampleBSplineDeformableTransformImageFilter.h"
#if ITK_VERSION_MAJOR >= 4
#include "itkTransformToDisplacementFieldSource.h"
#else
#include "itkTransformToDeformationFieldSource.h"
#endif
+#include "itkBSplineDeformableTransform.h"
-//-------------------------------------------------------------------
-// Convert Coefficient image to DVF
-//-------------------------------------------------------------------
-template<class DisplacementFieldType>
-typename DisplacementFieldType::Pointer
-CoeffsToDVF(std::string fileName, std::string likeFileName, bool verbose = false)
+namespace clitk
{
- typedef clitk::BSplineDeformableTransform<double, DisplacementFieldType::ImageDimension, DisplacementFieldType::ImageDimension> TransformType;
- typedef typename TransformType::CoefficientImageType CoefficientImageType;
+ //-------------------------------------------------------------------
+ // Initialize transform from coefficient images
+ //-------------------------------------------------------------------
+ template <class TransformType>
+ void
+ SetInitialTransformParameters(typename TransformType::Pointer transform, const typename TransformType::CoefficientImageType::Pointer coefficientImage, typename TransformType::CoefficientImageType::SpacingType outputSpacing)
+ {
+ unsigned int dim = TransformType::CoefficientImageType::ImageDimension;
+ transform->SetSplineOrder(3);
+ transform->SetGridRegion( coefficientImage->GetLargestPossibleRegion() );
+ transform->SetGridOrigin( coefficientImage->GetOrigin() );
+ transform->SetGridSpacing( coefficientImage->GetSpacing() );
+ transform->SetGridDirection( coefficientImage->GetDirection() );
+ typename TransformType::RegionType::SizeType samplingFactors;
+ for (unsigned int i=0; i< dim; i++) {
+ samplingFactors[i]= (int) ( coefficientImage->GetSpacing()[i]/ outputSpacing[i]);
+ }
+ transform->SetLUTSamplingFactors(samplingFactors);
+
+ typedef typename TransformType::ParametersType ParametersType;
+ const unsigned int numberOfParameters = transform->GetNumberOfParameters();
+ ParametersType params(numberOfParameters);
+ params.Fill( 0.0 );
- typedef itk::ImageFileReader<CoefficientImageType> CoeffReaderType;
- typename CoeffReaderType::Pointer reader = CoeffReaderType::New();
- reader->SetFileName(fileName);
- reader->Update();
+ typedef itk::ImageRegionConstIterator<typename TransformType::CoefficientImageType> Iterator;
+ Iterator it (coefficientImage, coefficientImage->GetLargestPossibleRegion() );
+ it.GoToBegin();
+ unsigned int i = 0;
+ while (! it.IsAtEnd()) {
+ for (unsigned int j = 0; j < dim; j++)
+ params[i+j]=it.Get()[j];
- typename TransformType::Pointer transform = TransformType::New();
- transform->SetCoefficientImage(reader->GetOutput());
-
-#if ITK_VERSION_MAJOR >= 4
- typedef itk::TransformToDisplacementFieldSource<DisplacementFieldType, double> ConvertorType;
-#else
- typedef itk::TransformToDeformationFieldSource<DisplacementFieldType, double> ConvertorType;
-#endif
+ ++it;
+ i += dim;
+ }
- typedef itk::ImageIOBase ImageIOType;
- typename ImageIOType::Pointer imageIO = itk::ImageIOFactory::CreateImageIO(likeFileName.c_str(), itk::ImageIOFactory::ReadMode);
- imageIO->SetFileName(likeFileName);
- imageIO->ReadImageInformation();
-
- typename ConvertorType::Pointer convertor= ConvertorType::New();
- typename ConvertorType::SizeType output_size;
- typename ConvertorType::SpacingType output_spacing;
- typename ConvertorType::OriginType output_origin;
- typename ConvertorType::DirectionType output_direction;
- for (unsigned int i = 0; i < DisplacementFieldType::ImageDimension; i++) {
- output_size[i] = imageIO->GetDimensions(i);
- output_spacing[i] = imageIO->GetSpacing(i);
- output_origin[i] = imageIO->GetOrigin(i);
- for (unsigned int j = 0; j < DisplacementFieldType::ImageDimension; j++)
- output_direction[i][j] = imageIO->GetDirection(i)[j];
+ transform->SetParameters(params);
+ transform->SetBulkTransform(NULL);
}
-
- if (verbose) {
- std::cout << "Interpolating coefficients with grid:" << std::endl;
- std::cout << output_size << output_spacing << std::endl;
+
+ //-------------------------------------------------------------------
+ // Convert Coefficient image to DVF
+ //-------------------------------------------------------------------
+ template<class DisplacementFieldType>
+ typename DisplacementFieldType::Pointer
+ BLUTCoeffsToDVF(std::string fileName, std::string likeFileName, bool verbose = false)
+ {
+ const unsigned int dim = DisplacementFieldType::ImageDimension;
+ typedef clitk::BSplineDeformableTransform<double, DisplacementFieldType::ImageDimension, DisplacementFieldType::ImageDimension> TransformType;
+ typedef typename TransformType::CoefficientImageType CoefficientImageType;
+
+ typedef itk::ImageFileReader<CoefficientImageType> CoeffReaderType;
+ typename CoeffReaderType::Pointer reader = CoeffReaderType::New();
+ reader->SetFileName(fileName);
+ reader->Update();
+
+ #if ITK_VERSION_MAJOR >= 4
+ typedef itk::TransformToDisplacementFieldSource<DisplacementFieldType, double> ConvertorType;
+ #else
+ typedef itk::TransformToDeformationFieldSource<DisplacementFieldType, double> ConvertorType;
+ #endif
+
+ typedef itk::ImageIOBase ImageIOType;
+ typename ImageIOType::Pointer imageIO = itk::ImageIOFactory::CreateImageIO(likeFileName.c_str(), itk::ImageIOFactory::ReadMode);
+ imageIO->SetFileName(likeFileName);
+ imageIO->ReadImageInformation();
+
+ typename ConvertorType::Pointer convertor= ConvertorType::New();
+ typename ConvertorType::SizeType output_size;
+ typename ConvertorType::SpacingType output_spacing;
+ typename ConvertorType::OriginType output_origin;
+ typename ConvertorType::DirectionType output_direction;
+ for (unsigned int i = 0; i < dim; i++) {
+ output_size[i] = imageIO->GetDimensions(i);
+ output_spacing[i] = imageIO->GetSpacing(i);
+ output_origin[i] = imageIO->GetOrigin(i);
+ for (unsigned int j = 0; j < DisplacementFieldType::ImageDimension; j++)
+ output_direction[i][j] = imageIO->GetDirection(i)[j];
+ }
+
+ typename CoefficientImageType::Pointer coeffs = reader->GetOutput();
+ typename TransformType::Pointer transform = TransformType::New();
+ SetInitialTransformParameters<TransformType>(transform, coeffs, output_spacing);
+
+ if (verbose) {
+ std::cout << "Interpolating coefficients with grid:" << std::endl;
+ std::cout << output_size << output_spacing << std::endl;
+ }
+
+ convertor->SetNumberOfThreads(1);
+ convertor->SetTransform(transform);
+ convertor->SetOutputOrigin(output_origin);
+ convertor->SetOutputSpacing(output_spacing);
+ convertor->SetOutputSize(output_size);
+ convertor->SetOutputDirection(output_direction);
+ convertor->Update();
+
+ return convertor->GetOutput();
}
-
- convertor->SetNumberOfThreads(1);
- convertor->SetTransform(transform);
- convertor->SetOutputOrigin(output_origin);
- convertor->SetOutputSpacing(output_spacing);
- convertor->SetOutputSize(output_size);
- convertor->SetOutputDirection(output_direction);
- convertor->Update();
-
- return convertor->GetOutput();
}
#endif
--- /dev/null
+#include "clitkCoeffsToDVF_ggo.h"
+#include "clitkCoeffsToDVF.h"
+#include "itkImage.h"
+#include "itkImageFileWriter.h"
+#include "itkImageIOFactory.h"
+#include <string>
+
+template <class DisplacementFieldType>
+void
+Write(typename DisplacementFieldType::Pointer dvf, std::string fileName)
+{
+ typedef itk::ImageFileWriter<DisplacementFieldType> ImageWriterType;
+ typename ImageWriterType::Pointer writer = ImageWriterType::New();
+ writer->SetFileName(fileName);
+ writer->SetInput(dvf);
+ writer->Update();
+}
+
+int main(int argc, char** argv)
+{
+ GGO(clitkCoeffsToDVF, args_info);
+ CLITK_INIT;
+
+ typename itk::ImageIOBase::Pointer image_io = itk::ImageIOFactory::CreateImageIO(args_info.input_arg, itk::ImageIOFactory::ReadMode);
+ image_io->SetFileName(args_info.input_arg);
+ image_io->ReadImageInformation();
+
+ unsigned int ndim = image_io->GetNumberOfDimensions();
+ switch (ndim) {
+ case 2:
+ {
+ unsigned const dim = 2;
+ typedef itk::Vector<double, dim> PixelType;
+ typedef itk::Image<PixelType, dim> DVFType;
+ typename DVFType::Pointer dvf = clitk::BLUTCoeffsToDVF<DVFType>(args_info.input_arg, args_info.like_arg);
+ Write<DVFType>(dvf, args_info.output_arg);
+ }
+ break;
+
+ case 3:
+ {
+ unsigned const dim = 3;
+ typedef itk::Vector<double, dim> PixelType;
+ typedef itk::Image<PixelType, dim> DVFType;
+ typename DVFType::Pointer dvf = clitk::BLUTCoeffsToDVF<DVFType>(args_info.input_arg, args_info.like_arg);
+ Write<DVFType>(dvf, args_info.output_arg);
+ }
+ break;
+
+ default:
+ std::cerr << "Unsupported image dimension (either 2 or 3)" << std::endl;
+ return -1;
+ }
+
+ return 0;
+}
\ No newline at end of file