Orfeo Toolbox  4.0
otbStandardDSCostFunction.txx
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: ORFEO Toolbox
4  Language: C++
5  Date: $Date$
6  Version: $Revision$
7 
8 
9  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
10  See OTBCopyright.txt for details.
11 
12 
13  This software is distributed WITHOUT ANY WARRANTY; without even
14  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  PURPOSE. See the above copyright notices for more information.
16 
17 =========================================================================*/
18 #ifndef __otbStandardDSCostFunction_txx
19 #define __otbStandardDSCostFunction_txx
20 
22 
23 namespace otb
24 {
25 // Constructor
26 template <class TDSValidationFilter>
29  m_CriterionFormula("((Belief + Plausibility)/2.)"),
30  m_Weight(0.5)
31 {
32  m_GTVectorData = VectorDataType::New();
33  m_NSVectorData = VectorDataType::New();
34  m_Parser = ParserType::New();
35 }
36 
37 template <class TDSValidationFilter>
38 unsigned int
41  {
42  return m_DescriptorList.size()*4;
43  }
44 
45 template <class TDSValidationFilter>
47 ::MeasureType
49 ::GetValue(const ParametersType & parameters) const
50  {
51  if (parameters.size() != m_DescriptorList.size()*4)
52  {
53  itkExceptionMacro(<< "Wrong model!" )
54  }
55 
56  //Initialize parser
57  m_Parser->SetExpr(m_CriterionFormula);
58 
59  DescriptorsModelType descModel;
60  for (unsigned int i = 0; i < m_DescriptorList.size(); ++i)
61  {
62  std::vector<double> tmp;
63  for (unsigned int j = 0; j < 4; ++j)
64  {
65  tmp.push_back(parameters[4*i+j]);
66  }
67  PairType pair( m_DescriptorList[i], tmp);
68  descModel.push_back(pair);
69  }
70 
71  typename DSValidationFilterType::Pointer internalFunctionGT = DSValidationFilterType::New();
72  internalFunctionGT->SetCriterionFormula("1");
73  internalFunctionGT->SetInput(m_GTVectorData);
74  internalFunctionGT->SetBeliefHypothesis(m_BeliefHypothesis);
75  internalFunctionGT->SetPlausibilityHypothesis(m_PlausibilityHypothesis);
76  try
77  {
78  internalFunctionGT->SetDescriptorModels(descModel);
79  }
80  catch (itk::ExceptionObject /*& err*/)
81  {
82  return 1;
83  }
84  internalFunctionGT->Update();
85 
86  typename DSValidationFilterType::Pointer internalFunctionNS = DSValidationFilterType::New();
87  internalFunctionNS->SetCriterionFormula("1");
88  internalFunctionNS->SetInput(m_NSVectorData);
89  internalFunctionNS->SetBeliefHypothesis(m_BeliefHypothesis);
90  internalFunctionNS->SetBeliefHypothesis(m_PlausibilityHypothesis);
91  try
92  {
93  internalFunctionNS->SetDescriptorModels(descModel);
94  }
95  catch (itk::ExceptionObject &)
96  {
97  return 1;
98  }
99  internalFunctionNS->Update();
100 
101  double accGT = 0.0;
102  double accNS = 0.0;
103  double nGT = 0.0;
104  double nNS = 0.0;
105 
106  TreeIteratorType itVectorGT(internalFunctionGT->GetOutput()->GetDataTree());
107  itVectorGT.GoToBegin();
108  while (!itVectorGT.IsAtEnd())
109  {
110  if (!itVectorGT.Get()->IsRoot() && !itVectorGT.Get()->IsDocument() && !itVectorGT.Get()->IsFolder())
111  {
112  double belief = itVectorGT.Get()->GetFieldAsDouble("Belief");
113  double plausibility = itVectorGT.Get()->GetFieldAsDouble("Plausi");
114 
115  m_Parser->DefineVar("Belief", &belief);
116  m_Parser->DefineVar("Plausibility", &plausibility);
117 
118  accGT += ((1 - m_Parser->Eval()) * (1 - m_Parser->Eval()));
119  nGT += 1.0;
120 
121  m_Parser->ClearVar();
122  }
123  itVectorGT++;
124  }
125 
126  TreeIteratorType itVectorNS(internalFunctionNS->GetOutput()->GetDataTree());
127  itVectorNS.GoToBegin();
128  while (!itVectorNS.IsAtEnd())
129  {
130  if (!itVectorNS.Get()->IsRoot() && !itVectorNS.Get()->IsDocument() && !itVectorNS.Get()->IsFolder())
131  {
132  double belief = itVectorNS.Get()->GetFieldAsDouble("Belief");
133  double plausibility = itVectorNS.Get()->GetFieldAsDouble("Plausi");
134 
135  m_Parser->DefineVar("Belief", &belief);
136  m_Parser->DefineVar("Plausibility", &plausibility);
137 
138  accNS += (m_Parser->Eval() * m_Parser->Eval());
139  nNS += 1.0;
140 
141  m_Parser->ClearVar();
142  }
143  itVectorNS++;
144  }
145  return (m_Weight * accGT / nGT + (1 - m_Weight) * accNS / nNS);
146 }
147 
148 template <class TDSValidationFilter>
149 void
151 ::GetDerivative(const ParametersType & parameters, DerivativeType & derivative) const
152  {
153  //Not necessary for Amoeba Optimizer
154  itkExceptionMacro(<< "Not Supposed to be used when using Amoeba Optimizer!")
155  }
156 
157 // PrintSelf Method
158 template <class TDSValidationFilter>
159 void
161 ::PrintSelf(std::ostream& os, itk::Indent indent) const
162 {
163  Superclass::PrintSelf(os, indent);
164 }
165 
166 
167 }// end namespace otb
168 
169 #endif

Generated at Sat Mar 8 2014 16:19:21 for Orfeo Toolbox with doxygen 1.8.3.1