X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=tools%2FclitkInvertVFGenericFilter.txx;h=5cd12c4ce202129c38ac67d59d5629fa590b58e1;hb=0ebe428ddf5c84373c3eb814e9465f048b1e7d48;hp=9835c2e739f64d3edd1af378e7955efc43059ba7;hpb=6194949c0beb1589904e22381b9aba1bbface172;p=clitk.git diff --git a/tools/clitkInvertVFGenericFilter.txx b/tools/clitkInvertVFGenericFilter.txx index 9835c2e..5cd12c4 100644 --- a/tools/clitkInvertVFGenericFilter.txx +++ b/tools/clitkInvertVFGenericFilter.txx @@ -18,6 +18,14 @@ #ifndef clitkInvertVFGenericFilter_txx #define clitkInvertVFGenericFilter_txx +#include "itkVectorResampleImageFilter.h" +#include "clitkBSplineDeformableTransform.h" +#if ITK_VERSION_MAJOR >= 4 +#include "itkTransformToDisplacementFieldSource.h" +#else +#include "itkTransformToDeformationFieldSource.h" +#endif + /* ================================================= * @file clitkInvertVFGenericFilter.txx * @author @@ -98,6 +106,65 @@ InvertVFGenericFilter::UpdateWithDim(std::string PixelType) // } } +//------------------------------------------------------------------- +// Convert Coefficient image to DVF +//------------------------------------------------------------------- +template +template +typename DisplacementFieldType::Pointer +InvertVFGenericFilter::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(); +} + //------------------------------------------------------------------- // Update with the number of dimensions and the pixeltype @@ -117,7 +184,7 @@ InvertVFGenericFilter::UpdateWithDimAndPixelType() typename InputReaderType::Pointer reader = InputReaderType::New(); reader->SetFileName( m_InputFileName); reader->Update(); - typename InputImageType::Pointer input= reader->GetOutput(); + typename InputImageType::Pointer input = reader->GetOutput(); // Filter typename OutputImageType::Pointer output; @@ -128,7 +195,30 @@ InvertVFGenericFilter::UpdateWithDimAndPixelType() // Create the InvertVFFilter typedef clitk::InvertVFFilter FilterType; typename FilterType::Pointer filter =FilterType::New(); - filter->SetInput(input); + if (m_ArgsInfo.like_given) { + typename FilterType::SpacingType spacing; + typename FilterType::SizeType size; + itk::ImageIOBase::Pointer header = readImageHeader(m_ArgsInfo.like_arg); + for(unsigned int i=0; iGetDimensions(i); + spacing[i] = header->GetSpacing(i); + } + + typedef itk::VectorResampleImageFilter ResampleFilterType; + typename ResampleFilterType::Pointer resampler = ResampleFilterType::New(); + resampler->SetInput(input); + resampler->SetOutputOrigin(input->GetOrigin()); + resampler->SetOutputDirection(input->GetDirection()); + resampler->SetOutputSpacing(spacing); + resampler->SetSize(size); + resampler->Update(); + spacing = resampler->GetOutput()->GetSpacing(); + size = resampler->GetOutput()->GetLargestPossibleRegion().GetSize(); + filter->SetInput(resampler->GetOutput()); + } + else + filter->SetInput(input); + filter->SetVerbose(m_Verbose); if (m_ArgsInfo.threads_given) filter->SetNumberOfThreads(m_ArgsInfo.threads_arg); if (m_ArgsInfo.pad_given) { @@ -147,6 +237,31 @@ InvertVFGenericFilter::UpdateWithDimAndPixelType() } case 1: { + // Create the InvertVFFilter + typedef clitk::InvertVFFilter FilterType; + typename FilterType::Pointer filter =FilterType::New(); + if (m_ArgsInfo.like_given) { + filter->SetInput(CoeffsToDVF(m_InputFileName, m_ArgsInfo.like_arg)); + } + + filter->SetVerbose(m_Verbose); + if (m_ArgsInfo.threads_given) filter->SetNumberOfThreads(m_ArgsInfo.threads_arg); + if (m_ArgsInfo.pad_given) { + PixelType pad; + if (m_ArgsInfo.pad_given != (pad.GetNumberOfComponents()) ) + pad.Fill(m_ArgsInfo.pad_arg[0]); + else + for(unsigned int i=0; iSetThreadSafe(m_ArgsInfo.threadSafe_flag); + filter->Update(); + output=filter->GetOutput(); + + break; + } + + case 2: { // Create the InverseDeformationFieldFilter #if ITK_VERSION_MAJOR >= 4 typedef itk::InverseDisplacementFieldImageFilter FilterType; @@ -165,6 +280,7 @@ InvertVFGenericFilter::UpdateWithDimAndPixelType() break; } + } // Output