Flip An Image Over Specified Axes

Synopsis

Flip an image over the specified axes. Pixels are swapped over the given axis.

Results

Input image

Input image

Output image

Output image

Code

C++

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

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

  constexpr unsigned 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 FlipImageFilterType = itk::FlipImageFilter<ImageType>;

  FlipImageFilterType::Pointer flipFilter = FlipImageFilterType::New();
  flipFilter->SetInput(reader->GetOutput());

  FlipImageFilterType::FlipAxesArrayType flipAxes;
  if (std::stoi(argv[3]) == 0)
  {
    flipAxes[0] = true;
    flipAxes[1] = false;
  }
  else
  {
    flipAxes[0] = false;
    flipAxes[1] = true;
  }

  flipFilter->SetFlipAxes(flipAxes);

  using WriterType = itk::ImageFileWriter<ImageType>;
  WriterType::Pointer writer = WriterType::New();
  writer->SetFileName(argv[2]);
  writer->SetInput(flipFilter->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) != 4:
    print("Usage: " + sys.argv[0] + " <InputImage> <OutputImage> <AxisToFlip>")
    sys.exit(1)

PixelType = itk.UC
Dimension = 2

ImageType = itk.Image[PixelType, Dimension]

reader = itk.ImageFileReader[ImageType].New()
reader.SetFileName(sys.argv[1])

flipFilter = itk.FlipImageFilter[ImageType].New()
flipFilter.SetInput(reader.GetOutput())

if int(sys.argv[3]) == 0:
    flipAxes = (True, False)
else:
    flipAxes = (False, True)

flipFilter.SetFlipAxes(flipAxes)

writer = itk.ImageFileWriter[ImageType].New()
writer.SetFileName(sys.argv[2])
writer.SetInput(flipFilter.GetOutput())

writer.Update()

Classes demonstrated