ITK/Examples/Images/NormalizedCorrelationImageFilter: Difference between revisions
Daviddoria (talk | contribs) No edit summary |
Daviddoria (talk | contribs) No edit summary |
||
Line 1: | Line 1: | ||
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. | |||
==NormalizedCorrelationImageFilter.cxx== | ==NormalizedCorrelationImageFilter.cxx== | ||
Line 26: | Line 26: | ||
return EXIT_FAILURE; | return EXIT_FAILURE; | ||
} | } | ||
std::string filename = argv[1]; | std::string filename = argv[1]; | ||
typedef itk::ImageFileReader<FloatImageType> ReaderType; | typedef itk::ImageFileReader<FloatImageType> ReaderType; | ||
Line 35: | Line 35: | ||
reader->SetFileName(filename.c_str()); | reader->SetFileName(filename.c_str()); | ||
reader->Update(); | reader->Update(); | ||
// Extract a small region | // Extract a small region | ||
typedef itk::RegionOfInterestImageFilter< FloatImageType, | typedef itk::RegionOfInterestImageFilter< FloatImageType, | ||
Line 46: | Line 46: | ||
FloatImageType::SizeType size; | FloatImageType::SizeType size; | ||
size.Fill( | size.Fill(51); | ||
FloatImageType::RegionType desiredRegion(start,size); | FloatImageType::RegionType desiredRegion(start,size); | ||
Line 53: | Line 53: | ||
extractFilter->SetInput(reader->GetOutput()); | extractFilter->SetInput(reader->GetOutput()); | ||
extractFilter->Update(); | extractFilter->Update(); | ||
// Perform normalized correlation | // Perform normalized correlation | ||
// <input type, mask type (not used), output type> | // <input type, mask type (not used), output type> | ||
Line 60: | Line 60: | ||
itk::ImageKernelOperator<float> kernelOperator; | itk::ImageKernelOperator<float> kernelOperator; | ||
kernelOperator.SetImageKernel(extractFilter->GetOutput()); | 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(); | CorrelationFilterType::Pointer correlationFilter = CorrelationFilterType::New(); | ||
correlationFilter->SetInput(reader->GetOutput()); | correlationFilter->SetInput(reader->GetOutput()); | ||
Line 74: | Line 80: | ||
minimumMaximumImageCalculatorFilter->SetImage(correlationFilter->GetOutput()); | minimumMaximumImageCalculatorFilter->SetImage(correlationFilter->GetOutput()); | ||
minimumMaximumImageCalculatorFilter->Compute(); | minimumMaximumImageCalculatorFilter->Compute(); | ||
std::cout << "Maximum: " << minimumMaximumImageCalculatorFilter->GetIndexOfMaximum() << std::endl; | std::cout << "Maximum: " << minimumMaximumImageCalculatorFilter->GetIndexOfMaximum() << 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::RescaleIntensityImageFilter< FloatImageType, UnsignedCharImageType > RescaleFilterType; | ||
Line 86: | Line 93: | ||
rescaleFilter->SetOutputMaximum(255); | rescaleFilter->SetOutputMaximum(255); | ||
rescaleFilter->Update(); | rescaleFilter->Update(); | ||
WriterType::Pointer writer = WriterType::New(); | WriterType::Pointer writer = WriterType::New(); | ||
writer->SetInput(rescaleFilter->GetOutput()); | writer->SetInput(rescaleFilter->GetOutput()); | ||
Line 99: | Line 106: | ||
rescaleFilter->SetOutputMaximum(255); | rescaleFilter->SetOutputMaximum(255); | ||
rescaleFilter->Update(); | rescaleFilter->Update(); | ||
WriterType::Pointer writer = WriterType::New(); | WriterType::Pointer writer = WriterType::New(); | ||
writer->SetInput(rescaleFilter->GetOutput()); | writer->SetInput(rescaleFilter->GetOutput()); | ||
Line 105: | Line 112: | ||
writer->Update(); | writer->Update(); | ||
} | } | ||
QuickView viewer; | QuickView viewer; | ||
viewer.AddImage(reader->GetOutput()); | viewer.AddImage(reader->GetOutput()); |
Revision as of 01:13, 4 February 2011
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.
NormalizedCorrelationImageFilter.cxx
<source lang="cpp">
- 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 size; size.Fill(51);
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());
// 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();
std::cout << "Maximum: " << minimumMaximumImageCalculatorFilter->GetIndexOfMaximum() << 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(); }
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>