OTB  9.0.0
Orfeo Toolbox
otbContingencyTableCalculator.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2022 Centre National d'Etudes Spatiales (CNES)
3  *
4  * This file is part of Orfeo Toolbox
5  *
6  * https://www.orfeo-toolbox.org/
7  *
8  * Licensed under the Apache License, Version 2.0 (the "License");
9  * you may not use this file except in compliance with the License.
10  * You may obtain a copy of the License at
11  *
12  * http://www.apache.org/licenses/LICENSE-2.0
13  *
14  * Unless required by applicable law or agreed to in writing, software
15  * distributed under the License is distributed on an "AS IS" BASIS,
16  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17  * See the License for the specific language governing permissions and
18  * limitations under the License.
19  */
20 
21 #ifndef otbContingencyTableCalculator_hxx
22 #define otbContingencyTableCalculator_hxx
23 
25 #include "itkMacro.h"
26 #include "itkVariableLengthVector.h"
27 #include "itkListSample.h"
28 
29 #include <set>
30 #include <iterator>
31 
32 namespace otb
33 {
34 template <class TClassLabel>
35 ContingencyTableCalculator<TClassLabel>::ContingencyTableCalculator() : m_NumberOfRefClasses(0), m_NumberOfProdClasses(0), m_NumberOfSamples(0)
36 {
37 }
38 
39 template <class TClassLabel>
41 {
42  m_LabelCount.clear();
43  m_NumberOfRefClasses = 0;
44  m_NumberOfProdClasses = 0;
45  m_NumberOfSamples = 0;
46 }
47 
48 template <class TClassLabel>
49 template <class TRefIterator, class TProdIterator>
50 void ContingencyTableCalculator<TClassLabel>::Compute(TRefIterator refBegin, TRefIterator refEnd, TProdIterator prodBegin, TProdIterator prodEnd)
51 {
52  while (refBegin != refEnd && prodBegin != prodEnd)
53  {
54  ++m_LabelCount[refBegin.GetMeasurementVector()[0]][prodBegin.GetMeasurementVector()[0]];
55  ++refBegin;
56  ++prodBegin;
57  ++m_NumberOfSamples;
58  }
59 
60  if (refBegin != refEnd || prodBegin != prodEnd)
61  itkExceptionMacro(<< "The references and produced labels did not end simultaneously.");
62 }
63 
64 template <class TClassLabel>
65 template <class TRefIterator, class TProdIterator>
66 void ContingencyTableCalculator<TClassLabel>::Compute(TRefIterator itRef, TProdIterator itProd, bool refHasNoData,
67  typename TRefIterator::InternalPixelType refNoData, bool prodHasNoData,
68  typename TProdIterator::InternalPixelType prodNoData)
69 {
70  while (!itRef.IsAtEnd() && !itProd.IsAtEnd())
71  {
72  if ((!prodHasNoData || itProd.Get() != prodNoData) && (!refHasNoData || itRef.Get() != refNoData))
73  {
74  ++m_LabelCount[itRef.Get()][itProd.Get()];
75  ++m_NumberOfSamples;
76  }
77  ++itRef;
78  ++itProd;
79  }
80 
81  if (!itRef.IsAtEnd() || !itProd.IsAtEnd())
82  itkExceptionMacro(<< "The references and produced labels did not end simultaneously.");
83 }
84 
85 
86 template <class TClassLabel>
88 {
89  std::set<TClassLabel> refLabels;
90  std::set<TClassLabel> prodLabels;
91 
92  // Retrieve all labels needed to iterate over all labelCount
93  for (typename MapOfClassesType::const_iterator refIt = m_LabelCount.begin(); refIt != m_LabelCount.end(); ++refIt)
94  {
95  refLabels.insert(refIt->first);
96  CountMapType cmt = refIt->second;
97  for (typename CountMapType::const_iterator prodIt = cmt.begin(); prodIt != cmt.end(); ++prodIt)
98  {
99  prodLabels.insert(prodIt->first);
100  }
101  }
102 
103  m_NumberOfRefClasses = refLabels.size();
104  m_NumberOfProdClasses = prodLabels.size();
105 
106  unsigned int rows = static_cast<unsigned int>(m_NumberOfRefClasses);
107  unsigned int cols = static_cast<unsigned int>(m_NumberOfProdClasses);
108 
109  std::vector<TClassLabel> referenceLabels(refLabels.begin(), refLabels.end());
110  std::vector<TClassLabel> producedLabels(prodLabels.begin(), prodLabels.end());
111 
112  ContingencyTablePointerType contingencyTable = ContingencyTableType::New();
113  contingencyTable->SetLabels(referenceLabels, producedLabels);
114 
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]];
118 
119 
120  return contingencyTable;
121 }
122 }
123 
124 #endif
otb::ContingencyTableCalculator::ContingencyTablePointerType
ContingencyTableType::Pointer ContingencyTablePointerType
Definition: otbContingencyTableCalculator.h:58
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::ContingencyTableCalculator::ContingencyTableCalculator
ContingencyTableCalculator()
Definition: otbContingencyTableCalculator.hxx:35
otb::ContingencyTableCalculator::Compute
void Compute(TRefIterator itRef, TProdIterator itProd, bool refHasNoData=false, typename TRefIterator::InternalPixelType refNoData=0, bool prodHasNoData=false, typename TProdIterator::InternalPixelType prodNoData=0)
Definition: otbContingencyTableCalculator.hxx:66
otb::ContingencyTableCalculator::Clear
void Clear()
Definition: otbContingencyTableCalculator.hxx:40
otb::ContingencyTableCalculator::BuildContingencyTable
ContingencyTablePointerType BuildContingencyTable()
Definition: otbContingencyTableCalculator.hxx:87
otb::ContingencyTableCalculator::CountMapType
std::map< TClassLabel, unsigned long > CountMapType
Definition: otbContingencyTableCalculator.h:60
otbContingencyTableCalculator.h