OTB  9.0.0
Orfeo Toolbox
otbNeighborhoodMajorityVotingImageFilter.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 #ifndef otbNeighborhoodMajorityVotingImageFilter_h
22 #define otbNeighborhoodMajorityVotingImageFilter_h
23 
24 // First make sure that the configuration is available.
25 // This line can be removed once the optimized versions
26 // gets integrated into the main directories.
27 #include "itkConfigure.h"
28 
29 #include "itkMorphologyImageFilter.h"
30 #include "itkBinaryBallStructuringElement.h"
31 
32 namespace otb
33 {
34 
65 template <class TInputImage, class TOutputImage = TInputImage,
66  class TKernel = typename itk::BinaryBallStructuringElement<typename TInputImage::PixelType, TInputImage::ImageDimension>>
67 class ITK_EXPORT NeighborhoodMajorityVotingImageFilter : public itk::MorphologyImageFilter<TInputImage, TOutputImage, TKernel>
68 {
69 public:
70 
73  typedef itk::MorphologyImageFilter<TInputImage, TOutputImage, TKernel> Superclass;
74  typedef itk::SmartPointer<Self> Pointer;
75  typedef itk::SmartPointer<const Self> ConstPointer;
76 
78  itkNewMacro(Self);
79 
81  itkTypeMacro(NeighborhoodMajorityVotingImageFilter, itk::MorphologyImageFilter);
82 
84  typedef typename Superclass::PixelType PixelType;
85 
86 
88  typedef typename Superclass::KernelIteratorType KernelIteratorType;
89 
91  typedef typename Superclass::NeighborhoodIteratorType NeighborhoodIteratorType;
92 
94  typedef typename Superclass::KernelType KernelType;
95 
96 
98  typedef typename Superclass::DefaultBoundaryConditionType DefaultBoundaryConditionType;
99 
101  itkStaticConstMacro(InputImageDimension, unsigned int, TInputImage::ImageDimension);
102  itkStaticConstMacro(OutputImageDimension, unsigned int, TOutputImage::ImageDimension);
103  itkStaticConstMacro(KernelDimension, unsigned int, TKernel::NeighborhoodDimension);
105 
106 
108  typedef typename TKernel::PixelType KernelPixelType;
109 
110 #ifdef ITK_USE_CONCEPT_CHECKING
111 
112  itkConceptMacro(InputConvertibleToOutputCheck, (itk::Concept::Convertible<PixelType, typename TOutputImage::PixelType>));
113  itkConceptMacro(SameDimensionCheck1, (itk::Concept::SameDimension<InputImageDimension, OutputImageDimension>));
114  itkConceptMacro(SameDimensionCheck2, (itk::Concept::SameDimension<InputImageDimension, KernelDimension>));
115  itkConceptMacro(InputGreaterThanComparableCheck, (itk::Concept::GreaterThanComparable<PixelType>));
116  itkConceptMacro(KernelGreaterThanComparableCheck, (itk::Concept::GreaterThanComparable<KernelPixelType>));
117 
119 #endif
120 
121 
122  // Creates a SetLabelForNoDataPixels method
123  virtual void SetLabelForNoDataPixels(const PixelType _arg)
124  {
125  itkDebugMacro("setting LabelForNoDataPixels to " << _arg);
126  if (this->m_LabelForNoDataPixels != _arg)
127  {
128  this->m_LabelForNoDataPixels = _arg;
129 
130  m_MajorityVotingBoundaryCondition.SetConstant(m_LabelForNoDataPixels);
131  this->OverrideBoundaryCondition(&m_MajorityVotingBoundaryCondition);
132 
133  this->Modified();
134  }
135  }
136 
137  // Creates a SetLabelForUndecidedPixels method
138  itkSetMacro(LabelForUndecidedPixels, PixelType);
139 
140  // Creates a SetKeepOriginalLabelBool method
141  itkSetMacro(KeepOriginalLabelBool, bool);
142 
143  // Process only isolated pixels
144  itkSetMacro(OnlyIsolatedPixels, bool);
145  itkSetMacro(IsolatedThreshold, unsigned int);
146 
147 
148 protected:
151 
158  PixelType Evaluate(const NeighborhoodIteratorType& nit, const KernelIteratorType kernelBegin, const KernelIteratorType kernelEnd) override;
159 
160  void GenerateOutputInformation() override;
161 
162 
163  // Type to store the useful information from the label histogram
165  {
166  unsigned int freqCenterLabel;
169  };
170 
172  {
173  typedef std::pair<PixelType, unsigned int> HistoValueType;
174  bool operator()(const HistoValueType& a, const HistoValueType& b)
175  {
176  return a.second > b.second;
177  }
178  };
179 
180  // Get a histogram of frequencies of labels with the 2 highest
181  // frequencies sorted in decreasing order and return the frequency
182  // of the label of the center pixel
183  const HistoSummary ComputeNeighborhoodHistogramSummary(const NeighborhoodIteratorType& nit, const KernelIteratorType kernelBegin,
184  const KernelIteratorType kernelEnd) const;
185 
186 private:
187  NeighborhoodMajorityVotingImageFilter(const Self&) = delete;
188  void operator=(const Self&) = delete;
189 
190  // Default boundary condition for majority voting filter, defaults to
192 
196 
197  // Choose to filter only isolated pixels
199  // The center pixel is isolated if there are fewer neighbours than
200  // this threshold with the same label
201  unsigned int m_IsolatedThreshold;
202 
203 }; // end of class
204 
205 } // end namespace otb
206 
207 #ifndef ITK_MANUAL_INSTANTIATION
209 #endif
210 
211 
212 #endif
otb::NeighborhoodMajorityVotingImageFilter::CompareHistoFequencies
Definition: otbNeighborhoodMajorityVotingImageFilter.h:171
otb::NeighborhoodMajorityVotingImageFilter::SetLabelForNoDataPixels
virtual void SetLabelForNoDataPixels(const PixelType _arg)
Definition: otbNeighborhoodMajorityVotingImageFilter.h:123
otb::NeighborhoodMajorityVotingImageFilter
Definition: otbNeighborhoodMajorityVotingImageFilter.h:67
otb::NeighborhoodMajorityVotingImageFilter::m_LabelForUndecidedPixels
PixelType m_LabelForUndecidedPixels
Definition: otbNeighborhoodMajorityVotingImageFilter.h:194
otb::NeighborhoodMajorityVotingImageFilter::m_KeepOriginalLabelBool
bool m_KeepOriginalLabelBool
Definition: otbNeighborhoodMajorityVotingImageFilter.h:195
otb::NeighborhoodMajorityVotingImageFilter::NeighborhoodIteratorType
Superclass::NeighborhoodIteratorType NeighborhoodIteratorType
Definition: otbNeighborhoodMajorityVotingImageFilter.h:91
otb::NeighborhoodMajorityVotingImageFilter::m_LabelForNoDataPixels
PixelType m_LabelForNoDataPixels
Definition: otbNeighborhoodMajorityVotingImageFilter.h:193
otb::NeighborhoodMajorityVotingImageFilter::~NeighborhoodMajorityVotingImageFilter
~NeighborhoodMajorityVotingImageFilter() override
Definition: otbNeighborhoodMajorityVotingImageFilter.h:150
otb
The "otb" namespace contains all Orfeo Toolbox (OTB) classes.
Definition: otbJoinContainer.h:32
otb::NeighborhoodMajorityVotingImageFilter::DefaultBoundaryConditionType
Superclass::DefaultBoundaryConditionType DefaultBoundaryConditionType
Definition: otbNeighborhoodMajorityVotingImageFilter.h:98
otb::NeighborhoodMajorityVotingImageFilter::HistoSummary
Definition: otbNeighborhoodMajorityVotingImageFilter.h:164
otb::NeighborhoodMajorityVotingImageFilter::CompareHistoFequencies::operator()
bool operator()(const HistoValueType &a, const HistoValueType &b)
Definition: otbNeighborhoodMajorityVotingImageFilter.h:174
otb::NeighborhoodMajorityVotingImageFilter::ConstPointer
itk::SmartPointer< const Self > ConstPointer
Definition: otbNeighborhoodMajorityVotingImageFilter.h:75
otbNeighborhoodMajorityVotingImageFilter.hxx
otb::NeighborhoodMajorityVotingImageFilter::KernelType
Superclass::KernelType KernelType
Definition: otbNeighborhoodMajorityVotingImageFilter.h:94
otb::NeighborhoodMajorityVotingImageFilter::CompareHistoFequencies::HistoValueType
std::pair< PixelType, unsigned int > HistoValueType
Definition: otbNeighborhoodMajorityVotingImageFilter.h:173
otb::NeighborhoodMajorityVotingImageFilter::HistoSummary::majorityLabel
PixelType majorityLabel
Definition: otbNeighborhoodMajorityVotingImageFilter.h:167
otb::NeighborhoodMajorityVotingImageFilter::m_MajorityVotingBoundaryCondition
DefaultBoundaryConditionType m_MajorityVotingBoundaryCondition
Definition: otbNeighborhoodMajorityVotingImageFilter.h:191
otb::NeighborhoodMajorityVotingImageFilter::HistoSummary::majorityUnique
bool majorityUnique
Definition: otbNeighborhoodMajorityVotingImageFilter.h:168
otb::NeighborhoodMajorityVotingImageFilter::Self
NeighborhoodMajorityVotingImageFilter Self
Definition: otbNeighborhoodMajorityVotingImageFilter.h:72
otb::NeighborhoodMajorityVotingImageFilter::m_OnlyIsolatedPixels
bool m_OnlyIsolatedPixels
Definition: otbNeighborhoodMajorityVotingImageFilter.h:198
otb::NeighborhoodMajorityVotingImageFilter::Superclass
itk::MorphologyImageFilter< TInputImage, TOutputImage, TKernel > Superclass
Definition: otbNeighborhoodMajorityVotingImageFilter.h:73
otb::NeighborhoodMajorityVotingImageFilter::HistoSummary::freqCenterLabel
unsigned int freqCenterLabel
Definition: otbNeighborhoodMajorityVotingImageFilter.h:166
otb::NeighborhoodMajorityVotingImageFilter::PixelType
Superclass::PixelType PixelType
Definition: otbNeighborhoodMajorityVotingImageFilter.h:81
otb::NeighborhoodMajorityVotingImageFilter::m_IsolatedThreshold
unsigned int m_IsolatedThreshold
Definition: otbNeighborhoodMajorityVotingImageFilter.h:201
otb::NeighborhoodMajorityVotingImageFilter::KernelIteratorType
Superclass::KernelIteratorType KernelIteratorType
Definition: otbNeighborhoodMajorityVotingImageFilter.h:88
otb::NeighborhoodMajorityVotingImageFilter::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbNeighborhoodMajorityVotingImageFilter.h:74
otb::NeighborhoodMajorityVotingImageFilter::KernelPixelType
TKernel::PixelType KernelPixelType
Definition: otbNeighborhoodMajorityVotingImageFilter.h:108