int
main(int argc, char * argv[])
{
if (argc < 3)
{
std::cerr << "Missing command line arguments" << std::endl;
std::cerr << "Usage : ImageHistogram4 inputRGBImageFileName ";
std::cerr << " histogramFilename.raw" << 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 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] = 256;
size[1] = 256;
size[2] = 256;
histogramFilter->SetHistogramSize(size);
using HistogramMeasurementVectorType =
HistogramFilterType::HistogramMeasurementVectorType;
HistogramMeasurementVectorType binMinimum(3);
HistogramMeasurementVectorType binMaximum(3);
binMinimum[0] = -0.5;
binMinimum[1] = -0.5;
binMinimum[2] = -0.5;
binMaximum[0] = 255.5;
binMaximum[1] = 255.5;
binMaximum[2] = 255.5;
histogramFilter->SetHistogramBinMinimum(binMinimum);
histogramFilter->SetHistogramBinMaximum(binMaximum);
histogramFilter->SetInput(reader->GetOutput());
histogramFilter->SetMarginalScale(10.0);
histogramFilter->Update();
using HistogramType = HistogramFilterType::HistogramType;
const HistogramType * histogram = histogramFilter->GetOutput();
const unsigned int histogramSize = histogram->Size();
std::cout << "Histogram size " << histogramSize << std::endl;
std::ofstream histogramFile;
histogramFile.open(argv[2]);
HistogramType::ConstIterator itr = histogram->Begin();
HistogramType::ConstIterator end = histogram->End();
using AbsoluteFrequencyType = HistogramType::AbsoluteFrequencyType;
while (itr != end)
{
const AbsoluteFrequencyType frequency = itr.GetFrequency();
histogramFile.write((const char *)(&frequency), sizeof(frequency));
if (frequency != 0)
{
index = histogram->
GetIndex(itr.GetInstanceIdentifier());
std::cout << "Index = " << index << ", Frequency = " << frequency
<< std::endl;
}
++itr;
}
histogramFile.close();
return EXIT_SUCCESS;
}