Create Histogram From List of Measurements#

Synopsis#

Create a histogram from a list of sample measurements.

Results#

Output:

Histogram vector size: 1
Frequency of 0 : (1 to 1.1001) = 2
Frequency of 1 : (1.1001 to 1.2002) = 0
Frequency of 2 : (1.2002 to 1.3003) = 0
Frequency of 3 : (1.3003 to 1.4004) = 0
Frequency of 4 : (1.4004 to 1.5005) = 0
Frequency of 5 : (1.5005 to 1.6006) = 0
Frequency of 6 : (1.6006 to 1.7007) = 0
Frequency of 7 : (1.7007 to 1.8008) = 0
Frequency of 8 : (1.8008 to 1.9009) = 0
Frequency of 9 : (1.9009 to 2.001) = 1
Total count 3

Code#

C++#

#include "itkSampleToHistogramFilter.h"
#include "itkListSample.h"
#include "itkHistogram.h"

using MeasurementVectorType = itk::Vector<unsigned char, 1>;
using SampleType = itk::Statistics::ListSample<MeasurementVectorType>;

using HistogramType = itk::Statistics::Histogram<float, itk::Statistics::DenseFrequencyContainer2>;

void
CreateSample(SampleType::Pointer sample);

int
main()
{
  auto sample = SampleType::New();
  CreateSample(sample);

  using SampleToHistogramFilterType = itk::Statistics::SampleToHistogramFilter<SampleType, HistogramType>;
  auto sampleToHistogramFilter = SampleToHistogramFilterType::New();
  sampleToHistogramFilter->SetInput(sample);

  SampleToHistogramFilterType::HistogramSizeType histogramSize(1);
  histogramSize.Fill(10);
  sampleToHistogramFilter->SetHistogramSize(histogramSize);

  sampleToHistogramFilter->Update();

  const HistogramType * histogram = sampleToHistogramFilter->GetOutput();
  std::cout << "Histogram vector size: " << histogram->GetMeasurementVectorSize() << std::endl;


  for (unsigned int i = 0; i < histogram->GetSize()[0]; ++i)
  {
    std::cout << "Frequency of " << i << " : (" << histogram->GetBinMin(0, i) << " to " << histogram->GetBinMax(0, i)
              << ") = " << histogram->GetFrequency(i) << std::endl;
  }

  std::cout << "Total count " << histogram->GetTotalFrequency() << std::endl;

  return EXIT_SUCCESS;
}

void
CreateSample(SampleType::Pointer sample)
{
  MeasurementVectorType mv;
  mv[0] = 1.0;
  sample->PushBack(mv);

  mv[0] = 1.0;
  sample->PushBack(mv);

  mv[0] = 2.0;
  sample->PushBack(mv);
}

Classes demonstrated#

template<typename TSample, typename THistogram>
class SampleToHistogramFilter : public itk::ProcessObject

Computes the Histogram corresponding to a Sample.

This filter produces as output the histogram corresponding to the values of a Sample.

See

Sample, Histogram

ITK Sphinx Examples:

See itk::Statistics::SampleToHistogramFilter for additional documentation.