ITK/Examples/SpectralAnalysis/VnlFFTRealToComplexConjugateImageFilter: Difference between revisions

From KitwarePublic
< ITK‎ | Examples
Jump to navigationJump to search
No edit summary
Line 6: Line 6:
#include "itkComplexToRealImageFilter.h"
#include "itkComplexToRealImageFilter.h"
#include "itkComplexToImaginaryImageFilter.h"
#include "itkComplexToImaginaryImageFilter.h"
#include "itkComplexToModulusImageFilter.h"
#include "itkImageFileReader.h"
#include "itkImageFileReader.h"
#include "itkCastImageFilter.h"
#include "itkCastImageFilter.h"
#include "itkPasteImageFilter.h"


#include <itksys/SystemTools.hxx>
#include <itksys/SystemTools.hxx>
Line 15: Line 17:
#include <itkImageToVTKImageFilter.h>
#include <itkImageToVTKImageFilter.h>


#include "vtkImageViewer.h"
#include "QuickView.h"
#include "vtkRenderWindowInteractor.h"
#include "vtkSmartPointer.h"
#include "vtkImageActor.h"
#include "vtkInteractorStyleImage.h"
#include "vtkRenderer.h"


int main(int argc, char*argv[])
int main(int argc, char*argv[])
Line 29: Line 26:
     return EXIT_FAILURE;
     return EXIT_FAILURE;
     }
     }
   
 
   typedef itk::Image<unsigned char, 2> UnsignedCharImageType;
   typedef itk::Image<unsigned char, 2> UnsignedCharImageType;
   typedef itk::Image<float, 2> FloatImageType;
   typedef itk::Image<float, 2> FloatImageType;
 
   typedef itk::ImageFileReader<FloatImageType> ReaderType;
   typedef itk::ImageFileReader<FloatImageType> ReaderType;
   ReaderType::Pointer reader = ReaderType::New();
   ReaderType::Pointer reader = ReaderType::New();
   reader->SetFileName(argv[1]);
   reader->SetFileName(argv[1]);
  reader->Update();
  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;
  // The SetDestinationIndex() method prescribes where in the first input to start pasting data from the second input.
  // The SetSourceRegion method prescribes the section of the second image to paste into the first.


  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());
 
  // Take the FFT
 
   typedef itk::VnlFFTRealToComplexConjugateImageFilter<FloatImageType::PixelType, 2> FFTType;
   typedef itk::VnlFFTRealToComplexConjugateImageFilter<FloatImageType::PixelType, 2> FFTType;
   FFTType::Pointer fftFilter = FFTType::New();
   FFTType::Pointer fftFilter = FFTType::New();
   fftFilter->SetInput(reader->GetOutput());
   fftFilter->SetInput(image);
   fftFilter->Update();
   fftFilter->Update();
 
 
  // Extract the real part
   typedef itk::ComplexToRealImageFilter<FFTType::OutputImageType, UnsignedCharImageType> RealFilterType;
   typedef itk::ComplexToRealImageFilter<FFTType::OutputImageType, UnsignedCharImageType> RealFilterType;
   RealFilterType::Pointer realFilter = RealFilterType::New();
   RealFilterType::Pointer realFilter = RealFilterType::New();
Line 47: Line 92:
   realFilter->Update();
   realFilter->Update();


  typedef itk::RescaleIntensityImageFilter<UnsignedCharImageType, UnsignedCharImageType> RescaleFilterType;
   // Extract the real part
  RescaleFilterType::Pointer rescaleFilter = RescaleFilterType::New();
   typedef itk::ComplexToImaginaryImageFilter<FFTType::OutputImageType, UnsignedCharImageType> ImaginaryFilterType;
  rescaleFilter->SetInput(realFilter->GetOutput());
   ImaginaryFilterType::Pointer imaginaryFilter = ImaginaryFilterType::New();
  rescaleFilter->SetOutputMinimum(0);
   imaginaryFilter->SetInput(fftFilter->GetOutput());
  rescaleFilter->SetOutputMaximum(255);
   imaginaryFilter->Update();
  rescaleFilter->Update();
 
   // Visualize original image
   typedef itk::CastImageFilter< FloatImageType, UnsignedCharImageType > CastFilterType;
  CastFilterType::Pointer inputCastFilter = CastFilterType::New();
  inputCastFilter->SetInput(reader->GetOutput());
 
  typedef itk::ImageToVTKImageFilter<UnsignedCharImageType> ConnectorType;
   ConnectorType::Pointer originalConnector = ConnectorType::New();
   originalConnector->SetInput(inputCastFilter->GetOutput());
 
  vtkSmartPointer<vtkImageActor> originalActor =
    vtkSmartPointer<vtkImageActor>::New();
   originalActor->SetInput(originalConnector->GetOutput());


   // Visualize FFT
   // Compute the magnitude
   typedef itk::ImageToVTKImageFilter<UnsignedCharImageType> ConnectorType;
   typedef itk::ComplexToModulusImageFilter<FFTType::OutputImageType, UnsignedCharImageType> ModulusFilterType;
   ConnectorType::Pointer fftConnector = ConnectorType::New();
   ModulusFilterType::Pointer modulusFilter = ModulusFilterType::New();
   fftConnector->SetInput(rescaleFilter->GetOutput());
   modulusFilter->SetInput(fftFilter->GetOutput());
  modulusFilter->Update();


   vtkSmartPointer<vtkImageActor> fftActor =
   QuickView viewer;
    vtkSmartPointer<vtkImageActor>::New();
  viewer.AddImage(image.GetPointer());
   fftActor->SetInput(fftConnector->GetOutput());
    
 
  viewer.AddImage(realFilter->GetOutput());
   // Define viewport ranges
   viewer.AddImage(imaginaryFilter->GetOutput());
  // (xmin, ymin, xmax, ymax)
   viewer.AddImage(modulusFilter->GetOutput());
   double leftViewport[4] = {0.0, 0.0, 0.5, 1.0};
   viewer.Visualize();
   double rightViewport[4] = {0.5, 0.0, 1.0, 1.0};


  // Setup both renderers
  vtkSmartPointer<vtkRenderWindow> renderWindow =
    vtkSmartPointer<vtkRenderWindow>::New();
  renderWindow->SetSize(600,300);
  vtkSmartPointer<vtkRenderer> leftRenderer =
    vtkSmartPointer<vtkRenderer>::New();
  renderWindow->AddRenderer(leftRenderer);
  leftRenderer->SetViewport(leftViewport);
  vtkSmartPointer<vtkRenderer> rightRenderer =
    vtkSmartPointer<vtkRenderer>::New();
  renderWindow->AddRenderer(rightRenderer);
  rightRenderer->SetViewport(rightViewport);
  leftRenderer->AddActor(originalActor);
  rightRenderer->AddActor(fftActor);
  vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor =
    vtkSmartPointer<vtkRenderWindowInteractor>::New();
  vtkSmartPointer<vtkInteractorStyleImage> style =
    vtkSmartPointer<vtkInteractorStyleImage>::New();
  renderWindowInteractor->SetInteractorStyle(style);
  renderWindowInteractor->SetRenderWindow(renderWindow);
  renderWindowInteractor->Initialize();
  renderWindowInteractor->Start();
 
   return EXIT_SUCCESS;
   return EXIT_SUCCESS;
}
}
Line 122: Line 123:


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


FIND_PACKAGE(VTK REQUIRED)
FIND_PACKAGE(VTK REQUIRED)
Line 129: Line 131:
INCLUDE(${ITK_USE_FILE})
INCLUDE(${ITK_USE_FILE})


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




</source>
</source>

Revision as of 19:31, 24 January 2011

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[]) {

 if(argc < 2)
   {
   std::cerr << "Required: filename" << std::endl;
   return EXIT_FAILURE;
   }
 typedef itk::Image<unsigned char, 2> UnsignedCharImageType;
 typedef itk::Image<float, 2> FloatImageType;
 
 typedef itk::ImageFileReader<FloatImageType> ReaderType;
 ReaderType::Pointer reader = ReaderType::New();
 reader->SetFileName(argv[1]);
 reader->Update();
 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;
 // The SetDestinationIndex() method prescribes where in the first input to start pasting data from the second input.
 // The SetSourceRegion method prescribes the section of the second image to paste into the first.
 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());
 
 // Take 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, UnsignedCharImageType> RealFilterType;
 RealFilterType::Pointer realFilter = RealFilterType::New();
 realFilter->SetInput(fftFilter->GetOutput());
 realFilter->Update();
 // Extract the real part
 typedef itk::ComplexToImaginaryImageFilter<FFTType::OutputImageType, UnsignedCharImageType> ImaginaryFilterType;
 ImaginaryFilterType::Pointer imaginaryFilter = ImaginaryFilterType::New();
 imaginaryFilter->SetInput(fftFilter->GetOutput());
 imaginaryFilter->Update();
 // Compute the magnitude
 typedef itk::ComplexToModulusImageFilter<FFTType::OutputImageType, UnsignedCharImageType> ModulusFilterType;
 ModulusFilterType::Pointer modulusFilter = ModulusFilterType::New();
 modulusFilter->SetInput(fftFilter->GetOutput());
 modulusFilter->Update();
 QuickView viewer;
 viewer.AddImage(image.GetPointer());
 
 viewer.AddImage(realFilter->GetOutput());
 viewer.AddImage(imaginaryFilter->GetOutput());
 viewer.AddImage(modulusFilter->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>