X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=tools%2FclitkWarpImageGenericFilter.txx;h=d1d1f714f3a76b46978fd0e9f3335740c1006a2c;hb=71021d34bda3e0e61917cdc1780f7ed901714965;hp=425368d65745ef97b234d33b7b2c117e3569b6f2;hpb=9bcf64b7f7c5a7f0a39843df634c05b5d9e4e237;p=clitk.git diff --git a/tools/clitkWarpImageGenericFilter.txx b/tools/clitkWarpImageGenericFilter.txx index 425368d..d1d1f71 100644 --- a/tools/clitkWarpImageGenericFilter.txx +++ b/tools/clitkWarpImageGenericFilter.txx @@ -1,223 +1,246 @@ +/*========================================================================= + Program: vv http://www.creatis.insa-lyon.fr/rio/vv + + Authors belong to: + - University of LYON http://www.universite-lyon.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 + the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR + PURPOSE. See the copyright notices for more information. + + It is distributed under dual licence + + - 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() +{ - //------------------------------------------------------------------- - // 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(); - + // 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); - // Intensity interpolator + + // ------------------------------------------- + // 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); - - - // ------------------------------------------- - // 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->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->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() ); + 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