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
- BSD See included LICENSE.txt file
- CeCILL-B http://www.cecill.info/licences/Licence_CeCILL-B_V1-en.html
- ======================================================================-====*/
+ ===========================================================================**/
// clitk
-#include "clitkCommon.h"
+#include "clitkDD.h"
// itk include
#include "itkImage.h"
#include "itkResampleImageFilter.h"
#include "itkAffineTransform.h"
#include "itkNearestNeighborInterpolateImageFunction.h"
+#include "itkWindowedSincInterpolateImageFunction.h"
#include "itkLinearInterpolateImageFunction.h"
#include "itkBSplineInterpolateImageFunction.h"
#include "itkBSplineInterpolateImageFunctionWithLUT.h"
#include "itkCommand.h"
//--------------------------------------------------------------------
-template <class TInputImage, class TOutputImage>
-clitk::ResampleImageWithOptionsFilter<TInputImage, TOutputImage>::
-ResampleImageWithOptionsFilter():itk::ImageToImageFilter<TInputImage, TOutputImage>()
+template <class InputImageType, class OutputImageType>
+clitk::ResampleImageWithOptionsFilter<InputImageType, OutputImageType>::
+ResampleImageWithOptionsFilter():itk::ImageToImageFilter<InputImageType, OutputImageType>()
{
static const unsigned int dim = InputImageType::ImageDimension;
this->SetNumberOfRequiredInputs(1);
m_OutputSpacing[i] = -1;
m_GaussianSigma[i] = -1;
}
+ m_OutputOrigin.Fill(0);
+ m_OutputDirection.SetIdentity();
m_VerboseOptions = false;
+ SetDefaultPixelValue(0);
}
//--------------------------------------------------------------------
//--------------------------------------------------------------------
-template <class TInputImage, class TOutputImage>
+template <class InputImageType, class OutputImageType>
void
-clitk::ResampleImageWithOptionsFilter<TInputImage, TOutputImage>::
-SetInput(const InputImageType * image)
+clitk::ResampleImageWithOptionsFilter<InputImageType, OutputImageType>::
+SetInput(const InputImageType * image)
{
// Process object is not const-correct so the const casting is required.
this->SetNthInput(0, const_cast<InputImageType *>(image));
//--------------------------------------------------------------------
-template <class TInputImage, class TOutputImage>
+template <class InputImageType, class OutputImageType>
void
-clitk::ResampleImageWithOptionsFilter<TInputImage, TOutputImage>::
-GenerateInputRequestedRegion()
+clitk::ResampleImageWithOptionsFilter<InputImageType, OutputImageType>::
+GenerateInputRequestedRegion()
{
// call the superclass's implementation of this method
Superclass::GenerateInputRequestedRegion();
// get pointers to the input and output
InputImagePointer inputPtr =
- const_cast< TInputImage *>( this->GetInput() );
+ const_cast< InputImageType *>( this->GetInput() );
// Request the entire input image
InputImageRegionType inputRegion;
//--------------------------------------------------------------------
-template <class TInputImage, class TOutputImage>
+template <class InputImageType, class OutputImageType>
void
-clitk::ResampleImageWithOptionsFilter<TInputImage, TOutputImage>::
-GenerateOutputInformation()
+clitk::ResampleImageWithOptionsFilter<InputImageType, OutputImageType>::
+GenerateOutputInformation()
{
static const unsigned int dim = InputImageType::ImageDimension;
if (m_OutputIsoSpacing != -1) { // apply isoSpacing
for(unsigned int i=0; i<dim; i++) {
m_OutputSpacing[i] = m_OutputIsoSpacing;
- m_OutputSize[i] = (int)lrint(inputSize[i]*inputSpacing[i]/m_OutputSpacing[i]);
+ // floor() is used to intentionally reduce the number of slices
+ // because, from a clinical point of view, it's better to
+ // remove data than to add data that privously didn't exist.
+ if(inputSpacing[i]*m_OutputSpacing[i]<0)
+ itkExceptionMacro( << "Input and output spacings don't have the same signs, can't cope with that" );
+ m_OutputSize[i] = (int)floor(inputSize[i]*inputSpacing[i]/m_OutputSpacing[i]);
}
- } else {
+ }
+ else if(m_OutputSpacing[0]==-1 || m_OutputSize[0]==0){
if (m_OutputSpacing[0] != -1) { // apply spacing, compute size
for(unsigned int i=0; i<dim; i++) {
- m_OutputSize[i] = (int)lrint(inputSize[i]*inputSpacing[i]/m_OutputSpacing[i]);
+ if(inputSpacing[i]*m_OutputSpacing[i]<0) {
+ itkExceptionMacro( << "Input and output spacings don't have the same signs, can't cope with that" );
+ }
+ // see comment above for the use of floor()
+ m_OutputSize[i] = (int)floor(inputSize[i]*inputSpacing[i]/m_OutputSpacing[i]);
}
- } else {
+ }
+ else {
if (m_OutputSize[0] != 0) { // apply size, compute spacing
for(unsigned int i=0; i<dim; i++) {
m_OutputSpacing[i] = (double)inputSize[i]*inputSpacing[i]/(double)m_OutputSize[i];
}
- } else { // copy input size/spacing ... (no resampling)
+ }
+ else { // copy input size/spacing ... (no resampling)
m_OutputSize = inputSize;
m_OutputSpacing = inputSpacing;
}
m_OutputSize[l] = inputSize[l];
m_OutputSpacing[l] = inputSpacing[l];
}
-
+
// Set Size/Spacing
OutputImagePointer outputImage = this->GetOutput(0);
// OutputImageRegionType region;
//--------------------------------------------------------------------
-template <class TInputImage, class TOutputImage>
-void
-clitk::ResampleImageWithOptionsFilter<TInputImage, TOutputImage>::
-GenerateData()
+template <class InputImageType, class OutputImageType>
+void
+clitk::ResampleImageWithOptionsFilter<InputImageType, OutputImageType>::
+GenerateData()
{
-
+
// Get input pointer
InputImagePointer input = dynamic_cast<InputImageType*>(itk::ProcessObject::GetInput(0));
static const unsigned int dim = InputImageType::ImageDimension;
- // Set regions and allocate
- //this->GetOutput()->SetRegions(m_OutputRegion);
- //this->GetOutput()->Allocate();
- // this->GetOutput()->FillBuffer(m_DefaultPixelValue);
-
// Create main Resample Image Filter
typedef itk::ResampleImageFilter<InputImageType,OutputImageType> FilterType;
typename FilterType::Pointer filter = FilterType::New();
filter->GraftOutput(this->GetOutput());
- // this->GetOutput()->Print(std::cout);
- // this->GetOutput()->SetBufferedRegion(this->GetOutput()->GetLargestPossibleRegion());
- // this->GetOutput()->Print(std::cout);
+ this->GetOutput()->SetBufferedRegion(this->GetOutput()->GetLargestPossibleRegion());
// Print options if needed
if (m_VerboseOptions) {
case Linear: std::cout << "Linear" << std::endl; break;
case BSpline: std::cout << "BSpline " << m_BSplineOrder << std::endl; break;
case B_LUT: std::cout << "B-LUT " << m_BSplineOrder << " " << m_BLUTSamplingFactor << std::endl; break;
+ case WSINC: std::cout << "Windowed Sinc" << std::endl; break;
}
+#if ITK_VERSION_MAJOR <= 4
std::cout << "Threads = " << this->GetNumberOfThreads() << std::endl;
+#else
+ std::cout << "Threads = " << this->GetNumberOfWorkUnits() << std::endl;
+#endif
std::cout << "LastDimIsTime = " << m_LastDimensionIsTime << std::endl;
}
+ // Compute origin based on image corner
+ for(unsigned int i=0; i<OutputImageType::ImageDimension; i++) {
+ m_OutputOrigin[i] -= 0.5 * input->GetSpacing()[i];
+ m_OutputOrigin[i] += 0.5 * m_OutputSpacing[i];
+ }
+
// Instance of the transform object to be passed to the resample
// filter. By default, identity transform is applied
filter->SetTransform(m_Transform);
filter->SetSize(m_OutputSize);
filter->SetOutputSpacing(m_OutputSpacing);
- filter->SetOutputOrigin(input->GetOrigin());
+ filter->SetOutputOrigin(m_OutputOrigin);
filter->SetDefaultPixelValue(m_DefaultPixelValue);
- filter->SetNumberOfThreads(this->GetNumberOfThreads());
- filter->SetOutputDirection(input->GetDirection()); // <-- NEEDED if we want to keep orientation (in case of PermutAxes for example)
+#if ITK_VERSION_MAJOR <= 4
+ filter->SetNumberOfThreads(this->GetNumberOfThreads());
+#else
+ filter->SetNumberOfWorkUnits(this->GetNumberOfWorkUnits());
+#endif
+ filter->SetOutputDirection(m_OutputDirection); // <-- NEEDED if we want to keep orientation (in case of PermutAxes for example)
// Select interpolator
switch (m_InterpolationType) {
filter->SetInterpolator(interpolator);
break;
}
+ case WSINC: {
+ typedef itk::WindowedSincInterpolateImageFunction<InputImageType, 4> InterpolatorType;
+ typename InterpolatorType::Pointer interpolator = InterpolatorType::New();
+ filter->SetInterpolator(interpolator);
+ break;
+ }
}
// Initial Gaussian blurring if needed
filter->Update();
// Set output
- // DD("before Graft");
+ this->GraftOutput(filter->GetOutput());
+}
+//--------------------------------------------------------------------
- //this->GraftOutput(filter->GetOutput());
- this->SetNthOutput(0, filter->GetOutput());
- // DD("after Graft");
+//--------------------------------------------------------------------
+template<class InputImageType>
+typename InputImageType::Pointer
+clitk::ResampleImageSpacing(typename InputImageType::Pointer input,
+ typename InputImageType::SpacingType spacing,
+ int interpolationType)
+{
+ typedef clitk::ResampleImageWithOptionsFilter<InputImageType> ResampleFilterType;
+ typename ResampleFilterType::Pointer resampler = ResampleFilterType::New();
+ resampler->SetInput(input);
+ resampler->SetOutputSpacing(spacing);
+ typename ResampleFilterType::InterpolationTypeEnumeration inter=ResampleFilterType::NearestNeighbor;
+ switch(interpolationType) {
+ case 0: inter = ResampleFilterType::NearestNeighbor; break;
+ case 1: inter = ResampleFilterType::Linear; break;
+ case 2: inter = ResampleFilterType::BSpline; break;
+ case 3: inter = ResampleFilterType::B_LUT; break;
+ case 4: inter = ResampleFilterType::WSINC; break;
+ }
+ resampler->SetInterpolationType(inter);
+ resampler->SetGaussianFilteringEnabled(true);
+ resampler->Update();
+ return resampler->GetOutput();
}
//--------------------------------------------------------------------