Multiply Image by Scalar#

Synopsis#

Multiply one image by a given scalar value.

Results#

Input image

Input image#

Output image

Output image#

Code#

C++#

#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkMultiplyImageFilter.h"

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

  const char *           inputFileName = argv[1];
  const double           factor = std::stod(argv[2]);
  const char *           outputFileName = argv[3];
  constexpr unsigned int Dimension = 2;

  using InputPixelType = unsigned char;
  using InputImageType = itk::Image<InputPixelType, Dimension>;

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

  using OutputPixelType = double;
  using OutputImageType = itk::Image<OutputPixelType, Dimension>;

  using FilterType = itk::MultiplyImageFilter<InputImageType, InputImageType, OutputImageType>;
  auto filter = FilterType::New();
  filter->SetInput(input);
  filter->SetConstant(factor);

  try
  {
    itk::WriteImage(filter->GetOutput(), outputFileName);
  }
  catch (const itk::ExceptionObject & error)
  {
    std::cerr << "Error: " << error << std::endl;
    return EXIT_FAILURE;
  }

  return EXIT_SUCCESS;
}

Classes demonstrated#

template<typename TInputImage1, typename TInputImage2 = TInputImage1, typename TOutputImage = TInputImage1>
class MultiplyImageFilter : public itk::BinaryGeneratorImageFilter<TInputImage1, TInputImage2, TOutputImage>

Pixel-wise multiplication of two images.

This class is templated over the types of the two input images and the type of the output image. Numeric conversions (castings) are done by the C++ defaults.

ITK Sphinx Examples:

See itk::MultiplyImageFilter for additional documentation.