Separate Foreround and Background Using Otsu Method#

Note

Wish List Still needs additional work to finish proper creation of example.

Synopsis#

Separate foreground and background using Otsu’s method.

Results#

Note

Help Wanted Implementation of Results for sphinx examples containing this message. Reconfiguration of CMakeList.txt may be necessary. Write An Example <https://itk.org/ITKExamples/Documentation/Contribute/WriteANewExample.html>

Code#

C++#

#include "itkImage.h"
#include "itkOtsuThresholdImageFilter.h"
#include "itkImageFileReader.h"
#include "itkImageRegionIterator.h"
#include "itkNumericTraits.h"

#include "itksys/SystemTools.hxx"
#include <sstream>

#ifdef ENABLE_QUICKVIEW
#  include "QuickView.h"
#endif

namespace
{
using PixelType = unsigned char;
using ImageType = itk::Image<PixelType, 2>;
} // namespace

static void
CreateImage(ImageType::Pointer image);

int
main(int argc, char * argv[])
{
  ImageType::Pointer image;
  if (argc < 2)
  {
    image = ImageType::New();
    CreateImage(image.GetPointer());
  }
  else
  {
    image = itk::ReadImage<ImageType>(argv[1]);
  }

  using FilterType = itk::OtsuThresholdImageFilter<ImageType, ImageType>;
  auto otsuFilter = FilterType::New();
  otsuFilter->SetInput(image);
  otsuFilter->Update(); // To compute threshold

#ifdef ENABLE_QUICKVIEW
  QuickView viewer;
  viewer.AddImage(
    image.GetPointer(), true, argc > 1 ? itksys::SystemTools::GetFilenameName(argv[1]) : "Generated image");

  std::stringstream desc;
  desc << "Otsu Threshold: " << itk::NumericTraits<FilterType::InputPixelType>::PrintType(otsuFilter->GetThreshold());
  viewer.AddImage(otsuFilter->GetOutput(), true, desc.str());

  viewer.Visualize();
#endif

  return EXIT_SUCCESS;
}

void
CreateImage(ImageType::Pointer image)
{
  // Create an image
  ImageType::IndexType start;
  start.Fill(0);

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

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

  image->SetRegions(region);
  image->Allocate();

  // Make the whole image white
  itk::ImageRegionIterator<ImageType> iterator(image, image->GetLargestPossibleRegion());

  /*
   //Create a square
  while(!iterator.IsAtEnd())
    {
    iterator.Set(255);
    ++iterator;
    }
  */
}

Classes demonstrated#

template<typename TInputImage, typename TOutputImage, typename TMaskImage = TOutputImage>
class OtsuThresholdImageFilter : public itk::HistogramThresholdImageFilter<TInputImage, TOutputImage, TMaskImage>

Threshold an image using the Otsu Threshold.

This filter creates a binary thresholded image that separates an image into foreground and background components. The filter computes the threshold using the OtsuThresholdCalculator and applies that threshold to the input image using the BinaryThresholdImageFilter.

This implementation was taken from the Insight Journal paper:

https://www.insight-journal.org/browse/publication/811
Author

Richard Beare

Author

Gaetan Lehmann. Biologie du Developpement et de la Reproduction, INRA de Jouy-en-Josas, France.

See

HistogramThresholdImageFilter

ITK Sphinx Examples:

See itk::OtsuThresholdImageFilter for additional documentation.