21 #ifndef otbNeighborhoodMajorityVotingImageFilter_hxx
22 #define otbNeighborhoodMajorityVotingImageFilter_hxx
29 #include "itkDefaultConvertPixelTraits.h"
30 #include "itkMetaDataObject.h"
39 template <
class TInputImage,
class TOutputImage,
class TKernel>
42 this->SetLabelForNoDataPixels(itk::NumericTraits<PixelType>::NonpositiveMin());
43 this->SetLabelForUndecidedPixels(itk::NumericTraits<PixelType>::NonpositiveMin());
44 this->SetKeepOriginalLabelBool(
true);
45 this->SetOnlyIsolatedPixels(
false);
46 this->SetIsolatedThreshold(1);
51 template <
class TInputImage,
class TOutputImage,
class TKernel>
56 const PixelType centerPixel = nit.GetCenterPixel();
57 if (centerPixel == m_LabelForNoDataPixels)
59 return m_LabelForNoDataPixels;
64 const HistoSummary histoSummary = this->ComputeNeighborhoodHistogramSummary(nit, kernelBegin, kernelEnd);
65 if (m_OnlyIsolatedPixels && histoSummary.
freqCenterLabel > m_IsolatedThreshold)
77 if (m_KeepOriginalLabelBool ==
true)
83 return m_LabelForUndecidedPixels;
92 template <
class TInputImage,
class TOutputImage,
class TKernel>
98 typedef std::map<PixelType, unsigned int> HistogramType;
99 typedef std::vector<std::pair<PixelType, unsigned int>> HistoAsVectorType;
100 HistogramType histoNeigh;
102 for (
KernelIteratorType kernel_it = kernelBegin; kernel_it < kernelEnd; ++kernel_it, ++i)
109 if ((*kernel_it > itk::NumericTraits<KernelPixelType>::Zero) && (label != m_LabelForNoDataPixels))
111 histoNeigh[label] += 1;
115 assert(!histoNeigh.empty());
116 if (histoNeigh.size() == 1)
124 HistoAsVectorType histoNeighVec(histoNeigh.begin(), histoNeigh.end());
126 std::nth_element(histoNeighVec.begin(), histoNeighVec.begin() + 1, histoNeighVec.end(),
CompareHistoFequencies());
129 result.
majorityUnique = (histoNeighVec[0].second != histoNeighVec[1].second);
134 template <
class TInputImage,
class TOutputImage,
class TKernel>
137 Superclass::GenerateOutputInformation();
139 TOutputImage* outputPtr = this->GetOutput();
142 const unsigned int& nbBands = outputPtr->GetNumberOfComponentsPerPixel();
143 std::vector<bool> noDataValueAvailable;
144 noDataValueAvailable.resize(nbBands,
true);
145 std::vector<double> noDataValue;
146 noDataValue.resize(nbBands, 0.0);
147 for (
unsigned int i = 0; i < nbBands; ++i)
149 noDataValue[i] = itk::DefaultConvertPixelTraits<PixelType>::GetNthComponent(i, m_LabelForNoDataPixels);
152 WriteNoDataFlags(noDataValueAvailable, noDataValue, outputPtr->GetImageMetadata());