ITK/Examples/SpectralAnalysis/InverseFFTImageFilter

From KitwarePublic
< ITK‎ | Examples
Revision as of 13:00, 19 October 2012 by Daviddoria (talk | contribs) (Correct loop counter type.)
Jump to navigationJump to search

InverseFFTImageFilter.cxx

<source lang="cpp">

  1. include "itkImage.h"
  2. include "itkForwardFFTImageFilter.h"
  3. include "itkInverseFFTImageFilter.h"
  4. include "itkRescaleIntensityImageFilter.h"
  5. include "itkImageFileReader.h"
  6. include "itkCastImageFilter.h"
  7. include "itkImageFileWriter.h"
  1. include "QuickView.h"

typedef itk::Image<float, 2> FloatImageType;

static void CreateImage(FloatImageType* const image);

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

 FloatImageType::Pointer image;
 // Verify input
 if(argc < 2)
   {
   image = FloatImageType::New();
   CreateImage(image);
   //std::cerr << "Required: filename" << std::endl;
   //return EXIT_FAILURE;
   }
 else
 {
   // Read the image
   typedef itk::ImageFileReader<FloatImageType> ReaderType;
   ReaderType::Pointer reader = ReaderType::New();
   reader->SetFileName(argv[1]);
   reader->Update();
   image = reader->GetOutput();
 }
 // Define some types
 typedef itk::Image<unsigned char, 2> UnsignedCharImageType;
 // Compute the FFT
 typedef itk::ForwardFFTImageFilter<FloatImageType> FFTType;
 FFTType::Pointer fftFilter = FFTType::New();
 fftFilter->SetInput(image);
 fftFilter->Update();
 // Compute the IFFT
 //typedef itk::InverseFFTImageFilter<FFTType::OutputImageType, UnsignedCharImageType> IFFTType; // This does not work - output type seems to need to be float, but it is just an error, not a concept check error...
 typedef itk::InverseFFTImageFilter<FFTType::OutputImageType, FloatImageType> IFFTType;
 IFFTType::Pointer ifftFilter = IFFTType::New();
 ifftFilter->SetInput(fftFilter->GetOutput());
 ifftFilter->Update();

// QuickView viewer; // viewer.AddImage(image.GetPointer()); // viewer.AddImage(ifftFilter->GetOutput()); // viewer.Visualize();

 typedef itk::CastImageFilter< FloatImageType, UnsignedCharImageType > CastFilterType;
 CastFilterType::Pointer castFilter = CastFilterType::New();
 castFilter->SetInput(ifftFilter->GetOutput());
 castFilter->Update();
 
 typedef itk::ImageFileWriter<UnsignedCharImageType> WriterType;
 WriterType::Pointer writer = WriterType::New();
 writer->SetFileName("ifft.png");
 writer->SetInput(castFilter->GetOutput());
 writer->Update();
 
 return EXIT_SUCCESS;

}

void CreateImage(FloatImageType* const image) {

 itk::Index<2> corner = Template:0,0;
 itk::Size<2> size = Template:200,200;
 itk::ImageRegion<2> region(corner, size);
 image->SetRegions(region);
 image->Allocate();
 // Make a square
 for(FloatImageType::IndexValueType r = 40; r < 100; r++)
   {
   for(FloatImageType::IndexValueType c = 40; c < 100; c++)
     {
     FloatImageType::IndexType pixelIndex = Template:R,c;
     image->SetPixel(pixelIndex, 100);
     }
   }

}

</source>

CMakeLists.txt

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

project(InverseFFTImageFilter)

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(InverseFFTImageFilter MACOSX_BUNDLE InverseFFTImageFilter.cxx) target_link_libraries(InverseFFTImageFilter

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

</syntaxhighlight>

Download and Build InverseFFTImageFilter

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

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

./InverseFFTImageFilter

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.