1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135
|
/*
* Copyright (C) 2005-2017 Centre National d'Etudes Spatiales (CNES)
*
* This file is part of Orfeo Toolbox
*
* https://www.orfeo-toolbox.org/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "itkHistogram.h"
#include "otbHistogramStatisticsFunction.h"
int otbHistogramStatisticsFunction(int itkNotUsed(argc), char * argv[])
{
unsigned int NbOfBins((unsigned int) ::atoi(argv[1]));
typedef float MeasurementType;
typedef itk::Statistics::Histogram<MeasurementType, itk::Statistics::DenseFrequencyContainer2> HistogramType;
HistogramType::Pointer histogram = HistogramType::New();
// initialize histogram
HistogramType::SizeType size(1);
size.Fill(NbOfBins);
HistogramType::MeasurementVectorType lowerBound(1);
HistogramType::MeasurementVectorType upperBound(1);
lowerBound[0] = 0.0;
upperBound[0] = NbOfBins;
histogram->SetMeasurementVectorSize(1);
histogram->Initialize(size, lowerBound, upperBound);
// create histogram with same value for each frequency
for (HistogramType::Iterator iter = histogram->Begin(); iter != histogram->End(); ++iter)
{
iter.SetFrequency(1.0);
}
typedef otb::HistogramStatisticsFunction<HistogramType, MeasurementType> HistogramStatisticsFunctionType;
HistogramStatisticsFunctionType::Pointer HistogramStatisticsFunction = HistogramStatisticsFunctionType::New();
HistogramStatisticsFunction->SetInputHistogram(histogram.GetPointer());
MeasurementType Entropy;
MeasurementType Mean;
MeasurementType Covariance;
std::cout << "Update OK : " << std::endl;
std::cout << "Entropy 1 : " << HistogramStatisticsFunction->GetEntropy()[0] << std::endl;
Entropy = HistogramStatisticsFunction->GetEntropy()[0];
std::cout << "Entropy 1 : " << Entropy << std::endl;
if (vcl_abs(Entropy - vcl_log(static_cast<double>(NbOfBins))) > 0.00001)
{
std::cout << "Error in entropy estimation" << std::endl;
return EXIT_FAILURE;
}
Mean = HistogramStatisticsFunction->GetMean()[0];
std::cout << "Mean 1 : " << Mean << std::endl;
if (Mean != NbOfBins / 2.)
{
std::cout << "Error in mean estimation" << std::endl;
return EXIT_FAILURE;
}
Covariance = HistogramStatisticsFunction->GetCovariance()[0];
std::cout << "Covariance 1 : " << Covariance << std::endl;
if (Covariance != 0)
{
std::cout << "Error in covariance estimation" << std::endl;
return EXIT_FAILURE;
}
// create histogram just all value equal to zero except the first one
for (HistogramType::Iterator iter = histogram->Begin(); iter != histogram->End(); ++iter)
{
if (iter == histogram->Begin())
{
iter.SetFrequency(1.0);
}
else
{
iter.SetFrequency(0.0);
}
}
HistogramStatisticsFunction->Compute();
Entropy = HistogramStatisticsFunction->GetEntropy()[0];
std::cout << "Entropy 2 : " << Entropy << std::endl;
if (Entropy != 0.0)
{
std::cout << "Error in entropy estimation" << std::endl;
return EXIT_FAILURE;
}
Mean = HistogramStatisticsFunction->GetMean()[0];
std::cout << "Mean 2 : " << Mean << std::endl;
if (Mean != 0.5)
{
std::cout << "Error in mean estimation" << std::endl;
return EXIT_FAILURE;
}
Covariance = HistogramStatisticsFunction->GetCovariance()[0];
std::cout << "Covariance 2 : " << Covariance << std::endl;
if (Covariance != 0)
{
std::cout << "Error in covariance estimation" << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
|