From d30475c0a4841215c4e86852ac7020f92cc6e343 Mon Sep 17 00:00:00 2001
From: Jordi Inglada <jordi.inglada@cesbio.cnes.fr>
Date: Mon, 19 Sep 2016 15:44:29 +0200
Subject: [PATCH] REFAC: method for filling the histogram

---
 ...otbNeighborhoodMajorityVotingImageFilter.h |   5 +
 ...bNeighborhoodMajorityVotingImageFilter.txx | 107 ++++++++++--------
 2 files changed, 67 insertions(+), 45 deletions(-)

diff --git a/Modules/Fusion/MajorityVoting/include/otbNeighborhoodMajorityVotingImageFilter.h b/Modules/Fusion/MajorityVoting/include/otbNeighborhoodMajorityVotingImageFilter.h
index f7ce6b84e5..ad285ad9d4 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 397d3876e6..17236cd82c 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>
-- 
GitLab