int
main(int argc, char * argv[])
{
if (argc < 2)
{
std::cerr << "Missing command line arguments" << std::endl;
std::cerr << "Usage : ImageHistogram3 inputRGBImageFileName "
<< std::endl;
return EXIT_FAILURE;
}
using PixelComponentType = unsigned char;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName(argv[1]);
try
{
reader->Update();
}
catch (const itk::ExceptionObject & excp)
{
std::cerr << "Problem encoutered while reading image file : " << argv[1]
<< std::endl;
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}
using HistogramFilterType =
HistogramFilterType::Pointer histogramFilter = HistogramFilterType::New();
using SizeType = HistogramFilterType::HistogramSizeType;
size[0] = 255;
size[1] = 1;
size[2] = 1;
histogramFilter->SetHistogramSize(size);
histogramFilter->SetMarginalScale(10.0);
HistogramFilterType::HistogramMeasurementVectorType lowerBound(3);
HistogramFilterType::HistogramMeasurementVectorType upperBound(3);
lowerBound[0] = 0;
lowerBound[1] = 0;
lowerBound[2] = 0;
upperBound[0] = 256;
upperBound[1] = 256;
upperBound[2] = 256;
histogramFilter->SetHistogramBinMinimum(lowerBound);
histogramFilter->SetHistogramBinMaximum(upperBound);
histogramFilter->SetInput(reader->GetOutput());
histogramFilter->Update();
using HistogramType = HistogramFilterType::HistogramType;
const HistogramType * histogram = histogramFilter->GetOutput();
const unsigned int histogramSize = histogram->Size();
std::cout << "Histogram size " << histogramSize << std::endl;
unsigned int channel = 0;
std::cout << "Histogram of the red component" << std::endl;
for (unsigned int bin = 0; bin < histogramSize; ++bin)
{
std::cout << "bin = " << bin << " frequency = ";
std::cout << histogram->GetFrequency(bin, channel) << std::endl;
}
size[0] = 1;
size[1] = 255;
size[2] = 1;
histogramFilter->SetHistogramSize(size);
histogramFilter->Update();
channel = 1;
std::cout << "Histogram of the green component" << std::endl;
for (unsigned int bin = 0; bin < histogramSize; ++bin)
{
std::cout << "bin = " << bin << " frequency = ";
std::cout << histogram->GetFrequency(bin, channel) << std::endl;
}
size[0] = 1;
size[1] = 1;
size[2] = 255;
histogramFilter->SetHistogramSize(size);
histogramFilter->Update();
channel = 2;
std::cout << "Histogram of the blue component" << std::endl;
for (unsigned int bin = 0; bin < histogramSize; ++bin)
{
std::cout << "bin = " << bin << " frequency = ";
std::cout << histogram->GetFrequency(bin, channel) << std::endl;
}
return EXIT_SUCCESS;
}