ITK/Examples/Broken/Images/NormalizedCorrelationImageFilterMasked

From KitwarePublic
< ITK‎ | Examples
Revision as of 16:12, 24 December 2012 by Lorensen (talk | contribs)
Jump to navigationJump to search

I believe this is basically correct. Perhaps a better/more intuitive example could be used.

NormalizedCorrelationImageFilterMasked.cxx

<source lang="cpp"> // Maximum value is inf??

  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"
  1. include <iostream>
  2. include <string>

typedef itk::Image<unsigned char, 2> ImageType; typedef itk::Image<unsigned char, 2> MaskType; typedef itk::Image<float, 2> FloatImageType;

void CreateMask(MaskType* const mask); void CreateImage(ImageType* const image); void CreateImageOfSquare(ImageType* const image, const itk::Index<2>& cornerOfSquare);

template <typename TImage> void WriteImage(const TImage* const image, const std::string& filename);

int main(int argc, char *argv[]) {

 // Setup mask
 MaskType::Pointer mask = MaskType::New();
 CreateMask(mask);
 WriteImage(mask.GetPointer(), "mask.png");
 // Setup image1
 ImageType::Pointer image1 = ImageType::New();
 itk::Index<2> cornerOfSquare1;
 cornerOfSquare1[0] = 3;
 cornerOfSquare1[1] = 8;
 CreateImageOfSquare(image1, cornerOfSquare1);
 WriteImage(image1.GetPointer(), "image1.png");
 // Setup image2
 itk::Index<2> offset;
 offset[0] = 20;
 offset[1] = 6;
 ImageType::Pointer image2 = ImageType::New();
 itk::Index<2> cornerOfSquare2;
 cornerOfSquare2[0] = cornerOfSquare1[0] + offset[0];
 cornerOfSquare2[1] = cornerOfSquare1[1] + offset[1];
 CreateImageOfSquare(image2, cornerOfSquare2);
 
 WriteImage(image2.GetPointer(), "image2.png");
 // Create a kernel from an image
 itk::ImageKernelOperator<unsigned char> kernelOperator;
 kernelOperator.SetImageKernel(image1);
 // The radius of the kernel must be the radius of the patch, NOT the size of the patch
 itk::Size<2> radius;
 radius[0] = image1->GetLargestPossibleRegion().GetSize()[0]/2;
 radius[1] = image1->GetLargestPossibleRegion().GetSize()[1]/2;
 if(radius[0] % 2 == 0 || radius[1] % 2 == 0)
   {
   std::cerr << "Input must have odd dimensions!" << std::endl;
   return EXIT_FAILURE;
   }
 kernelOperator.CreateToRadius(radius);
 // Perform normalized correlation
 // <input type, mask type, output type>
 typedef itk::NormalizedCorrelationImageFilter<ImageType, MaskType, FloatImageType, unsigned char> CorrelationFilterType;
 CorrelationFilterType::Pointer correlationFilter = CorrelationFilterType::New();
 correlationFilter->SetInput(image2);
 correlationFilter->SetMaskImage(mask);
 correlationFilter->SetTemplate(kernelOperator);
 correlationFilter->Update();
 WriteImage(correlationFilter->GetOutput(), "correlation.mha");
 typedef itk::RescaleIntensityImageFilter<FloatImageType, ImageType> RescaleFilterType;
 RescaleFilterType::Pointer rescaleFilter = RescaleFilterType::New();
 rescaleFilter->SetInput(correlationFilter->GetOutput());
 rescaleFilter->SetOutputMinimum(0);
 rescaleFilter->SetOutputMaximum(255);
 rescaleFilter->Update();
 WriteImage(rescaleFilter->GetOutput(), "correlation.png");
 
 typedef itk::MinimumMaximumImageCalculator<FloatImageType> MinimumMaximumImageCalculatorType;
 MinimumMaximumImageCalculatorType::Pointer minimumMaximumImageCalculatorFilter = MinimumMaximumImageCalculatorType::New ();
 minimumMaximumImageCalculatorFilter->SetImage(correlationFilter->GetOutput());
 minimumMaximumImageCalculatorFilter->Compute();
 itk::Index<2> maximumCorrelationPatchCenter = minimumMaximumImageCalculatorFilter->GetIndexOfMaximum();
 // This is the value we expect!
 std::cout << "Maximum location fixed: " << maximumCorrelationPatchCenter - radius << std::endl;
 // If the images can be perfectly aligned, the value is 1
 std::cout << "Maximum value: " << minimumMaximumImageCalculatorFilter->GetMaximum() << std::endl;
 return EXIT_SUCCESS;

}

void CreateMask(MaskType* const mask) {

 ImageType::IndexType start;
 start.Fill(0);
 ImageType::SizeType size;
 size.Fill(51);
 ImageType::RegionType region(start,size);
 mask->SetRegions(region);
 mask->Allocate();
 mask->FillBuffer(255); // Make the whole mask "valid"
 //unsigned int squareSize = 8;
 unsigned int squareSize = 3;
 itk::Index<2> cornerOfSquare = Template:3,8;
 // Remove pixels from the mask in a small square. The correlationw will not be computed at these pixels.
 itk::ImageRegionIterator<MaskType> maskIterator(mask, region);
 while(!maskIterator.IsAtEnd())
   {
   if(maskIterator.GetIndex()[0] > cornerOfSquare[0] &&
      maskIterator.GetIndex()[0] < cornerOfSquare[0] + squareSize &&
      maskIterator.GetIndex()[1] > cornerOfSquare[1] &&
      maskIterator.GetIndex()[1] < cornerOfSquare[1] + squareSize)
     {
     maskIterator.Set(0);
     }
   ++maskIterator;
   }

}

void CreateImage(ImageType* const image) {

 ImageType::IndexType start;
 start.Fill(0);
 ImageType::SizeType size;
 size.Fill(51);
 ImageType::RegionType region(start,size);
 image->SetRegions(region);
 image->Allocate();
 image->FillBuffer(0);

}

void CreateImageOfSquare(ImageType* const image, const itk::Index<2>& cornerOfSquare) {

 ImageType::IndexType start;
 start.Fill(0);
 ImageType::SizeType size;
 size.Fill(51);
 ImageType::RegionType region(start,size);
 image->SetRegions(region);
 image->Allocate();
 image->FillBuffer(0);
 itk::ImageRegionIterator<ImageType> imageIterator(image,region);
 unsigned int squareSize = 8;
 while(!imageIterator.IsAtEnd())
   {
   if(imageIterator.GetIndex()[0] > cornerOfSquare[0] &&
      imageIterator.GetIndex()[0] < cornerOfSquare[0] + squareSize &&
      imageIterator.GetIndex()[1] > cornerOfSquare[1] &&
      imageIterator.GetIndex()[1] < cornerOfSquare[1] + squareSize)
     {
     imageIterator.Set(255);
     }
   ++imageIterator;
   }

}

template <typename TImage> void WriteImage(const TImage* const image, const std::string& filename) {

 typedef  itk::ImageFileWriter<TImage> WriterType;
 typename WriterType::Pointer writer = WriterType::New();
 writer->SetFileName(filename);
 writer->SetInput(image);
 writer->Update();

}

</source>

CMakeLists.txt

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

project(NormalizedCorrelationImageFilterMasked)

find_package(ITK REQUIRED) include(${ITK_USE_FILE}) if (ITKVtkGlue_LOADED)

 find_package(VTK REQUIRED)
 include(${VTK_USE_FILE})

endif()

add_executable(NormalizedCorrelationImageFilterMasked MACOSX_BUNDLE NormalizedCorrelationImageFilterMasked.cxx)

if( "${ITK_VERSION_MAJOR}" LESS 4 )

 target_link_libraries(NormalizedCorrelationImageFilterMasked ITKReview ${ITK_LIBRARIES})

else( "${ITK_VERSION_MAJOR}" LESS 4 )

 target_link_libraries(NormalizedCorrelationImageFilterMasked ${ITK_LIBRARIES})

endif( "${ITK_VERSION_MAJOR}" LESS 4 )

</syntaxhighlight>

Download and Build NormalizedCorrelationImageFilterMasked

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

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

./NormalizedCorrelationImageFilterMasked

WINDOWS USERS PLEASE NOTE: Be sure to add the ITK bin directory to your path. This will resolve the 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.