From e1b2b1e5f899b07d0b2fc911abff3b61e69b574e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?C=C3=A9dric=20Traizet?= <cedric.traizet@c-s.fr>
Date: Wed, 19 Sep 2018 10:27:39 +0200
Subject: [PATCH] ENH : LabelImageSmallRegionMergingFilter is now a
 ProcessObject instead of a ImageToImageFilter

---
 .../app/otbSmallRegionsMerging.cxx            |  2 +-
 .../otbLabelImageSmallRegionMergingFilter.h   | 15 ++++++++++---
 .../otbLabelImageSmallRegionMergingFilter.hxx | 22 +++++++++----------
 3 files changed, 24 insertions(+), 15 deletions(-)

diff --git a/Modules/Applications/AppSegmentation/app/otbSmallRegionsMerging.cxx b/Modules/Applications/AppSegmentation/app/otbSmallRegionsMerging.cxx
index 9f4d81647b..4112c9da43 100644
--- a/Modules/Applications/AppSegmentation/app/otbSmallRegionsMerging.cxx
+++ b/Modules/Applications/AppSegmentation/app/otbSmallRegionsMerging.cxx
@@ -143,7 +143,7 @@ private:
       
     // Compute the LUT from the original label image to the merged output label image.
     auto regionMergingFilter = LabelImageSmallRegionMergingFilterType::New();
-    regionMergingFilter->SetInput( labelIn );
+    regionMergingFilter->SetInputLabelImage( labelIn );
     regionMergingFilter->SetLabelPopulation( labelPopulation );
     regionMergingFilter->SetLabelStatistic( meanValues );
     
diff --git a/Modules/Segmentation/Conversion/include/otbLabelImageSmallRegionMergingFilter.h b/Modules/Segmentation/Conversion/include/otbLabelImageSmallRegionMergingFilter.h
index 710e577a17..66efb3c2cc 100644
--- a/Modules/Segmentation/Conversion/include/otbLabelImageSmallRegionMergingFilter.h
+++ b/Modules/Segmentation/Conversion/include/otbLabelImageSmallRegionMergingFilter.h
@@ -25,6 +25,7 @@
 #include "otbPersistentFilterStreamingDecorator.h"
 
 #include <unordered_map>
+#include <unordered_set>
 
 namespace otb
 {
@@ -177,12 +178,12 @@ private:
  */
 template <class TInputLabelImage>
 class ITK_EXPORT LabelImageSmallRegionMergingFilter 
-  : public itk::ImageToImageFilter<TInputLabelImage, TInputLabelImage>
+  : public itk::ProcessObject
 {
 public:
   /** Standard Self typedef */
   typedef LabelImageSmallRegionMergingFilter                         Self;
-  typedef itk::ImageToImageFilter<TInputLabelImage, TInputLabelImage>      Superclass;
+  typedef itk::ProcessObject      Superclass;
   typedef itk::SmartPointer<Self>                                     Pointer;
   typedef itk::SmartPointer<const Self>                               ConstPointer;
 
@@ -190,7 +191,7 @@ public:
   itkNewMacro(Self);
 
   /** Creation through object factory macro */
-  itkTypeMacro(LabelImageSmallRegionMergingFilter, itk::ImageToImageFilter);
+  itkTypeMacro(LabelImageSmallRegionMergingFilter, itk::ProcessObject);
 
   // Small region merging filter typedefs
   typedef PersistentLabelImageSmallRegionMergingFilter< TInputLabelImage > PersistentLabelImageSmallRegionMergingFilterType;
@@ -205,6 +206,12 @@ public:
   itkGetMacro(MinSize , unsigned int);
   itkSetMacro(MinSize , unsigned int);
 
+  /** Set the Label population map */
+  void SetInputLabelImage( const TInputLabelImage * labelImage )
+  {
+    m_SmallRegionMergingFilter->GetFilter()->SetInput( labelImage );
+  }
+  
   /** Set the Label population map */
   void SetLabelPopulation( LabelPopulationType const & labelPopulation )
   {
@@ -235,6 +242,8 @@ public:
     return m_SmallRegionMergingFilter->GetFilter()->GetLUT();
   }
   
+  void Update(void) override;
+
 protected:
   /** Constructor */
   LabelImageSmallRegionMergingFilter();
diff --git a/Modules/Segmentation/Conversion/include/otbLabelImageSmallRegionMergingFilter.hxx b/Modules/Segmentation/Conversion/include/otbLabelImageSmallRegionMergingFilter.hxx
index 50a8e180bf..6cabcbaf2c 100644
--- a/Modules/Segmentation/Conversion/include/otbLabelImageSmallRegionMergingFilter.hxx
+++ b/Modules/Segmentation/Conversion/include/otbLabelImageSmallRegionMergingFilter.hxx
@@ -23,11 +23,9 @@
 #define otbLabelImageSmallRegionMergingFilter_hxx
 
 #include "otbLabelImageSmallRegionMergingFilter.h"
-#include "itkImageConstIterator.h"
 #include "itkConstShapedNeighborhoodIterator.h"
 #include "itkProgressReporter.h"
 
-#include <time.h>
 namespace otb
 {
 template <class TInputLabelImage >
@@ -64,7 +62,7 @@ PersistentLabelImageSmallRegionMergingFilter< TInputLabelImage >
   // to the euclidian distance between the corresponding m_labelStatistic elements.
   for (auto const & neighbours : neighboursMap)
     {
-    double proximity = std::numeric_limits<double>::max();
+    double proximity = itk::NumericTraits<double>::max();
     InputLabelType label = neighbours.first;
     InputLabelType closestNeighbour = label;
     for (auto const & neighbour : neighbours.second)
@@ -72,11 +70,8 @@ PersistentLabelImageSmallRegionMergingFilter< TInputLabelImage >
       auto statsLabel = m_LabelStatistic[ label ];
       auto statsNeighbour = m_LabelStatistic[ neighbour ];
       assert( statsLabel.Size() == statsNeighbour.Size() );
-      double distance = 0;
-      for (unsigned int i = 0 ; i < statsLabel.Size(); i++)
-        {
-        distance += (statsLabel[i] - statsNeighbour[i]) * (statsLabel[i] - statsNeighbour[i]);
-        }
+      
+      double distance = (statsLabel - statsNeighbour).GetSquaredNorm();
       if (distance < proximity)
         {
         proximity = distance;
@@ -203,7 +198,6 @@ PersistentLabelImageSmallRegionMergingFilter< TInputLabelImage >
     assert( !itN.IsAtEnd() );
     int currentLabel = m_LUT[ it.Get() ];
     
-    
     if ( m_LabelPopulation[currentLabel] == m_Size )
       {
       for (auto ci = itN.Begin() ; !ci.IsAtEnd(); ci++)
@@ -231,14 +225,20 @@ LabelImageSmallRegionMergingFilter< TInputLabelImage >
   m_SmallRegionMergingFilter = LabelImageSmallRegionMergingFilterType::New();
 }
 
+template <class TInputLabelImage>
+void
+LabelImageSmallRegionMergingFilter<TInputLabelImage>
+::Update(void)
+{
+  this->GenerateData();
+}
+
 template <class TInputLabelImage >
 void
 LabelImageSmallRegionMergingFilter< TInputLabelImage >
 ::GenerateData()
 {
   this->SetProgress(0.0);
-  auto labelImage = this->GetInput();
-  m_SmallRegionMergingFilter->GetFilter()->SetInput( labelImage );
 
   // Update the filter for all sizes.
   for (unsigned int size = 1; size < m_MinSize; size++)
-- 
GitLab