Absolute Value of Image#

Synopsis#

Compute the absolute value of an image.

Results#

../../../../_images/AbsValueOfImage.png

Output In VTK Window#

Code#

C++#

#include "itkImage.h"
#include "itkImageFileWriter.h"
#include "itkRescaleIntensityImageFilter.h"
#include "itkAbsImageFilter.h"

#ifdef ENABLE_QUICKVIEW
#  include "QuickView.h"
#endif

using UnsignedCharImageType = itk::Image<unsigned char, 2>;
using FloatImageType = itk::Image<float, 2>;

static void
CreateImage(FloatImageType::Pointer image);

int
main()
{
  auto image = FloatImageType::New();
  CreateImage(image);

  // Take the absolute value of the image
  using AbsImageFilterType = itk::AbsImageFilter<FloatImageType, FloatImageType>;

  auto absFilter = AbsImageFilterType::New();
  absFilter->SetInput(image);

#ifdef ENABLE_QUICKVIEW
  QuickView viewer;
  viewer.AddImage<FloatImageType>(image);
  viewer.AddImage<FloatImageType>(absFilter->GetOutput());
  viewer.Visualize();
#endif
  return EXIT_SUCCESS;
}

void
CreateImage(FloatImageType::Pointer image)
{
  // Create an image with negative values
  FloatImageType::RegionType region;
  FloatImageType::IndexType  start;
  start[0] = 0;
  start[1] = 0;

  FloatImageType::SizeType size;
  size[0] = 200;
  size[1] = 300;

  region.SetSize(size);
  region.SetIndex(start);

  image->SetRegions(region);
  image->Allocate();

  itk::ImageRegionIterator<FloatImageType> imageIterator(image, region);

  while (!imageIterator.IsAtEnd())
  {
    imageIterator.Set(imageIterator.GetIndex()[0] - imageIterator.GetIndex()[1]);
    ++imageIterator;
  }
}

Classes demonstrated#

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

Computes the absolute value of each pixel.

itk::Math::abs() is used to perform the computation.

ITK Sphinx Examples:

See itk::AbsImageFilter for additional documentation.