ITK/Examples/Images/NormalizedCorrelationImageFilter

From KitwarePublic
< ITK‎ | Examples
Revision as of 16:09, 3 February 2011 by Daviddoria (talk | contribs)
Jump to navigationJump to search

Output neither the minimum nor maximum of the correlation image is the "correct" result (the pixel that we extracted the patch to correlate).

NormalizedCorrelationImageFilter.cxx

<source lang="cpp">

  1. include "itkImage.h"
  2. include "itkImageFileReader.h"
  3. include "itkNormalizedCorrelationImageFilter.h"
  4. include "itkRegionOfInterestImageFilter.h"
  5. include "itkImageKernelOperator.h"
  6. include "itkRescaleIntensityImageFilter.h"
  7. include "itkImageFileWriter.h"
  8. include "itkMinimumMaximumImageCalculator.h"
  9. include "QuickView.h"
  1. include <iostream>
  2. 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 size;
 size.Fill(100);
 FloatImageType::RegionType desiredRegion(start,size);
 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());
 kernelOperator.CreateToRadius(extractFilter->GetOutput()->GetLargestPossibleRegion().GetSize());
 
 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();
 
 std::cout << "Maximum: " << minimumMaximumImageCalculatorFilter->GetIndexOfMaximum() << std::endl;
 std::cout << "Minimum: " << minimumMaximumImageCalculatorFilter->GetIndexOfMinimum() << std::endl;
 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();
 }
 
 QuickView viewer;
 viewer.AddImage(reader->GetOutput());
 viewer.AddImage(extractFilter->GetOutput());
 viewer.AddImage(correlationFilter->GetOutput());
 viewer.Visualize();
 return EXIT_SUCCESS;

}

</source>

CMakeLists.txt

<source lang="cmake"> cmake_minimum_required(VERSION 2.6)

PROJECT(NormalizedCorrelationImageFilter)

include_directories(/home/doriad/ITKWikiExamples/ItkVtkGlue)

FIND_PACKAGE(VTK REQUIRED) INCLUDE(${VTK_USE_FILE})

FIND_PACKAGE(ITK REQUIRED) INCLUDE(${ITK_USE_FILE})

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

</source>