OTB  10.0.0
Orfeo Toolbox
otbBoxAndWhiskerImageFilter.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2024 Centre National d'Etudes Spatiales (CNES)
3  * Copyright (C) 2007-2012 Institut Mines Telecom / Telecom Bretagne
4  *
5  * This file is part of Orfeo Toolbox
6  *
7  * https://www.orfeo-toolbox.org/
8  *
9  * Licensed under the Apache License, Version 2.0 (the "License");
10  * you may not use this file except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  * http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  */
21 
22 #ifndef otbBoxAndWhiskerImageFilter_hxx
23 #define otbBoxAndWhiskerImageFilter_hxx
24 
25 #include <vector>
26 #include <algorithm>
27 #include "otbMacro.h" //for
28 #include "itkConstNeighborhoodIterator.h"
29 #include "itkImageRegionIterator.h"
30 #include "itkNeighborhoodAlgorithm.h"
31 #include "itkProgressReporter.h"
32 
35 
36 namespace otb
37 {
38 
39 template <class TInputImage>
41 {
42  this->SetNumberOfRequiredInputs(1);
43  this->SetNumberOfRequiredOutputs(1);
44  this->InPlaceOn();
45  m_Beta = 1.5;
46  m_Radius.Fill(1);
47  m_NumberFound = 0L;
48 }
49 
50 template <class TInputImage>
52 {
53  const TInputImage* inputPtr = this->GetInput();
54  OutputImageType* outputPtr = this->GetOutput();
55 
56  // data-set boundary faces
57  typedef typename itk::NeighborhoodAlgorithm::ImageBoundaryFacesCalculator<InputImageType> ImageBoundaryFacesCalculatorType;
58  typename ImageBoundaryFacesCalculatorType::FaceListType faceList;
59  ImageBoundaryFacesCalculatorType boundaryCalculator;
60  faceList = boundaryCalculator(inputPtr, outputRegionForThread, this->GetRadius());
61 
62  // face iterator
63  typename ImageBoundaryFacesCalculatorType::FaceListType::iterator faceIterator;
64 
65  // local iterators
66  itk::ImageRegionConstIterator<InputImageType> inputIter;
67  itk::ImageRegionIterator<OutputImageType> outputIter;
68 
73  for (faceIterator = faceList.begin(); faceIterator != faceList.end(); ++faceIterator)
74  {
75  inputIter = itk::ImageRegionConstIterator<InputImageType>(inputPtr, *faceIterator);
76  inputIter.GoToBegin();
78 
79  outputIter = itk::ImageRegionIterator<OutputImageType>(outputPtr, *faceIterator);
80  outputIter.GoToBegin();
81 
82  while (!outputIter.IsAtEnd())
83  {
84  outputIter.Set(this->PerformBoxAndWhiskerDetection(inputIter.Get()));
85 
86  ++inputIter;
87  ++outputIter;
88 
89  }
90  }
91 }
92 
93 template <class TInputImage>
95 {
96  typedef std::vector<ValueType> VectorType;
98 
99  unsigned int i;
100  const unsigned int vectorSize = pixel.Size();
101  const unsigned int medianPosition = vectorSize / 2;
102  const unsigned int firstQuartilePosition = vectorSize / 4;
103  const unsigned int thirdQuartilePosition = (3 * vectorSize) / 4;
104 
105  VectorType data(vectorSize);
106  for (i = 0; i < vectorSize; ++i)
107  data[i] = pixel[i];
108 
109  std::sort(data.begin(), data.end());
110 
111  double box = m_Beta * static_cast<double>(data[thirdQuartilePosition] - data[firstQuartilePosition]);
112  double median = ::fabs(static_cast<double>(data[medianPosition]));
113 
114  PixelType outputPixel(pixel);
115  for (i = 0; i < vectorSize; ++i)
116  {
117  if (::fabs(static_cast<double>(outputPixel[i]) - box) > median)
118  {
119  OutlierType::SetToMissingValue(outputPixel[i]);
120  m_NumberFound++;
121  }
122  }
123 
124  return outputPixel;
125 }
126 
127 template <class TInputImage>
129 {
130  Superclass::GenerateOutputInformation();
131  this->GetOutput()->SetNumberOfComponentsPerPixel(this->GetInput()->GetNumberOfComponentsPerPixel());
132 
133  this->GetOutput()->CopyInformation(this->GetInput());
134 }
135 
136 template <class TInputImage>
138 {
139  OutputImageType* output = this->GetOutput();
140  output->SetNumberOfComponentsPerPixel(this->GetInput()->GetNumberOfComponentsPerPixel());
141  output->SetBufferedRegion(output->GetRequestedRegion());
142  output->Allocate();
143 }
144 
145 } // end of namespace otb
146 
147 #endif
Superclass::OutputImageRegionType OutputImageRegionType
Superclass::OutputImageType OutputImageType
PixelType PerformBoxAndWhiskerDetection(const PixelType &pixel)
void DynamicThreadedGenerateData(const OutputImageRegionType &outputRegionForThread) override
std::vector< double > VectorType
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.