OTB  9.0.0
Orfeo Toolbox
otbBoxAndWhiskerImageFilter.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2005-2022 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 
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>
51 void BoxAndWhiskerImageFilter<TInputImage>::ThreadedGenerateData(const OutputImageRegionType& outputRegionForThread, itk::ThreadIdType threadId)
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 
69  // support progress methods/callbacks
70  itk::ProgressReporter progress(this, threadId, outputRegionForThread.GetNumberOfPixels());
71 
76  for (faceIterator = faceList.begin(); faceIterator != faceList.end(); ++faceIterator)
77  {
78  inputIter = itk::ImageRegionConstIterator<InputImageType>(inputPtr, *faceIterator);
79  inputIter.GoToBegin();
81 
82  outputIter = itk::ImageRegionIterator<OutputImageType>(outputPtr, *faceIterator);
83  outputIter.GoToBegin();
84 
85  while (!outputIter.IsAtEnd())
86  {
87  outputIter.Set(this->PerformBoxAndWhiskerDetection(inputIter.Get()));
88 
89  ++inputIter;
90  ++outputIter;
91 
92  progress.CompletedPixel();
93  }
94  }
95 }
96 
97 template <class TInputImage>
99 {
100  typedef std::vector<ValueType> VectorType;
102 
103  unsigned int i;
104  const unsigned int vectorSize = pixel.Size();
105  const unsigned int medianPosition = vectorSize / 2;
106  const unsigned int firstQuartilePosition = vectorSize / 4;
107  const unsigned int thirdQuartilePosition = (3 * vectorSize) / 4;
108 
109  VectorType data(vectorSize);
110  for (i = 0; i < vectorSize; ++i)
111  data[i] = pixel[i];
112 
113  std::sort(data.begin(), data.end());
114 
115  double box = m_Beta * static_cast<double>(data[thirdQuartilePosition] - data[firstQuartilePosition]);
116  double median = ::fabs(static_cast<double>(data[medianPosition]));
117 
118  PixelType outputPixel(pixel);
119  for (i = 0; i < vectorSize; ++i)
120  {
121  if (::fabs(static_cast<double>(outputPixel[i]) - box) > median)
122  {
123  OutlierType::SetToMissingValue(outputPixel[i]);
124  m_NumberFound++;
125  }
126  }
127 
128  return outputPixel;
129 }
130 
131 template <class TInputImage>
133 {
134  Superclass::GenerateOutputInformation();
135  this->GetOutput()->SetNumberOfComponentsPerPixel(this->GetInput()->GetNumberOfComponentsPerPixel());
136 
137  this->GetOutput()->CopyInformation(this->GetInput());
138 }
139 
140 template <class TInputImage>
142 {
143  OutputImageType* output = this->GetOutput();
144  output->SetNumberOfComponentsPerPixel(this->GetInput()->GetNumberOfComponentsPerPixel());
145  output->SetBufferedRegion(output->GetRequestedRegion());
146  output->Allocate();
147 }
148 
149 } // end of namespace otb
150 
151 #endif
otb::BoxAndWhiskerImageFilter::PerformBoxAndWhiskerDetection
PixelType PerformBoxAndWhiskerDetection(const PixelType &pixel)
Definition: otbBoxAndWhiskerImageFilter.hxx:98
otb::BoxAndWhiskerImageFilter::BoxAndWhiskerImageFilter
BoxAndWhiskerImageFilter()
Definition: otbBoxAndWhiskerImageFilter.hxx:40
otb::BoxAndWhiskerImageFilter::OutputImageRegionType
Superclass::OutputImageRegionType OutputImageRegionType
Definition: otbBoxAndWhiskerImageFilter.h:68
otbBoxAndWhiskerImageFilter.h
otbEuclideanDistanceMetricWithMissingValue.h
otb::BoxAndWhiskerImageFilter::AllocateOutputs
void AllocateOutputs() override
Definition: otbBoxAndWhiskerImageFilter.hxx:141
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::MetaDataKey::VectorType
std::vector< double > VectorType
Definition: otbMetaDataKey.h:119
otb::BoxAndWhiskerImageFilter::PixelType
InputImageType::PixelType PixelType
Definition: otbBoxAndWhiskerImageFilter.h:77
otb::BoxAndWhiskerImageFilter::GenerateOutputInformation
void GenerateOutputInformation() override
Definition: otbBoxAndWhiskerImageFilter.hxx:132
otb::median
Definition: otbParserXPlugins.h:324
otb::Statistics::EuclideanDistanceMetricWithMissingValue
Euclidean distance function facing missing value.
Definition: otbEuclideanDistanceMetricWithMissingValue.h:51
otb::BoxAndWhiskerImageFilter::OutputImageType
Superclass::OutputImageType OutputImageType
Definition: otbBoxAndWhiskerImageFilter.h:63
otb::BoxAndWhiskerImageFilter::ThreadedGenerateData
void ThreadedGenerateData(const OutputImageRegionType &outputRegionForThread, itk::ThreadIdType threadId) override
Definition: otbBoxAndWhiskerImageFilter.hxx:51