Combine Two Images With Checker Board Pattern#

Synopsis#

Combine two images by alternating blocks of a checkerboard pattern.

In this example the first image is black and the second one is white.

Results#

Output image

Output image#

Code#

Python#

#!/usr/bin/env python

import itk
import argparse

parser = argparse.ArgumentParser(
    description="Combine Two Images With Checker Board Pattern."
)
parser.add_argument("output_image")
args = parser.parse_args()

dimension = 2
pixel_type = itk.UC
image_type = itk.Image[pixel_type, dimension]

# Create an image
start = itk.Index[dimension]()
start.Fill(0)

size = itk.Size[dimension]()
size.Fill(100)

region = itk.ImageRegion[dimension]()
region.SetSize(size)
region.SetIndex(start)

image1 = image_type.New(Regions=region)
image1.Allocate()
image1.FillBuffer(0)

image2 = image_type.New(Regions=region)
image2.Allocate()
image2.FillBuffer(255)

output = itk.checker_board_image_filter(image1, image2)

itk.imwrite(output, args.output_image)

C++#

#include "itkImage.h"
#include "itkCheckerBoardImageFilter.h"
#include "itkImageFileWriter.h"

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

  constexpr unsigned int Dimension = 2;

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

  ImageType::IndexType start;
  start.Fill(0);

  ImageType::SizeType size;
  size.Fill(100);

  ImageType::RegionType region;
  region.SetSize(size);
  region.SetIndex(start);

  auto image1 = ImageType::New();
  image1->SetRegions(region);
  image1->Allocate();
  image1->FillBuffer(0);

  auto image2 = ImageType::New();
  image2->SetRegions(region);
  image2->Allocate();
  image2->FillBuffer(255);

  using CheckerBoardFilterType = itk::CheckerBoardImageFilter<ImageType>;
  auto checkerBoardFilter = CheckerBoardFilterType::New();
  checkerBoardFilter->SetInput1(image1);
  checkerBoardFilter->SetInput2(image2);

  try
  {
    itk::WriteImage(checkerBoardFilter->GetOutput(), argv[1]);
  }
  catch (const itk::ExceptionObject & error)
  {
    std::cerr << "Error: " << error << std::endl;
    return EXIT_FAILURE;
  }

  return EXIT_SUCCESS;
}

Classes demonstrated#

template<typename TImage>
class CheckerBoardImageFilter : public itk::ImageToImageFilter<TImage, TImage>

Combines two images in a checkerboard pattern.

CheckerBoardImageFilter takes two input images that must have the same dimension, size, origin and spacing and produces an output image of the same size by combining the pixels from the two input images in a checkerboard pattern. This filter is commonly used for visually comparing two images, in particular for evaluating the results of an image registration process.

This filter is implemented as a multithreaded filter. It provides a DynamicThreadedGenerateData() method for its implementation.

ITK Sphinx Examples:

See itk::CheckerBoardImageFilter for additional documentation.