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;
}
typedef unsigned char PixelComponentType;
ReaderType::Pointer reader = ReaderType::New();
reader->SetFileName( argv[1] );
try
{
reader->Update();
}
{
std::cerr << "Problem reading image file : " << argv[1] << std::endl;
std::cerr << excp << std::endl;
return EXIT_FAILURE;
}
RGBImageType > HistogramFilterType;
HistogramFilterType::Pointer histogramFilter =
HistogramFilterType::New();
typedef HistogramFilterType::HistogramSizeType
SizeType;
SizeType size(3);
size[0] = 256;
size[1] = 256;
size[2] = 256;
histogramFilter->SetHistogramSize( size );
typedef HistogramFilterType::HistogramMeasurementVectorType
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();
typedef HistogramFilterType::HistogramType 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();
typedef HistogramType::AbsoluteFrequencyType 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;
}