OTB  9.0.0
Orfeo Toolbox
otbBCOInterpolateImageFunction.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 otbBCOInterpolateImageFunction_hxx
22 #define otbBCOInterpolateImageFunction_hxx
23 
25 
26 #include "itkNumericTraits.h"
27 
28 namespace otb
29 {
30 
31 template <class TInputImage, class TCoordRep>
32 void BCOInterpolateImageFunctionBase<TInputImage, TCoordRep>::PrintSelf(std::ostream& os, itk::Indent indent) const
33 {
34  Superclass::PrintSelf(os, indent);
35  os << indent << "Radius: " << m_Radius << std::endl;
36  os << indent << "Alpha: " << m_Alpha << std::endl;
37 }
38 
39 template <class TInputImage, class TCoordRep>
41 {
42  if (radius < 2)
43  {
44  itkExceptionMacro(<< "Radius must be strictly greater than 1");
45  }
46  else
47  {
48  m_Radius = radius;
49  m_WinSize = 2 * m_Radius + 1;
50  }
51 }
52 
53 template <class TInputImage, class TCoordRep>
55 {
56  return m_Radius;
57 }
58 
59 template <class TInputImage, class TCoordRep>
61 {
62  m_Alpha = alpha;
63 }
64 
65 template <class TInputImage, class TCoordRep>
67 {
68  return m_Alpha;
69 }
70 
71 template <class TInputImage, class TCoordRep>
74 {
75  double offset, dist, position, step;
76 
78 
79  offset = indexValue - itk::Math::Floor<IndexValueType>(indexValue + 0.5);
80 
81  // Compute BCO coefficients
82  step = 4. / static_cast<double>(2 * m_Radius);
83  position = -static_cast<double>(m_Radius) * step;
84 
85  double sum = 0.0;
86  auto alpha = this->m_Alpha;
87 
88  for (unsigned int i = 0; i < m_WinSize; ++i)
89  {
90  // Compute the BCO coefficients according to alpha.
91  dist = std::abs(position - offset * step);
92 
93  double coef;
94  if (dist <= 2.)
95  {
96  if (dist <= 1.)
97  {
98  coef = (alpha + 2.) * dist * dist * dist - (alpha + 3.) * dist * dist + 1;
99  }
100  else
101  {
102  coef = alpha * dist * dist * dist - 5 * alpha * dist * dist + 8 * alpha * dist - 4 * alpha;
103  }
104  }
105  else
106  {
107  coef = 0.0;
108  }
109 
110  sum += coef;
111  position += step;
112 
113  bcoCoef[i] = coef;
114  }
115 
116  for (unsigned int i = 0; i < m_WinSize; ++i)
117  bcoCoef[i] /= sum;
118 
119  return bcoCoef;
120 }
121 
122 template <class TInputImage, class TCoordRep>
123 void BCOInterpolateImageFunction<TInputImage, TCoordRep>::PrintSelf(std::ostream& os, itk::Indent indent) const
124 {
125  Superclass::PrintSelf(os, indent);
126 }
127 
128 template <class TInputImage, class TCoordRep>
131 {
132  unsigned int dim;
133 
134  IndexType baseIndex;
135  IndexType neighIndex;
136 
137  RealType value = itk::NumericTraits<RealType>::Zero;
138 
139  const auto& BCOCoefX = this->EvaluateCoef(index[0]);
140  const auto& BCOCoefY = this->EvaluateCoef(index[1]);
141 
142  // Compute base index = closet index
143  for (dim = 0; dim < ImageDimension; dim++)
144  {
145  baseIndex[dim] = itk::Math::Floor<IndexValueType>(index[dim] + 0.5);
146  }
147 
148  for (unsigned int i = 0; i < this->m_WinSize; ++i)
149  {
150  RealType lineRes = 0.;
151  for (unsigned int j = 0; j < this->m_WinSize; ++j)
152  {
153  // get neighbor index
154  neighIndex[0] = baseIndex[0] + i - this->m_Radius;
155  neighIndex[1] = baseIndex[1] + j - this->m_Radius;
156 
157  if (neighIndex[0] > this->m_EndIndex[0])
158  {
159  neighIndex[0] = this->m_EndIndex[0];
160  }
161  if (neighIndex[0] < this->m_StartIndex[0])
162  {
163  neighIndex[0] = this->m_StartIndex[0];
164  }
165  if (neighIndex[1] > this->m_EndIndex[1])
166  {
167  neighIndex[1] = this->m_EndIndex[1];
168  }
169  if (neighIndex[1] < this->m_StartIndex[1])
170  {
171  neighIndex[1] = this->m_StartIndex[1];
172  }
173  lineRes += static_cast<RealType>(this->GetInputImage()->GetPixel(neighIndex)) * BCOCoefY[j];
174  }
175  value += lineRes * BCOCoefX[i];
176  }
177 
178 
179  return (static_cast<OutputType>(value));
180 }
181 
182 template <typename TPixel, unsigned int VImageDimension, class TCoordRep>
183 void BCOInterpolateImageFunction<otb::VectorImage<TPixel, VImageDimension>, TCoordRep>::PrintSelf(std::ostream& os, itk::Indent indent) const
184 {
185  Superclass::PrintSelf(os, indent);
186 }
187 
188 template <typename TPixel, unsigned int VImageDimension, class TCoordRep>
190 BCOInterpolateImageFunction<otb::VectorImage<TPixel, VImageDimension>, TCoordRep>::EvaluateAtContinuousIndex(const ContinuousIndexType& index) const
191 {
192  typedef typename itk::NumericTraits<InputPixelType>::ScalarRealType ScalarRealType;
193 
194 
195  unsigned int dim;
196  unsigned int componentNumber = this->GetInputImage()->GetNumberOfComponentsPerPixel();
197 
198  IndexType baseIndex;
199  IndexType neighIndex;
200 
201 
202 #if BOOST_VERSION >= 105800
203  // faster path for <= 8 components
204  boost::container::small_vector<ScalarRealType, 8> lineRes(componentNumber);
205 #else
206  std::vector<ScalarRealType> lineRes(componentNumber);
207 #endif
208 
209  OutputType output(componentNumber);
210  output.Fill(itk::NumericTraits<ScalarRealType>::Zero);
211 
212  const auto& BCOCoefX = this->EvaluateCoef(index[0]);
213  const auto& BCOCoefY = this->EvaluateCoef(index[1]);
214 
215  // Compute base index = closet index
216  for (dim = 0; dim < ImageDimension; dim++)
217  {
218  baseIndex[dim] = itk::Math::Floor<IndexValueType>(index[dim] + 0.5);
219  }
220 
221  for (unsigned int i = 0; i < this->m_WinSize; ++i)
222  {
223  std::fill(lineRes.begin(), lineRes.end(), itk::NumericTraits<ScalarRealType>::Zero);
224  for (unsigned int j = 0; j < this->m_WinSize; ++j)
225  {
226  // get neighbor index
227  neighIndex[0] = baseIndex[0] + i - this->m_Radius;
228  neighIndex[1] = baseIndex[1] + j - this->m_Radius;
229  if (neighIndex[0] > this->m_EndIndex[0])
230  {
231  neighIndex[0] = this->m_EndIndex[0];
232  }
233  if (neighIndex[0] < this->m_StartIndex[0])
234  {
235  neighIndex[0] = this->m_StartIndex[0];
236  }
237  if (neighIndex[1] > this->m_EndIndex[1])
238  {
239  neighIndex[1] = this->m_EndIndex[1];
240  }
241  if (neighIndex[1] < this->m_StartIndex[1])
242  {
243  neighIndex[1] = this->m_StartIndex[1];
244  }
245 
246  const InputPixelType& pixel = this->GetInputImage()->GetPixel(neighIndex);
247  for (unsigned int k = 0; k < componentNumber; ++k)
248  {
249  lineRes[k] += pixel.GetElement(k) * BCOCoefY[j];
250  }
251  }
252  for (unsigned int k = 0; k < componentNumber; ++k)
253  {
254  output[k] += lineRes[k] * BCOCoefX[i];
255  }
256  }
257 
258  return (output);
259 }
260 
261 } // namespace otb
262 
263 #endif
otb::BCOInterpolateImageFunction< otb::VectorImage< TPixel, VImageDimension >, TCoordRep >::InputPixelType
Superclass::InputPixelType InputPixelType
Definition: otbBCOInterpolateImageFunction.h:204
otb::BCOInterpolateImageFunctionBase::GetRadius
virtual unsigned int GetRadius() const
Definition: otbBCOInterpolateImageFunction.hxx:54
otb::BCOInterpolateImageFunction::IndexType
Superclass::IndexType IndexType
Definition: otbBCOInterpolateImageFunction.h:168
otb::BCOInterpolateImageFunctionBase::EvaluateCoef
CoefContainerType EvaluateCoef(const ContinuousIndexValueType &indexValue) const
Definition: otbBCOInterpolateImageFunction.hxx:73
otb::BCOInterpolateImageFunction::RealType
Superclass::RealType RealType
Definition: otbBCOInterpolateImageFunction.h:167
otb::BCOInterpolateImageFunction::EvaluateAtContinuousIndex
OutputType EvaluateAtContinuousIndex(const ContinuousIndexType &index) const override
Definition: otbBCOInterpolateImageFunction.hxx:130
otb::BCOInterpolateImageFunction< otb::VectorImage< TPixel, VImageDimension >, TCoordRep >::IndexType
Superclass::IndexType IndexType
Definition: otbBCOInterpolateImageFunction.h:206
otb::BCOInterpolateImageFunctionBase::SetRadius
virtual void SetRadius(unsigned int radius)
Definition: otbBCOInterpolateImageFunction.hxx:40
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otbBCOInterpolateImageFunction.h
otb::BCOInterpolateImageFunction< otb::VectorImage< TPixel, VImageDimension >, TCoordRep >::OutputType
Superclass::OutputType OutputType
Definition: otbBCOInterpolateImageFunction.h:202
otb::BCOInterpolateImageFunction< otb::VectorImage< TPixel, VImageDimension >, TCoordRep >::ContinuousIndexType
Superclass::ContinuousIndexType ContinuousIndexType
Definition: otbBCOInterpolateImageFunction.h:209
otb::BCOInterpolateImageFunctionBase::CoefContainerType
vnl_vector< double > CoefContainerType
Definition: otbBCOInterpolateImageFunction.h:105
otb::BCOInterpolateImageFunctionBase::GetAlpha
virtual double GetAlpha() const
Definition: otbBCOInterpolateImageFunction.hxx:66
otb::BCOInterpolateImageFunction
Interpolate an image at specified positions using bicubic interpolation.
Definition: otbBCOInterpolateImageFunction.h:151
otb::BCOInterpolateImageFunction::PrintSelf
void PrintSelf(std::ostream &os, itk::Indent indent) const override
Definition: otbBCOInterpolateImageFunction.hxx:123
otb::BCOInterpolateImageFunctionBase::SetAlpha
virtual void SetAlpha(double alpha)
Definition: otbBCOInterpolateImageFunction.hxx:60
otb::BCOInterpolateImageFunctionBase< otb::VectorImage< TPixel, VImageDimension >, TCoordRep >::ContinuousIndexValueType
TCoordRep ContinuousIndexValueType
Definition: otbBCOInterpolateImageFunction.h:96
otb::BCOInterpolateImageFunction::OutputType
Superclass::OutputType OutputType
Definition: otbBCOInterpolateImageFunction.h:164
otb::BCOInterpolateImageFunctionBase::PrintSelf
void PrintSelf(std::ostream &os, itk::Indent indent) const override
Definition: otbBCOInterpolateImageFunction.hxx:32
otb::BCOInterpolateImageFunction::ContinuousIndexType
Superclass::ContinuousIndexType ContinuousIndexType
Definition: otbBCOInterpolateImageFunction.h:171