diff --git a/Modules/Fusion/MajorityVoting/include/otbNeighborhoodMajorityVotingImageFilter.h b/Modules/Fusion/MajorityVoting/include/otbNeighborhoodMajorityVotingImageFilter.h index f7ce6b84e5e25ebbf74c5c3d1cd517dc8a43fce5..ad285ad9d4c3aba9c1c2824daafa0989d8a8a3be 100644 --- a/Modules/Fusion/MajorityVoting/include/otbNeighborhoodMajorityVotingImageFilter.h +++ b/Modules/Fusion/MajorityVoting/include/otbNeighborhoodMajorityVotingImageFilter.h @@ -166,6 +166,11 @@ protected: void GenerateOutputInformation() ITK_OVERRIDE; + void FillNeighborhoodHistogram(std::map<PixelType, unsigned int>& histoNeigh, + const NeighborhoodIteratorType &nit, + const KernelIteratorType kernelBegin, + const KernelIteratorType kernelEnd); + 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 397d3876e6dd1879ba21d9fd1174e0837dd9d5ea..17236cd82cb43185187d08e703b69ccdebdac4bb 100644 --- a/Modules/Fusion/MajorityVoting/include/otbNeighborhoodMajorityVotingImageFilter.txx +++ b/Modules/Fusion/MajorityVoting/include/otbNeighborhoodMajorityVotingImageFilter.txx @@ -53,75 +53,92 @@ const KernelIteratorType kernelEnd) - std::map<PixelType, unsigned int> histoNeigh; + PixelType centerPixel = nit.GetCenterPixel(); + if (centerPixel != m_LabelForNoDataPixels) - { - unsigned int i; - KernelIteratorType kernel_it; - for (i = 0, kernel_it = kernelBegin; kernel_it < kernelEnd; ++kernel_it, ++i) { - // if structuring element is positive, use the pixel under that element - // in the image - - PixelType label = nit.GetPixel(i); - if ((*kernel_it > itk::NumericTraits<KernelPixelType>::Zero) && (label != m_LabelForNoDataPixels)) - { - // note we use GetPixel() on the SmartNeighborhoodIterator to - // respect boundary conditions - - //If the current label has already been added to the histogram histoNeigh - if (histoNeigh.count(label) > 0) - { - histoNeigh[label]++; - } - else - { - histoNeigh[label] = 1; - } - } - } + 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) { + if (histoIt->second > majorityFreq) + { 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) - { - if ( (histoIt->second == majorityFreq) && (histoIt->first != majorityLabel) ) { - if (m_KeepOriginalLabelBool == true) - { - majorityLabel = centerPixel; - } - else - { - majorityLabel = m_LabelForUndecidedPixels; - } - break; + if ( (histoIt->second == majorityFreq) && (histoIt->first != majorityLabel) ) + { + if (m_KeepOriginalLabelBool == true) + { + majorityLabel = centerPixel; + } + else + { + majorityLabel = m_LabelForUndecidedPixels; + } + break; + } } - } - }//END if (centerPixel != m_LabelForNoDataPixels) + }//END if (centerPixel != m_LabelForNoDataPixels) //If (centerPixel == m_LabelForNoDataPixels) - else - { - majorityLabel = m_LabelForNoDataPixels; - } + else + { + majorityLabel = m_LabelForNoDataPixels; + } return majorityLabel; } +template<class TInputImage, class TOutputImage, class TKernel> +void NeighborhoodMajorityVotingImageFilter<TInputImage, + TOutputImage, + TKernel>::FillNeighborhoodHistogram(std::map<PixelType, unsigned int>& histoNeigh, + const NeighborhoodIteratorType &nit, + const KernelIteratorType kernelBegin, + const KernelIteratorType kernelEnd) +{ + PixelType centerPixel = nit.GetCenterPixel(); + unsigned int i; + KernelIteratorType kernel_it; + for (i = 0, kernel_it = kernelBegin; kernel_it < kernelEnd; ++kernel_it, ++i) + { + // if structuring element is positive, use the pixel under that element + // in the image + + PixelType label = nit.GetPixel(i); + if ((*kernel_it > itk::NumericTraits<KernelPixelType>::Zero) && (label != m_LabelForNoDataPixels)) + { + // note we use GetPixel() on the SmartNeighborhoodIterator to + // respect boundary conditions + + //If the current label has already been added to the histogram histoNeigh + if (histoNeigh.count(label) > 0) + { + histoNeigh[label]++; + } + else + { + histoNeigh[label] = 1; + } + } + } + +} + + + template<class TInputImage, class TOutputImage, class TKernel> void NeighborhoodMajorityVotingImageFilter<TInputImage, TOutputImage, TKernel>