ITK/Examples/ImageProcessing/ResampleSegmentedImage: Difference between revisions

From KitwarePublic
< ITK‎ | Examples
Jump to navigationJump to search
(New example)
 
No edit summary
Line 1: Line 1:
<div class="floatcenter">[[File:ITK_Examples_Baseline_ImageProcessing_TestResampleSegmentedImage.png]]</div>
==ResampleSegmentedImage.cxx==
==ResampleSegmentedImage.cxx==
<source lang="cpp">
<source lang="cpp">

Revision as of 14:02, 25 February 2015

ITK Examples Baseline ImageProcessing TestResampleSegmentedImage.png

ResampleSegmentedImage.cxx

<source lang="cpp">

  1. include "itkImage.h"
  2. include "itkImageFileReader.h"
  3. include "itkIdentityTransform.h"
  4. include "itkLabelImageGaussianInterpolateImageFunction.h"
  5. include "itkNearestNeighborInterpolateImageFunction.h"
  6. include "itkResampleImageFilter.h"
  1. include "itkCustomColormapFunction.h"
  2. include "itkScalarToRGBColormapImageFilter.h"
  3. include "itkRGBPixel.h"
  4. include "itkMersenneTwisterRandomVariateGenerator.h"
  1. include "QuickView.h"
  1. include <iostream>

typedef unsigned short InputPixelType; typedef itk::Image<InputPixelType, 2> ImageType; typedef itk::RGBPixel<unsigned char> RGBPixelType; typedef itk::Image<RGBPixelType, 2> RGBImageType;

typedef itk::Function::CustomColormapFunction<

 ImageType::PixelType, RGBImageType::PixelType> ColormapType;

static void CreateRandomColormap(unsigned int size, ColormapType::Pointer colormap);

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

 if( argc != 3 )
   { 
   std::cerr << "Usage: " << argv[0]
             << " inputImageFile pixelSize"
             << std::endl;
   
   return EXIT_FAILURE;
   }
 double spacing = atof(argv[2]);

 typedef itk::ImageFileReader<ImageType> ReaderType;

 // 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>
   TransformType;

 typedef itk::LabelImageGaussianInterpolateImageFunction<ImageType, double>
   GaussianInterpolatorType;
 typedef itk::NearestNeighborInterpolateImageFunction<ImageType, double>
   NearestNeighborInterpolatorType;

 typedef itk::ResampleImageFilter<ImageType, ImageType>
   ResampleFilterType;
 // Prepare the reader and update it right away to know the sizes beforehand.
 ReaderType::Pointer reader =
   ReaderType::New();
 reader->SetFileName( argv[1] );
 reader->Update();

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

 // Instantiate the interpolators
 GaussianInterpolatorType::Pointer gaussianInterpolator =
   GaussianInterpolatorType::New();
 gaussianInterpolator->SetSigma(1.0);
 gaussianInterpolator->SetAlpha(3.0);
 NearestNeighborInterpolatorType::Pointer nearestNeighborInterpolator =
   NearestNeighborInterpolatorType::New();
 
 // Instantiate the resamplers. Wire in the transforms and the interpolators.
 ResampleFilterType::Pointer resizeFilter1 =
   ResampleFilterType::New();
 resizeFilter1->SetTransform(transform);
 resizeFilter1->SetInterpolator(gaussianInterpolator);
 ResampleFilterType::Pointer resizeFilter2 =
   ResampleFilterType::New();
 resizeFilter2->SetTransform(transform);
 resizeFilter2->SetInterpolator(nearestNeighborInterpolator);

 //     Compute and set the output size
 //     
 //     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
 //     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)
 //
 // Fetch original image spacing.
 const ImageType::SpacingType& inputSpacing = 
   reader->GetOutput()->GetSpacing();

 double outputSpacing[2];
 outputSpacing[0] = spacing;
 outputSpacing[1] = spacing;

 // Fetch original image size
 const ImageType::RegionType& inputRegion = 
   reader->GetOutput()->GetLargestPossibleRegion();
 const ImageType::SizeType& inputSize = inputRegion.GetSize();
 unsigned int oldWidth = inputSize[0];
 unsigned int oldHeight = inputSize[1];
 unsigned int newWidth = (double) oldWidth * inputSpacing[0] / spacing;
 unsigned int newHeight = (double) oldHeight * inputSpacing[1] / spacing;
 // Set the output spacing as specified on the command line
 resizeFilter1->SetOutputSpacing(outputSpacing);
 resizeFilter2->SetOutputSpacing(outputSpacing);
 // Set the computed size
 itk::Size<2> outputSize = { {newWidth, newHeight} };
 resizeFilter1->SetSize(outputSize);
 resizeFilter2->SetSize(outputSize);

 // Specify the input for the resamplers
 resizeFilter1->SetInput(reader->GetOutput());
 resizeFilter2->SetInput(reader->GetOutput());

 // Display the images
 typedef itk::ScalarToRGBColormapImageFilter<ImageType, RGBImageType> ColormapFilterType;
 ColormapFilterType::Pointer colormapFilter1 =
   ColormapFilterType::New();
 ColormapType::Pointer colormap
   = ColormapType::New();
 CreateRandomColormap(4096, colormap);
 colormapFilter1->SetInput (reader->GetOutput());
 colormapFilter1->SetColormap(colormap);
 ColormapFilterType::Pointer colormapFilter2 =
  ColormapFilterType::New();
 colormapFilter2->SetInput (resizeFilter1->GetOutput());
 colormapFilter2->SetColormap(colormap);
 ColormapFilterType::Pointer colormapFilter3 =
  ColormapFilterType::New();
 colormapFilter3->SetInput (resizeFilter2->GetOutput());
 colormapFilter3->SetColormap(colormap);
 QuickView viewer;
 std::stringstream desc;
 desc << itksys::SystemTools::GetFilenameName(argv[1]) << ": " << oldWidth << ", " << oldHeight;
 viewer.AddRGBImage(
   colormapFilter1->GetOutput(),
   true,
   desc.str());  
 std::stringstream desc2;
 desc2 << "Gaussian Interpolation: " << newWidth << ", " << newHeight;
 viewer.AddRGBImage(
   colormapFilter2->GetOutput(),
   true,
   desc2.str());
 std::stringstream desc3;
 desc3 << "Nearest Neighbor Interpolation: " << newWidth << ", " << newHeight;
 viewer.AddRGBImage(
   colormapFilter3->GetOutput(),
   true,
   desc3.str());
 viewer.Visualize();
 return EXIT_SUCCESS;

}

void CreateRandomColormap(unsigned int size, ColormapType::Pointer colormap) {

  1. define LOW .3
 ColormapType::ChannelType redChannel;
 ColormapType::ChannelType greenChannel;
 ColormapType::ChannelType blueChannel;
 itk::Statistics::MersenneTwisterRandomVariateGenerator::Pointer random =
   itk::Statistics::MersenneTwisterRandomVariateGenerator::New();
 random->SetSeed ( 8775070 );
 redChannel.push_back(LOW);
 greenChannel.push_back(LOW);
 blueChannel.push_back(LOW);
 for (unsigned int i = 1; i < size; ++i)
   {
   redChannel.push_back(static_cast<ColormapType::RealType>
                        (random->GetUniformVariate(LOW, 1.0)));
   greenChannel.push_back(static_cast<ColormapType::RealType>
                          (random->GetUniformVariate(LOW, 1.0)));
   blueChannel.push_back(static_cast<ColormapType::RealType>
                         (random->GetUniformVariate(LOW, 1.0)));
   }
 colormap->SetRedChannel(redChannel);
 colormap->SetGreenChannel(greenChannel);
 colormap->SetBlueChannel(blueChannel);

} </source>

CMakeLists.txt

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

project(ResampleSegmentedImage)

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

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

else()

 find_package(ItkVtkGlue REQUIRED)
 include(${ItkVtkGlue_USE_FILE})
 set(Glue ItkVtkGlue)

endif()

add_executable(ResampleSegmentedImage MACOSX_BUNDLE ResampleSegmentedImage.cxx) target_link_libraries(ResampleSegmentedImage

 ${Glue}  ${VTK_LIBRARIES} ${ITK_LIBRARIES})

</syntaxhighlight>

Download and Build ResampleSegmentedImage

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

cd ResampleSegmentedImage/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:

./ResampleSegmentedImage

WINDOWS USERS PLEASE NOTE: Be sure to add the VTK and ITK bin directories to your path. This will resolve the VTK and 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.