ITK Examples Baseline Images TestNormalizedCorrelationImageFilter.png

This example extracts a patch from an image and searches for it using normalized correlation. The output of the correlation filter is a correlation map. The maximum of this map is the "best" position of the query patch. We know the correct location because we extracted the patch from the image, so it will match exactly.

Contents

NormalizedCorrelationImageFilter.cxx

#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkNormalizedCorrelationImageFilter.h"
#include "itkRegionOfInterestImageFilter.h"
#include "itkImageKernelOperator.h"
#include "itkRescaleIntensityImageFilter.h"
#include "itkImageFileWriter.h"
#include "itkMinimumMaximumImageCalculator.h"
#include "QuickView.h"
 
#include <iostream>
#include <string>
 
typedef itk::Image<float, 2> FloatImageType;
typedef itk::Image<unsigned char, 2> UnsignedCharImageType;
 
int main(int argc, char *argv[])
{
  if(argc < 2)
    {
    std::cerr << "Required: filename" << std::endl;
    return EXIT_FAILURE;
    }
 
  std::string filename = argv[1];
 
  typedef itk::ImageFileReader<FloatImageType> ReaderType;
 
  // Read the image
  ReaderType::Pointer reader = ReaderType::New();
  reader->SetFileName(filename.c_str());
  reader->Update();
 
  // Extract a small region
  typedef itk::RegionOfInterestImageFilter< FloatImageType,
                                            FloatImageType > ExtractFilterType;
 
  ExtractFilterType::Pointer extractFilter = ExtractFilterType::New();
 
  FloatImageType::IndexType start;
  start.Fill(50);
 
  FloatImageType::SizeType patchSize;
  patchSize.Fill(51);
 
  FloatImageType::RegionType desiredRegion(start,patchSize);
 
  extractFilter->SetRegionOfInterest(desiredRegion);
  extractFilter->SetInput(reader->GetOutput());
  extractFilter->Update();
 
  // Perform normalized correlation
  // <input type, mask type (not used), output type>
  typedef itk::NormalizedCorrelationImageFilter<FloatImageType, FloatImageType, FloatImageType> CorrelationFilterType;
 
  itk::ImageKernelOperator<float> kernelOperator;
  kernelOperator.SetImageKernel(extractFilter->GetOutput());
 
  // The radius of the kernel must be the radius of the patch, NOT the size of the patch
  itk::Size<2> radius = extractFilter->GetOutput()->GetLargestPossibleRegion().GetSize();
  radius[0] = (radius[0]-1) / 2;
  radius[1] = (radius[1]-1) / 2;
 
  kernelOperator.CreateToRadius(radius);
 
  CorrelationFilterType::Pointer correlationFilter = CorrelationFilterType::New();
  correlationFilter->SetInput(reader->GetOutput());
  correlationFilter->SetTemplate(kernelOperator);
  correlationFilter->Update();
 
  typedef itk::MinimumMaximumImageCalculator <FloatImageType>
          MinimumMaximumImageCalculatorType;
 
  MinimumMaximumImageCalculatorType::Pointer minimumMaximumImageCalculatorFilter
          = MinimumMaximumImageCalculatorType::New ();
  minimumMaximumImageCalculatorFilter->SetImage(correlationFilter->GetOutput());
  minimumMaximumImageCalculatorFilter->Compute();
 
  itk::Index<2> maximumCorrelationPatchCenter = minimumMaximumImageCalculatorFilter->GetIndexOfMaximum();
  std::cout << "Maximum: " << maximumCorrelationPatchCenter << std::endl;
 
  // Note that the best correlation is at the center of the patch we extracted (ie. (75, 75) rather than the corner (50,50)
 
  typedef itk::RescaleIntensityImageFilter< FloatImageType, UnsignedCharImageType > RescaleFilterType;
  typedef itk::ImageFileWriter<UnsignedCharImageType> WriterType;
  {
  RescaleFilterType::Pointer rescaleFilter = RescaleFilterType::New();
  rescaleFilter->SetInput(correlationFilter->GetOutput());
  rescaleFilter->SetOutputMinimum(0);
  rescaleFilter->SetOutputMaximum(255);
  rescaleFilter->Update();
 
  WriterType::Pointer writer = WriterType::New();
  writer->SetInput(rescaleFilter->GetOutput());
  writer->SetFileName("correlation.png");
  writer->Update();
  }
 
  {
  RescaleFilterType::Pointer rescaleFilter = RescaleFilterType::New();
  rescaleFilter->SetInput(extractFilter->GetOutput());
  rescaleFilter->SetOutputMinimum(0);
  rescaleFilter->SetOutputMaximum(255);
  rescaleFilter->Update();
 
  WriterType::Pointer writer = WriterType::New();
  writer->SetInput(rescaleFilter->GetOutput());
  writer->SetFileName("patch.png");
  writer->Update();
  }
 
  // Extract the best matching patch
  FloatImageType::IndexType bestPatchStart;
  bestPatchStart[0] = maximumCorrelationPatchCenter[0] - radius[0];
  bestPatchStart[1] = maximumCorrelationPatchCenter[1] - radius[1];
 
  FloatImageType::RegionType bestPatchRegion(bestPatchStart,patchSize);
 
  ExtractFilterType::Pointer bestPatchExtractFilter = ExtractFilterType::New();
  bestPatchExtractFilter->SetRegionOfInterest(bestPatchRegion);
  bestPatchExtractFilter->SetInput(reader->GetOutput());
  bestPatchExtractFilter->Update();
 
  QuickView viewer;
  viewer.AddImage(reader->GetOutput());
  viewer.AddImage(extractFilter->GetOutput());
  viewer.AddImage(correlationFilter->GetOutput());
  viewer.AddImage(bestPatchExtractFilter->GetOutput());
  viewer.Visualize();
 
  return EXIT_SUCCESS;
}

CMakeLists.txt

cmake_minimum_required(VERSION 2.8)
 
project(NormalizedCorrelationImageFilter)
 
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(NormalizedCorrelationImageFilter MACOSX_BUNDLE NormalizedCorrelationImageFilter.cxx)
target_link_libraries(NormalizedCorrelationImageFilter
  ${Glue}  ${VTK_LIBRARIES} ${ITK_LIBRARIES})

Download and Build NormalizedCorrelationImageFilter

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

cd NormalizedCorrelationImageFilter/build 
cmake ..
cmake -DITK_DIR:PATH=/home/me/itk_build ..

Build the project,

make

and run it:

./NormalizedCorrelationImageFilter

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.