OTB  6.1.0
Orfeo Toolbox
otbStandardDSCostFunction.txx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2017 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 otbStandardDSCostFunction_txx
22 #define otbStandardDSCostFunction_txx
23 
25 
26 namespace otb
27 {
28 // Constructor
29 template <class TDSValidationFilter>
32  m_CriterionFormula("((Belief + Plausibility)/2.)"),
33  m_Weight(0.5)
34 {
35  m_GTVectorData = VectorDataType::New();
36  m_NSVectorData = VectorDataType::New();
37  m_Parser = ParserType::New();
38 }
39 
40 template <class TDSValidationFilter>
41 unsigned int
44  {
45  return m_DescriptorList.size()*4;
46  }
47 
48 template <class TDSValidationFilter>
50 ::MeasureType
52 ::GetValue(const ParametersType & parameters) const
53  {
54  if (parameters.size() != m_DescriptorList.size()*4)
55  {
56  itkExceptionMacro(<< "Wrong model!" )
57  }
58 
59  //Initialize parser
60  m_Parser->SetExpr(m_CriterionFormula);
61 
62  DescriptorsModelType descModel;
63  for (unsigned int i = 0; i < m_DescriptorList.size(); ++i)
64  {
65  std::vector<double> tmp;
66  for (unsigned int j = 0; j < 4; ++j)
67  {
68  tmp.push_back(parameters[4*i+j]);
69  }
70  PairType pair( m_DescriptorList[i], tmp);
71  descModel.push_back(pair);
72  }
73 
74  typename DSValidationFilterType::Pointer internalFunctionGT = DSValidationFilterType::New();
75  internalFunctionGT->SetCriterionFormula("1");
76  internalFunctionGT->SetInput(m_GTVectorData);
77  internalFunctionGT->SetBeliefHypothesis(m_BeliefHypothesis);
78  internalFunctionGT->SetPlausibilityHypothesis(m_PlausibilityHypothesis);
79  try
80  {
81  internalFunctionGT->SetDescriptorModels(descModel);
82  }
83  catch (itk::ExceptionObject /*& err*/)
84  {
85  return 1;
86  }
87  internalFunctionGT->Update();
88 
89  typename DSValidationFilterType::Pointer internalFunctionNS = DSValidationFilterType::New();
90  internalFunctionNS->SetCriterionFormula("1");
91  internalFunctionNS->SetInput(m_NSVectorData);
92  internalFunctionNS->SetBeliefHypothesis(m_BeliefHypothesis);
93  internalFunctionNS->SetBeliefHypothesis(m_PlausibilityHypothesis);
94  try
95  {
96  internalFunctionNS->SetDescriptorModels(descModel);
97  }
98  catch (itk::ExceptionObject &)
99  {
100  return 1;
101  }
102  internalFunctionNS->Update();
103 
104  double accGT = 0.0;
105  double accNS = 0.0;
106  double nGT = 0.0;
107  double nNS = 0.0;
108 
109  TreeIteratorType itVectorGT(internalFunctionGT->GetOutput()->GetDataTree());
110  itVectorGT.GoToBegin();
111  while (!itVectorGT.IsAtEnd())
112  {
113  if (!itVectorGT.Get()->IsRoot() && !itVectorGT.Get()->IsDocument() && !itVectorGT.Get()->IsFolder())
114  {
115  double belief = itVectorGT.Get()->GetFieldAsDouble("Belief");
116  double plausibility = itVectorGT.Get()->GetFieldAsDouble("Plausi");
117 
118  m_Parser->DefineVar("Belief", &belief);
119  m_Parser->DefineVar("Plausibility", &plausibility);
120 
121  accGT += ((1 - m_Parser->Eval()) * (1 - m_Parser->Eval()));
122  nGT += 1.0;
123 
124  m_Parser->ClearVar();
125  }
126  itVectorGT++;
127  }
128 
129  TreeIteratorType itVectorNS(internalFunctionNS->GetOutput()->GetDataTree());
130  itVectorNS.GoToBegin();
131  while (!itVectorNS.IsAtEnd())
132  {
133  if (!itVectorNS.Get()->IsRoot() && !itVectorNS.Get()->IsDocument() && !itVectorNS.Get()->IsFolder())
134  {
135  double belief = itVectorNS.Get()->GetFieldAsDouble("Belief");
136  double plausibility = itVectorNS.Get()->GetFieldAsDouble("Plausi");
137 
138  m_Parser->DefineVar("Belief", &belief);
139  m_Parser->DefineVar("Plausibility", &plausibility);
140 
141  accNS += (m_Parser->Eval() * m_Parser->Eval());
142  nNS += 1.0;
143 
144  m_Parser->ClearVar();
145  }
146  itVectorNS++;
147  }
148  return (m_Weight * accGT / nGT + (1 - m_Weight) * accNS / nNS);
149 }
150 
151 template <class TDSValidationFilter>
152 void
154 ::GetDerivative(const ParametersType & itkNotUsed(parameters), DerivativeType & itkNotUsed(derivative)) const
155  {
156  //Not necessary for Amoeba Optimizer
157  itkExceptionMacro(<< "Not Supposed to be used when using Amoeba Optimizer!")
158  }
159 
160 // PrintSelf Method
161 template <class TDSValidationFilter>
162 void
164 ::PrintSelf(std::ostream& os, itk::Indent indent) const
165 {
166  Superclass::PrintSelf(os, indent);
167 }
168 
169 
170 }// end namespace otb
171 
172 #endif
void PrintSelf(std::ostream &os, itk::Indent indent) const ITK_OVERRIDE
unsigned int GetNumberOfParameters(void) const ITK_OVERRIDE
void GetDerivative(const ParametersType &parameters, DerivativeType &derivative) const ITK_OVERRIDE
MeasureType GetValue(const ParametersType &parameters) const ITK_OVERRIDE
FuzzyDescriptorsModelManager::PairType PairType
DSValidationFilterType::TreeIteratorType TreeIteratorType
Standard Cost Function used to estimate the fuzzy model parameters in the Dempster-Shafer framework...
FuzzyDescriptorsModelManager::DescriptorsModelType DescriptorsModelType