X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=registration%2FclitkBSplineDeformableTransformInitializer.txx;fp=registration%2FclitkBSplineDeformableTransformInitializer.txx;h=0a93ab0432ae814347454e5b47121e016e8bbedd;hb=657652a78c2e2717a6f77e027049173442ca29f0;hp=0000000000000000000000000000000000000000;hpb=8abe9edbdc63a33f8fbb1f321073b762e08be9aa;p=clitk.git diff --git a/registration/clitkBSplineDeformableTransformInitializer.txx b/registration/clitkBSplineDeformableTransformInitializer.txx new file mode 100644 index 0000000..0a93ab0 --- /dev/null +++ b/registration/clitkBSplineDeformableTransformInitializer.txx @@ -0,0 +1,244 @@ +/*========================================================================= + 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://oncora1.lyon.fnclcc.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 __clitkBSplineDeformableTransformInitializer_txx +#define __clitkBSplineDeformableTransformInitializer_txx +#include "clitkBSplineDeformableTransformInitializer.h" + +namespace clitk +{ + + + template < class TTransform, class TImage > + BSplineDeformableTransformInitializer + ::BSplineDeformableTransformInitializer() + { + this->m_NumberOfControlPointsInsideTheImage.Fill( 5 ); + this->m_ControlPointSpacing.Fill(64.); + this->m_ChosenSpacing.Fill(64.); + m_NumberOfControlPointsIsGiven=false; + m_ControlPointSpacingIsGiven=false; + m_TransformRegionIsGiven=false; + m_SamplingFactorIsGiven=false; + m_SplineOrders.Fill(3); + m_Parameters=NULL; + } + + template < class TTransform, class TImage > + void + BSplineDeformableTransformInitializer + ::InitializeTransform() + { + // Sanity check: + // The image is required for the region and spacing + if( ! this->m_Image ) + { + itkExceptionMacro( "Reference Image has not been set" ); + return; + } + + // A pointer to the transform is required + if( ! this->m_Transform ) + { + itkExceptionMacro( "Transform has not been set" ); + return; + } + + // If the image come from a filter, then update that filter. + if( this->m_Image->GetSource() ) + { + this->m_Image->GetSource()->Update(); + } + + + //-------------------------------------- + // Spacing & Size on image + //-------------------------------------- + SpacingType fixedImageSpacing = m_Image->GetSpacing(); + SizeType fixedImageSize=m_Image->GetLargestPossibleRegion().GetSize(); + typename RegionType::SizeType gridBorderSize; + typename RegionType::SizeType totalGridSize; + + // Only spacing given: adjust if necessary + if (m_ControlPointSpacingIsGiven && !m_NumberOfControlPointsIsGiven) + { + for(unsigned int r=0; rTransformIndexToPhysicalPoint(m_Image->GetLargestPossibleRegion().GetIndex(),fixedImageOrigin); + typename ImageType::DirectionType gridDirection = m_Image->GetDirection(); + SizeType orderShift; + + // Shift depends on order + for(unsigned int r=0; rm_Transform->SetSplineOrders(m_SplineOrders); + this->m_Transform->SetGridRegion( gridRegion ); + this->m_Transform->SetGridOrigin( gridOrigin ); + this->m_Transform->SetGridSpacing( m_ControlPointSpacing ); + this->m_Transform->SetGridDirection( gridDirection ); + this->m_Transform->SetLUTSamplingFactors(m_SamplingFactors); + + } + + template < class TTransform, class TImage > + void + BSplineDeformableTransformInitializer + ::SetInitialParameters( const std::string& s, ParametersType& params) + { + //--------------------------------------- + // Read Initial parameters + //--------------------------------------- + typedef itk::ImageFileReader CoefficientReaderType; + typename CoefficientReaderType::Pointer coeffReader=CoefficientReaderType::New(); + coeffReader->SetFileName(s); + if(m_Verbose) std::cout<<"Reading initial coefficients from file: "<Update(); + typename CoefficientImageType::Pointer coefficientImage=coeffReader->GetOutput(); + this->SetInitialParameters(coefficientImage, params); + } + + template < class TTransform, class TImage > + void + BSplineDeformableTransformInitializer + ::SetInitialParameters(const typename CoefficientImageType::Pointer coefficientImage, ParametersType& params) + { + // Keep a reference + m_Parameters=¶ms; + + // Resample + typedef ResampleBSplineDeformableTransformImageFilter ResamplerType; + typename ResamplerType::Pointer resampler=ResamplerType::New(); + resampler->SetInput(coefficientImage); + resampler->SetOutputParametersFromImage(m_Transform->GetCoefficientImage()); + if(m_Verbose) std::cout<<"Resampling initial coefficients..."<Update(); + + // Copy parameters into the existing array, I know its crappy + typedef itk::ImageRegionConstIterator Iterator; + Iterator it (resampler->GetOutput(), resampler->GetOutput()->GetLargestPossibleRegion() ); + it.GoToBegin(); + unsigned int i=0; + while(! it.IsAtEnd()) + { + for (unsigned int j=0; jm_Transform->SetParameters(params); + } + + + +} // namespace itk + +#endif