ITK/Examples/Images/FFTNormalizedCorrelationImageFilter
FFTNormalizedCorrelationImageFilter.cxx
<source lang="cpp">
- include "itkImage.h"
- include "itkImageFileReader.h"
- include "itkFFTNormalizedCorrelationImageFilter.h"
- include "itkRegionOfInterestImageFilter.h"
- include "itkImageKernelOperator.h"
- include "itkRescaleIntensityImageFilter.h"
- include "itkImageFileWriter.h"
- include "itkMinimumMaximumImageCalculator.h"
- include <iostream>
- include <string>
typedef itk::Image<unsigned char, 2> ImageType; typedef itk::Image<float, 2> FloatImageType;
//typedef itk::Image<unsigned char, 2> UnsignedCharImageType;
static void CreateImage(ImageType::Pointer image, const itk::Index<2>& cornerOfSquare);
template <typename TImage> static 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;
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->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();
}
</source>
CMakeLists.txt
<syntaxhighlight lang="cmake"> cmake_minimum_required(VERSION 3.9.5)
project(FFTNormalizedCorrelationImageFilter)
find_package(ITK REQUIRED) include(${ITK_USE_FILE}) if (ITKVtkGlue_LOADED)
find_package(VTK REQUIRED) include(${VTK_USE_FILE})
endif()
add_executable(FFTNormalizedCorrelationImageFilter MACOSX_BUNDLE FFTNormalizedCorrelationImageFilter.cxx)
if( "${ITK_VERSION_MAJOR}" LESS 4 )
target_link_libraries(FFTNormalizedCorrelationImageFilter ITKReview ${ITK_LIBRARIES})
else( "${ITK_VERSION_MAJOR}" LESS 4 )
target_link_libraries(FFTNormalizedCorrelationImageFilter ${ITK_LIBRARIES})
endif( "${ITK_VERSION_MAJOR}" LESS 4 )
</syntaxhighlight>
Download and Build FFTNormalizedCorrelationImageFilter
Click here to download FFTNormalizedCorrelationImageFilter and its CMakeLists.txt file. Once the tarball FFTNormalizedCorrelationImageFilter.tar has been downloaded and extracted,
cd FFTNormalizedCorrelationImageFilter/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:
./FFTNormalizedCorrelationImageFilter
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.