Cast Image To Another Type But Clamp To Output Range

Synopsis

Cast an image from one type to another but clamp to the output value range.

Results

Code

C++

#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkClampImageFilter.h"

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

static void
CreateImage(FloatImageType * const image);

int
main(int argc, char * argv[])
{
  FloatImageType::Pointer image;

  // No input image argument provided
  if (argc < 2)
  {
    image = FloatImageType::New();
    CreateImage(image);
  }
  else // Input image argument provided
  {
    using ReaderType = itk::ImageFileReader<FloatImageType>;
    ReaderType::Pointer reader = ReaderType::New();
    reader->SetFileName(argv[1]);
    reader->Update();
    image = reader->GetOutput();
  }

  using ClampFilterType = itk::ClampImageFilter<FloatImageType, UnsignedCharImageType>;
  ClampFilterType::Pointer clampFilter = ClampFilterType::New();
  clampFilter->SetInput(image);
  clampFilter->Update();

  return EXIT_SUCCESS;
}

void
CreateImage(FloatImageType * const image)
{
  // Create an image with 2 connected components
  FloatImageType::IndexType corner = { { 0, 0 } };

  FloatImageType::SizeType size;
  unsigned int             NumRows = 200;
  unsigned int             NumCols = 300;
  size[0] = NumRows;
  size[1] = NumCols;

  FloatImageType::RegionType region(corner, size);

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

  // Make a square
  for (unsigned int r = 40; r < 100; r++)
  {
    for (unsigned int c = 40; c < 100; c++)
    {
      FloatImageType::IndexType pixelIndex;
      pixelIndex[0] = r;
      pixelIndex[1] = c;

      image->SetPixel(pixelIndex, 15);
    }
  }
}

Classes demonstrated