21 #ifndef otbContingencyTableCalculator_hxx
22 #define otbContingencyTableCalculator_hxx
26 #include "itkVariableLengthVector.h"
27 #include "itkListSample.h"
34 template <
class TClassLabel>
39 template <
class TClassLabel>
43 m_NumberOfRefClasses = 0;
44 m_NumberOfProdClasses = 0;
45 m_NumberOfSamples = 0;
48 template <
class TClassLabel>
49 template <
class TRefIterator,
class TProdIterator>
52 while (refBegin != refEnd && prodBegin != prodEnd)
54 ++m_LabelCount[refBegin.GetMeasurementVector()[0]][prodBegin.GetMeasurementVector()[0]];
60 if (refBegin != refEnd || prodBegin != prodEnd)
61 itkExceptionMacro(<<
"The references and produced labels did not end simultaneously.");
64 template <
class TClassLabel>
65 template <
class TRefIterator,
class TProdIterator>
67 typename TRefIterator::InternalPixelType refNoData,
bool prodHasNoData,
68 typename TProdIterator::InternalPixelType prodNoData)
70 while (!itRef.IsAtEnd() && !itProd.IsAtEnd())
72 if ((!prodHasNoData || itProd.Get() != prodNoData) && (!refHasNoData || itRef.Get() != refNoData))
74 ++m_LabelCount[itRef.Get()][itProd.Get()];
81 if (!itRef.IsAtEnd() || !itProd.IsAtEnd())
82 itkExceptionMacro(<<
"The references and produced labels did not end simultaneously.");
86 template <
class TClassLabel>
89 std::set<TClassLabel> refLabels;
90 std::set<TClassLabel> prodLabels;
93 for (
typename MapOfClassesType::const_iterator refIt = m_LabelCount.begin(); refIt != m_LabelCount.end(); ++refIt)
95 refLabels.insert(refIt->first);
97 for (
typename CountMapType::const_iterator prodIt = cmt.begin(); prodIt != cmt.end(); ++prodIt)
99 prodLabels.insert(prodIt->first);
103 m_NumberOfRefClasses = refLabels.size();
104 m_NumberOfProdClasses = prodLabels.size();
106 unsigned int rows =
static_cast<unsigned int>(m_NumberOfRefClasses);
107 unsigned int cols =
static_cast<unsigned int>(m_NumberOfProdClasses);
109 std::vector<TClassLabel> referenceLabels(refLabels.begin(), refLabels.end());
110 std::vector<TClassLabel> producedLabels(prodLabels.begin(), prodLabels.end());
113 contingencyTable->SetLabels(referenceLabels, producedLabels);
115 for (
unsigned int i = 0; i < rows; ++i)
116 for (
unsigned int j = 0; j < cols; ++j)
117 contingencyTable->matrix(i, j) = m_LabelCount[referenceLabels[i]][producedLabels[j]];
120 return contingencyTable;