Compute Sigmoid

Synopsis

Computes the sigmoid function pixel-wise.

Results

Input image

Input image

Output image

Output image

Code

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]);

  using ReaderType = itk::ImageFileReader<ImageType>;
  ReaderType::Pointer reader = ReaderType::New();
  reader->SetFileName(inputFileName);

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

  using WriterType = itk::ImageFileWriter<ImageType>;
  WriterType::Pointer writer = WriterType::New();
  writer->SetFileName(outputFileName);
  writer->SetInput(sigmoidFilter->GetOutput());

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

  return EXIT_SUCCESS;
}

Python

#!/usr/bin/env python
import itk

if len(sys.argv) != 7:
    print("Usage: " + sys.argv[0] + " <inputImage> <outputImage>"
          "<OutputMin> <OutputMax> <Alpha> <Beta>")
    sys.exit(1)

inputImage = sys.argv[1]
outputImage = sys.argv[2]

PixelType = itk.UC
Dimension = 2

ImageType = itk.Image[PixelType, Dimension]

outputMinimum = int(sys.argv[3])
outputMaximum = int(sys.argv[4])
alpha = float(sys.argv[5])
beta = float(sys.argv[6])

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

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

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

writer.Update()

Classes demonstrated