From 49cbadd53b745689c7656471bad28c5981a2775c Mon Sep 17 00:00:00 2001
From: Emmanuel Christophe <emmanuel.christophe@orfeo-toolbox.org>
Date: Wed, 16 Jul 2008 11:29:20 +0000
Subject: [PATCH] Ajout d'une option de normalisation au filtre de convolution

---
 Code/BasicFilters/otbConvolutionImageFilter.h  | 12 ++++++++++++
 .../BasicFilters/otbConvolutionImageFilter.txx | 18 ++++++++++++------
 .../BasicFilters/otbConvolutionImageFilter.cxx |  1 +
 3 files changed, 25 insertions(+), 6 deletions(-)

diff --git a/Code/BasicFilters/otbConvolutionImageFilter.h b/Code/BasicFilters/otbConvolutionImageFilter.h
index 1cdd5152c5..670ab78991 100644
--- a/Code/BasicFilters/otbConvolutionImageFilter.h
+++ b/Code/BasicFilters/otbConvolutionImageFilter.h
@@ -34,6 +34,9 @@ namespace otb
  * The radius of the input filter is provided by the \code SetInput() \endcode 
  * method and the filters coefficients are given by an itk::Array passed to the 
  * \code SetFilter() \endcode method.
+ * 
+ * By default, the input filter is not normalized but it can be using the 
+ * NormalizeFilterOn() method.
  *
  * \sa Image
  * \sa Neighborhood
@@ -119,6 +122,14 @@ public:
     }
   itkGetConstReferenceMacro(Filter, ArrayType);
 
+  
+  /**
+   * Set/Get methods for the normalization of the filter
+  */
+  itkSetMacro(NormalizeFilter, bool);
+  itkGetMacro(NormalizeFilter, bool);
+  itkBooleanMacro(NormalizeFilter);
+  
   /** ConvolutionImageFilter needs a larger input requested region than
    * the output requested region.  As such, ConvolutionImageFilter needs
    * to provide an implementation for GenerateInputRequestedRegion()
@@ -158,6 +169,7 @@ private:
 
   InputSizeType m_Radius;
   ArrayType m_Filter;
+  bool m_NormalizeFilter;
   
 };
   
diff --git a/Code/BasicFilters/otbConvolutionImageFilter.txx b/Code/BasicFilters/otbConvolutionImageFilter.txx
index 71e69979ed..c917935239 100644
--- a/Code/BasicFilters/otbConvolutionImageFilter.txx
+++ b/Code/BasicFilters/otbConvolutionImageFilter.txx
@@ -37,6 +37,7 @@ ConvolutionImageFilter<TInputImage, TOutputImage>
   m_Radius.Fill(1);
   m_Filter.SetSize(3*3);
   m_Filter.Fill(1);
+  m_NormalizeFilter = false;
 }
 
 template <class TInputImage, class TOutputImage>
@@ -124,9 +125,6 @@ ConvolutionImageFilter< TInputImage, TOutputImage>
     { 
     bit = itk::ConstNeighborhoodIterator<InputImageType>(m_Radius,
                                                     input, *fit);
-    //std::cout << m_Radius << std::endl;	    
-    //std::cout << bit << std::endl;	    
-    
 
     it = itk::ImageRegionIterator<OutputImageType>(output, *fit);
     bit.OverrideBoundaryCondition(&nbc);
@@ -138,13 +136,21 @@ ConvolutionImageFilter< TInputImage, TOutputImage>
 	sum = itk::NumericTraits<InputRealType>::Zero;
 	norm = itk::NumericTraits<InputRealType>::Zero;
 	for (i = 0; i < neighborhoodSize; ++i)
-	  {
+	{
 	    sum += static_cast<InputRealType>( bit.GetPixel(i)*m_Filter(i) );
 	    norm += static_cast<InputRealType>( m_Filter(i) );
-	  }
+	}
 	
 	// get the mean value
-	it.Set( static_cast<OutputPixelType>(sum / double(norm)) );
+        
+        if (m_NormalizeFilter)
+        {
+	  it.Set( static_cast<OutputPixelType>(sum / double(norm)) );
+        }
+        else
+        {
+          it.Set( static_cast<OutputPixelType>(sum));
+        }
 	
 	++bit;
 	++it;
diff --git a/Testing/Code/BasicFilters/otbConvolutionImageFilter.cxx b/Testing/Code/BasicFilters/otbConvolutionImageFilter.cxx
index f1277c380d..c698f1ac4e 100755
--- a/Testing/Code/BasicFilters/otbConvolutionImageFilter.cxx
+++ b/Testing/Code/BasicFilters/otbConvolutionImageFilter.cxx
@@ -57,6 +57,7 @@ int otbConvolutionImageFilter( int argc, char * argv[] )
 
   convFilter->SetRadius(radius);
   convFilter->SetFilter(filterCoeffs);
+  convFilter->NormalizeFilterOn();
   
   convFilter->SetInput( reader->GetOutput() );
   writer->SetInput( convFilter->GetOutput() );
-- 
GitLab