X-Git-Url: https://git.creatis.insa-lyon.fr/pubgit/?a=blobdiff_plain;f=filters%2FclitkImageResampleGenericFilter.cxx;h=3e4e28e19f92c9e9eabff1cb77134b321a25370e;hb=a97c480d25285a6d143bd035d6b5fa0804a7d068;hp=0187e493f9c76a2a83912a332a780e4d30994057;hpb=931a42358442f4ee4f314613c991c838d4b4e3b7;p=clitk.git diff --git a/filters/clitkImageResampleGenericFilter.cxx b/filters/clitkImageResampleGenericFilter.cxx index 0187e49..3e4e28e 100644 --- a/filters/clitkImageResampleGenericFilter.cxx +++ b/filters/clitkImageResampleGenericFilter.cxx @@ -1,6 +1,22 @@ +/*========================================================================= + 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 CLITKIMAGERESAMPLEGENERICFILTER2_CXX #define CLITKIMAGERESAMPLEGENERICFILTER2_CXX - /** ------------------------------------------------------------------- * @file clitkImageResampleGenericFilter.cxx @@ -13,31 +29,182 @@ #include "clitkImageResampleGenericFilter.h" +// itk include +#include "itkImage.h" +#include "itkImageFileReader.h" +#include "itkImageSeriesReader.h" +#include "itkImageFileWriter.h" +#include "itkRecursiveGaussianImageFilter.h" +#include "itkResampleImageFilter.h" +#include "itkAffineTransform.h" +#include "itkNearestNeighborInterpolateImageFunction.h" +#include "itkLinearInterpolateImageFunction.h" +#include "itkBSplineInterpolateImageFunction.h" +#include "itkBSplineInterpolateImageFunctionWithLUT.h" +#include "itkCommand.h" + //-------------------------------------------------------------------- -clitk::ImageResampleGenericFilter::ImageResampleGenericFilter() { +clitk::ImageResampleGenericFilter::ImageResampleGenericFilter(): + ImageToImageGenericFilter("ImageResample") { mApplyGaussianFilterBefore = false; mDefaultPixelValue = 0.0; mInterpolatorName = "NN"; mBSplineOrder=3; + InitializeImageTypeWithDim<2>(); + InitializeImageTypeWithDim<3>(); + InitializeImageTypeWithDim<4>(); } //-------------------------------------------------------------------- + +//-------------------------------------------------------------------- +template +void clitk::ImageResampleGenericFilter::InitializeImageTypeWithDim() { + ADD_DEFAULT_IMAGE_TYPES(Dim); +} +//-------------------------------------------------------------------- + + //-------------------------------------------------------------------- -void clitk::ImageResampleGenericFilter::Update() { +template +void clitk::ImageResampleGenericFilter::UpdateWithInputImageType() { - // Determine dim, pixel type, number of components - this->GetInputImageDimensionAndPixelType(mDim,mPixelTypeName,mNbOfComponents); + // Some typedefs + typedef typename InputImageType::SizeType SizeType; + typedef typename InputImageType::SpacingType SpacingType; + typedef typename InputImageType::PointType PointType; + typedef typename InputImageType::PixelType PixelType; + static unsigned int dim = InputImageType::ImageDimension; - // Switch by dimension - if (mDim == 2) { Update_WithDim<2>(); return; } - if (mDim == 3) { Update_WithDim<3>(); return; } - if (mDim == 4) { Update_WithDim<4>(); return; } + // Reading input + typename InputImageType::Pointer input = this->GetInput(0); + + // Warning + if (!std::numeric_limits::is_signed) { + if ((mInterpolatorName == "bspline") || (mInterpolatorName == "blut")) { + std::cerr << "Warning : input pixel type is not signed, use bspline interpolation at your own risk ..." << std::endl; + } + } + + // Check options + if (mOutputSize.size() != dim) { + std::cerr << "Please set size with " << dim << " dimensions." << std::endl; + return; + } + if (mOutputSpacing.size() != dim) { + std::cerr << "Please set spacing with " << dim << " dimensions." << std::endl; + return; + } + mOutputOrigin.resize(dim); + + if (mApplyGaussianFilterBefore && mSigma.size() != dim) { + std::cerr << "Please set sigma with " << dim << " dimensions." << std::endl; + return; + } + + // Create Image Filter + typedef itk::ResampleImageFilter FilterType; + typename FilterType::Pointer filter = FilterType::New(); + + // Instance of the transform object to be passed to the resample + // filter. By default, identity transform is applied + typedef itk::AffineTransform TransformType; + typename TransformType::Pointer transform = TransformType::New(); + filter->SetTransform(transform); + + // Set filter's parameters + SizeType outputSize; + SpacingType outputSpacing; + PointType outputOrigin; + for(unsigned int i=0; iGetOrigin()[i]; + } + + filter->SetSize(outputSize); + filter->SetOutputSpacing(outputSpacing); + filter->SetOutputOrigin(outputOrigin); + filter->SetDefaultPixelValue(static_cast(mDefaultPixelValue));//DS TODO//JV comme ça? + + // Select interpolator + if (mInterpolatorName == "nn") { + typedef itk::NearestNeighborInterpolateImageFunction InterpolatorType; + typename InterpolatorType::Pointer interpolator = InterpolatorType::New(); + filter->SetInterpolator(interpolator); + } + else { + if (mInterpolatorName == "linear") { + typedef itk::LinearInterpolateImageFunction InterpolatorType; + typename InterpolatorType::Pointer interpolator = InterpolatorType::New(); + filter->SetInterpolator(interpolator); + } + else { + if (mInterpolatorName == "bspline") { + typedef itk::BSplineInterpolateImageFunction InterpolatorType; + typename InterpolatorType::Pointer interpolator = InterpolatorType::New(); + interpolator->SetSplineOrder(mBSplineOrder); + filter->SetInterpolator(interpolator); + } + else { + if (mInterpolatorName == "blut") { + typedef itk::BSplineInterpolateImageFunctionWithLUT InterpolatorType; + typename InterpolatorType::Pointer interpolator = InterpolatorType::New(); + interpolator->SetSplineOrder(mBSplineOrder); + interpolator->SetLUTSamplingFactor(mSamplingFactors[0]); + filter->SetInterpolator(interpolator); + } + else { + std::cerr << "Sorry, I do not know the interpolator '" << mInterpolatorName + << "'. Known interpolators are : nn, linear, bspline, blut" << std::endl; + exit(0); + } + } + } + } + + // Build initial Gaussian bluring (if needed) + typedef itk::RecursiveGaussianImageFilter GaussianFilterType; + std::vector gaussianFilters; + if (mApplyGaussianFilterBefore) { + for(unsigned int i=0; iSetDirection(i); + gaussianFilters[i]->SetOrder(GaussianFilterType::ZeroOrder); + gaussianFilters[i]->SetNormalizeAcrossScale(false); + gaussianFilters[i]->SetSigma(mSigma[i]); // in millimeter ! + // Set input + if (i==0) gaussianFilters[i]->SetInput(input); + else gaussianFilters[i]->SetInput(gaussianFilters[i-1]->GetOutput()); + } + filter->SetInput(gaussianFilters[InputImageType::ImageDimension-1]->GetOutput()); + } + else { + filter->SetInput(input); + } + + // Go ! + try { + filter->Update(); + } + catch( itk::ExceptionObject & err ) { + std::cerr << "Error while filtering " << mInputFilenames[0].c_str() + << " " << err << std::endl; + exit(0); + } + + // Get result + typename InputImageType::Pointer outputImage = filter->GetOutput(); + + // Write/save results + this->SetNextOutput(outputImage); - std::cerr << "Error, dimension of input image is " << mDim << ", but I only work with 2,3,4." << std::endl; - exit(0); } //-------------------------------------------------------------------- + //-------------------------------------------------------------------- void clitk::ImageResampleGenericFilter::SetOutputSize(const std::vector & size) { mOutputSize.resize(size.size());