ITK/Examples/SpectralAnalysis/VnlFFTRealToComplexConjugateImageFilter

From KitwarePublic
< ITK‎ | Examples
Revision as of 19:39, 24 January 2011 by Daviddoria (talk | contribs)
Jump to navigationJump to search

VnlFFTRealToComplexConjugateImageFilter.cxx

<source lang="cpp">

  1. include "itkImage.h"
  2. include "itkRescaleIntensityImageFilter.h"
  3. include "itkVnlFFTRealToComplexConjugateImageFilter.h"
  4. include "itkComplexToRealImageFilter.h"
  5. include "itkComplexToImaginaryImageFilter.h"
  6. include "itkComplexToModulusImageFilter.h"
  7. include "itkImageFileReader.h"
  8. include "itkCastImageFilter.h"
  9. include "itkPasteImageFilter.h"
  1. include <itksys/SystemTools.hxx>
  2. include "vnl/vnl_sample.h"
  3. include <math.h>
  1. include <itkImageToVTKImageFilter.h>
  1. include "QuickView.h"

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

 // Verify input
 if(argc < 2)
   {
   std::cerr << "Required: filename" << std::endl;
   return EXIT_FAILURE;
   }
 // Define some types
 typedef itk::Image<unsigned char, 2> UnsignedCharImageType;
 typedef itk::Image<float, 2> FloatImageType;
 // Read the image
 typedef itk::ImageFileReader<FloatImageType> ReaderType;
 ReaderType::Pointer reader = ReaderType::New();
 reader->SetFileName(argv[1]);
 reader->Update();
 // Compute the smallest power of two that is bigger than the image
 unsigned int powerOfTwo = 0;
 for(unsigned int i = 0; i < 10; i++)
   {
   if(pow(2, i) > reader->GetOutput()->GetLargestPossibleRegion().GetSize()[0] &&
      pow(2, i) > reader->GetOutput()->GetLargestPossibleRegion().GetSize()[1])
     {
     powerOfTwo = i;
     break;
     }
   }
 // Create an image bigger than the input image and that has dimensions which are powers of two
 itk::Index<2> start;
 start.Fill(0);
 itk::Size<2> size;
 size.Fill(pow(2,powerOfTwo));
 itk::ImageRegion<2> region(start, size);
 
 FloatImageType::Pointer image = FloatImageType::New();
 image->SetRegions(region);
 image->Allocate();
 
 // The image dimensions must be powers of two
 typedef itk::PasteImageFilter <FloatImageType, FloatImageType >
   PasteImageFilterType;
 itk::Index<2> destinationIndex;
 destinationIndex.Fill(0);
 
 PasteImageFilterType::Pointer pasteFilter
   = PasteImageFilterType::New ();
 pasteFilter->SetSourceImage(reader->GetOutput());
 pasteFilter->SetDestinationImage(image);
 pasteFilter->SetSourceRegion(reader->GetOutput()->GetLargestPossibleRegion());
 pasteFilter->SetDestinationIndex(destinationIndex);
 pasteFilter->Update();
 
 image->Graft(pasteFilter->GetOutput());
 
 // Compute the FFT
 
 typedef itk::VnlFFTRealToComplexConjugateImageFilter<FloatImageType::PixelType, 2> FFTType;
 FFTType::Pointer fftFilter = FFTType::New();
 fftFilter->SetInput(image);
 fftFilter->Update();
 
 // Extract the real part
 typedef itk::ComplexToRealImageFilter<FFTType::OutputImageType, FloatImageType> RealFilterType;
 RealFilterType::Pointer realFilter = RealFilterType::New();
 realFilter->SetInput(fftFilter->GetOutput());
 realFilter->Update();
 typedef itk::RescaleIntensityImageFilter< FloatImageType, UnsignedCharImageType > RescaleFilterType;
 RescaleFilterType::Pointer realRescaleFilter = RescaleFilterType::New();
 realRescaleFilter->SetInput(realFilter->GetOutput());
 realRescaleFilter->SetOutputMinimum(0);
 realRescaleFilter->SetOutputMaximum(255);
 realRescaleFilter->Update();
 // Extract the real part
 typedef itk::ComplexToImaginaryImageFilter<FFTType::OutputImageType, FloatImageType> ImaginaryFilterType;
 ImaginaryFilterType::Pointer imaginaryFilter = ImaginaryFilterType::New();
 imaginaryFilter->SetInput(fftFilter->GetOutput());
 imaginaryFilter->Update();
 RescaleFilterType::Pointer imaginaryRescaleFilter = RescaleFilterType::New();
 imaginaryRescaleFilter->SetInput(imaginaryFilter->GetOutput());
 imaginaryRescaleFilter->SetOutputMinimum(0);
 imaginaryRescaleFilter->SetOutputMaximum(255);
 imaginaryRescaleFilter->Update();
 // Compute the magnitude
 typedef itk::ComplexToModulusImageFilter<FFTType::OutputImageType, FloatImageType> ModulusFilterType;
 ModulusFilterType::Pointer modulusFilter = ModulusFilterType::New();
 modulusFilter->SetInput(fftFilter->GetOutput());
 modulusFilter->Update();
 RescaleFilterType::Pointer magnitudeRescaleFilter = RescaleFilterType::New();
 magnitudeRescaleFilter->SetInput(modulusFilter->GetOutput());
 magnitudeRescaleFilter->SetOutputMinimum(0);
 magnitudeRescaleFilter->SetOutputMaximum(255);
 magnitudeRescaleFilter->Update();
 QuickView viewer;
 viewer.AddImage(image.GetPointer());
 viewer.AddImage(realRescaleFilter->GetOutput());
 viewer.AddImage(imaginaryRescaleFilter->GetOutput());
 viewer.AddImage(magnitudeRescaleFilter->GetOutput());
 viewer.Visualize();
 return EXIT_SUCCESS;

} </source>

CMakeLists.txt

<source lang="cmake"> cmake_minimum_required(VERSION 2.6)

PROJECT(VnlFFTRealToComplexConjugateImageFilter)

include_directories(/home/doriad/src/ITK/Wrapping/WrapITK/ExternalProjects/ItkVtkGlue/src/) include_directories(/home/doriad/ITKWikiExamples/ItkVtkGlue)

FIND_PACKAGE(VTK REQUIRED) INCLUDE(${VTK_USE_FILE})

FIND_PACKAGE(ITK REQUIRED) INCLUDE(${ITK_USE_FILE})

ADD_EXECUTABLE(VnlFFTRealToComplexConjugateImageFilter VnlFFTRealToComplexConjugateImageFilter.cxx /home/doriad/ITKWikiExamples/ItkVtkGlue/QuickView.cxx) TARGET_LINK_LIBRARIES(VnlFFTRealToComplexConjugateImageFilter vtkHybrid ITKNumerics ITKBasicFilters ITKCommon ITKIO)


</source>