From: Romulo Pinho Date: Wed, 18 Jan 2012 16:51:04 +0000 (+0100) Subject: VF composition with BSpline coefficients X-Git-Tag: v1.3.0~120 X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=commitdiff_plain;h=fcae585b19c2d7a0bba334b812133598a515c654;p=clitk.git VF composition with BSpline coefficients - output resolution taken from --like image --- diff --git a/tools/clitkComposeVF.cxx b/tools/clitkComposeVF.cxx index 72b95cf..c2cead2 100644 --- a/tools/clitkComposeVF.cxx +++ b/tools/clitkComposeVF.cxx @@ -28,14 +28,23 @@ int main( int argc, char *argv[] ) // Init command line GGO(clitkComposeVF, args_info); CLITK_INIT; + + if (args_info.type_arg == 1 && !args_info.like_given) { + std::cerr << "--like must be used when type = 1" << std::endl; + return -1; + } + //Creation of the generic filter clitk::ComposeVFGenericFilter::Pointer ComposeVFGenericFilter= clitk::ComposeVFGenericFilter::New(); - + //Pass the parameters ComposeVFGenericFilter->SetInput1(args_info.input1_arg); ComposeVFGenericFilter->SetInput2(args_info.input2_arg); ComposeVFGenericFilter->SetOutput(args_info.output_arg); + ComposeVFGenericFilter->SetInputType(args_info.type_arg); + if (args_info.like_given) + ComposeVFGenericFilter->SetLikeImage(args_info.like_arg); //JV how to pass for different dims? //ComposeVFGenericFilter->SetEdgePaddingValue(args_info.pad_arg); ComposeVFGenericFilter->SetVerbose(args_info.verbose_flag); diff --git a/tools/clitkComposeVF.ggo b/tools/clitkComposeVF.ggo index 52f3e20..bf67f8c 100644 --- a/tools/clitkComposeVF.ggo +++ b/tools/clitkComposeVF.ggo @@ -9,6 +9,8 @@ option "config" - "Config file" string no option "input1" i "Input1 VF filename" string yes option "input2" j "Input2 VF filename" string yes option "output" o "Output VF filename" string yes +option "type" t "Type of input images (0: DVF, 1: b-spline coefficient images)" int yes default="1" +option "like" l "Image to read output parameters from (obligatory if type=1)" string no option "pad" p "Edgepadding value" float multiple no default="0.0" option "verbose" v "Verbose" flag off diff --git a/tools/clitkComposeVFGenericFilter.cxx b/tools/clitkComposeVFGenericFilter.cxx index deeaad6..03b3d14 100644 --- a/tools/clitkComposeVFGenericFilter.cxx +++ b/tools/clitkComposeVFGenericFilter.cxx @@ -25,6 +25,7 @@ namespace clitk { clitk::ComposeVFGenericFilter::ComposeVFGenericFilter() { m_Verbose=false; + m_Type = 0; } diff --git a/tools/clitkComposeVFGenericFilter.h b/tools/clitkComposeVFGenericFilter.h index c3e8bd3..b86f22b 100644 --- a/tools/clitkComposeVFGenericFilter.h +++ b/tools/clitkComposeVFGenericFilter.h @@ -51,6 +51,8 @@ namespace clitk void SetInput1(const std::string m){m_InputName1=m;} void SetInput2(const std::string m){m_InputName2=m;} void SetOutput(const std::string m){m_OutputName=m;} + void SetLikeImage(const std::string like){m_LikeImage=like;} + void SetInputType(int t){m_Type=t;} void SetVerbose(const bool m){m_Verbose=m;} @@ -65,10 +67,14 @@ namespace clitk //Templated members template void UpdateWithDim(std::string PixelType); template void UpdateWithDimAndPixelType(); - + template typename DisplacementFieldType::Pointer CoeffsToDVF(std::string fileName, std::string likeFileName); + std::string m_InputName1; std::string m_InputName2; std::string m_OutputName; + std::string m_LikeImage; + int m_Type; + bool m_Verbose; }; diff --git a/tools/clitkComposeVFGenericFilter.txx b/tools/clitkComposeVFGenericFilter.txx index 80d1d00..07df988 100644 --- a/tools/clitkComposeVFGenericFilter.txx +++ b/tools/clitkComposeVFGenericFilter.txx @@ -19,6 +19,13 @@ #define __clitkComposeVFGenericFilter_txx #include "clitkComposeVFGenericFilter.h" +#include "clitkBSplineDeformableTransform.h" +#include "clitkBSplineDeformableTransformInitializer.h" +#if ITK_VERSION_MAJOR >= 4 +#include "itkTransformToDisplacementFieldSource.h" +#else +#include "itkTransformToDeformationFieldSource.h" +#endif namespace clitk { @@ -36,43 +43,102 @@ namespace clitk } } + template + typename DisplacementFieldType::Pointer ComposeVFGenericFilter::CoeffsToDVF(std::string fileName, std::string likeFileName) + { + typedef clitk::BSplineDeformableTransform TransformType; + typedef typename TransformType::CoefficientImageType CoefficientImageType; + + typedef itk::ImageFileReader CoeffReaderType; + typename CoeffReaderType::Pointer reader = CoeffReaderType::New(); + reader->SetFileName(fileName); + reader->Update(); + + typename TransformType::Pointer transform = TransformType::New(); + transform->SetCoefficientImage(reader->GetOutput()); + +#if ITK_VERSION_MAJOR >= 4 + typedef itk::TransformToDisplacementFieldSource ConvertorType; +#else + typedef itk::TransformToDeformationFieldSource 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 < 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]; + } + + if (m_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(); + } + template void ComposeVFGenericFilter::UpdateWithDimAndPixelType() { - - //Define the image type typedef itk::Vector DisplacementType; typedef itk::Image ImageType; + typename ImageType::Pointer input1, input2; - //Read the input1 - typedef itk::ImageFileReader ImageReaderType; - typename ImageReaderType::Pointer reader1= ImageReaderType::New(); - reader1->SetFileName(m_InputName1); - reader1->Update(); - typename ImageType::Pointer input1 =reader1->GetOutput(); - - //Read the input2 - typename ImageReaderType::Pointer reader2= ImageReaderType::New(); - reader2->SetFileName(m_InputName2); - reader2->Update(); - typename ImageType::Pointer input2=reader2->GetOutput(); - - //Create the ComposeVFFilter - typedef clitk::ComposeVFFilter FilterType; - typename FilterType::Pointer filter =FilterType::New(); - filter->SetInput1(input1); - filter->SetInput2(input2); - filter->SetVerbose(m_Verbose); - filter->Update(); - - //Write the output - typedef itk::ImageFileWriter WriterType; - typename WriterType::Pointer writer = WriterType::New(); - writer->SetFileName(m_OutputName); - writer->SetInput(filter->GetOutput()); - writer->Update(); + //Define the image type + if (m_Type == 1) { + input1 = this->CoeffsToDVF(m_InputName1, m_LikeImage); + input2 = this->CoeffsToDVF(m_InputName2, m_LikeImage); + } + else { + //Read the input1 + typedef itk::ImageFileReader ImageReaderType; + typename ImageReaderType::Pointer reader1= ImageReaderType::New(); + reader1->SetFileName(m_InputName1); + reader1->Update(); + input1 =reader1->GetOutput(); + + //Read the input2 + typename ImageReaderType::Pointer reader2= ImageReaderType::New(); + reader2->SetFileName(m_InputName2); + reader2->Update(); + input2=reader2->GetOutput(); + } + //Create the ComposeVFFilter + typedef clitk::ComposeVFFilter FilterType; + typename FilterType::Pointer filter =FilterType::New(); + filter->SetInput1(input1); + filter->SetInput2(input2); + filter->SetVerbose(m_Verbose); + filter->Update(); + + //Write the output + typedef itk::ImageFileWriter WriterType; + typename WriterType::Pointer writer = WriterType::New(); + writer->SetFileName(m_OutputName); + writer->SetInput(filter->GetOutput()); + writer->Update(); + } }