X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;ds=sidebyside;f=tools%2FclitkComposeVFGenericFilter.txx;fp=tools%2FclitkComposeVFGenericFilter.txx;h=07df9882fe573cafd268b4b78a83d9e914cc80f7;hb=fcae585b19c2d7a0bba334b812133598a515c654;hp=80d1d001a9bdf172a8df55a572ed7c4fb8e770e7;hpb=42f68068ef3488c8ca2d921b6835d34fe6494d5f;p=clitk.git 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(); + } }