Visualize a Static Sparse Malcolm 2D Level-Set Layers#

Synopsis#

Visualize a static sparse Malcolm level-set function 2D’s layers. From the input image, first an otsu thresholding technique is used to get a binary mask, which is then converted to a sparse level-set function.

Note that Malcolm’s representation is composed of a single layer (value = {0});

Results#

Input image (cells)

Input image#

Static level-sets zero-sets

Static level-sets#

Code#

C++#

#include "itkVTKVisualize2DSparseLevelSetLayers.h"

#include "itkBinaryImageToLevelSetImageAdaptor.h"
#include "itkImageFileReader.h"
#include "itkMalcolmSparseLevelSetImage.h"

#include "itkOtsuMultipleThresholdsImageFilter.h"
#include "itkRescaleIntensityImageFilter.h"
#include "vtkRenderWindowInteractor.h"

int
main(int argc, char * argv[])
{
  if (argc != 3)
  {
    std::cerr << "Missing Arguments" << std::endl;
    std::cerr << argv[0] << std::endl;
    std::cerr << "<Input Image> <Interactive (0 or 1)>" << std::endl;
    return EXIT_FAILURE;
  }

  // Image Dimension
  constexpr unsigned int Dimension = 2;

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

  InputImageType::Pointer input = itk::ReadImage<InputImageType>(argv[1]);

  using LevelSetType = itk::MalcolmSparseLevelSetImage<Dimension>;

  // Generate a binary mask that will be used as initialization for the level
  // set evolution.
  using OtsuFilterType = itk::OtsuMultipleThresholdsImageFilter<InputImageType, InputImageType>;
  auto otsu = OtsuFilterType::New();
  otsu->SetInput(input);
  otsu->SetNumberOfHistogramBins(256);
  otsu->SetNumberOfThresholds(1);

  using RescaleType = itk::RescaleIntensityImageFilter<InputImageType, InputImageType>;
  auto rescaler = RescaleType::New();
  rescaler->SetInput(otsu->GetOutput());
  rescaler->SetOutputMinimum(0);
  rescaler->SetOutputMaximum(1);

  // convert a binary mask to a level-set function
  using BinaryImageToLevelSetType = itk::BinaryImageToLevelSetImageAdaptor<InputImageType, LevelSetType>;

  auto adaptor = BinaryImageToLevelSetType::New();
  adaptor->SetInputImage(rescaler->GetOutput());
  adaptor->Initialize();

  LevelSetType::Pointer levelSet = adaptor->GetModifiableLevelSet();

  // Create the visualizer
  using VisualizationType = itk::VTKVisualize2DSparseLevelSetLayers<InputImageType, LevelSetType>;
  auto visualizer = VisualizationType::New();
  visualizer->SetInputImage(input);
  visualizer->SetLevelSet(levelSet);
  visualizer->SetScreenCapture(true);

  vtkSmartPointer<vtkRenderWindowInteractor> renderWindowInteractor = vtkSmartPointer<vtkRenderWindowInteractor>::New();
  renderWindowInteractor->SetRenderWindow(visualizer->GetRenderWindow());

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

  bool interactive = (std::stoi(argv[2]) != 0);
  if (interactive)
  {
    renderWindowInteractor->Start();
  }

  return EXIT_SUCCESS;
}

Classes demonstrated#