Skip to content
Snippets Groups Projects
Commit aa6bab1a authored by Patrick Imbo's avatar Patrick Imbo
Browse files

HistogramStatisticFunction : implémentation de la classe et des testing

parent 6a3b7af8
No related branches found
No related tags found
No related merge requests found
......@@ -3,7 +3,7 @@
FILE(GLOB OTBCommon_SRCS "*.cxx" )
ADD_LIBRARY(OTBCommon ${OTBCommon_SRCS})
TARGET_LINK_LIBRARIES (OTBCommon ITKStatistics ITKCommon)
TARGET_LINK_LIBRARIES (OTBCommon ITKAlgorithms ITKStatistics ITKCommon)
INSTALL_TARGETS(/lib/otb OTBCommon )
INSTALL_FILES(/include/otb/Common "(\\.h|\\.txx)$")
/*=========================================================================
Program: ORFEO Toolbox
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
See OTBCopyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#ifndef __otbHistogramStatisticFunction_h
#define __otbHistogramStatisticFunction_h
#include "itkHistogramAlgorithmBase.h"
namespace otb
{
/** \class HistogramStatisticFunction
* \brief Computes parameters for a histogram.
*
* You plug in the target histogram using SetInputHistogram method and
* get the entropy value from the histogram by calling the method GetEntropy
* or the GenerateData method.
*
* \ingroup Function
*/
template< class TInputHistogram, class TOutput >
class HistogramStatisticFunction :
public itk::HistogramAlgorithmBase< TInputHistogram >
{
public:
/**Standard class typedefs. */
typedef HistogramStatisticFunction Self;
typedef itk::HistogramAlgorithmBase<TInputHistogram> Superclass;
typedef itk::SmartPointer<Self> Pointer;
typedef itk::SmartPointer<const Self> ConstPointer;
typedef typename TInputHistogram::MeasurementType MeasurementType;
typedef typename TInputHistogram::FrequencyType FrequencyType;
typedef typename itk::NumericTraits<MeasurementType>::RealType EntropyType;
/**Standard Macros */
itkTypeMacro(HistogramStatisticFunction, HistogramAlgorithmsBase);
itkNewMacro(Self) ;
/** Typedef for the output type */
typedef TOutput OutputType;
/** Returns the entropy value */
OutputType GetEntropy();
protected:
HistogramStatisticFunction() ;
virtual ~HistogramStatisticFunction() {}
void PrintSelf(std::ostream& os, itk::Indent indent) const;
/** Calculates the thresholds and save them */
void GenerateData() ;
private:
OutputType m_entropy ;
} ; // end of class
} // end of namespace otb
#ifndef OTB_MANUAL_INSTANTIATION
#include "otbHistogramStatisticFunction.txx"
#endif
#endif
/*=========================================================================
Program: ORFEO Toolbox
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
See OTBCopyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#ifndef _otbHistogramStatisticFunction_txx
#define _otbHistogramStatisticFunction_txx
#include "otbHistogramStatisticFunction.h"
namespace otb
{
template< class TInputHistogram, class TOutput >
HistogramStatisticFunction< TInputHistogram, TOutput>
::HistogramStatisticFunction()
{
}
template< class TInputHistogram, class TOutput >
typename HistogramStatisticFunction< TInputHistogram, TOutput>::OutputType
HistogramStatisticFunction< TInputHistogram, TOutput>
::GetEntropy()
{
typename TInputHistogram::ConstPointer histogram = this->GetInputHistogram();
// TODO: as an improvement, the class could accept multi-dimensional histograms
// and the user could specify the dimension to apply the algorithm to.
if (histogram->GetSize().GetSizeDimension() != 1)
{
itkExceptionMacro(<<"Histogram must be 1-dimensional.");
}
// compute global mean
typename TInputHistogram::ConstIterator iter = histogram->Begin() ;
typename TInputHistogram::ConstIterator end = histogram->End() ;
EntropyType entropy = itk::NumericTraits<EntropyType>::Zero;
FrequencyType globalFrequency = histogram->GetTotalFrequency();
while (iter != end)
{
EntropyType Proba = static_cast<EntropyType>(iter.GetMeasurementVector()[0]);
Proba /= static_cast<EntropyType>(globalFrequency);
entropy += Proba * log (Proba);
++iter ;
}
m_entropy = static_cast<OutputType>(entropy);
return m_entropy;
}
template< class TInputHistogram, class TOutput >
void
HistogramStatisticFunction< TInputHistogram, TOutput>
::GenerateData()
{
m_entropy=GetEntropy();
}
template< class TInputHistogram, class TOutput >
void
HistogramStatisticFunction< TInputHistogram, TOutput>
::PrintSelf(std::ostream& os, itk::Indent indent) const
{
Superclass::PrintSelf(os,indent);
}
} // end namespace otb
#endif
......@@ -220,6 +220,11 @@ ADD_TEST(coTuPathListToHistogramGeneratorNew ${COMMON_TESTS}
ADD_TEST(coTuPathListToHistogramGenerator ${COMMON_TESTS}
otbPathListToHistogramGenerator 100 2)
# ------- otb::HistogramStatisticFunction ---------------------------
ADD_TEST(coTuHistogramStatisticFunction ${COMMON_TESTS}
otbHistogramStatisticFunction)
# ------- Fichiers sources CXX -----------------------------------
SET(BasicCommon_SRCS
......@@ -244,6 +249,7 @@ otbDrawLineSpatialObjectList.cxx
otbImageToLineSpatialObjectListNew.cxx
otbPathListToHistogramGeneratorNew.cxx
otbPathListToHistogramGenerator.cxx
otbHistogramStatisticFunction.cxx
)
......@@ -252,7 +258,7 @@ INCLUDE_DIRECTORIES("${OTBTesting_BINARY_DIR}")
# ${TIFF_LIBRARY}
ADD_EXECUTABLE(otbCommonTests otbCommonTests.cxx ${BasicCommon_SRCS})
TARGET_LINK_LIBRARIES(otbCommonTests OTBIO OTBCommon gdal ITKIO ITKStatistics ITKCommon)
TARGET_LINK_LIBRARIES(otbCommonTests OTBIO OTBCommon gdal ITKIO ITKAlgorithms ITKStatistics ITKCommon)
ADD_EXECUTABLE(otbTestExtractROI otbTestExtractROI.cxx )
TARGET_LINK_LIBRARIES(otbTestExtractROI OTBIO OTBCommon gdal ITKIO ITKCommon)
......
......@@ -47,4 +47,5 @@ REGISTER_TEST(otbDrawLineSpatialObjectList);
REGISTER_TEST(otbImageToLineSpatialObjectListNew);
REGISTER_TEST(otbPathListToHistogramGeneratorNew);
REGISTER_TEST(otbPathListToHistogramGenerator);
REGISTER_TEST(otbHistogramStatisticFunction);
}
/*=========================================================================
Program: ORFEO Toolbox
Language: C++
Date: $Date$
Version: $Revision$
Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
See OTBCopyright.txt for details.
This software is distributed WITHOUT ANY WARRANTY; without even
the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
PURPOSE. See the above copyright notices for more information.
=========================================================================*/
#if defined(_MSC_VER)
#pragma warning ( disable : 4786 )
#endif
#define MAIN
#include "itkHistogram.h"
#include "otbHistogramStatisticFunction.h"
int otbHistogramStatisticFunction(int, char*[])
{
typedef float MeasurementType ;
typedef itk::Statistics::Histogram< MeasurementType, 1 > HistogramType ;
HistogramType::Pointer histogram = HistogramType::New() ;
// initialize histogram
HistogramType::SizeType size;
size.Fill(64) ;
HistogramType::MeasurementVectorType lowerBound ;
HistogramType::MeasurementVectorType upperBound ;
lowerBound[0] = 0.0 ;
upperBound[0] = 64.0 ;
histogram->Initialize(size, lowerBound, upperBound ) ;
// create histogram
for (HistogramType::Iterator iter = histogram->Begin(); iter != histogram->End(); ++iter)
{
iter.SetFrequency(1.0);
}
typedef otb::HistogramStatisticFunction<HistogramType,MeasurementType> HistogramStatisticFunctionType;
HistogramStatisticFunctionType::Pointer HistogramStatisticFunction = HistogramStatisticFunctionType::New();
HistogramStatisticFunction->SetInputHistogram(histogram.GetPointer());
MeasurementType Entropy;
Entropy = HistogramStatisticFunction->GetEntropy();
std::cout << "Entropy : " << Entropy << std::endl;
if(fabs(Entropy)>0.0 )
{
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
......@@ -74,7 +74,6 @@ int otbPathListToHistogramGenerator( int argc, char* argv[] )
histogramGenerator->SetInput( PathList );
histogramGenerator->SetNumberOfBins( hsize );
histogramGenerator->SetMarginalScale( 10.0 );
histogramGenerator->Compute();
typedef HistogramGeneratorType::HistogramType HistogramType;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment