Segment Pixels With Similar Statistics


Segment pixels with similar statistics using connectivity.


VTK Window

Output In VTK Window



#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkConfidenceConnectedImageFilter.h"

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

#  include "QuickView.h"

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

main(int argc, char * argv[])
  if (argc < 4)
    std::cerr << "Required: filename.png seedX seedY" << std::endl;

    return EXIT_FAILURE;
  std::string inputFileName = argv[1];

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

  using ConfidenceConnectedFilterType = itk::ConfidenceConnectedImageFilter<ImageType, ImageType>;
  ConfidenceConnectedFilterType::Pointer confidenceConnectedFilter = ConfidenceConnectedFilterType::New();

  // Set seed
  ImageType::IndexType seed;
  seed[0] = std::stoi(argv[2]);
  seed[1] = std::stoi(argv[3]);

  QuickView viewer;
  viewer.AddImage(reader->GetOutput(), true, itksys::SystemTools::GetFilenameName(inputFileName));

  std::stringstream desc;
  desc << "ConfidenceConnected Seed: " << seed[0] << ", " << seed[1];
  viewer.AddImage(confidenceConnectedFilter->GetOutput(), true, desc.str());


  return EXIT_SUCCESS;

Classes demonstrated

template<typename TInputImage, typename TOutputImage>
class ConfidenceConnectedImageFilter : public itk::ImageToImageFilter<TInputImage, TOutputImage>

Segment pixels with similar statistics using connectivity.

This filter extracts a connected set of pixels whose pixel intensities are consistent with the pixel statistics of a seed point. The mean and variance across a neighborhood (8-connected, 26-connected, etc.) are calculated for a seed point. Then pixels connected to this seed point whose values are within the confidence interval for the seed point are grouped. The width of the confidence interval is controlled by the “Multiplier” variable (the confidence interval is the mean plus or minus the “Multiplier” times the standard deviation). If the intensity variations across a segment were gaussian, a “Multiplier” setting of 2.5 would define a confidence interval wide enough to capture 99% of samples in the segment.

After this initial segmentation is calculated, the mean and variance are re-calculated. All the pixels in the previous segmentation are used to calculate the mean the standard deviation (as opposed to using the pixels in the neighborhood of the seed point). The segmentation is then recalculated using these refined estimates for the mean and variance of the pixel values. This process is repeated for the specified number of iterations. Setting the “NumberOfIterations” to zero stops the algorithm after the initial segmentation from the seed point.

NOTE: the lower and upper threshold are restricted to lie within the valid numeric limits of the input data pixel type. Also, the limits may be adjusted to contain the seed point’s intensity.

ITK Sphinx Examples:

See itk::ConfidenceConnectedImageFilter for additional documentation.