X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=tools%2FclitkWarpImageGenericFilter.txx;h=84bfbaaec7148cc5f133180c3b5290ca608eb491;hb=543b72e23ad001ac2a7743b9beacf48e2d0054ac;hp=3d73221de9ad4e72193e0a02853ce36ce93531f1;hpb=0b7c9b1e1215634b02cbd38d4e4ba101d6111ba8;p=clitk.git diff --git a/tools/clitkWarpImageGenericFilter.txx b/tools/clitkWarpImageGenericFilter.txx index 3d73221..84bfbaa 100644 --- a/tools/clitkWarpImageGenericFilter.txx +++ b/tools/clitkWarpImageGenericFilter.txx @@ -1,9 +1,9 @@ /*========================================================================= Program: vv http://www.creatis.insa-lyon.fr/rio/vv - Authors belong to: + Authors belong to: - University of LYON http://www.universite-lyon.fr/ - - Léon Bérard cancer center http://oncora1.lyon.fnclcc.fr + - Léon Bérard cancer center http://www.centreleonberard.fr - CREATIS CNRS laboratory http://www.creatis.insa-lyon.fr This software is distributed WITHOUT ANY WARRANTY; without even @@ -14,227 +14,236 @@ - BSD See included LICENSE.txt file - CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html -======================================================================-====*/ +===========================================================================**/ #ifndef clitkWarpImageGenericFilter_txx #define clitkWarpImageGenericFilter_txx /* ================================================= * @file clitkWarpImageGenericFilter.txx - * @author - * @date - * - * @brief - * + * @author + * @date + * + * @brief + * ===================================================*/ +#include "itkVectorResampleImageFilter.h" +#include "clitkConvertBLUTCoeffsToVFFilter.h" namespace clitk { - //------------------------------------------------------------------- - // Update with the number of dimensions - //------------------------------------------------------------------- - template - void - WarpImageGenericFilter::UpdateWithDim(std::string PixelType) - { - if (m_Verbose) std::cout << "Image was detected to be "<(); - } - // else if(PixelType == "unsigned_short"){ - // if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and unsigned_short..." << std::endl; - // UpdateWithDimAndPixelType(); - // } - - else if (PixelType == "unsigned_char"){ - if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and unsigned_char..." << std::endl; - UpdateWithDimAndPixelType(); - } - - // else if (PixelType == "char"){ - // if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and signed_char..." << std::endl; - // UpdateWithDimAndPixelType(); - // } - else { - if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and float..." << std::endl; - UpdateWithDimAndPixelType(); - } +//------------------------------------------------------------------- +// Update with the number of dimensions +//------------------------------------------------------------------- +template +void +WarpImageGenericFilter::UpdateWithDim(std::string PixelType) +{ + if (m_Verbose) std::cout << "Image was detected to be "<(); } + // else if(PixelType == "unsigned_short"){ + // if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and unsigned_short..." << std::endl; + // UpdateWithDimAndPixelType(); + // } + + else if (PixelType == "unsigned_char") { + if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and unsigned_char..." << std::endl; + UpdateWithDimAndPixelType(); + } + + // else if (PixelType == "char"){ + // if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and signed_char..." << std::endl; + // UpdateWithDimAndPixelType(); + // } + else { + if (m_Verbose) std::cout << "Launching filter in "<< Dimension <<"D and float..." << std::endl; + UpdateWithDimAndPixelType(); + } +} - //------------------------------------------------------------------- - // Update with the number of dimensions and the pixeltype - //------------------------------------------------------------------- - template - void - WarpImageGenericFilter::UpdateWithDimAndPixelType() - { - - // ImageTypes - typedef itk::Image InputImageType; - typedef itk::Image OutputImageType; - typedef itk::Vector DisplacementType; - typedef itk::Image DeformationFieldType; - - - // Read the input - typedef itk::ImageFileReader InputReaderType; - typename InputReaderType::Pointer reader = InputReaderType::New(); - reader->SetFileName( m_InputFileName); - reader->Update(); - typename InputImageType::Pointer input= reader->GetOutput(); - +//------------------------------------------------------------------- +// Update with the number of dimensions and the pixeltype +//------------------------------------------------------------------- +template +void +WarpImageGenericFilter::UpdateWithDimAndPixelType() +{ + + // ImageTypes + typedef itk::Image InputImageType; + typedef itk::Image OutputImageType; + typedef itk::Vector DisplacementType; + typedef itk::Image DeformationFieldType; + + // Read the input + typedef itk::ImageFileReader InputReaderType; + typename InputReaderType::Pointer reader = InputReaderType::New(); + reader->SetFileName( m_InputFileName); + reader->Update(); + typename InputImageType::Pointer input= reader->GetOutput(); + + typename DeformationFieldType::Pointer deformationField; + if (m_ArgsInfo.coeff_given) { + typedef ConvertBLUTCoeffsToVFFilter FilterType; + typename FilterType::Pointer filter = FilterType::New(); + filter->SetInputFileName(m_ArgsInfo.coeff_arg); + filter->SetLikeFileName(m_InputFileName); + filter->SetVerbose(m_Verbose); + filter->Update(); + deformationField = filter->GetOutput(); + } + else { //Read the deformation field typedef itk::ImageFileReader DeformationFieldReaderType; typename DeformationFieldReaderType::Pointer deformationFieldReader= DeformationFieldReaderType::New(); deformationFieldReader->SetFileName(m_ArgsInfo.vf_arg); deformationFieldReader->Update(); - typename DeformationFieldType::Pointer deformationField =deformationFieldReader->GetOutput(); + deformationField =deformationFieldReader->GetOutput(); + } + + // Intensity interpolator + typedef clitk::GenericVectorInterpolator GenericInterpolatorType; + typename GenericInterpolatorType::Pointer genericInterpolator=GenericInterpolatorType::New(); + genericInterpolator->SetArgsInfo(m_ArgsInfo); + + + // ------------------------------------------- + // Spacing like DVF + // ------------------------------------------- + if (m_ArgsInfo.spacing_arg == 0) { + // Calculate the region + typename DeformationFieldType::SizeType newSize; + for (unsigned int i=0 ; i GetLargestPossibleRegion().GetSize()[i]*input->GetSpacing()[i]/deformationField->GetSpacing()[i]); - // Intensity interpolator + // Get the interpolator typedef clitk::GenericVectorInterpolator GenericInterpolatorType; typename GenericInterpolatorType::Pointer genericInterpolator=GenericInterpolatorType::New(); genericInterpolator->SetArgsInfo(m_ArgsInfo); - - - // ------------------------------------------- - // Spacing like DVF - // ------------------------------------------- - if (m_ArgsInfo.spacing_arg == 0) - { - // Calculate the region - typename DeformationFieldType::SizeType newSize; - for (unsigned int i=0 ; i GetLargestPossibleRegion().GetSize()[i]*input->GetSpacing()[i]/deformationField->GetSpacing()[i]); - - // Get the interpolator - typedef clitk::GenericVectorInterpolator GenericInterpolatorType; - typename GenericInterpolatorType::Pointer genericInterpolator=GenericInterpolatorType::New(); - genericInterpolator->SetArgsInfo(m_ArgsInfo); - - // Resample to match the extent of the input - typename itk::VectorResampleImageFilter::Pointer - resampler =itk::VectorResampleImageFilter::New(); - resampler->SetInput(deformationField); - resampler->SetOutputSpacing(deformationField->GetSpacing()); - resampler->SetSize(newSize); - resampler->SetOutputOrigin(input->GetOrigin()); - resampler->SetInterpolator(genericInterpolator->GetInterpolatorPointer()); - - // Update - if (m_Verbose) std::cout<< "Resampling the VF..." <Update(); - } - catch( itk::ExceptionObject & excp ) { - std::cerr << "Problem resampling the input vector field file" << std::endl; - std::cerr << excp << std::endl; - return; - } - deformationField= resampler->GetOutput(); - - } - - // ------------------------------------------- - // Spacing like input - // ------------------------------------------- - else - { - // Get size - typename DeformationFieldType::SizeType newSize; - for (unsigned int i=0 ; i GetLargestPossibleRegion().GetSize()[i]; - - // Resample to match the extent of the input - typename itk::VectorResampleImageFilter::Pointer - resampler =itk::VectorResampleImageFilter::New(); - resampler->SetInput(deformationField); - resampler->SetOutputSpacing(input->GetSpacing()); - resampler->SetSize(newSize); - resampler->SetOutputOrigin(input->GetOrigin()); - resampler->SetInterpolator(genericInterpolator->GetInterpolatorPointer()); - - // Update - if (m_Verbose) std::cout<< "Resampling the VF..." <Update(); - } - catch( itk::ExceptionObject & excp ) { - std::cerr << "Problem resampling the input vector field file" << std::endl; - std::cerr << excp << std::endl; - return; - } - deformationField= resampler->GetOutput(); - } - - - // ------------------------------------------- - // Forward Warp - // ------------------------------------------- - typename itk::ImageToImageFilter::Pointer warpFilter; - if (m_ArgsInfo.forward_flag) - { - //Forward warping: always linear - typedef clitk::ForwardWarpImageFilter ForwardWarpFilterType; - typename ForwardWarpFilterType::Pointer forwardWarpFilter= ForwardWarpFilterType::New(); - forwardWarpFilter->SetDeformationField( deformationField ); - forwardWarpFilter->SetEdgePaddingValue( static_cast(m_ArgsInfo.pad_arg) ); - warpFilter=forwardWarpFilter; - } - - // ------------------------------------------- - // Backward Warp - // ------------------------------------------- - else - { - // Get the interpolator - typedef clitk::GenericInterpolator GenericInterpolatorType; - typename GenericInterpolatorType::Pointer genericInterpolator=GenericInterpolatorType::New(); - genericInterpolator->SetArgsInfo(m_ArgsInfo); - - //Backward mapping - typedef itk::WarpImageFilter BackwardWarpFilterType; - typename BackwardWarpFilterType::Pointer backwardWarpFilter= BackwardWarpFilterType::New(); - backwardWarpFilter->SetDeformationField( deformationField ); - backwardWarpFilter->SetEdgePaddingValue( static_cast(m_ArgsInfo.pad_arg) ); - backwardWarpFilter->SetOutputSpacing( deformationField->GetSpacing() ); - backwardWarpFilter->SetOutputOrigin( input->GetOrigin() ); - backwardWarpFilter->SetOutputSize( deformationField->GetLargestPossibleRegion().GetSize() ); - typename itk::VectorResampleImageFilter::Pointer - resampler =itk::VectorResampleImageFilter::New(); - backwardWarpFilter->SetInterpolator(genericInterpolator->GetInterpolatorPointer()); - warpFilter=backwardWarpFilter; - } - - - // ------------------------------------------- + + // Resample to match the extent of the input + typename itk::VectorResampleImageFilter::Pointer + resampler =itk::VectorResampleImageFilter::New(); + resampler->SetInput(deformationField); + resampler->SetOutputSpacing(deformationField->GetSpacing()); + resampler->SetOutputDirection(deformationField->GetDirection()); + resampler->SetSize(newSize); + resampler->SetOutputOrigin(input->GetOrigin()); + resampler->SetInterpolator(genericInterpolator->GetInterpolatorPointer()); + // Update - // ------------------------------------------- - warpFilter->SetInput(input); - if (m_Verbose) std::cout<< "Warping the input..." <Update(); + resampler->Update(); + } catch( itk::ExceptionObject & excp ) { + std::cerr << "Problem resampling the input vector field file" << std::endl; + std::cerr << excp << std::endl; + return; } - catch( itk::ExceptionObject & excp ) { - std::cerr << "Problem warping the input image" << std::endl; + deformationField= resampler->GetOutput(); + + } + + // ------------------------------------------- + // Spacing like input + // ------------------------------------------- + else if (!m_ArgsInfo.coeff_given) { + // Get size + typename DeformationFieldType::SizeType newSize; + for (unsigned int i=0 ; i GetLargestPossibleRegion().GetSize()[i]; + + // Resample to match the extent of the input + typename itk::VectorResampleImageFilter::Pointer + resampler =itk::VectorResampleImageFilter::New(); + resampler->SetInput(deformationField); + resampler->SetOutputSpacing(input->GetSpacing()); + resampler->SetOutputDirection(deformationField->GetDirection()); + resampler->SetSize(newSize); + resampler->SetOutputOrigin(input->GetOrigin()); + resampler->SetInterpolator(genericInterpolator->GetInterpolatorPointer()); + + // Update + if (m_Verbose) std::cout<< "Resampling the VF..." <Update(); + } catch( itk::ExceptionObject & excp ) { + std::cerr << "Problem resampling the input vector field file" << std::endl; std::cerr << excp << std::endl; return; - } - + } + deformationField= resampler->GetOutput(); + } - // Output - typedef itk::ImageFileWriter WriterType; - typename WriterType::Pointer writer = WriterType::New(); - writer->SetFileName(m_ArgsInfo.output_arg); - writer->SetInput(warpFilter->GetOutput()); - writer->Update(); + // ------------------------------------------- + // Forward Warp + // ------------------------------------------- + typename itk::ImageToImageFilter::Pointer warpFilter; + if (m_ArgsInfo.forward_flag) { + //Forward warping: always linear + typedef clitk::ForwardWarpImageFilter ForwardWarpFilterType; + typename ForwardWarpFilterType::Pointer forwardWarpFilter= ForwardWarpFilterType::New(); + forwardWarpFilter->SetDeformationField( deformationField ); + forwardWarpFilter->SetEdgePaddingValue( static_cast(m_ArgsInfo.pad_arg) ); + warpFilter=forwardWarpFilter; } + // ------------------------------------------- + // Backward Warp + // ------------------------------------------- + else { + // Get the interpolator + typedef clitk::GenericInterpolator GenericInterpolatorType; + typename GenericInterpolatorType::Pointer genericInterpolator=GenericInterpolatorType::New(); + genericInterpolator->SetArgsInfo(m_ArgsInfo); + + //Backward mapping + typedef itk::WarpImageFilter BackwardWarpFilterType; + typename BackwardWarpFilterType::Pointer backwardWarpFilter= BackwardWarpFilterType::New(); + backwardWarpFilter->SetDisplacementField( deformationField ); + backwardWarpFilter->SetEdgePaddingValue( static_cast(m_ArgsInfo.pad_arg) ); + backwardWarpFilter->SetOutputSpacing( deformationField->GetSpacing() ); + backwardWarpFilter->SetOutputOrigin( input->GetOrigin() ); + backwardWarpFilter->SetOutputSize( deformationField->GetLargestPossibleRegion().GetSize() ); + backwardWarpFilter->SetOutputDirection( input->GetDirection() ); + typename itk::VectorResampleImageFilter::Pointer + resampler =itk::VectorResampleImageFilter::New(); + backwardWarpFilter->SetInterpolator(genericInterpolator->GetInterpolatorPointer()); + warpFilter=backwardWarpFilter; + } + + + // ------------------------------------------- + // Update + // ------------------------------------------- + warpFilter->SetInput(input); + if (m_Verbose) std::cout<< "Warping the input..." <Update(); + } catch( itk::ExceptionObject & excp ) { + std::cerr << "Problem warping the input image" << std::endl; + std::cerr << excp << std::endl; + return; + } + + + // Output + typedef itk::ImageFileWriter WriterType; + typename WriterType::Pointer writer = WriterType::New(); + writer->SetFileName(m_ArgsInfo.output_arg); + writer->SetInput(warpFilter->GetOutput()); + writer->Update(); + +} + }//end clitk - + #endif //#define clitkWarpImageGenericFilter_txx