Segment Pixels With Similar Statistics¶
Synopsis¶
Segment pixels with similar statistics using connectivity.
Results¶
Code¶
C++¶
#include "itkImage.h"
#include "itkImageFileReader.h"
#include "itkConfidenceConnectedImageFilter.h"
#include "itksys/SystemTools.hxx"
#include <sstream>
#ifdef ENABLE_QUICKVIEW
# include "QuickView.h"
#endif
using ImageType = itk::Image<unsigned char, 2>;
int
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();
reader>SetFileName(inputFileName.c_str());
reader>Update();
using ConfidenceConnectedFilterType = itk::ConfidenceConnectedImageFilter<ImageType, ImageType>;
ConfidenceConnectedFilterType::Pointer confidenceConnectedFilter = ConfidenceConnectedFilterType::New();
confidenceConnectedFilter>SetInitialNeighborhoodRadius(3);
confidenceConnectedFilter>SetMultiplier(3);
confidenceConnectedFilter>SetNumberOfIterations(25);
confidenceConnectedFilter>SetReplaceValue(255);
// Set seed
ImageType::IndexType seed;
seed[0] = std::stoi(argv[2]);
seed[1] = std::stoi(argv[3]);
confidenceConnectedFilter>SetSeed(seed);
confidenceConnectedFilter>SetInput(reader>GetOutput());
#ifdef ENABLE_QUICKVIEW
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());
viewer.Visualize();
#endif
return EXIT_SUCCESS;
}
Classes demonstrated¶

template<typename
TInputImage
, typenameTOutputImage
>
classConfidenceConnectedImageFilter
: 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 (8connected, 26connected, 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 recalculated. 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: