diff --git a/Modules/Fusion/MajorityVoting/include/otbNeighborhoodMajorityVotingImageFilter.h b/Modules/Fusion/MajorityVoting/include/otbNeighborhoodMajorityVotingImageFilter.h index ad285ad9d4c3aba9c1c2824daafa0989d8a8a3be..f20276571aee58470209f60401d1c1cade1c3cfa 100644 --- a/Modules/Fusion/MajorityVoting/include/otbNeighborhoodMajorityVotingImageFilter.h +++ b/Modules/Fusion/MajorityVoting/include/otbNeighborhoodMajorityVotingImageFilter.h @@ -171,6 +171,15 @@ protected: const KernelIteratorType kernelBegin, const KernelIteratorType kernelEnd); + struct CompareHistoFequencies + { + typedef std::pair<PixelType, unsigned int> HistoValueType; + bool operator()(const HistoValueType& a, const HistoValueType& b) + { + return a.second < b.second; + } + }; + private: NeighborhoodMajorityVotingImageFilter(const Self&); //purposely not implemented void operator=(const Self&); //purposely not implemented diff --git a/Modules/Fusion/MajorityVoting/include/otbNeighborhoodMajorityVotingImageFilter.txx b/Modules/Fusion/MajorityVoting/include/otbNeighborhoodMajorityVotingImageFilter.txx index 17236cd82cb43185187d08e703b69ccdebdac4bb..281f8bbd33925c3c7427d1ec8570e8a32b0564e0 100644 --- a/Modules/Fusion/MajorityVoting/include/otbNeighborhoodMajorityVotingImageFilter.txx +++ b/Modules/Fusion/MajorityVoting/include/otbNeighborhoodMajorityVotingImageFilter.txx @@ -50,29 +50,19 @@ const KernelIteratorType kernelEnd) { PixelType majorityLabel = 0; //Value of the more representative pixels in the neighborhood unsigned int majorityFreq = 0; //Number of pixels with the more representative value (majorityLabel) in the neighborhood - - - - - + PixelType centerPixel = nit.GetCenterPixel(); - if (centerPixel != m_LabelForNoDataPixels) { std::map<PixelType, unsigned int> histoNeigh; this->FillNeighborhoodHistogram(histoNeigh, nit, kernelBegin, kernelEnd); //Extraction of the more representative Label in the neighborhood (majorityLabel) and its Frequency (majorityFreq) - typename std::map<PixelType, unsigned int>::iterator histoIt; - for (histoIt = histoNeigh.begin(); histoIt != histoNeigh.end(); ++histoIt) - { - if (histoIt->second > majorityFreq) - { - majorityFreq = histoIt->second; //Frequency - majorityLabel = histoIt->first; //Label - } - } + typename std::map<PixelType, unsigned int>::iterator histoIt + = std::max_element(histoNeigh.begin(), histoNeigh.end(), CompareHistoFequencies()); + majorityFreq = histoIt->second; //Frequency + majorityLabel = histoIt->first; //Label //If the majorityLabel is NOT unique in the neighborhood for (histoIt = histoNeigh.begin(); histoIt != histoNeigh.end(); ++histoIt) @@ -93,10 +83,10 @@ const KernelIteratorType kernelEnd) }//END if (centerPixel != m_LabelForNoDataPixels) //If (centerPixel == m_LabelForNoDataPixels) - else - { - majorityLabel = m_LabelForNoDataPixels; - } + else + { + majorityLabel = m_LabelForNoDataPixels; + } return majorityLabel; }