OTB  9.0.0
Orfeo Toolbox
otbLocalRxDetectorFilter.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 
22 #ifndef otbLocalRxDetectorFilter_h
23 #define otbLocalRxDetectorFilter_h
24 
25 #include "itkImageToImageFilter.h"
26 
27 #include "itkListSample.h"
28 #include "itkCovarianceSampleFilter.h"
29 #include "itkConstNeighborhoodIterator.h"
30 #include "otbVectorImage.h"
31 
32 namespace otb
33 {
34 
43 namespace Functor
44 {
45 template <typename TInput, typename TOutput = TInput>
47 {
48 public:
50  typedef typename itk::Neighborhood<itk::VariableLengthVector<TInput>>::OffsetType OffsetType;
51  typedef typename itk::VariableLengthVector<TInput> VectorMeasurementType;
52  typedef itk::Statistics::ListSample<VectorMeasurementType> ListSampleType;
53  typedef itk::Statistics::CovarianceSampleFilter<ListSampleType> CovarianceCalculatorType;
54  typedef typename CovarianceCalculatorType::MeasurementVectorRealType MeasurementVectorRealType;
55  typedef typename CovarianceCalculatorType::MatrixType MatrixType;
56 
57 private:
58  // Internal radius along the X axis
59  unsigned int m_InternalRadiusX;
60 
61  // Internal radius along the Y axis
62  unsigned int m_InternalRadiusY;
63 
64 public:
66 
67  void SetInternalRadius(const unsigned int internalRadiusX, const unsigned int internalRadiusY)
68  {
69  m_InternalRadiusX = internalRadiusX;
70  m_InternalRadiusY = internalRadiusY;
71  };
72 
74  {
75  return m_InternalRadiusX;
76  };
77 
79  {
80  return m_InternalRadiusY;
81  };
82 
83  auto operator()(const itk::ConstNeighborhoodIterator<otb::VectorImage<TInput>>& in) const
84  {
85  // Create a list sample with the pixels of the neighborhood located between
86  // the two radius.
87  typename ListSampleType::Pointer listSample = ListSampleType::New();
88 
89  // The pixel on whih we will compute the Rx score, we load it now to get the input vector size.
90  auto centerPixel = in.GetCenterPixel();
91  listSample->SetMeasurementVectorSize(centerPixel.Size());
92 
93  OffsetType off;
94 
95  auto externalRadius = in.GetRadius();
96 
97  // Cache radiuses attributes for threading performances
98  const int internalRadiusX = m_InternalRadiusX;
99  const int internalRadiusY = m_InternalRadiusY;
100 
101  // Cache radiuses attributes for threading performances
102  const int externalRadiusX = static_cast<int>(externalRadius[0]);
103  const int externalRadiusY = static_cast<int>(externalRadius[0]);
104 
105  for (int y = -externalRadiusY; y <= externalRadiusY; y++)
106  {
107  off[1] = y;
108  for (int x = -externalRadiusX; x <= externalRadiusX; x++)
109  {
110  off[0] = x;
111  if ((abs(x) > internalRadiusX) || (abs(y) > internalRadiusY))
112  {
113  listSample->PushBack(in.GetPixel(off));
114  }
115  }
116  }
117 
118  // Compute mean & inverse covariance matrix
119  typename CovarianceCalculatorType::Pointer covarianceCalculator = CovarianceCalculatorType::New();
120  covarianceCalculator->SetInput(listSample);
121  covarianceCalculator->Update();
122 
123  MeasurementVectorRealType meanVector = covarianceCalculator->GetMean();
124 
125  VectorMeasurementType meanVec(meanVector.GetNumberOfElements());
126  for (unsigned int i = 0; i < meanVector.GetNumberOfElements(); ++i)
127  {
128  meanVec.SetElement(i, meanVector.GetElement(i));
129  }
130 
131  MatrixType covarianceMatrix = covarianceCalculator->GetCovarianceMatrix();
132  typename MatrixType::InternalMatrixType invCovMat = covarianceMatrix.GetInverse();
133 
134  typename MatrixType::InternalMatrixType centeredTestPixMat(meanVector.GetNumberOfElements(), 1);
135 
136  for (unsigned int i = 0; i < centeredTestPixMat.rows(); ++i)
137  {
138  centeredTestPixMat.put(i, 0, (centerPixel.GetElement(i) - meanVector.GetElement(i)));
139  }
140 
141  // Rx score computation
142  typename MatrixType::InternalMatrixType rxValue = centeredTestPixMat.transpose() * invCovMat * centeredTestPixMat;
143 
144  return static_cast<TOutput>(rxValue.get(0, 0));
145  }
146 };
147 
148 } // end namespace functor
149 } // end namespace otb
150 
151 #endif
otb::Functor::LocalRxDetectionFunctor::operator()
auto operator()(const itk::ConstNeighborhoodIterator< otb::VectorImage< TInput >> &in) const
Definition: otbLocalRxDetectorFilter.h:83
otbVectorImage.h
otb::Functor::LocalRxDetectionFunctor::LocalRxDetectionFunctor
LocalRxDetectionFunctor()
Definition: otbLocalRxDetectorFilter.h:65
otb::Functor::LocalRxDetectionFunctor::CovarianceCalculatorType
itk::Statistics::CovarianceSampleFilter< ListSampleType > CovarianceCalculatorType
Definition: otbLocalRxDetectorFilter.h:53
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::Functor::LocalRxDetectionFunctor::m_InternalRadiusY
unsigned int m_InternalRadiusY
Definition: otbLocalRxDetectorFilter.h:62
otb::Functor::LocalRxDetectionFunctor::GetInternalRadiusX
int GetInternalRadiusX()
Definition: otbLocalRxDetectorFilter.h:73
otb::Functor::LocalRxDetectionFunctor::MeasurementVectorRealType
CovarianceCalculatorType::MeasurementVectorRealType MeasurementVectorRealType
Definition: otbLocalRxDetectorFilter.h:54
otb::Functor::LocalRxDetectionFunctor::OffsetType
itk::Neighborhood< itk::VariableLengthVector< TInput > >::OffsetType OffsetType
Definition: otbLocalRxDetectorFilter.h:50
otb::Functor::LocalRxDetectionFunctor::m_InternalRadiusX
unsigned int m_InternalRadiusX
Definition: otbLocalRxDetectorFilter.h:59
otb::Functor::LocalRxDetectionFunctor::GetInternalRadiusY
int GetInternalRadiusY()
Definition: otbLocalRxDetectorFilter.h:78
otb::Functor::LocalRxDetectionFunctor::SetInternalRadius
void SetInternalRadius(const unsigned int internalRadiusX, const unsigned int internalRadiusY)
Definition: otbLocalRxDetectorFilter.h:67
otb::Functor::LocalRxDetectionFunctor::VectorMeasurementType
itk::VariableLengthVector< TInput > VectorMeasurementType
Definition: otbLocalRxDetectorFilter.h:51
otb::Functor::LocalRxDetectionFunctor::ListSampleType
itk::Statistics::ListSample< VectorMeasurementType > ListSampleType
Definition: otbLocalRxDetectorFilter.h:52
otb::Functor::LocalRxDetectionFunctor
Definition: otbLocalRxDetectorFilter.h:46
otb::VectorImage
Creation of an "otb" vector image which contains metadata.
Definition: otbVectorImage.h:45
otb::Functor::LocalRxDetectionFunctor::MatrixType
CovarianceCalculatorType::MatrixType MatrixType
Definition: otbLocalRxDetectorFilter.h:55