ITK/Examples/ImageProcessing/Upsampling: Difference between revisions

From KitwarePublic
< ITK‎ | Examples
Jump to navigationJump to search
No edit summary
Line 5: Line 5:
   \date 15 april 2011
   \date 15 april 2011
   \author Francis Girard
   \author Francis Girard
 
   A simple example showing how to use the ResampleImageFilter and the  
   A simple example showing how to use the ResampleImageFilter and the  
   BSplineInterpolateImageFunction to (up)scale an image using bicubic
   BSplineInterpolateImageFunction to (up)scale an image using bicubic
   interpolation.
   interpolation.
*/
*/
 
#include "itkImage.h"
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileReader.h"
Line 17: Line 17:
#include "itkBSplineInterpolateImageFunction.h"
#include "itkBSplineInterpolateImageFunction.h"
#include "itkResampleImageFilter.h"
#include "itkResampleImageFilter.h"
 
#include <iostream>
#include <iostream>
 
#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif
 
#ifdef __BORLANDC__
#define ITK_LEAN_AND_MEAN
#endif
 
 
int main( int argc, char * argv[] )
int main( int argc, char * argv[] )
{
{
Line 38: Line 29:
               << " inputImageFile outputImageFile nNewWidth nNewHeight"
               << " inputImageFile outputImageFile nNewWidth nNewHeight"
               << std::endl;
               << std::endl;
   
     return EXIT_FAILURE;
     return EXIT_FAILURE;
   }
   }
 
   //
   // Typedef's for pixel, image, reader and writer types
  // -- 1 --
  // Usual typedef's for pixel, image, reader and writer types
  //
   typedef unsigned char T_InputPixel;
   typedef unsigned char T_InputPixel;
   typedef unsigned char T_OutputPixel;
   typedef unsigned char T_OutputPixel;
    
    
  // Doesn't work for RGB pixels
  //typedef itk::CovariantVector<unsigned char, 3> T_InputPixel;
  //typedef itk::CovariantVector<unsigned char, 3> T_OutputPixel;
   typedef itk::Image<T_InputPixel, 2> T_Image;
   typedef itk::Image<T_InputPixel, 2> T_Image;
   typedef itk::ImageFileReader<T_Image> T_Reader;
   typedef itk::ImageFileReader<T_Image> T_Reader;
 
   typedef unsigned char T_WritePixel;
   typedef unsigned char T_WritePixel;
   typedef itk::Image<T_WritePixel, 2> T_WriteImage;
   typedef itk::Image<T_WritePixel, 2> T_WriteImage;
   typedef itk::ImageFileWriter<T_WriteImage> T_Writer;
   typedef itk::ImageFileWriter<T_WriteImage> T_Writer;
 
  //
  // -- 2 --
   // Typedefs for the different (numerous!) elements of the "resampling"
   // Typedefs for the different (numerous!) elements of the "resampling"
  //
 
   // Identity transform.
   // Identity transform.
   // We don't want any transform on our image except rescaling which is not
   // We don't want any transform on our image except rescaling which is not
Line 68: Line 57:
   typedef itk::IdentityTransform<double, 2>
   typedef itk::IdentityTransform<double, 2>
               T_Transform;
               T_Transform;
 
   // If ITK resampler determines there is something to interpolate which is
   // If ITK resampler determines there is something to interpolate which is
   // usually the case when upscaling (!) then we must specify the interpolation
   // usually the case when upscaling (!) then we must specify the interpolation
Line 76: Line 65:
   typedef itk::BSplineInterpolateImageFunction<T_Image, double, double>
   typedef itk::BSplineInterpolateImageFunction<T_Image, double, double>
               T_Interpolator;
               T_Interpolator;
 
   // The resampler type itself.
   // The resampler type itself.
   typedef itk::ResampleImageFilter<T_Image, T_Image>
   typedef itk::ResampleImageFilter<T_Image, T_Image>
               T_ResampleFilter;
               T_ResampleFilter;
 
  //
 
  // -- 3 --
   // Prepare the reader and update it right away to know the sizes beforehand.
   // Prepare the reader and update it right away to know the sizes beforehand.
  //
 
   T_Reader::Pointer pReader = T_Reader::New();
   T_Reader::Pointer pReader = T_Reader::New();
   pReader->SetFileName( argv[1] );
   pReader->SetFileName( argv[1] );
   pReader->Update();
   pReader->Update();
 
  //
  // -- 4 --
   // Prepare the resampler.
   // Prepare the resampler.
  //
 
   // Instantiate the transform and specify it should be the id transform.
   // 4.1 Instantiate the transform and specify it should be the id transform.
   T_Transform::Pointer _pTransform = T_Transform::New();
   T_Transform::Pointer _pTransform = T_Transform::New();
   _pTransform->SetIdentity();
   _pTransform->SetIdentity();
 
   // 4.2 Instantiate the b-spline interpolator and set it as the third order
   // Instantiate the b-spline interpolator and set it as the third order
   //     for bicubic.
   // for bicubic.
   T_Interpolator::Pointer _pInterpolator = T_Interpolator::New();
   T_Interpolator::Pointer _pInterpolator = T_Interpolator::New();
   _pInterpolator->SetSplineOrder(3);
   _pInterpolator->SetSplineOrder(3);
 
   // 4.3 Instantiate the resampler. Wire in the transform and the interpolator.
   // Instantiate the resampler. Wire in the transform and the interpolator.
  //   
   T_ResampleFilter::Pointer _pResizeFilter = T_ResampleFilter::New();
   T_ResampleFilter::Pointer _pResizeFilter = T_ResampleFilter::New();
   _pResizeFilter->SetTransform(_pTransform);
   _pResizeFilter->SetTransform(_pTransform);
   _pResizeFilter->SetInterpolator(_pInterpolator);
   _pResizeFilter->SetInterpolator(_pInterpolator);
 
   // 4.4 Set the output origin. You may shift the original image "inside" the
   // Set the output origin. You may shift the original image "inside" the
   //     new image size by specifying something else than 0.0, 0.0 here.
   // new image size by specifying something else than 0.0, 0.0 here.
  //
 
   const double vfOutputOrigin[2]  = { 0.0, 0.0 };
   const double vfOutputOrigin[2]  = { 0.0, 0.0 };
   _pResizeFilter->SetOutputOrigin(vfOutputOrigin);
   _pResizeFilter->SetOutputOrigin(vfOutputOrigin);
 
   // 4.5 Compute and set the output spacing
   //     Compute and set the output spacing
   //    Compute the output spacing from input spacing and old and new sizes.
   //    Compute the output spacing from input spacing and old and new sizes.
   //     
   //     
Line 137: Line 121:
   unsigned int nNewWidth = atoi(argv[3]);
   unsigned int nNewWidth = atoi(argv[3]);
   unsigned int nNewHeight = atoi(argv[4]);
   unsigned int nNewHeight = atoi(argv[4]);
 
   // Fetch original image size.
   // Fetch original image size.
   const T_Image::RegionType& inputRegion =  
   const T_Image::RegionType& inputRegion =  
Line 144: Line 128:
   unsigned int nOldWidth = vnInputSize[0];
   unsigned int nOldWidth = vnInputSize[0];
   unsigned int nOldHeight = vnInputSize[1];
   unsigned int nOldHeight = vnInputSize[1];
 
   // Fetch original image spacing.
   // Fetch original image spacing.
   const T_Image::SpacingType& vfInputSpacing =  
   const T_Image::SpacingType& vfInputSpacing =  
Line 150: Line 134:
                                             // Will be {1.0, 1.0} in the usual
                                             // Will be {1.0, 1.0} in the usual
                                             // case.
                                             // case.
 
   double vfOutputSpacing[2];
   double vfOutputSpacing[2];
   vfOutputSpacing[0] = vfInputSpacing[0] * (double) nOldWidth / (double) nNewWidth;
   vfOutputSpacing[0] = vfInputSpacing[0] * (double) nOldWidth / (double) nNewWidth;
   vfOutputSpacing[1] = vfInputSpacing[1] * (double) nOldHeight / (double) nNewHeight;
   vfOutputSpacing[1] = vfInputSpacing[1] * (double) nOldHeight / (double) nNewHeight;
 
   // Set the output spacing. If you comment out the following line, the original
   // Set the output spacing. If you comment out the following line, the original
   // image will be simply put in the upper left corner of the new image without
   // image will be simply put in the upper left corner of the new image without
   // any scaling.
   // any scaling.
   _pResizeFilter->SetOutputSpacing(vfOutputSpacing);
   _pResizeFilter->SetOutputSpacing(vfOutputSpacing);
 
   // 4.6 Set the output size as specified on the command line.
   // Set the output size as specified on the command line.
  //
 
   itk::Size<2> vnOutputSize = {nNewWidth, nNewHeight};
   itk::Size<2> vnOutputSize = { {nNewWidth, nNewHeight} };
   _pResizeFilter->SetSize(vnOutputSize);
   _pResizeFilter->SetSize(vnOutputSize);
 
   // 4.7 Well, some input to our elaborate filter.
   // Specify the input.
  //
 
   _pResizeFilter->SetInput(pReader->GetOutput());
   _pResizeFilter->SetInput(pReader->GetOutput());
 
  // -- 5 --
   // Write the result
   // Done ! Prepare the writer, do the wiring and "Update()" as usual.
   T_Writer::Pointer pWriter = T_Writer::New();
   T_Writer::Pointer pWriter = T_Writer::New();
   pWriter->SetFileName(argv[2]);
   pWriter->SetFileName(argv[2]);
   pWriter->SetInput(_pResizeFilter->GetOutput());
   pWriter->SetInput(_pResizeFilter->GetOutput());
   pWriter->Update();
   pWriter->Update();
 
  // Go home.
   return EXIT_SUCCESS;
   return 0;
}
}
</source>
</source>


{{ITKCMakeLists|Upsampling|}}
{{ITKCMakeLists|Upsampling|}}

Revision as of 19:58, 20 April 2011

Upsampling.cxx

<source lang="cpp"> /**

 \file Upsampling.cxx
 \date 15 april 2011
 \author Francis Girard

 A simple example showing how to use the ResampleImageFilter and the 
 BSplineInterpolateImageFunction to (up)scale an image using bicubic
 interpolation.
  • /
  1. include "itkImage.h"
  2. include "itkImageFileReader.h"
  3. include "itkImageFileWriter.h"
  4. include "itkIdentityTransform.h"
  5. include "itkBSplineInterpolateImageFunction.h"
  6. include "itkResampleImageFilter.h"
  1. include <iostream>

int main( int argc, char * argv[] ) {

 if( argc != 5 ) 
 { 
   std::cerr << "Usage: "
             << std::endl
             << argv[0]
             << " inputImageFile outputImageFile nNewWidth nNewHeight"
             << std::endl;

   return EXIT_FAILURE;
 }

 // Typedef's for pixel, image, reader and writer types
 typedef unsigned char T_InputPixel;
 typedef unsigned char T_OutputPixel;
 
 // Doesn't work for RGB pixels
 //typedef itk::CovariantVector<unsigned char, 3> T_InputPixel;
 //typedef itk::CovariantVector<unsigned char, 3> T_OutputPixel;

 typedef itk::Image<T_InputPixel, 2> T_Image;
 typedef itk::ImageFileReader<T_Image> T_Reader;

 typedef unsigned char T_WritePixel;
 typedef itk::Image<T_WritePixel, 2> T_WriteImage;
 typedef itk::ImageFileWriter<T_WriteImage> T_Writer;

 // Typedefs for the different (numerous!) elements of the "resampling"

 // Identity transform.
 // We don't want any transform on our image except rescaling which is not
 // specified by a transform but by the input/output spacing as we will see
 // later.
 // So no transform will be specified.
 typedef itk::IdentityTransform<double, 2>
              T_Transform;

 // If ITK resampler determines there is something to interpolate which is
 // usually the case when upscaling (!) then we must specify the interpolation
 // algorithm. In our case, we want bicubic interpolation. One way to implement
 // it is with a third order b-spline. So the type is specified here and the
 // order will be specified with a method call later on.
 typedef itk::BSplineInterpolateImageFunction<T_Image, double, double>
              T_Interpolator;

 // The resampler type itself.
 typedef itk::ResampleImageFilter<T_Image, T_Image>
              T_ResampleFilter;

 // Prepare the reader and update it right away to know the sizes beforehand.
 T_Reader::Pointer pReader = T_Reader::New();
 pReader->SetFileName( argv[1] );
 pReader->Update();

 // Prepare the resampler.

 // Instantiate the transform and specify it should be the id transform.
 T_Transform::Pointer _pTransform = T_Transform::New();
 _pTransform->SetIdentity();

 // Instantiate the b-spline interpolator and set it as the third order
 // for bicubic.
 T_Interpolator::Pointer _pInterpolator = T_Interpolator::New();
 _pInterpolator->SetSplineOrder(3);

 // Instantiate the resampler. Wire in the transform and the interpolator.
 T_ResampleFilter::Pointer _pResizeFilter = T_ResampleFilter::New();
 _pResizeFilter->SetTransform(_pTransform);
 _pResizeFilter->SetInterpolator(_pInterpolator);

 // Set the output origin. You may shift the original image "inside" the
 // new image size by specifying something else than 0.0, 0.0 here.
 const double vfOutputOrigin[2]  = { 0.0, 0.0 };
 _pResizeFilter->SetOutputOrigin(vfOutputOrigin);

 //     Compute and set the output spacing
 //     Compute the output spacing from input spacing and old and new sizes.
 //     
 //     The computation must be so that the following holds:
 //     
 //     new width         old x spacing
 //     ----------   =   ---------------
 //     old width         new x spacing
 //    
 //    
 //     new height         old y spacing
 //    ------------  =   ---------------
 //     old height         new y spacing
 //
 //     So either we specify new height and width and compute new spacings (as
 //     we do here) or we specify new spacing and compute new height and width
 //     and computations that follows need to be modified a little (as it is
 //     done at step 2 there:
 //       http://itk.org/Wiki/ITK/Examples/DICOM/ResampleDICOM)
 //
 unsigned int nNewWidth = atoi(argv[3]);
 unsigned int nNewHeight = atoi(argv[4]);

 // Fetch original image size.
 const T_Image::RegionType& inputRegion = 
                             pReader->GetOutput()->GetLargestPossibleRegion();
 const T_Image::SizeType& vnInputSize = inputRegion.GetSize();
 unsigned int nOldWidth = vnInputSize[0];
 unsigned int nOldHeight = vnInputSize[1];

 // Fetch original image spacing.
 const T_Image::SpacingType& vfInputSpacing = 
                                           pReader->GetOutput()->GetSpacing();
                                           // Will be {1.0, 1.0} in the usual
                                           // case.

 double vfOutputSpacing[2];
 vfOutputSpacing[0] = vfInputSpacing[0] * (double) nOldWidth / (double) nNewWidth;
 vfOutputSpacing[1] = vfInputSpacing[1] * (double) nOldHeight / (double) nNewHeight;

 // Set the output spacing. If you comment out the following line, the original
 // image will be simply put in the upper left corner of the new image without
 // any scaling.
 _pResizeFilter->SetOutputSpacing(vfOutputSpacing);

 // Set the output size as specified on the command line.
 itk::Size<2> vnOutputSize = { {nNewWidth, nNewHeight} };
 _pResizeFilter->SetSize(vnOutputSize);

 // Specify the input.
 _pResizeFilter->SetInput(pReader->GetOutput());

 // Write the result
 T_Writer::Pointer pWriter = T_Writer::New();
 pWriter->SetFileName(argv[2]);
 pWriter->SetInput(_pResizeFilter->GetOutput());
 pWriter->Update();

 return EXIT_SUCCESS;

} </source>

CMakeLists.txt

<syntaxhighlight lang="cmake"> cmake_minimum_required(VERSION 3.9.5)

project(Upsampling)

find_package(ITK REQUIRED) include(${ITK_USE_FILE}) if (ITKVtkGlue_LOADED)

 find_package(VTK REQUIRED)
 include(${VTK_USE_FILE})

endif()

add_executable(Upsampling MACOSX_BUNDLE Upsampling.cxx)

if( "${ITK_VERSION_MAJOR}" LESS 4 )

 target_link_libraries(Upsampling ITKReview ${ITK_LIBRARIES})

else( "${ITK_VERSION_MAJOR}" LESS 4 )

 target_link_libraries(Upsampling ${ITK_LIBRARIES})

endif( "${ITK_VERSION_MAJOR}" LESS 4 )

</syntaxhighlight>

Download and Build Upsampling

Click here to download Upsampling and its CMakeLists.txt file. Once the tarball Upsampling.tar has been downloaded and extracted,

cd Upsampling/build
  • If ITK is installed:
cmake ..
  • If ITK is not installed but compiled on your system, you will need to specify the path to your ITK build:
cmake -DITK_DIR:PATH=/home/me/itk_build ..

Build the project:

make

and run it:

./Upsampling

WINDOWS USERS PLEASE NOTE: Be sure to add the ITK bin directory to your path. This will resolve the ITK dll's at run time.

Building All of the Examples

Many of the examples in the ITK Wiki Examples Collection require VTK. You can build all of the the examples by following these instructions. If you are a new VTK user, you may want to try the Superbuild which will build a proper ITK and VTK.

ItkVtkGlue

ITK >= 4

For examples that use QuickView (which depends on VTK), you must have built ITK with Module_ITKVtkGlue=ON.

ITK < 4

Some of the ITK Examples require VTK to display the images. If you download the entire ITK Wiki Examples Collection, the ItkVtkGlue directory will be included and configured. If you wish to just build a few examples, then you will need to download ItkVtkGlue and build it. When you run cmake it will ask you to specify the location of the ItkVtkGlue binary directory.