diff --git a/Modules/Fusion/MajorityVoting/include/otbNeighborhoodMajorityVotingImageFilter.h b/Modules/Fusion/MajorityVoting/include/otbNeighborhoodMajorityVotingImageFilter.h index f20276571aee58470209f60401d1c1cade1c3cfa..8d7baf9cdb7c11005b5090a53777f1847de09a5b 100644 --- a/Modules/Fusion/MajorityVoting/include/otbNeighborhoodMajorityVotingImageFilter.h +++ b/Modules/Fusion/MajorityVoting/include/otbNeighborhoodMajorityVotingImageFilter.h @@ -166,7 +166,8 @@ protected: void GenerateOutputInformation() ITK_OVERRIDE; - void FillNeighborhoodHistogram(std::map<PixelType, unsigned int>& histoNeigh, + //Get a histogram of frequencies of labels with the 2 highest frequencies sorted in decreasing order + void FillNeighborhoodHistogram(std::vector<std::pair<PixelType, unsigned int> >& histoNeigh, const NeighborhoodIteratorType &nit, const KernelIteratorType kernelBegin, const KernelIteratorType kernelEnd); @@ -176,7 +177,7 @@ protected: typedef std::pair<PixelType, unsigned int> HistoValueType; bool operator()(const HistoValueType& a, const HistoValueType& b) { - return a.second < b.second; + return a.second > b.second; } }; diff --git a/Modules/Fusion/MajorityVoting/include/otbNeighborhoodMajorityVotingImageFilter.txx b/Modules/Fusion/MajorityVoting/include/otbNeighborhoodMajorityVotingImageFilter.txx index 281f8bbd33925c3c7427d1ec8570e8a32b0564e0..5e35b7db30774be2736f05855f34a1e0de078342 100644 --- a/Modules/Fusion/MajorityVoting/include/otbNeighborhoodMajorityVotingImageFilter.txx +++ b/Modules/Fusion/MajorityVoting/include/otbNeighborhoodMajorityVotingImageFilter.txx @@ -55,50 +55,47 @@ const KernelIteratorType kernelEnd) if (centerPixel != m_LabelForNoDataPixels) { - std::map<PixelType, unsigned int> histoNeigh; - this->FillNeighborhoodHistogram(histoNeigh, nit, kernelBegin, kernelEnd); + std::vector< std::pair<PixelType, unsigned int> > histoNeighVec; + //Get a histogram of label frequencies where the 2 highest are at the beginning and sorted + this->FillNeighborhoodHistogram(histoNeighVec, 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 - = std::max_element(histoNeigh.begin(), histoNeigh.end(), CompareHistoFequencies()); - majorityFreq = histoIt->second; //Frequency - majorityLabel = histoIt->first; //Label + majorityFreq = histoNeighVec[0].second; //Frequency + majorityLabel = histoNeighVec[0].first; //Label //If the majorityLabel is NOT unique in the neighborhood - for (histoIt = histoNeigh.begin(); histoIt != histoNeigh.end(); ++histoIt) + if(histoNeighVec[1].second == majorityFreq && histoNeighVec[1].first != majorityLabel) { - if ( (histoIt->second == majorityFreq) && (histoIt->first != majorityLabel) ) + if (m_KeepOriginalLabelBool == true) { - if (m_KeepOriginalLabelBool == true) - { - majorityLabel = centerPixel; - } - else - { - majorityLabel = m_LabelForUndecidedPixels; - } - break; + majorityLabel = centerPixel; + } + else + { + majorityLabel = m_LabelForUndecidedPixels; } } + }//END if (centerPixel != m_LabelForNoDataPixels) - //If (centerPixel == m_LabelForNoDataPixels) - else - { - majorityLabel = m_LabelForNoDataPixels; - } +//If (centerPixel == m_LabelForNoDataPixels) +else + { + majorityLabel = m_LabelForNoDataPixels; + } - return majorityLabel; +return majorityLabel; } template<class TInputImage, class TOutputImage, class TKernel> void NeighborhoodMajorityVotingImageFilter<TInputImage, TOutputImage, - TKernel>::FillNeighborhoodHistogram(std::map<PixelType, unsigned int>& histoNeigh, + TKernel>::FillNeighborhoodHistogram(std::vector<std::pair<PixelType, unsigned int> >& histoNeighVec, const NeighborhoodIteratorType &nit, const KernelIteratorType kernelBegin, const KernelIteratorType kernelEnd) { + std::map<PixelType, unsigned int> histoNeigh; PixelType centerPixel = nit.GetCenterPixel(); unsigned int i; KernelIteratorType kernel_it; @@ -124,6 +121,10 @@ void NeighborhoodMajorityVotingImageFilter<TInputImage, } } } + std::copy(histoNeigh.begin(), histoNeigh.end(), std::back_inserter(histoNeighVec)); + typename std::vector<std::pair<PixelType, unsigned int> >::iterator histoIt = histoNeighVec.begin(); + std::nth_element(histoNeighVec.begin(), histoIt+1, + histoNeighVec.end(), CompareHistoFequencies()); }