int
main(int argc, char * argv[])
{
if (argc < 2)
{
std::cerr << "Missing command line arguments" << std::endl;
std::cerr << "Usage : ImageHistogram1 inputImageFileName " << std::endl;
return EXIT_FAILURE;
}
using PixelType = unsigned char;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(argv[1]);
try
{
reader->Update();
}
catch (const itk::ExceptionObject & excp)
{
std::cerr << "Problem reading image file : " << argv[1] << std::endl;
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}
using HistogramGeneratorType =
HistogramGeneratorType::Pointer histogramGenerator =
HistogramGeneratorType::New();
histogramGenerator->SetInput(reader->GetOutput());
histogramGenerator->SetNumberOfBins(256);
histogramGenerator->SetMarginalScale(10.0);
histogramGenerator->SetHistogramMin(-0.5);
histogramGenerator->SetHistogramMax(255.5);
histogramGenerator->Compute();
using HistogramType = HistogramGeneratorType::HistogramType;
const HistogramType * histogram = histogramGenerator->GetOutput();
const unsigned int histogramSize = histogram->Size();
std::cout << "Histogram size " << histogramSize << std::endl;
unsigned int bin;
for (bin = 0; bin < histogramSize; bin++)
{
std::cout << "bin = " << bin << " frequency = ";
std::cout << histogram->GetFrequency(bin, 0) << std::endl;
}
HistogramType::ConstIterator itr = histogram->Begin();
HistogramType::ConstIterator end = histogram->End();
unsigned int binNumber = 0;
while (itr != end)
{
std::cout << "bin = " << binNumber << " frequency = ";
std::cout << itr.GetFrequency() << std::endl;
++itr;
++binNumber;
}
return EXIT_SUCCESS;
}