Compute Sigmoid#

Synopsis#

Computes the sigmoid function pixel-wise.

Results#

Input image

Input image#

Output image

Output image#

Code#

Python#

#!/usr/bin/env python

import itk
import argparse

parser = argparse.ArgumentParser(description="Compute Sigmoid.")
parser.add_argument("input_image")
parser.add_argument("output_image")
parser.add_argument("output_min", type=int)
parser.add_argument("output_max", type=int)
parser.add_argument("alpha", type=float)
parser.add_argument("beta", type=float)
args = parser.parse_args()

PixelType = itk.UC
Dimension = 2

ImageType = itk.Image[PixelType, Dimension]

reader = itk.ImageFileReader[ImageType].New()
reader.SetFileName(args.input_image)

sigmoidFilter = itk.SigmoidImageFilter[ImageType, ImageType].New()
sigmoidFilter.SetInput(reader.GetOutput())
sigmoidFilter.SetOutputMinimum(args.output_min)
sigmoidFilter.SetOutputMaximum(args.output_max)
sigmoidFilter.SetAlpha(args.alpha)
sigmoidFilter.SetBeta(args.beta)

writer = itk.ImageFileWriter[ImageType].New()
writer.SetFileName(args.output_image)
writer.SetInput(sigmoidFilter.GetOutput())

writer.Update()

C++#

#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkSigmoidImageFilter.h"

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

  const char * inputFileName = argv[1];
  const char * outputFileName = argv[2];

  constexpr unsigned int Dimension = 2;

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

  const auto       outputMinimum = static_cast<PixelType>(atoi(argv[3]));
  const auto       outputMaximum = static_cast<PixelType>(atoi(argv[4]));
  const ScalarType alpha = std::stod(argv[5]);
  const ScalarType beta = std::stod(argv[6]);

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

  using FilterType = itk::SigmoidImageFilter<ImageType, ImageType>;
  auto sigmoidFilter = FilterType::New();
  sigmoidFilter->SetInput(input);
  sigmoidFilter->SetOutputMinimum(outputMinimum);
  sigmoidFilter->SetOutputMaximum(outputMaximum);
  sigmoidFilter->SetAlpha(alpha);
  sigmoidFilter->SetBeta(beta);

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

  return EXIT_SUCCESS;
}

Classes demonstrated#

template<typename TInputImage, typename TOutputImage>
class SigmoidImageFilter : public itk::UnaryFunctorImageFilter<TInputImage, TOutputImage, Functor::Sigmoid<TInputImage::PixelType, TOutputImage::PixelType>>

Computes the sigmoid function pixel-wise.

A linear transformation is applied first on the argument of the sigmoid function. The resulting total transform is given by

f(x) = (Max-Min) \cdot \frac{1}{\left(1+e^{- \frac{ x - \beta }{\alpha}}\right)} + Min

Every output pixel is equal to f(x). Where x is the intensity of the homologous input pixel, and alpha and beta are user-provided constants.

ITK Sphinx Examples:

See itk::SigmoidImageFilter for additional documentation.