Computer Magnitude in Vector Image to Make Magnitude Image#

Synopsis#

Compute the magnitude of each pixel in a vector image to produce a magnitude image

Results#

Yinyang.png

Yinyang.png#

output.png

output.png#

Code#

C++#

#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkRescaleIntensityImageFilter.h"
#include "itkVectorImage.h"
#include "itkVectorMagnitudeImageFilter.h"

int
main(int argc, char * argv[])
{
  // Verify command line arguments
  if (argc < 3)
  {
    std::cerr << "Usage: " << std::endl;
    std::cerr << argv[0] << " inputImageFile outputImageFile" << std::endl;
    return EXIT_FAILURE;
  }

  // Parse command line arguments
  std::string inputFileName = argv[1];
  std::string outputFileName = argv[2];

  // Setup types
  using VectorImageType = itk::VectorImage<float, 2>;
  using UnsignedCharImageType = itk::Image<unsigned char, 2>;

  const auto input = itk::ReadImage<VectorImageType>(inputFileName);

  using VectorMagnitudeFilterType = itk::VectorMagnitudeImageFilter<VectorImageType, UnsignedCharImageType>;
  auto magnitudeFilter = VectorMagnitudeFilterType::New();
  magnitudeFilter->SetInput(input);

  // To write the magnitude image file, we should rescale the gradient values
  // to a reasonable range
  using rescaleFilterType = itk::RescaleIntensityImageFilter<UnsignedCharImageType, UnsignedCharImageType>;

  auto rescaler = rescaleFilterType::New();
  rescaler->SetOutputMinimum(0);
  rescaler->SetOutputMaximum(255);
  rescaler->SetInput(magnitudeFilter->GetOutput());

  itk::WriteImage(rescaler->GetOutput(), outputFileName);

  return EXIT_SUCCESS;
}

Classes demonstrated#

template<typename TInputImage, typename TOutputImage>
class VectorMagnitudeImageFilter : public itk::UnaryGeneratorImageFilter<TInputImage, TOutputImage>

Take an image of vectors as input and produce an image with the magnitude of those vectors.

The filter expects the input image pixel type to be a vector and the output image pixel type to be a scalar.

This filter assumes that the PixelType of the input image is a VectorType that provides a GetNorm() method.

ITK Sphinx Examples:

See itk::VectorMagnitudeImageFilter for additional documentation.