OTB
9.0.0
Orfeo Toolbox
Modules
Core
ImageManipulation
include
otbSpectralInformationDivergenceFunctor.h
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 otbSpectralInformationDivergenceFunctor_h
22
#define otbSpectralInformationDivergenceFunctor_h
23
24
#include "
otbMath.h
"
25
#include <algorithm>
26
#include <vector>
27
#include <numeric>
28
#include <stdexcept>
29
30
namespace
otb
31
{
32
namespace
Functor
33
{
34
44
template
<
class
TInput,
class
TReference,
class
TOutput>
45
class
SpectralInformationDivergenceFunctor
46
{
47
public
:
48
SpectralInformationDivergenceFunctor
() =
default
;
49
virtual
~SpectralInformationDivergenceFunctor
() =
default
;
50
51
using
OutputValueType
=
typename
TOutput::ValueType;
52
53
// Binary operator
54
inline
TOutput
operator()
(
const
TInput& input)
const
55
{
56
TOutput res;
57
res.SetSize(
m_ReferenceProbabilities
.size());
58
59
auto
inputProbability =
ComputeProbabilityMassFunction
(input);
60
61
for
(
unsigned
int
i = 0; i<
m_ReferenceProbabilities
.size(); i++)
62
{
63
res[i] =
ComputeSpectralInformationDivergence
(inputProbability,
m_ReferenceProbabilities
[i]);
64
}
65
66
return
res;
67
}
68
69
size_t
OutputSize
(...)
const
70
{
71
return
m_ReferenceProbabilities
.size();
72
}
73
74
void
SetReferencePixels
(std::vector<TReference>
const
& ref)
75
{
76
// We only store the probability mass function associated with the reference pixels, are the latter are not needed
77
// in the sid computation.
78
m_ReferenceProbabilities
.clear();
79
for
(
auto
const
& pixel : ref)
80
{
81
m_ReferenceProbabilities
.push_back(
ComputeProbabilityMassFunction
(pixel));
82
}
83
}
84
85
private
:
86
inline
TInput
ComputeProbabilityMassFunction
(TInput
const
& input)
const
87
{
88
for
(
unsigned
int
i = 0; i < input.Size(); i++)
89
{
90
// Input pixel should be non negative (e.g. reflectance, radiance)
91
if
(input[i] <= 0)
92
{
93
throw
std::runtime_error(
"Input pixels of the spectral information divergence algorithm should be strictly positive."
);
94
}
95
}
96
97
return
input / std::accumulate(&input[0], &input[input.Size()], 0.0);
98
}
99
100
inline
OutputValueType
ComputeSpectralInformationDivergence
(TInput
const
& p, TInput
const
& q)
const
101
{
102
assert(p.Size() == q.Size());
103
OutputValueType
sid = 0.0;
104
for
(
unsigned
int
i = 0; i < p.Size(); i++)
105
{
106
// Compute SID : p[i] * std::log(p[i]/q[i]) + q[i] * std::log(q[i]/p[i]);
107
sid += (p[i] - q[i]) * std::log(p[i]/q[i]);
108
}
109
return
sid;
110
}
111
113
std::vector<TReference>
m_ReferenceProbabilities
;
114
};
115
116
}
// end namespace functor
117
}
// end namespace otb
118
119
#endif //otbSpectralInformationDivergenceFunctor_h
otb::Functor::SpectralInformationDivergenceFunctor::ComputeSpectralInformationDivergence
OutputValueType ComputeSpectralInformationDivergence(TInput const &p, TInput const &q) const
Definition:
otbSpectralInformationDivergenceFunctor.h:100
otb::Functor::SpectralInformationDivergenceFunctor::OutputSize
vcl_size_t OutputSize(...) const
Definition:
otbSpectralInformationDivergenceFunctor.h:69
otb::Functor::SpectralInformationDivergenceFunctor::OutputValueType
typename TOutput::ValueType OutputValueType
Definition:
otbSpectralInformationDivergenceFunctor.h:51
otbMath.h
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition:
otbJoinContainer.h:32
otb::Functor::SpectralInformationDivergenceFunctor::SetReferencePixels
void SetReferencePixels(std::vector< TReference > const &ref)
Definition:
otbSpectralInformationDivergenceFunctor.h:74
otb::Functor::SpectralInformationDivergenceFunctor::ComputeProbabilityMassFunction
TInput ComputeProbabilityMassFunction(TInput const &input) const
Definition:
otbSpectralInformationDivergenceFunctor.h:86
otb::Functor::SpectralInformationDivergenceFunctor::~SpectralInformationDivergenceFunctor
virtual ~SpectralInformationDivergenceFunctor()=default
otb::Functor::SpectralInformationDivergenceFunctor
Definition:
otbSpectralInformationDivergenceFunctor.h:45
otb::Functor::SpectralInformationDivergenceFunctor::SpectralInformationDivergenceFunctor
SpectralInformationDivergenceFunctor()=default
otb::Functor::SpectralInformationDivergenceFunctor::m_ReferenceProbabilities
std::vector< TReference > m_ReferenceProbabilities
Definition:
otbSpectralInformationDivergenceFunctor.h:113
otb::Functor::SpectralInformationDivergenceFunctor::operator()
TOutput operator()(const TInput &input) const
Definition:
otbSpectralInformationDivergenceFunctor.h:54
Generated at Wed Feb 28 2024 15:23:52 for
OTB
with
1.8.17