OTB  9.0.0
Orfeo Toolbox
otbConfusionMatrixCalculator.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 otbConfusionMatrixCalculator_hxx
22 #define otbConfusionMatrixCalculator_hxx
23 
25 
26 namespace otb
27 {
28 template <class TRefListLabel, class TProdListLabel>
30  : m_KappaIndex(0.0),
31  m_OverallAccuracy(0.0),
32  m_FalseNegativeValue(0.0),
33  m_TrueNegativeValue(0.0),
34  m_FalsePositiveValue(0.0),
35  m_TruePositiveValue(0.0),
36  m_Precision(0.0),
37  m_Recall(0.0),
38  m_FScore(0.0),
39  m_NumberOfClasses(0),
40  m_NumberOfSamples(0)
41 
42 {
44  m_ConfusionMatrix.Fill(0);
46  m_ReferenceLabels = RefListLabelType::New();
47  m_ProducedLabels = ProdListLabelType::New();
48 }
49 
50 template <class TRefListLabel, class TProdListLabel>
52 {
53  typename RefListLabelType::ConstIterator refIterator = m_ReferenceLabels->Begin();
54  typename ProdListLabelType::ConstIterator prodIterator = m_ProducedLabels->Begin();
55 
56  // check that both lists have the same number of samples
57  if ((m_ReferenceLabels->Size() != m_ProducedLabels->Size()) || (m_ReferenceLabels->Size() == 0) || (m_ProducedLabels->Size() == 0))
58  {
59  otbMsgDebugMacro(<< "refLabels size = " << m_ReferenceLabels->Size() << " / proLabels size = " << m_ProducedLabels->Size());
60  throw itk::ExceptionObject(__FILE__, __LINE__, "ListSample size mismatch", ITK_LOCATION);
61  }
62 
63  m_NumberOfSamples = m_ReferenceLabels->Size();
64 
65  // count the number of classes
66  int countClasses = 0;
67  while (refIterator != m_ReferenceLabels->End())
68  {
69  ClassLabelType currentLabel = refIterator.GetMeasurementVector()[0];
70  if (m_MapOfClasses.find(currentLabel) == m_MapOfClasses.end())
71  {
72  m_MapOfClasses[currentLabel] = countClasses;
73  m_MapOfIndices[countClasses] = currentLabel;
74  ++countClasses;
75  }
76  ++refIterator;
77  }
78 
79  m_NumberOfClasses = countClasses;
80 
81 
82  // SORTING of m_MapOfClasses and m_MapOfIndices according to increasing class labels
83  typename MapOfClassesType::iterator itMapOfClasses;
84  itMapOfClasses = m_MapOfClasses.begin();
85 
86  unsigned int itElt = 0;
87  while (itMapOfClasses != m_MapOfClasses.end())
88  {
89  ClassLabelType currentLabel = itMapOfClasses->first;
90  m_MapOfClasses[currentLabel] = itElt;
91  m_MapOfIndices[itElt] = currentLabel;
92  ++itMapOfClasses;
93  ++itElt;
94  }
95 
96 
97  std::vector<long int> samplesPerClass;
98 
99  for (unsigned int i = 0; i < m_NumberOfClasses; ++i)
100  samplesPerClass.push_back(0);
101 
102  m_ConfusionMatrix = ConfusionMatrixType(m_NumberOfClasses, m_NumberOfClasses);
103  m_ConfusionMatrix.Fill(0);
104 
105  refIterator = m_ReferenceLabels->Begin();
106  prodIterator = m_ProducedLabels->Begin();
107 
108  while (refIterator != m_ReferenceLabels->End())
109  {
110  int refLabel = refIterator.GetMeasurementVector()[0];
111  int prodLabel = prodIterator.GetMeasurementVector()[0];
112 
113  int refPos = m_MapOfClasses[refLabel];
114  int prodPos = m_MapOfClasses[prodLabel];
115 
116  ++samplesPerClass[refPos];
117  m_ConfusionMatrix(refPos, prodPos) += 1;
118 
119  ++refIterator;
120  ++prodIterator;
121  }
122 
123 
124  m_ConfMatMeasurements->SetConfusionMatrix(m_ConfusionMatrix);
125  m_ConfMatMeasurements->Compute();
126 
127  this->m_TruePositiveValues = m_ConfMatMeasurements->GetTruePositiveValues();
128  this->m_FalseNegativeValues = m_ConfMatMeasurements->GetFalseNegativeValues();
129  this->m_TrueNegativeValues = m_ConfMatMeasurements->GetTrueNegativeValues();
130  this->m_FalsePositiveValues = m_ConfMatMeasurements->GetFalsePositiveValues();
131 
132  this->m_Precisions = m_ConfMatMeasurements->GetPrecisions();
133  this->m_Recalls = m_ConfMatMeasurements->GetRecalls();
134  this->m_FScores = m_ConfMatMeasurements->GetFScores();
135 
136 
137  this->m_TruePositiveValue = m_ConfMatMeasurements->GetTruePositiveValue();
138  this->m_FalseNegativeValue = m_ConfMatMeasurements->GetFalseNegativeValue();
139  this->m_TrueNegativeValue = m_ConfMatMeasurements->GetTrueNegativeValue();
140  this->m_FalsePositiveValue = m_ConfMatMeasurements->GetFalsePositiveValue();
141 
142  this->m_Precision = m_ConfMatMeasurements->GetPrecision();
143  this->m_Recall = m_ConfMatMeasurements->GetRecall();
144  this->m_FScore = m_ConfMatMeasurements->GetFScore();
145 
146  this->m_OverallAccuracy = m_ConfMatMeasurements->GetOverallAccuracy();
147  this->m_KappaIndex = m_ConfMatMeasurements->GetKappaIndex();
148 }
149 
150 template <class TRefListLabel, class TProdListLabel>
151 void ConfusionMatrixCalculator<TRefListLabel, TProdListLabel>::PrintSelf(std::ostream& os, itk::Indent indent) const
152 {
153  os << indent << "TODO";
154 }
155 }
156 
157 #endif
otb::ConfusionMatrixMeasurements::New
static Pointer New()
otb::ConfusionMatrixCalculator::ConfusionMatrixType
itk::VariableSizeMatrix< unsigned long > ConfusionMatrixType
Definition: otbConfusionMatrixCalculator.h:85
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::ConfusionMatrixCalculator::m_ProducedLabels
ProdListLabelPointerType m_ProducedLabels
Definition: otbConfusionMatrixCalculator.h:181
otb::ConfusionMatrixCalculator::m_NumberOfClasses
unsigned short m_NumberOfClasses
Definition: otbConfusionMatrixCalculator.h:174
otb::ConfusionMatrixCalculator::m_ReferenceLabels
RefListLabelPointerType m_ReferenceLabels
Definition: otbConfusionMatrixCalculator.h:180
otb::ConfusionMatrixCalculator::PrintSelf
void PrintSelf(std::ostream &os, itk::Indent indent) const override
Definition: otbConfusionMatrixCalculator.hxx:151
otb::ConfusionMatrixCalculator::ConfusionMatrixCalculator
ConfusionMatrixCalculator()
Definition: otbConfusionMatrixCalculator.hxx:29
otb::ConfusionMatrixCalculator::Compute
void Compute(void)
Definition: otbConfusionMatrixCalculator.hxx:51
otbMsgDebugMacro
#define otbMsgDebugMacro(x)
Definition: otbMacro.h:62
otb::ConfusionMatrixCalculator::m_ConfusionMatrix
ConfusionMatrixType m_ConfusionMatrix
Definition: otbConfusionMatrixCalculator.h:177
otbConfusionMatrixCalculator.h
otb::ConfusionMatrixCalculator::ClassLabelType
RefListLabelType::ValueType::ValueType ClassLabelType
Definition: otbConfusionMatrixCalculator.h:80
otb::ConfusionMatrixCalculator::m_ConfMatMeasurements
ConfusionMatrixMeasurementsType::Pointer m_ConfMatMeasurements
Definition: otbConfusionMatrixCalculator.h:178