From d4f141cf00ba3147de37dcfe728604226aa8b9d8 Mon Sep 17 00:00:00 2001
From: Julien Michel <julien.michel@orfeo-toolbox.org>
Date: Fri, 1 Jun 2012 16:01:04 +0200
Subject: [PATCH] ENH: Adding a watershed composite filter to allow use of itk
 watershed inside large segmentation framework

---
 Code/OBIA/otbWatershedSegmentationFilter.h   | 89 ++++++++++++++++++++
 Code/OBIA/otbWatershedSegmentationFilter.txx | 54 ++++++++++++
 2 files changed, 143 insertions(+)
 create mode 100644 Code/OBIA/otbWatershedSegmentationFilter.h
 create mode 100644 Code/OBIA/otbWatershedSegmentationFilter.txx

diff --git a/Code/OBIA/otbWatershedSegmentationFilter.h b/Code/OBIA/otbWatershedSegmentationFilter.h
new file mode 100644
index 0000000000..fecdf06052
--- /dev/null
+++ b/Code/OBIA/otbWatershedSegmentationFilter.h
@@ -0,0 +1,89 @@
+/*=========================================================================
+
+  Program:   ORFEO Toolbox
+  Language:  C++
+  Date:      $Date$
+  Version:   $Revision$
+
+
+  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
+  See OTBCopyright.txt for details.
+
+
+     This software is distributed WITHOUT ANY WARRANTY; without even
+     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+     PURPOSE.  See the above copyright notices for more information.
+
+=========================================================================*/
+#ifndef __otbWatershedSegmentationFilter_h
+#define __otbWatershedSegmentationFilter_h
+
+#include "itkMacro.h"
+#include "itkCastImageFilter.h"
+#include "itkWatershedImageFilter.h"
+
+namespace otb {
+
+/** \class WatershedSegmentationFilter
+*
+*  
+*/
+
+
+template <class TInputImage,  class TOutputLabelImage >
+class WatershedSegmentationFilter 
+  : public itk::ImageToImageFilter<TInputImage, TOutputLabelImage>
+{
+public:
+  /** Standard Self typedef */
+  typedef WatershedSegmentationFilter                             Self;
+  typedef itk::ImageToImageFilter<TInputImage, TOutputLabelImage> Superclass;
+  typedef itk::SmartPointer<Self>                                 Pointer;
+  typedef itk::SmartPointer<const Self>                           ConstPointer;
+
+  /** Some convenient typedefs. */
+  typedef TInputImage                                             InputImageType;
+  typedef TOutputLabelImage                                       OutputLabelImageType;
+  
+  typedef itk::WatershedImageFilter<TInputImage>                  WatershedFilterType;
+  typedef typename WatershedFilterType::OutputImageType           InternalOutputImageType;
+
+  typedef itk::CastImageFilter<InternalOutputImageType,
+                               OutputLabelImageType>              CastImageFilterType;
+
+  /** Method for creation through the object factory. */
+  itkNewMacro(Self);
+
+  /** Runtime information support. */
+  itkTypeMacro(WatershedSegmentationFilter, ImageToImageFilter);
+
+  /** ImageDimension constants */
+  itkStaticConstMacro(ImageDimension, unsigned int, TInputImage::ImageDimension);
+
+  otbSetObjectMemberMacro(WatershedFilter,Level,float);
+  otbGetObjectMemberConstReferenceMacro(WatershedFilter,Level,float);
+
+  otbSetObjectMemberMacro(WatershedFilter,Threshold,float);
+  otbGetObjectMemberConstReferenceMacro(WatershedFilter,Threshold,float);
+
+
+protected:
+  WatershedSegmentationFilter();
+
+  virtual ~WatershedSegmentationFilter();
+
+  virtual void GenerateData();
+
+private:
+  typename CastImageFilterType::Pointer m_CastFilter;
+  typename WatershedFilterType::Pointer m_WatershedFilter;
+};
+
+
+} // end namespace otb
+
+#ifndef OTB_MANUAL_INSTANTIATION
+#include "otbWatershedSegmentationFilter.txx"
+#endif
+
+#endif
diff --git a/Code/OBIA/otbWatershedSegmentationFilter.txx b/Code/OBIA/otbWatershedSegmentationFilter.txx
new file mode 100644
index 0000000000..26510a70e4
--- /dev/null
+++ b/Code/OBIA/otbWatershedSegmentationFilter.txx
@@ -0,0 +1,54 @@
+/*=========================================================================
+
+  Program:   ORFEO Toolbox
+  Language:  C++
+  Date:      $Date$
+  Version:   $Revision$
+
+
+  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
+  See OTBCopyright.txt for details.
+
+
+     This software is distributed WITHOUT ANY WARRANTY; without even
+     the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
+     PURPOSE.  See the above copyright notices for more information.
+
+=========================================================================*/
+#ifndef __otbWatershedSegmentationFilter_txx
+#define __otbWatershedSegmentationFilter_txx
+
+#include "otbWatershedSegmentationFilter.h"
+
+namespace otb {
+
+template <class TInputImage,  class TOutputLabelImage>
+WatershedSegmentationFilter<TInputImage, TOutputLabelImage>
+::WatershedSegmentationFilter()
+{
+   m_WatershedFilter = WatershedFilterType::New();
+   m_CastFilter      = CastImageFilterType::New();
+   m_CastFilter->SetInput(m_WatershedFilter->GetOutput());
+   this->SetNthOutput(0,TOutputLabelImage::New());
+}
+
+template <class TInputImage,  class TOutputLabelImage>
+WatershedSegmentationFilter<TInputImage, TOutputLabelImage>
+::~WatershedSegmentationFilter()
+{}
+
+
+template <class TInputImage,  class TOutputLabelImage>
+void
+WatershedSegmentationFilter<TInputImage, TOutputLabelImage>
+::GenerateData()
+{
+  this->m_WatershedFilter->SetInput(this->GetInput());
+  m_CastFilter->GraftOutput(this->GetOutput());
+  m_CastFilter->Update();
+  this->GraftOutput(m_CastFilter->GetOutput());
+
+}
+
+} // end namespace otb
+#endif
-- 
GitLab