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

From KitwarePublic
< ITK‎ | Examples
Jump to navigationJump to search
No edit summary
No edit summary
Line 1: Line 1:
I believe this is basically correct. Perhaps a better/more intuitive example could be used.
==NormalizedCorrelationImageFilterMasked.cxx==
==NormalizedCorrelationImageFilterMasked.cxx==
<source lang="cpp">
<source lang="cpp">
Line 25: Line 27:
template <typename TImage>
template <typename TImage>
void WriteImage(const TImage* const image, const std::string& filename);
void WriteImage(const TImage* const image, const std::string& filename);
template <typename TImage>
void FillRegion(TImage* const image, const itk::ImageRegion<2>& region,
                const typename TImage::PixelType& value);


int main(int argc, char *argv[])
int main(int argc, char *argv[])
Line 36: Line 34:
   CreateMask(mask);
   CreateMask(mask);


  itk::Index<2> maskIndex;
   WriteImage(mask.GetPointer(), "mask.png");
  maskIndex[0] = mask->GetLargestPossibleRegion().GetSize()[0]/2;
  maskIndex[1] = 0;
 
  itk::Size<2> maskSize;
  maskSize[0] = mask->GetLargestPossibleRegion().GetSize()[0]/2 + 1;
  maskSize[1] = mask->GetLargestPossibleRegion().GetSize()[1];
 
  itk::ImageRegion<2> maskRegion(maskIndex, maskSize);
  FillRegion(mask.GetPointer(), maskRegion, 255);
   WriteImage(mask.GetPointer(), "mask.mha");


   // Setup image1
   // Setup image1
Line 54: Line 42:
   cornerOfSquare1[1] = 8;
   cornerOfSquare1[1] = 8;
   CreateImageOfSquare(image1, cornerOfSquare1);
   CreateImageOfSquare(image1, cornerOfSquare1);
   WriteImage(image1.GetPointer(), "image1.mha");
   WriteImage(image1.GetPointer(), "image1.png");


   // Setup image2
   // Setup image2
Line 66: Line 54:
   cornerOfSquare2[1] = cornerOfSquare1[1] + offset[1];
   cornerOfSquare2[1] = cornerOfSquare1[1] + offset[1];
   CreateImageOfSquare(image2, cornerOfSquare2);
   CreateImageOfSquare(image2, cornerOfSquare2);
   FillRegion(image2.GetPointer(), maskRegion, 122); // If this line is uncommented, the result is infinity!?
    
   WriteImage(image2.GetPointer(), "image2.mha");
   WriteImage(image2.GetPointer(), "image2.png");


   // Create a kernel from an image
   // Create a kernel from an image
Line 133: Line 121:
   mask->SetRegions(region);
   mask->SetRegions(region);
   mask->Allocate();
   mask->Allocate();
   mask->FillBuffer(0);
   mask->FillBuffer(255); // Make the whole mask "valid"
 
  itk::ImageRegionIterator<MaskType> maskIterator(mask, region);


   //unsigned int squareSize = 8;
   //unsigned int squareSize = 8;
Line 141: Line 127:


   itk::Index<2> cornerOfSquare = {{3,8}};
   itk::Index<2> cornerOfSquare = {{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())
   while(!maskIterator.IsAtEnd())
Line 149: Line 138:
       maskIterator.GetIndex()[1] < cornerOfSquare[1] + squareSize)
       maskIterator.GetIndex()[1] < cornerOfSquare[1] + squareSize)
       {
       {
       maskIterator.Set(255);
       maskIterator.Set(0);
       }
       }


Line 211: Line 200:
   writer->SetInput(image);
   writer->SetInput(image);
   writer->Update();
   writer->Update();
}
template <typename TImage>
void FillRegion(TImage* const image, const itk::ImageRegion<2>& region,
                const typename TImage::PixelType& value)
{
  itk::ImageRegionIterator<TImage> imageIterator(image, region);
  while(!imageIterator.IsAtEnd())
    {
    imageIterator.Set(value);
    ++imageIterator;
    }


}
}

Revision as of 17:57, 26 June 2012

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})

else()

 find_package(ItkVtkGlue REQUIRED)
 include(${ItkVtkGlue_USE_FILE})
 set(Glue ItkVtkGlue)

endif()

add_executable(NormalizedCorrelationImageFilterMasked MACOSX_BUNDLE NormalizedCorrelationImageFilterMasked.cxx) target_link_libraries(NormalizedCorrelationImageFilterMasked

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

</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 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.