OTB  10.0.0
Orfeo Toolbox
otbBayesianFusionFilter.h
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2007 Julien Radoux
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 otbBayesianFusionFilter_h
22 #define otbBayesianFusionFilter_h
23 
24 #include "itkImageToImageFilter.h"
25 #include "otbVectorImage.h"
27 #include "otbFunctorImageFilter.h"
30 
31 namespace otb
32 {
33 
34 namespace Functor
35 {
42 template <class TInputMultiSpectral, class TInputMultiSpectralInterp, class TInputPanchro, class TOutput>
44 {
45 public:
47  {
48  }
49  virtual ~BayesianFunctor()
50  {
51  }
52  typedef typename TInputMultiSpectral::RealValueType RealType;
53  typedef typename itk::VariableSizeMatrix<RealType> MatrixType;
54 
55  void SetLambda(float lambda)
56  {
57  m_Lambda = lambda;
58  }
59  void SetS(float S)
60  {
61  m_S = S;
62  }
63  void SetAlpha(float alpha)
64  {
65  m_Alpha = alpha;
66  }
67  void SetBeta(MatrixType matrix)
68  {
69  m_Beta = matrix;
70  }
72  {
73  m_CovarianceInvMatrix = matrix;
74  }
75  void SetVcondopt(MatrixType matrix)
76  {
77  m_Vcondopt = matrix;
78  }
79  float GetLambda()
80  {
81  return m_Lambda;
82  }
83  float GetAlpha()
84  {
85  return m_Alpha;
86  }
87  float GetS()
88  {
89  return m_S;
90  }
92  {
93  return m_Beta;
94  }
96  {
97  return m_CovarianceInvMatrix;
98  }
100  {
101  return m_Vcondopt;
102  }
103 
104  void operator()(TOutput& obs, const TInputMultiSpectral& itkNotUsed(ms), const TInputMultiSpectralInterp& msi, const TInputPanchro& p)
105  {
106  MatrixType obsMat, msiVect;
107  obsMat.SetSize(1, obs.GetSize());
108  msiVect.SetSize(1, msi.GetSize());
109  for (unsigned int i = 0; i < msi.GetSize(); ++i)
110  {
111  msiVect(0, i) = msi[i];
112  }
113  obsMat = msiVect * m_CovarianceInvMatrix;
114  obsMat *= 2 * (1 - m_Lambda);
115  MatrixType PanVect;
116  PanVect = m_Beta.GetTranspose();
117  PanVect *= (p - m_Alpha);
118  PanVect /= m_S;
119  PanVect *= 2 * m_Lambda;
120 
125  if ((obsMat.Cols() != PanVect.Cols()) || (obsMat.Rows() != PanVect.Rows()))
126  {
127  itkGenericExceptionMacro(<< "Matrix with size (" << obsMat.Rows() << "," << obsMat.Cols() << ") cannot be subtracted from matrix with size ("
128  << PanVect.Rows() << "," << PanVect.Cols() << " )");
129  }
131 
132  for (unsigned int r = 0; r < obsMat.Rows(); ++r)
133  {
134  for (unsigned int c = 0; c < obsMat.Cols(); ++c)
135  {
136  obsMat(r, c) += PanVect(r, c);
137  }
138  }
140  obsMat *= m_Vcondopt;
141  for (unsigned int i = 0; i < obs.GetSize(); ++i)
142  {
143  obs[i] = static_cast<typename TOutput::ValueType>(obsMat(0U, i));
144  }
145  }
147 
148  constexpr size_t OutputSize(const std::array<size_t, 3> inputsNbBands) const
149  {
150  return inputsNbBands[1];
151  }
152 
153 private:
154  float m_Lambda;
155  float m_S;
156  float m_Alpha;
160 };
161 }
162 
163 /***** TODO ***
164  * Complete the description with J. Radoux text
165  */
166 
167 /***** END TODO ***/
168 
194 template <class TInputMultiSpectralImage, class TInputMultiSpectralInterpImage, class TInputPanchroImage, class TOutputImage>
195 class ITK_EXPORT BayesianFusionFilter
196  : public FunctorImageFilter<Functor::BayesianFunctor<typename TInputMultiSpectralImage::PixelType, typename TInputMultiSpectralInterpImage::PixelType,
197  typename TInputPanchroImage::PixelType, typename TOutputImage::PixelType>>
198 {
199 public:
201  itkStaticConstMacro(InputImageDimension, unsigned int, TInputMultiSpectralImage::ImageDimension);
202  itkStaticConstMacro(OutputImageDimension, unsigned int, TOutputImage::ImageDimension);
204 
206  typedef TInputMultiSpectralImage InputMultiSpectralImageType;
207  typedef TInputMultiSpectralInterpImage InputMultiSpectralInterpImageType;
208  typedef TInputPanchroImage InputPanchroImageType;
209  typedef TOutputImage OutputImageType;
210 
213  using BayesianFunctorType = Functor::BayesianFunctor<typename TInputMultiSpectralImage::PixelType, typename TInputMultiSpectralInterpImage::PixelType,
214  typename TInputPanchroImage::PixelType, typename TOutputImage::PixelType>;
216  typedef itk::SmartPointer<Self> Pointer;
217  typedef itk::SmartPointer<const Self> ConstPointer;
218 
220  itkNewMacro(Self);
221 
224 
226  typedef typename InputMultiSpectralImageType::PixelType InputMultiSpectralPixelType;
227  typedef typename InputMultiSpectralImageType::InternalPixelType InputMultiSpectralInternalPixelType;
228  typedef typename InputMultiSpectralInterpImageType::PixelType InputMultiSpectralInterpPixelType;
229  typedef typename InputMultiSpectralInterpImageType::InternalPixelType InputMultiSpectralInterpInternalPixelType;
230  typedef typename InputPanchroImageType::PixelType InputPanchroPixelType;
231  typedef typename OutputImageType::PixelType OutputPixelType;
232  typedef typename OutputImageType::InternalPixelType OutputInternalPixelType;
233 
235  typedef typename itk::NumericTraits<InputPanchroPixelType>::RealType InputPanchroRealType;
236  typedef typename itk::NumericTraits<InputMultiSpectralInternalPixelType>::RealType InputMultiSpectralRealType;
237  typedef typename InputMultiSpectralImageType::RegionType InputMultiSpectralImageRegionType;
238  typedef typename itk::NumericTraits<InputMultiSpectralInterpInternalPixelType>::RealType InputMultiSpectralInterpRealType;
239  typedef typename InputMultiSpectralInterpImageType::RegionType InputMultiSpectralInterpImageRegionType;
240  typedef typename InputPanchroImageType::RegionType InputPanchroImageRegionType;
241  typedef typename OutputImageType::RegionType OutputImageRegionType;
242 
244  typedef typename InputMultiSpectralImageType::SizeType SizeType;
245 
251 
253  {
254  this->template SetInput<0>(multiSpect);
255  }
256 
258  {
259  this->template SetInput<1>(multiSpectInterp);
260  }
261 
262  void SetPanchro(const InputPanchroImageType* panchro)
263  {
264  this->template SetInput<2>(panchro);
265  }
266 
268  {
269  return this->template GetInput<0>();
270  }
271 
273  {
274  return this->template GetInput<1>();
275  }
276 
278  {
279  return this->template GetInput<2>();
280  }
281 
283  itkSetMacro(Lambda, float);
284 
286  itkGetConstReferenceMacro(Lambda, float);
287 
289  itkSetMacro(Beta, MatrixType);
290 
292  itkGetConstReferenceMacro(Beta, MatrixType);
293 
295  itkSetMacro(CovarianceMatrix, MatrixType);
296 
298  itkGetConstReferenceMacro(CovarianceMatrix, MatrixType);
299 
301  itkSetMacro(CovarianceInvMatrix, MatrixType);
302 
304  itkGetConstReferenceMacro(CovarianceInvMatrix, MatrixType);
305 
307  itkSetMacro(Vcondopt, MatrixType);
308 
310  itkGetConstReferenceMacro(Vcondopt, MatrixType);
311 
313  itkSetMacro(S, float);
314 
316  itkGetConstReferenceMacro(S, float);
317 
318 protected:
320  {
321  m_Lambda = 0.9999;
322  m_S = 1;
323  m_StatisticsHaveBeenGenerated = false;
324  };
325 
326  ~BayesianFusionFilter() override{};
328  void BeforeThreadedGenerateData() override;
329 
331  void ComputeInternalStatistics(void);
332 
335  void Modified(void) const override;
336 
337 private:
339  float m_Lambda;
340  float m_S;
341 
344 
347 
350 
353 
356 };
357 
358 } // end namespace otb
359 
360 #ifndef OTB_MANUAL_INSTANTIATION
362 #endif
363 
364 #endif
Bayesian fusion filter. Contribution of Julien Radoux.
InputMultiSpectralInterpImageType::InternalPixelType InputMultiSpectralInterpInternalPixelType
OutputImageType::PixelType OutputPixelType
void SetMultiSpectInterp(const InputMultiSpectralInterpImageType *multiSpectInterp)
StreamingStatisticsVectorImageFilter< InputMultiSpectralInterpImageType > StreamingStatisticsVectorImageFilterType
FunctorImageFilter< BayesianFunctorType > Superclass
OutputImageType::RegionType OutputImageRegionType
itk::NumericTraits< InputPanchroPixelType >::RealType InputPanchroRealType
InputMultiSpectralInterpImageType::PixelType InputMultiSpectralInterpPixelType
InputMultiSpectralInterpImageType::RegionType InputMultiSpectralInterpImageRegionType
InputPanchroImageType::RegionType InputPanchroImageRegionType
InputMultiSpectralImageType::InternalPixelType InputMultiSpectralInternalPixelType
void SetPanchro(const InputPanchroImageType *panchro)
const InputPanchroImageType * GetPanchro()
TInputMultiSpectralImage InputMultiSpectralImageType
itk::SmartPointer< const Self > ConstPointer
ImageToVectorImageCastFilter< InputPanchroImageType, InputMultiSpectralImageType > CasterType
InputMultiSpectralImageType::SizeType SizeType
InputMultiSpectralImageType::RegionType InputMultiSpectralImageRegionType
const InputMultiSpectralInterpImageType * GetMultiSpectInterp()
itk::NumericTraits< InputMultiSpectralInterpInternalPixelType >::RealType InputMultiSpectralInterpRealType
void SetMultiSpect(const InputMultiSpectralImageType *multiSpect)
itk::NumericTraits< InputMultiSpectralInternalPixelType >::RealType InputMultiSpectralRealType
StreamingMatrixTransposeMatrixImageFilter< InputMultiSpectralImageType, InputMultiSpectralImageType > MSTransposeMSType
StreamingStatisticsVectorImageFilterType::MatrixType MatrixType
TInputMultiSpectralInterpImage InputMultiSpectralInterpImageType
OutputImageType::InternalPixelType OutputInternalPixelType
InputMultiSpectralImageType::PixelType InputMultiSpectralPixelType
itk::SmartPointer< Self > Pointer
const InputMultiSpectralImageType * GetMultiSpect()
TInputPanchroImage InputPanchroImageType
InputPanchroImageType::PixelType InputPanchroPixelType
A generic functor filter templated by its functor.
Functor for the bayesian fusion filter. Please refer to BayesianFusionFilter.
itk::VariableSizeMatrix< RealType > MatrixType
void SetCovarianceInvMatrix(MatrixType matrix)
TInputMultiSpectral::RealValueType RealType
void SetVcondopt(MatrixType matrix)
void operator()(TOutput &obs, const TInputMultiSpectral &, const TInputMultiSpectralInterp &msi, const TInputPanchro &p)
constexpr vcl_size_t OutputSize(const std::array< vcl_size_t, 3 > inputsNbBands) const
This is a helper class that convert an otb::Image into a single-channel otb::VectorImage.
This class streams the whole input image through the PersistentMatrixTransposeMatrixImageFilter.
This class streams the whole input image through the PersistentStatisticsImageFilter.
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.