ITK/Examples/Images/NormalizedCorrelationImageFilter

From KitwarePublic
Jump to navigationJump to search

Output is all black?

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 <itkCastImageFilter.h>
  7. include <itkRescaleIntensityImageFilter.h>
  1. include <itkImageToVTKImageFilter.h>
  1. include "vtkImageViewer.h"
  2. include "vtkRenderWindowInteractor.h"
  3. include "vtkSmartPointer.h"
  4. include "vtkImageActor.h"
  5. include "vtkInteractorStyleImage.h"
  6. include "vtkRenderer.h"
  1. include <iostream>
  2. include <string>

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::Image<unsigned char, 2> UnsignedCharImageType;
 typedef itk::Image<float, 2> FloatImageType;
 typedef itk::ImageFileReader<FloatImageType> ReaderType;
 typedef itk::ImageToVTKImageFilter<UnsignedCharImageType> ConnectorType;
 // Read the image
 ReaderType::Pointer reader = ReaderType::New();
 reader->SetFileName(filename.c_str());
 // Setup the original image actor
 typedef itk::CastImageFilter< FloatImageType,
                               UnsignedCharImageType > CastFilterType;
 CastFilterType::Pointer originalCastFilter = CastFilterType::New();
 originalCastFilter->SetInput(reader->GetOutput());
 
 ConnectorType::Pointer originalConnector = ConnectorType::New();
 originalConnector->SetInput(originalCastFilter->GetOutput());
 vtkSmartPointer<vtkImageActor> originalActor =
   vtkSmartPointer<vtkImageActor>::New();
 originalActor->SetInput(originalConnector->GetOutput());
 // Extract a small region
 typedef itk::RegionOfInterestImageFilter< FloatImageType,
                                           FloatImageType > ExtractFilterType;
 ExtractFilterType::Pointer extractFilter = ExtractFilterType::New();
 UnsignedCharImageType::IndexType start;
 start[0] = 50;
 start[1] = 50;
 UnsignedCharImageType::SizeType size;
 size[0] = 100;
 size[1] = 100;
 UnsignedCharImageType::RegionType desiredRegion;
 desiredRegion.SetSize(size);
 desiredRegion.SetIndex(start);
 extractFilter->SetRegionOfInterest(desiredRegion);
 extractFilter->SetInput(reader->GetOutput());
 // Display extracted region (kernel)
 CastFilterType::Pointer kernelCastFilter = CastFilterType::New();
 kernelCastFilter->SetInput(extractFilter->GetOutput());
 
 ConnectorType::Pointer extractedConnector = ConnectorType::New();
 extractedConnector->SetInput(kernelCastFilter->GetOutput());
 vtkSmartPointer<vtkImageActor> extractedActor =
   vtkSmartPointer<vtkImageActor>::New();
 extractedActor->SetInput(extractedConnector->GetOutput());
 // 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());
 
 CorrelationFilterType::Pointer correlationFilter = CorrelationFilterType::New();
 correlationFilter->SetInput(reader->GetOutput());
 correlationFilter->SetTemplate(kernelOperator);
 // Display the correlation image
 typedef itk::RescaleIntensityImageFilter< FloatImageType, UnsignedCharImageType > RescaleFilterType;
 RescaleFilterType::Pointer rescaleFilter = RescaleFilterType::New();
 rescaleFilter->SetInput(correlationFilter->GetOutput());
 rescaleFilter->SetOutputMinimum(0);
 rescaleFilter->SetOutputMaximum(255);
 ConnectorType::Pointer correlationConnector = ConnectorType::New();
 correlationConnector->SetInput(rescaleFilter->GetOutput());
 vtkSmartPointer<vtkImageActor> correlationActor =
   vtkSmartPointer<vtkImageActor>::New();
 correlationActor->SetInput(correlationConnector->GetOutput());
 // Visualize
 vtkSmartPointer<vtkRenderWindow> renderWindow =
   vtkSmartPointer<vtkRenderWindow>::New();
 renderWindow->SetSize(900, 300);
 vtkSmartPointer<vtkRenderWindowInteractor> interactor =
   vtkSmartPointer<vtkRenderWindowInteractor>::New();
 interactor->SetRenderWindow(renderWindow);
 double leftViewport[4] = {0.0, 0.0, 0.33, 1.0};
 double centerViewport[4] = {0.33, 0.0, 0.66, 1.0};
 double rightViewport[4] = {0.66, 0.0, 1.0, 1.0};
 vtkSmartPointer<vtkRenderer> leftRenderer =
   vtkSmartPointer<vtkRenderer>::New();
 renderWindow->AddRenderer(leftRenderer);
 leftRenderer->SetViewport(leftViewport);
 leftRenderer->SetBackground(.6, .5, .4);
 vtkSmartPointer<vtkRenderer> centerRenderer =
   vtkSmartPointer<vtkRenderer>::New();
 renderWindow->AddRenderer(centerRenderer);
 centerRenderer->SetViewport(centerViewport);
 centerRenderer->SetBackground(.5, .5, .6);
 vtkSmartPointer<vtkRenderer> rightRenderer =
   vtkSmartPointer<vtkRenderer>::New();
 renderWindow->AddRenderer(rightRenderer);
 rightRenderer->SetViewport(rightViewport);
 rightRenderer->SetBackground(.4, .5, .6);
 leftRenderer->AddActor(originalActor);
 centerRenderer->AddActor(extractedActor);
 rightRenderer->AddActor(correlationActor);
 leftRenderer->ResetCamera();
 rightRenderer->ResetCamera();
 renderWindow->Render();
 vtkSmartPointer<vtkInteractorStyleImage> style =
   vtkSmartPointer<vtkInteractorStyleImage>::New();
 interactor->SetInteractorStyle(style);
 interactor->Start();
 return EXIT_SUCCESS;

} </source>

CMakeLists.txt

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

PROJECT(NormalizedCorrelationImageFilter)

include_directories(/home/doriad/src/ITK/Wrapping/WrapITK/ExternalProjects/ItkVtkGlue/src/)

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

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

ADD_EXECUTABLE(NormalizedCorrelationImageFilter NormalizedCorrelationImageFilter.cpp) TARGET_LINK_LIBRARIES(NormalizedCorrelationImageFilter vtkHybrid ITKBasicFilters ITKCommon ITKIO)

</source>