namespace
{
int
ExampleMain(int argc, const char * const argv[])
{
if (argc < 3)
{
std::cerr << "Missing command line arguments" << std::endl;
std::cerr << "Usage : ImageEntropy1 inputImageFileName ";
std::cerr << "numberOfHistogramBins" << std::endl;
return EXIT_FAILURE;
}
using PixelType = unsigned char;
constexpr unsigned int Dimension = 3;
ImageType::Pointer input;
using HistogramGeneratorType =
auto histogramGenerator = HistogramGeneratorType::New();
const unsigned int numberOfHistogramBins = std::stoi(argv[2]);
histogramGenerator->SetNumberOfBins(numberOfHistogramBins);
histogramGenerator->SetMarginalScale(10.0);
histogramGenerator->SetInput(input);
histogramGenerator->Compute();
using HistogramType = HistogramGeneratorType::HistogramType;
const HistogramType * histogram = histogramGenerator->GetOutput();
const unsigned int histogramSize = histogram->Size();
std::cout << "Histogram size " << histogramSize << std::endl;
for (unsigned int bin = 0; bin < histogramSize; ++bin)
{
std::cout << "bin = " << bin << " frequency = ";
std::cout << histogram->GetFrequency(bin, 0) << std::endl;
}
HistogramType::ConstIterator itr = histogram->Begin();
const HistogramType::ConstIterator end = histogram->End();
const double Sum = histogram->GetTotalFrequency();
double Entropy = 0.0;
while (itr != end)
{
const double probability = itr.GetFrequency() / Sum;
if (probability > 0.99 / Sum)
{
Entropy += -probability * std::log(probability) / std::log(2.0);
}
++itr;
}
std::cout << "Image entropy = " << Entropy << " bits " << std::endl;
return EXIT_SUCCESS;
}
}
int
main(int argc, char * argv[])
{
try
{
return ExampleMain(argc, argv);
}
{
std::cerr << "ITK exception caught:\n" << exceptionObject << '\n';
}
catch (const std::exception & stdException)
{
std::cerr << "std exception caught:\n" << stdException.what() << '\n';
}
catch (...)
{
std::cerr << "Unhandled exception!\n";
}
return EXIT_FAILURE;
}
Standard exception handling object.
Templated n-dimensional image class.
TOutputImage::Pointer ReadImage(const std::string &filename)