Rescale an Image

Synopsis

Rescale a grayscale image

Results

Input image

Input image

Output image

Output image

Code

Python

#!/usr/bin/env python

import itk
import argparse

parser = argparse.ArgumentParser(description="Rescale Intensity.")
parser.add_argument("input_image")
parser.add_argument("output_image")
parser.add_argument("output_min", type=int)
parser.add_argument("output_max", type=int)
args = parser.parse_args()

image = itk.imread(args.input_image)

image = itk.rescale_intensity_image_filter(
    image, output_minimum=args.output_min, output_maximum=args.output_max
)

itk.imwrite(image, args.output_image)

C++

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

int
main(int argc, char * argv[])
{
  if (argc != 5)
  {
    std::cerr << "Usage: " << std::endl;
    std::cerr << argv[0];
    std::cerr << "<InputFileName> <OutputFileName> <OutputMin> <OutputMax>";
    std::cerr << std::endl;
    return EXIT_FAILURE;
  }

  constexpr unsigned int Dimension = 2;

  using PixelType = unsigned char;
  using ImageType = itk::Image<PixelType, Dimension>;

  using ReaderType = itk::ImageFileReader<ImageType>;
  ReaderType::Pointer reader = ReaderType::New();
  reader->SetFileName(argv[1]);

  using FilterType = itk::RescaleIntensityImageFilter<ImageType, ImageType>;
  FilterType::Pointer filter = FilterType::New();
  filter->SetInput(reader->GetOutput());
  filter->SetOutputMinimum(std::stoi(argv[3]));
  filter->SetOutputMaximum(std::stoi(argv[4]));

  using WriterType = itk::ImageFileWriter<ImageType>;
  WriterType::Pointer writer = WriterType::New();
  writer->SetFileName(argv[2]);
  writer->SetInput(filter->GetOutput());

  try
  {
    writer->Update();
  }
  catch (itk::ExceptionObject & error)
  {
    std::cerr << "Error: " << error << std::endl;
    return EXIT_FAILURE;
  }

  return EXIT_SUCCESS;
}

Classes demonstrated

template<typename TInputImage, typename TOutputImage = TInputImage>
class RescaleIntensityImageFilter : public itk::UnaryFunctorImageFilter<TInputImage, TOutputImage, Functor::IntensityLinearTransform<TInputImage::PixelType, TOutputImage::PixelType>>

Applies a linear transformation to the intensity levels of the input Image.

RescaleIntensityImageFilter applies pixel-wise a linear transformation to the intensity values of input image pixels. The linear transformation is defined by the user in terms of the minimum and maximum values that the output image should have.

The following equation gives the mapping of the intensity values

All computations are performed in the precision of the input pixel’s RealType. Before assigning the computed value to the output pixel.

outputPixel = ( inputPixel - inputMin) \cdot \frac{(outputMax - outputMin )}{(inputMax - inputMin)} + outputMin

NOTE: In this filter the minimum and maximum values of the input image are computed internally using the MinimumMaximumImageCalculator. Users are not supposed to set those values in this filter. If you need a filter where you can set the minimum and maximum values of the input, please use the IntensityWindowingImageFilter. If you want a filter that can use a user-defined linear transformation for the intensity, then please use the ShiftScaleImageFilter.

See

IntensityWindowingImageFilter

ITK Sphinx Examples:

See itk::RescaleIntensityImageFilter for additional documentation.