ITK  5.0.0
Insight Segmentation and Registration Toolkit
WikiExamples/Statistics/ImageToHistogramFilterGrayscale.cxx
#include "itkImage.h"
using ImageType = itk::Image<unsigned char, 2>;
void CreateImage(ImageType::Pointer image);
int main(int, char *[])
{
constexpr unsigned int MeasurementVectorSize = 1; // Grayscale
constexpr unsigned int binsPerDimension = 30;
ImageType::Pointer image = ImageType::New();
CreateImage(image);
using ImageToHistogramFilterType = itk::Statistics::ImageToHistogramFilter< ImageType >;
ImageToHistogramFilterType::HistogramType::MeasurementVectorType lowerBound(binsPerDimension);
lowerBound.Fill(0);
ImageToHistogramFilterType::HistogramType::MeasurementVectorType upperBound(binsPerDimension);
upperBound.Fill(255);
size.Fill(binsPerDimension);
ImageToHistogramFilterType::Pointer imageToHistogramFilter = ImageToHistogramFilterType::New();
imageToHistogramFilter->SetInput(image);
imageToHistogramFilter->SetHistogramBinMinimum(lowerBound);
imageToHistogramFilter->SetHistogramBinMaximum(upperBound);
imageToHistogramFilter->SetHistogramSize(size);
imageToHistogramFilter->Update();
ImageToHistogramFilterType::HistogramType* histogram = imageToHistogramFilter->GetOutput();
std::cout << "Frequency = ";
for(unsigned int i = 0; i < histogram->GetSize()[0]; ++i)
{
std::cout << histogram->GetFrequency(i) << " ";
}
std::cout << std::endl;
return EXIT_SUCCESS;
}
void CreateImage(ImageType::Pointer image)
{
// Create a black image with a red square and a green square.
// This should produce a histogram with very strong spikes.
size.Fill(10);
start.Fill(0);
itk::ImageRegion<2> region(start, size);
image->SetRegions(region);
image->Allocate();
image->FillBuffer(0);
itk::ImageRandomIteratorWithIndex<ImageType> imageIterator(image, image->GetLargestPossibleRegion());
imageIterator.SetNumberOfSamples(10);
while(!imageIterator.IsAtEnd())
{
imageIterator.Set(122);
++imageIterator;
}
}