From 55fe2db2309025f919691272d019f395a3f1dd99 Mon Sep 17 00:00:00 2001 From: Simon Rit Date: Thu, 23 Jun 2011 19:44:22 +0200 Subject: [PATCH] Added --transform_grid which uses input->GetDirection (output->GetDirection is identity) and transform matrix to determine the output grid. --- tools/clitkAffineTransform.ggo | 1 + tools/clitkAffineTransformGenericFilter.txx | 36 +++++++++++++++++++-- 2 files changed, 34 insertions(+), 3 deletions(-) diff --git a/tools/clitkAffineTransform.ggo b/tools/clitkAffineTransform.ggo index ba03536..8eeb3fc 100644 --- a/tools/clitkAffineTransform.ggo +++ b/tools/clitkAffineTransform.ggo @@ -10,6 +10,7 @@ section "I/O" option "input" i "Input image filename" string yes option "output" o "Output image filename" string yes option "like" l "Resample output this image (size, spacing,origin)" string no +option "transform_grid" - "Apply affine transform to input grid for output's" flag off section "Options" option "size" - "New output size if different from input" int no multiple diff --git a/tools/clitkAffineTransformGenericFilter.txx b/tools/clitkAffineTransformGenericFilter.txx index 5a8cfa9..c758cb2 100644 --- a/tools/clitkAffineTransformGenericFilter.txx +++ b/tools/clitkAffineTransformGenericFilter.txx @@ -163,6 +163,33 @@ AffineTransformGenericFilter::UpdateWithDimAndPixelType() likeReader->SetFileName(m_ArgsInfo.like_arg); likeReader->Update(); resampler->SetOutputParametersFromImage(likeReader->GetOutput()); + } else if(m_ArgsInfo.transform_grid_flag) { + typename itk::Matrix invMatrix( matrix.GetInverse() ); + typename itk::Matrix invRotMatrix( clitk::GetRotationalPartMatrix(invMatrix) ); + typename itk::Vector invTrans = clitk::GetTranslationPartMatrix(invMatrix); + + // Size is converted to double, transformed and converted back to size type + vnl_vector vnlOutputSize(Dimension); + for(unsigned int i=0; i< Dimension; i++) { + vnlOutputSize[i] = input->GetLargestPossibleRegion().GetSize()[i]; + } + vnlOutputSize = invRotMatrix * + input->GetDirection().GetVnlMatrix() * + vnlOutputSize; + typename OutputImageType::SizeType outputSize; + for(unsigned int i=0; i< Dimension; i++) { + outputSize[i] = lrint(vnlOutputSize[i]); + } + resampler->SetSize( outputSize ); + + // Spacing can be dictly computed in the same way + resampler->SetOutputSpacing ( invRotMatrix * + input->GetDirection() * + input->GetSpacing() ); + // Origin is influenced by translation but not by direction + resampler->SetOutputOrigin ( invRotMatrix * + input->GetOrigin() + + invTrans ); } else { //Size typename OutputImageType::SizeType outputSize; @@ -170,7 +197,6 @@ AffineTransformGenericFilter::UpdateWithDimAndPixelType() for(unsigned int i=0; i< Dimension; i++) outputSize[i]=m_ArgsInfo.size_arg[i]; } else outputSize=input->GetLargestPossibleRegion().GetSize(); - std::cout<<"Setting the size to "<::UpdateWithDimAndPixelType() for(unsigned int i=0; i< Dimension; i++) outputSpacing[i]=m_ArgsInfo.spacing_arg[i]; } else outputSpacing=input->GetSpacing(); - std::cout<<"Setting the spacing to "<::UpdateWithDimAndPixelType() for(unsigned int i=0; i< Dimension; i++) outputOrigin[i]=m_ArgsInfo.origin_arg[i]; } else outputOrigin=input->GetOrigin(); - std::cout<<"Setting the origin to "<SetSize( outputSize ); @@ -195,6 +219,12 @@ AffineTransformGenericFilter::UpdateWithDimAndPixelType() } + if (m_ArgsInfo.verbose_flag) { + std::cout << "Setting the output size to " << resampler->GetSize() << "..." << std::endl; + std::cout << "Setting the output spacing to " << resampler->GetOutputSpacing() << "..." << std::endl; + std::cout << "Setting the output origin to " << resampler->GetOutputOrigin() << "..." << std::endl; + } + resampler->SetInput( input ); resampler->SetTransform( affineTransform ); resampler->SetInterpolator( genericInterpolator->GetInterpolatorPointer()); -- 2.47.1