ITK/Examples/Images/MaskedFFTNormalizedCorrelationImageFilter: Difference between revisions

From KitwarePublic
< ITK‎ | Examples
Jump to navigationJump to search
(Fix type of constants.)
No edit summary
Line 166: Line 166:
</source>
</source>


{{ITKVTKCMakeLists|MaskedFFTNormalizedCorrelationImageFilter|}}
{{ITKCMakeLists|{{SUBPAGENAME}}}}

Revision as of 15:20, 24 December 2012

MaskedFFTNormalizedCorrelationImageFilter.cxx

<source lang="cpp">

  1. include "itkImage.h"
  2. include "itkImageFileReader.h"
  3. include "itkFFTNormalizedCorrelationImageFilter.h"
  4. include "itkRegionOfInterestImageFilter.h"
  5. include "itkImageKernelOperator.h"
  6. include "itkRescaleIntensityImageFilter.h"
  7. include "itkImageFileWriter.h"
  8. include "itkMinimumMaximumImageCalculator.h"
  1. include <iostream>
  2. include <string>

typedef itk::Image<unsigned char, 2> ImageType; typedef itk::Image<float, 2> FloatImageType; typedef itk::Image<unsigned char, 2> MaskType;

static void CreateMask(MaskType* const mask);

static void CreateImage(ImageType::Pointer image, const itk::Index<2>& cornerOfSquare);

template <typename TImage> void WriteImage(TImage* const image, const std::string& filename);

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

 itk::Index<2> offset;
 offset[0] = 5;
 offset[1] = 6;
 // Setup mask
 MaskType::Pointer mask = MaskType::New();
 CreateMask(mask);
 
 ImageType::Pointer fixedImage = ImageType::New();
 itk::Index<2> cornerOfFixedSquare;
 cornerOfFixedSquare[0] = 3;
 cornerOfFixedSquare[1] = 8;
 CreateImage(fixedImage, cornerOfFixedSquare);
 WriteImage(fixedImage.GetPointer(), "fixedImage.png");
 ImageType::Pointer movingImage = ImageType::New();
 itk::Index<2> cornerOfMovingSquare;
 cornerOfMovingSquare[0] = cornerOfFixedSquare[0] + offset[0];
 cornerOfMovingSquare[1] = cornerOfFixedSquare[1] + offset[1];
 CreateImage(movingImage, cornerOfMovingSquare);
 WriteImage(movingImage.GetPointer(), "movingImage.png");
 // Perform normalized correlation
 typedef itk::FFTNormalizedCorrelationImageFilter<ImageType, FloatImageType> CorrelationFilterType;
 CorrelationFilterType::Pointer correlationFilter = CorrelationFilterType::New();
 correlationFilter->SetFixedImage(fixedImage);
 correlationFilter->SetMovingImage(movingImage);
 correlationFilter->SetMovingImageMask(mask);
 //correlationFilter->SetFixedImageMask(mask);
 correlationFilter->Update();
 WriteImage(correlationFilter->GetOutput(), "correlation.mha");
 typedef itk::RescaleIntensityImageFilter<FloatImageType, ImageType> RescaleFilterType;
 RescaleFilterType::Pointer rescaleFilter = RescaleFilterType::New();
 rescaleFilter->SetInput(correlationFilter->GetOutput());
 rescaleFilter->SetOutputMinimum(0);
 rescaleFilter->SetOutputMaximum(255);
 rescaleFilter->Update();
 WriteImage(rescaleFilter->GetOutput(), "correlation.png");
 typedef itk::MinimumMaximumImageCalculator<FloatImageType> MinimumMaximumImageCalculatorType;
 MinimumMaximumImageCalculatorType::Pointer minimumMaximumImageCalculatorFilter = MinimumMaximumImageCalculatorType::New ();
 minimumMaximumImageCalculatorFilter->SetImage(correlationFilter->GetOutput());
 minimumMaximumImageCalculatorFilter->Compute();
 itk::Index<2> maximumCorrelationPatchCenter = minimumMaximumImageCalculatorFilter->GetIndexOfMaximum();
 itk::Size<2> outputSize = correlationFilter->GetOutput()->GetLargestPossibleRegion().GetSize();
 itk::Index<2> maximumCorrelationPatchCenterFixed;
 maximumCorrelationPatchCenterFixed[0] = outputSize[0]/2 - maximumCorrelationPatchCenter[0];
 maximumCorrelationPatchCenterFixed[1] = outputSize[1]/2 - maximumCorrelationPatchCenter[1];
 std::cout << "Maximum location: " << maximumCorrelationPatchCenter << std::endl;
 std::cout << "Maximum location fixed: " << maximumCorrelationPatchCenterFixed << std::endl; // This is the value we expect!
 std::cout << "Maximum value: " << minimumMaximumImageCalculatorFilter->GetMaximum() << std::endl; // If the images can be perfectly aligned, the value is 1
 return EXIT_SUCCESS;

}

void CreateImage(ImageType::Pointer image, const itk::Index<2>& cornerOfSquare) {

 ImageType::IndexType start;
 start.Fill(0);
 ImageType::SizeType size;
 size.Fill(51);
 ImageType::RegionType region(start,size);
 image->SetRegions(region);
 image->Allocate();
 image->FillBuffer(0);
 itk::ImageRegionIterator<ImageType> imageIterator(image,region);
 ImageType::IndexValueType squareSize = 8;
 while(!imageIterator.IsAtEnd())
   {
   if(imageIterator.GetIndex()[0] > cornerOfSquare[0] && imageIterator.GetIndex()[0] < cornerOfSquare[0] + squareSize &&
     imageIterator.GetIndex()[1] > cornerOfSquare[1] && imageIterator.GetIndex()[1] < cornerOfSquare[1] + squareSize)
     {
     imageIterator.Set(255);
     }
   ++imageIterator;
   }

}

template <typename TImage> void WriteImage(TImage* const image, const std::string& filename) {

 typedef  itk::ImageFileWriter<TImage> WriterType;
 typename WriterType::Pointer writer = WriterType::New();
 writer->SetFileName(filename);
 writer->SetInput(image);
 writer->Update();

}


void CreateMask(MaskType* const mask) {

 ImageType::IndexType start;
 start.Fill(0);
 ImageType::SizeType size;
 size.Fill(51);
 ImageType::RegionType region(start,size);
 mask->SetRegions(region);
 mask->Allocate();
 mask->FillBuffer(255); // Make the whole mask "valid"
 //unsigned int squareSize = 8;
 ImageType::IndexValueType squareSize = 3;
 itk::Index<2> cornerOfSquare = Template:3,8;
 // Remove pixels from the mask in a small square. The correlationw will not be computed at these pixels.
 itk::ImageRegionIterator<MaskType> maskIterator(mask, region);
 while(!maskIterator.IsAtEnd())
   {
   if(maskIterator.GetIndex()[0] > cornerOfSquare[0] &&
      maskIterator.GetIndex()[0] < cornerOfSquare[0] + squareSize &&
      maskIterator.GetIndex()[1] > cornerOfSquare[1] &&
      maskIterator.GetIndex()[1] < cornerOfSquare[1] + squareSize)
     {
     maskIterator.Set(0);
     }
   ++maskIterator;
   }

}

</source>

CMakeLists.txt

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

project(MaskedFFTNormalizedCorrelationImageFilter)

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

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

endif()

add_executable(MaskedFFTNormalizedCorrelationImageFilter MACOSX_BUNDLE MaskedFFTNormalizedCorrelationImageFilter.cxx)

if( "${ITK_VERSION_MAJOR}" LESS 4 )

 target_link_libraries(MaskedFFTNormalizedCorrelationImageFilter ITKReview ${ITK_LIBRARIES})

else( "${ITK_VERSION_MAJOR}" LESS 4 )

 target_link_libraries(MaskedFFTNormalizedCorrelationImageFilter ${ITK_LIBRARIES})

endif( "${ITK_VERSION_MAJOR}" LESS 4 )

</syntaxhighlight>

Download and Build MaskedFFTNormalizedCorrelationImageFilter

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

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

./MaskedFFTNormalizedCorrelationImageFilter

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.