diff --git a/Modules/Fusion/MajorityVoting/include/otbNeighborhoodMajorityVotingImageFilter.h b/Modules/Fusion/MajorityVoting/include/otbNeighborhoodMajorityVotingImageFilter.h index e858746b7ee17892f31ddfff2a8c34a32aed0edf..075ecf3c711716d58d072d7ac195954692d33203 100644 --- a/Modules/Fusion/MajorityVoting/include/otbNeighborhoodMajorityVotingImageFilter.h +++ b/Modules/Fusion/MajorityVoting/include/otbNeighborhoodMajorityVotingImageFilter.h @@ -169,7 +169,8 @@ protected: const KernelIteratorType kernelEnd) ITK_OVERRIDE; void GenerateOutputInformation() ITK_OVERRIDE; - + + //Type to store the useful information from the label histogram struct HistoSummary { @@ -179,21 +180,22 @@ protected: unsigned int secondFreq; PixelType secondLabel; }; - //Functor sort histogram in decreasing order - struct CompareHistoFequencies + + struct CompareHistoFequencies + { + typedef std::pair<PixelType, unsigned int> HistoValueType; + bool operator()(const HistoValueType& a, const HistoValueType& b) { - typedef std::pair<PixelType, unsigned int> HistoValueType; - bool operator()(const HistoValueType& a, const HistoValueType& b) - { - return a.second > b.second; - } - }; - - //Get a histogram of frequencies of labels with the 2 highest frequencies sorted in decreasing order - // and return the frequency of the label of the center pixel - const HistoSummary ComputeNeighborhoodHistogramSummary(const NeighborhoodIteratorType &nit, - const KernelIteratorType kernelBegin, - const KernelIteratorType kernelEnd) const; + return a.second > b.second; + } + }; + + //Get a histogram of frequencies of labels with the 2 highest + // frequencies sorted in decreasing order and return the frequency + // of the label of the center pixel + const HistoSummary ComputeNeighborhoodHistogramSummary(const NeighborhoodIteratorType &nit, + const KernelIteratorType kernelBegin, + const KernelIteratorType kernelEnd) const; private: NeighborhoodMajorityVotingImageFilter(const Self&); //purposely not implemented diff --git a/Modules/Fusion/MajorityVoting/include/otbNeighborhoodMajorityVotingImageFilter.txx b/Modules/Fusion/MajorityVoting/include/otbNeighborhoodMajorityVotingImageFilter.txx index 68c320f85f02834bbd02bfc2ebac06accd43d409..fa3baa84457f245a56bc246e38a240ef1ec15552 100644 --- a/Modules/Fusion/MajorityVoting/include/otbNeighborhoodMajorityVotingImageFilter.txx +++ b/Modules/Fusion/MajorityVoting/include/otbNeighborhoodMajorityVotingImageFilter.txx @@ -46,12 +46,13 @@ NeighborhoodMajorityVotingImageFilter<TInputImage, TOutputImage, TKernel>::Neigh template<class TInputImage, class TOutputImage, class TKernel> typename NeighborhoodMajorityVotingImageFilter<TInputImage, TOutputImage, TKernel>::PixelType -NeighborhoodMajorityVotingImageFilter<TInputImage, TOutputImage, - TKernel>::Evaluate(const NeighborhoodIteratorType &nit, - const KernelIteratorType kernelBegin, - const KernelIteratorType kernelEnd) +NeighborhoodMajorityVotingImageFilter<TInputImage, + TOutputImage, TKernel>::Evaluate(const NeighborhoodIteratorType &nit, + const KernelIteratorType kernelBegin, + const KernelIteratorType kernelEnd) { const PixelType centerPixel = nit.GetCenterPixel(); + PixelType result = centerPixel; if (centerPixel != m_LabelForNoDataPixels) { //Get a histogram of label frequencies where the 2 highest are at the beginning and sorted @@ -62,32 +63,32 @@ NeighborhoodMajorityVotingImageFilter<TInputImage, TOutputImage, //If we want to filter only isolated pixels, keep the label if //there are enough pixels with the center label to consider that //it is not isolated - return centerPixel; + result = centerPixel; } else { + //Extraction of the more representative Label in the neighborhood (majorityLabel) + result = histoSummary.majorityLabel; //If the majorityLabel is NOT unique in the neighborhood - if(histoSummary.secondFreq == histoSummary.majorityFreq && - histoSummary.secondLabel != histoSummary.majorityLabel) + if(histoSummary.secondFreq == histoSummary.majorityFreq && histoSummary.secondLabel != histoSummary.majorityLabel) { if (m_KeepOriginalLabelBool == true) { - return centerPixel; + result = centerPixel; } else { - return m_LabelForUndecidedPixels; + result = m_LabelForUndecidedPixels; } } } }//END if (centerPixel != m_LabelForNoDataPixels) -//If (centerPixel == m_LabelForNoDataPixels) + //If (centerPixel == m_LabelForNoDataPixels) else { - return m_LabelForNoDataPixels; + result = m_LabelForNoDataPixels; } - - return centerPixel; + return result; } template<class TInputImage, class TOutputImage, class TKernel> @@ -123,15 +124,13 @@ NeighborhoodMajorityVotingImageFilter<TInputImage, TOutputImage, typename NeighborhoodMajorityVotingImageFilter<TInputImage, TOutputImage, TKernel>::HistoSummary result; result.freqCenterLabel = histoNeigh[centerPixel]; - result.majorityFreq = histoNeighVec[0].first; - result.majorityLabel = histoNeighVec[0].second; - result.secondFreq = histoNeighVec[1].first; - result.secondLabel = histoNeighVec[1].second; + result.majorityFreq = histoNeighVec[0].second; + result.majorityLabel = histoNeighVec[0].first; + result.secondFreq = histoNeighVec[1].second; + result.secondLabel = histoNeighVec[1].first; return result; } - - template<class TInputImage, class TOutputImage, class TKernel> void NeighborhoodMajorityVotingImageFilter<TInputImage, TOutputImage, TKernel>