1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310
|
/*
* Copyright (C) 2005-2020 Centre National d'Etudes Spatiales (CNES)
*
* This file is part of Orfeo Toolbox
*
* https://www.orfeo-toolbox.org/
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef otbSpectralResponse_hxx
#define otbSpectralResponse_hxx
#include "itkNumericTraits.h"
#include "otbSpectralResponse.h"
#include <algorithm>
#include <fstream>
namespace otb
{
template <class TPrecision, class TValuePrecision>
SpectralResponse<TPrecision, TValuePrecision>::SpectralResponse()
{
m_SensitivityThreshold = 0.01;
m_IntervalComputed = false;
m_PosGuess = 0;
m_UsePosGuess = false;
}
template <class TPrecision, class TValuePrecision>
void SpectralResponse<TPrecision, TValuePrecision>::Load(const std::string& filename, ValuePrecisionType coefNormalization)
{
// Parse JPL file spectral response (ASCII file)
// Begin line 27
std::ifstream fin(filename);
if (fin.fail())
{
itkExceptionMacro(<< "Error opening file" << filename);
}
int NumLigne = 26; // Go to the line 27
// Ignore first 26th lines which are metadatas information
for (int i = 0; i < NumLigne; ++i)
fin.ignore(std::numeric_limits<std::streamsize>::max(), '\n');
while (!fin.eof())
{
// For each
std::pair<TPrecision, TValuePrecision> currentPair;
fin >> currentPair.first;
fin >> currentPair.second;
currentPair.second = currentPair.second / coefNormalization;
if (currentPair.first != itk::NumericTraits<TPrecision>::ZeroValue() && currentPair.second != itk::NumericTraits<TValuePrecision>::ZeroValue())
// Add not null pair of values to the vector
m_Response.push_back(currentPair);
}
fin.close();
// Sort the vector using the specific functor sort_pair
std::sort(m_Response.begin(), m_Response.end(), sort_pair());
m_IntervalComputed = false;
}
template <class TPrecision, class TValuePrecision>
bool SpectralResponse<TPrecision, TValuePrecision>::Clear()
{
m_Response.clear();
m_IntervalComputed = false;
return true;
}
template <class TPrecision, class TValuePrecision>
unsigned int SpectralResponse<TPrecision, TValuePrecision>::Size() const
{
return m_Response.size();
}
template <class TPrecision, class TValuePrecision>
void SpectralResponse<TPrecision, TValuePrecision>::SetPosGuessMin(const PrecisionType& lambda)
{
m_PosGuess = 0;
if (m_Response.size() <= 1)
{
itkExceptionMacro(<< "ERROR spectral response need at least 2 value to perform interpolation.");
}
TPrecision lambdaMax = this->GetInterval().second;
if (lambda > lambdaMax)
return;
typename VectorPairType::const_iterator it = m_Response.begin();
while (((*it).first < lambda))
{
m_PosGuess++;
++it;
if (it == (m_Response.end()))
return;
}
if (m_PosGuess > 0)
m_PosGuess--;
return;
}
template <class TPrecision, class TValuePrecision>
inline typename SpectralResponse<TPrecision, TValuePrecision>::ValuePrecisionType SpectralResponse<TPrecision, TValuePrecision>::
operator()(const PrecisionType& lambda)
{
// Suppose that the vector is sorted
// Guess a starting lambda
if (m_Response.size() <= 1)
{
itkExceptionMacro(<< "ERROR spectral response need at least 2 value to perform interpolation.");
}
typename VectorPairType::const_iterator beg = m_Response.begin();
typename VectorPairType::const_iterator last = m_Response.end();
--last;
TPrecision lambdaMin = this->GetInterval().first;
TPrecision lambdaMax = this->GetInterval().second;
if (lambda < lambdaMin)
return static_cast<TValuePrecision>(0.0);
if (lambda > lambdaMax)
return static_cast<TValuePrecision>(0.0);
typename VectorPairType::const_iterator it;
if (m_UsePosGuess)
it = beg + m_PosGuess;
else
it = beg;
TPrecision lambda1 = (*beg).first;
TValuePrecision SR1 = static_cast<TValuePrecision>(0.0);
while (((*it).first < lambda))
{
lambda1 = (*it).first;
SR1 = (*it).second;
++it;
if (it == (m_Response.end()))
{
return static_cast<TValuePrecision>(0.0);
}
}
TPrecision lambda2 = (*it).first;
// if the guess is just right
if (lambda2 == lambda)
{
return (*it).second;
}
else
{
TPrecision lambdaDist = lambda - lambda1;
TPrecision ratio = lambdaDist / (lambda2 - lambda1);
TValuePrecision SR2 = (*it).second;
return static_cast<TValuePrecision>(ratio * SR1 + (1 - ratio) * SR2);
}
}
template <class TPrecision, class TValuePrecision>
typename SpectralResponse<TPrecision, TValuePrecision>::ImagePointerType SpectralResponse<TPrecision, TValuePrecision>::GetImage(ImagePointerType image) const
{
typename ImageType::IndexType start;
start[0] = 0;
start[1] = 0;
typename ImageType::SizeType size;
size[0] = 1;
size[1] = 1;
typename ImageType::PointType origin;
origin[0] = 0;
origin[1] = 0;
typename ImageType::SpacingType spacing;
spacing[0] = 1;
spacing[1] = 1;
typename ImageType::RegionType region;
region.SetSize(size);
region.SetIndex(start);
image->SetRegions(region);
image->SetNumberOfComponentsPerPixel(this->Size());
image->Allocate();
typename ImageType::IndexType idx;
typename ImageType::PixelType pixel;
pixel.SetSize(this->Size());
for (unsigned int j = 0; j < this->Size(); ++j)
{
pixel[j] = m_Response[j].second;
}
idx[0] = 0;
idx[1] = 0;
image->SetPixel(idx, pixel);
return image;
}
template <class TPrecision, class TValuePrecision>
void SpectralResponse<TPrecision, TValuePrecision>::SetFromImage(ImagePointerType image)
{
typename ImageType::IndexType idx;
idx[0] = 0;
idx[1] = 0;
for (unsigned int j = 0; j < this->Size(); ++j)
{
m_Response[j].second = image->GetPixel(idx)[j];
}
m_IntervalComputed = false;
}
template <class TPrecision, class TValuePrecision>
typename SpectralResponse<TPrecision, TValuePrecision>::FilterFunctionValuesPointerType
SpectralResponse<TPrecision, TValuePrecision>::GetFilterFunctionValues(double step)
{
// Assume that the SR is sorted
typename FilterFunctionValuesType::ValuesVectorType valuesVector;
Self& responseCalculator = *this;
for (double i = m_Response.front().first; i <= m_Response.back().first; i += step)
{
valuesVector.push_back(responseCalculator(i));
}
FilterFunctionValuesPointerType functionValues = FilterFunctionValuesType::New();
functionValues->SetFilterFunctionValues(valuesVector);
functionValues->SetMinSpectralValue(m_Response.front().first);
functionValues->SetMaxSpectralValue(m_Response.back().first);
functionValues->SetUserStep(step);
return functionValues;
}
template <class TPrecision, class TValuePrecision>
void SpectralResponse<TPrecision, TValuePrecision>::ComputeInterval()
{
typename VectorPairType::const_iterator it = m_Response.begin();
while ((*it).second <= m_SensitivityThreshold)
{
++it;
if (it == (m_Response.end() - 1))
{
m_Interval.first = static_cast<TPrecision>(0.0);
m_Interval.second = static_cast<TPrecision>(0.0);
m_IntervalComputed = true;
return;
}
}
m_Interval.first = (*it).first;
it = (m_Response.end() - 1);
while ((*it).second <= m_SensitivityThreshold)
{
if (it == (m_Response.begin()))
{
m_Interval.second = (*it).first;
m_IntervalComputed = true;
return;
}
--it;
}
m_Interval.second = (*it).first;
m_IntervalComputed = true;
}
template <class TPrecision, class TValuePrecision>
void SpectralResponse<TPrecision, TValuePrecision>::PrintSelf(std::ostream& os, itk::Indent indent) const
{
Superclass::PrintSelf(os, indent);
os << std::endl;
os << indent << "[Wavelength (micrometers), Reflectance (percent)]" << std::endl;
for (typename VectorPairType::const_iterator it = m_Response.begin(); it != m_Response.end(); ++it)
{
os << indent << "Num " << it - m_Response.begin() << ": [" << (*it).first << "," << (*it).second << "]" << std::endl;
}
}
} // end namespace otb
#endif
|