ITK/Examples/Broken/Images/MeanSquaresImageToImageMetric: Difference between revisions

From KitwarePublic
< ITK‎ | Examples
Jump to navigationJump to search
mNo edit summary
No edit summary
Line 194: Line 194:
</source>
</source>


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


PROJECT(MeanSquaresImageToImageMetric)
{{ITKVTKCMakeLists|MeanSquaresImageToImageMetric|}}
 
include_directories(/home/doriad/src/ITK/Wrapping/WrapITK/ExternalProjects/ItkVtkGlue/src/)
#include_directories(${ITK_SOURCE_DIR}/Wrapping/WrapITK/ExternalProjects/ItkVtkGlue/src/)
 
FIND_PACKAGE(VTK REQUIRED)
INCLUDE(${VTK_USE_FILE})
 
FIND_PACKAGE(ITK REQUIRED)
INCLUDE(${ITK_USE_FILE})
 
ADD_EXECUTABLE(MeanSquaresImageToImageMetric MeanSquaresImageToImageMetric.cxx)
TARGET_LINK_LIBRARIES(MeanSquaresImageToImageMetric
vtkHybrid
ITKBasicFilters ITKCommon ITKIO ITKNumerics)
 
</source>

Revision as of 23:21, 2 March 2011

This example extracts a patch from the input image and then attempts to find the best position of the patch in the input image (this process is clearly for demonstration only - we know the "ground truth" so we can compare it to the output).

The current output is (0,0) - this is incorrect. We expect either (50,50) (the bottom left corner of the extracted patch) or (75,75) (the center of the extracted patch).

MeanSquaresImageToImageMetric.cxx

<source lang="cpp">

  1. include <itkImage.h>
  2. include <itkTranslationTransform.h>
  3. include <itkImageFileReader.h>
  4. include <itkImageRegistrationMethod.h>
  5. include <itkLinearInterpolateImageFunction.h>
  6. include <itkRegularStepGradientDescentOptimizer.h>
  7. include <itkMeanSquaresImageToImageMetric.h>
  8. include <itkRegionOfInterestImageFilter.h>
  9. include <itkCastImageFilter.h>
  10. include <itkRescaleIntensityImageFilter.h>
  11. include <itkImageRegionIterator.h>
  1. include <itkImageToVTKImageFilter.h>
  1. include "vtkImageViewer.h"
  2. include "itkImageFileWriter.h"
  3. include "vtkRenderWindowInteractor.h"
  4. include "vtkSmartPointer.h"
  5. include "vtkImageActor.h"
  6. include "vtkInteractorStyleImage.h"
  7. include "vtkRenderer.h"
  8. include "vtkSphereSource.h"
  9. include "vtkPolyDataMapper.h"
  10. include "vtkActor.h"
  11. include "vtkProperty.h"
  1. include <iostream>
  2. include <string>

typedef itk::Image<float, 2> FloatImageType;

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::ImageFileReader<FloatImageType> ReaderType;
 typedef itk::ImageToVTKImageFilter<UnsignedCharImageType> ConnectorType;
 // Read the image
 ReaderType::Pointer reader = ReaderType::New();
 reader->SetFileName(filename.c_str());
 reader->Update();
 // 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();
 FloatImageType::IndexType start;
 start[0] = 50;
 start[1] = 50;
 FloatImageType::SizeType size;
 size[0] = 100;
 size[1] = 100;
 FloatImageType::RegionType desiredRegion;
 desiredRegion.SetSize(size);
 desiredRegion.SetIndex(start);
 extractFilter->SetRegionOfInterest(desiredRegion);
 extractFilter->SetInput(reader->GetOutput());
 extractFilter->Update();
 // 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 registration
 typedef itk::MeanSquaresImageToImageMetric<FloatImageType, FloatImageType> MetricType; // <TFixedImage, TMovingImage>
 typedef itk::TranslationTransform<double, 2> TransformType; // Compiler error if you change double to float??!
 typedef itk::RegularStepGradientDescentOptimizer OptimizerType;
 typedef itk::LinearInterpolateImageFunction<FloatImageType, double> InterpolatorType; // Compiler error if you change double to float??!
 typedef itk::ImageRegistrationMethod<FloatImageType, FloatImageType> RegistrationType;
 MetricType::Pointer metric = MetricType::New();
 TransformType::Pointer transform = TransformType::New();
 OptimizerType::Pointer optimizer = OptimizerType::New();
 InterpolatorType::Pointer interpolator = InterpolatorType::New();
 
 RegistrationType::Pointer registration = RegistrationType::New();
 registration->SetMetric(metric);
 registration->SetOptimizer(optimizer);
 registration->SetInterpolator(interpolator);
 registration->SetTransform(transform);
 registration->SetFixedImage(reader->GetOutput());
 registration->SetMovingImage(extractFilter->GetOutput());
 registration->SetFixedImageRegion(reader->GetOutput()->GetLargestPossibleRegion());
 RegistrationType::ParametersType initialParameters(transform->GetNumberOfParameters());
 initialParameters[0] = 0;
 initialParameters[1] = 0;
 registration->SetInitialTransformParameters(initialParameters);
 registration->Update();
 RegistrationType::ParametersType finalParameters = registration->GetLastTransformParameters();
 std::cout << "Final parameters: " << finalParameters << std::endl;
 vtkSmartPointer<vtkSphereSource> sphereSource =
   vtkSmartPointer<vtkSphereSource>::New();
 sphereSource->SetCenter(finalParameters[0], finalParameters[1], 0);
 sphereSource->SetRadius(10);
 sphereSource->Update();
 vtkSmartPointer<vtkPolyDataMapper> mapper =
   vtkSmartPointer<vtkPolyDataMapper>::New();
 mapper->SetInputConnection(sphereSource->GetOutputPort());
 vtkSmartPointer<vtkActor> sphereActor =
   vtkSmartPointer<vtkActor>::New();
 sphereActor->SetMapper(mapper);
 sphereActor->GetProperty()->SetColor(1,0,0);
 // 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.5, 1.0};
 double rightViewport[4] = {0.5, 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> rightRenderer =
   vtkSmartPointer<vtkRenderer>::New();
 renderWindow->AddRenderer(rightRenderer);
 rightRenderer->SetViewport(rightViewport);
 rightRenderer->SetBackground(.4, .5, .6);
 leftRenderer->AddActor(originalActor);
 leftRenderer->AddActor(sphereActor); // Display the location of the best position of the moving image in the fixed image
 
 rightRenderer->AddActor(extractedActor);
 
 leftRenderer->ResetCamera();
 rightRenderer->ResetCamera();
 renderWindow->Render();
 vtkSmartPointer<vtkInteractorStyleImage> style =
   vtkSmartPointer<vtkInteractorStyleImage>::New();
 interactor->SetInteractorStyle(style);
 interactor->Start();
 return EXIT_SUCCESS;

}

</source>


CMakeLists.txt

<syntaxhighlight lang="cmake"> cmake_minimum_required(VERSION 3.9.5)

project(MeanSquaresImageToImageMetric)

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(MeanSquaresImageToImageMetric MACOSX_BUNDLE MeanSquaresImageToImageMetric.cxx) target_link_libraries(MeanSquaresImageToImageMetric

 ${Glue}  ${VTK_LIBRARIES} ${ITK_LIBRARIES})

</syntaxhighlight>

Download and Build MeanSquaresImageToImageMetric

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

cd MeanSquaresImageToImageMetric/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:

./MeanSquaresImageToImageMetric

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.