From 59fef71ef2fd999292d851a25bba051fa8b71f3d Mon Sep 17 00:00:00 2001
From: Arnaud Jaen <arnaud.jaen@c-s.fr>
Date: Fri, 6 Apr 2012 15:44:32 +0200
Subject: [PATCH] ENH: Add option 4Connected/8Connected to GDALPolygonize in
 LabelImageToVectorDataFilter and LabelImageToOGRDataSourceFilter

---
 Code/OBIA/otbLabelImageToOGRDataSourceFilter.h  |  4 ++++
 .../OBIA/otbLabelImageToOGRDataSourceFilter.txx | 14 ++++++++++++--
 Code/OBIA/otbLabelImageToVectorDataFilter.h     |  3 +++
 Code/OBIA/otbLabelImageToVectorDataFilter.txx   | 16 +++++++++++++---
 .../otbStreamingVectorizedSegmentationOGR.h     | 17 ++++++++++++++---
 .../otbStreamingVectorizedSegmentationOGR.txx   |  6 +++++-
 .../OBIA/otbLabelImageToVectorDataFilter.cxx    |  1 +
 .../otbStreamingVectorizedSegmentationOGR.cxx   |  1 +
 8 files changed, 53 insertions(+), 9 deletions(-)

diff --git a/Code/OBIA/otbLabelImageToOGRDataSourceFilter.h b/Code/OBIA/otbLabelImageToOGRDataSourceFilter.h
index 36887dbced..7f8f3d9559 100644
--- a/Code/OBIA/otbLabelImageToOGRDataSourceFilter.h
+++ b/Code/OBIA/otbLabelImageToOGRDataSourceFilter.h
@@ -75,6 +75,9 @@ public:
   itkSetMacro(FieldName, std::string);
   itkGetMacro(FieldName, std::string);
   
+  itkSetMacro(Use8Connected, bool);
+  itkGetMacro(Use8Connected, bool);
+  
   const OGRDataSourceObjectType * GetOutput();
   
 protected:
@@ -96,6 +99,7 @@ private:
   void operator =(const Self&);      //purposely not implemented
   
   std::string m_FieldName;
+  bool m_Use8Connected;
 
 
 };
diff --git a/Code/OBIA/otbLabelImageToOGRDataSourceFilter.txx b/Code/OBIA/otbLabelImageToOGRDataSourceFilter.txx
index 97f4593004..63fa535d61 100644
--- a/Code/OBIA/otbLabelImageToOGRDataSourceFilter.txx
+++ b/Code/OBIA/otbLabelImageToOGRDataSourceFilter.txx
@@ -80,7 +80,7 @@ private:
 
 template <class TInputImage>
 LabelImageToOGRDataSourceFilter<TInputImage>
-::LabelImageToOGRDataSourceFilter() : m_FieldName("DN")
+::LabelImageToOGRDataSourceFilter() : m_FieldName("DN"), m_Use8Connected(false)
 {
    this->SetNumberOfRequiredInputs(1);
    this->SetNumberOfRequiredOutputs(1);
@@ -229,7 +229,17 @@ LabelImageToOGRDataSourceFilter<TInputImage>
     outputLayer->CreateField(&field, true);
 
     //Call GDALPolygonize()
-    GDALPolygonize(dataset->GetRasterBand(1), NULL, outputLayer, 0, NULL, NULL, NULL);
+    char ** options;
+    options = NULL;
+    char * option[1];
+    if (m_Use8Connected == true)
+    {
+      std::string opt("8CONNECTED:8");
+      option[0] = const_cast<char *>(opt.c_str());
+      options=option;
+    }
+    
+    GDALPolygonize(dataset->GetRasterBand(1), NULL, outputLayer, 0, options, NULL, NULL);
     
     
     OGRDataSourceObjectType * decoratedOutput =
diff --git a/Code/OBIA/otbLabelImageToVectorDataFilter.h b/Code/OBIA/otbLabelImageToVectorDataFilter.h
index 50d7fd1740..ddb36c00ba 100644
--- a/Code/OBIA/otbLabelImageToVectorDataFilter.h
+++ b/Code/OBIA/otbLabelImageToVectorDataFilter.h
@@ -82,6 +82,8 @@ public:
   itkSetMacro(FieldName, std::string);
   itkGetMacro(FieldName, std::string);
   
+  itkSetMacro(Use8Connected, bool);
+  itkGetMacro(Use8Connected, bool);
   
 protected:
   LabelImageToVectorDataFilter();
@@ -97,6 +99,7 @@ private:
   void operator =(const Self&);      //purposely not implemented
   
   std::string m_FieldName;
+  bool m_Use8Connected;
 
 };
 
diff --git a/Code/OBIA/otbLabelImageToVectorDataFilter.txx b/Code/OBIA/otbLabelImageToVectorDataFilter.txx
index 8a380f2c05..5f5e30bf3e 100644
--- a/Code/OBIA/otbLabelImageToVectorDataFilter.txx
+++ b/Code/OBIA/otbLabelImageToVectorDataFilter.txx
@@ -36,7 +36,7 @@ namespace otb
 
 template <class TInputImage, class TPrecision>
 LabelImageToVectorDataFilter<TInputImage, TPrecision>
-::LabelImageToVectorDataFilter() : m_FieldName("DN")
+::LabelImageToVectorDataFilter() : m_FieldName("DN"), m_Use8Connected(false)
 {
    this->SetNumberOfRequiredInputs(1);
    this->SetNumberOfRequiredOutputs(1);
@@ -159,13 +159,23 @@ LabelImageToVectorDataFilter<TInputImage, TPrecision>
     OGRSFDriver * ogrDriver = OGRSFDriverRegistrar::GetRegistrar()->GetDriverByName(driverName);
     OGRDataSource * dataSource = ogrDriver->CreateDataSource("Shape",NULL);
     
-    OGRLayer * outputLayer = dataSource->CreateLayer("toto",NULL,wkbMultiPolygon,NULL);
+    OGRLayer * outputLayer = dataSource->CreateLayer("layer",NULL,wkbMultiPolygon,NULL);
     
     OGRFieldDefn field(m_FieldName.c_str(),OFTInteger);
     outputLayer->CreateField(&field, true);
 
     //Call GDALPolygonize()
-    GDALPolygonize(dataset->GetRasterBand(1), NULL, outputLayer, 0, NULL, NULL, NULL);
+    char ** options;
+    options = NULL;
+    char * option[1];
+    if (m_Use8Connected == true)
+    {
+      std::string opt("8CONNECTED:8");
+      option[0] = const_cast<char *>(opt.c_str());
+      options=option;
+    }
+    
+    GDALPolygonize(dataset->GetRasterBand(1), NULL, outputLayer, 0, options, NULL, NULL);
     
     /** Convert OGR layer into VectorData */
     OGRFeatureDefn * dfn = outputLayer->GetLayerDefn();
diff --git a/Code/OBIA/otbStreamingVectorizedSegmentationOGR.h b/Code/OBIA/otbStreamingVectorizedSegmentationOGR.h
index 3887ed624c..69da1b6f93 100644
--- a/Code/OBIA/otbStreamingVectorizedSegmentationOGR.h
+++ b/Code/OBIA/otbStreamingVectorizedSegmentationOGR.h
@@ -135,15 +135,15 @@ public:
   
   itkGetObjectMacro(SegmentationFilter, SegmentationFilterType);
   
-  /*itkSetMacro(FieldName, std::string);
-  itkGetMacro(FieldName, std::string); */
-  
   void SetStartLabel(const LabelPixelType & label)
   {
      m_StartLabel = label;
      m_TileMaxLabel = label;
   }
   itkGetMacro(StartLabel, LabelPixelType);
+  
+  itkSetMacro(Use8Connected, bool);
+  itkGetMacro(Use8Connected, bool);
 
 protected:
   PersistentStreamingLabelImageToOGRDataFilter();
@@ -164,6 +164,7 @@ private:
   typename SegmentationFilterType::Pointer m_SegmentationFilter;
   
   unsigned int m_TileNumber;
+  bool m_Use8Connected;
   
   
 };
@@ -235,6 +236,16 @@ public:
      this->GetFilter()->Initialize();
   }
   
+  void SetUse8Connected(bool flag)
+  {
+     this->GetFilter()->SetUse8Connected(flag);
+  }
+  
+  const bool GetUse8Connected()
+  {
+     return this->GetFilter()->GetUse8Connected();
+  }
+  
 protected:
   /** Constructor */
   StreamingVectorizedSegmentationOGR() {}
diff --git a/Code/OBIA/otbStreamingVectorizedSegmentationOGR.txx b/Code/OBIA/otbStreamingVectorizedSegmentationOGR.txx
index fae0a52ba4..100e18762c 100644
--- a/Code/OBIA/otbStreamingVectorizedSegmentationOGR.txx
+++ b/Code/OBIA/otbStreamingVectorizedSegmentationOGR.txx
@@ -33,7 +33,7 @@ namespace otb
 
 template <class TImageType, class TSegmentationFilter>
 PersistentStreamingLabelImageToOGRDataFilter<TImageType, TSegmentationFilter>
-::PersistentStreamingLabelImageToOGRDataFilter() : m_TileMaxLabel(0), m_StartLabel(0)
+::PersistentStreamingLabelImageToOGRDataFilter() : m_TileMaxLabel(0), m_StartLabel(0), m_Use8Connected(false)
 {
    m_SegmentationFilter = SegmentationFilterType::New();
    m_TileNumber = 1;
@@ -85,6 +85,8 @@ PersistentStreamingLabelImageToOGRDataFilter<TImageType, TSegmentationFilter>
   extract->SetExtractionRegion( this->GetInput()->GetBufferedRegion() );
   extract->Update();
   
+  //std::cout<< "extract region " << extract->GetOutput()->GetLargestPossibleRegion()<<std::endl;
+  
   chrono.Stop();
   //std::cout<< "extract took " << chrono.GetTotal() << " sec"<<std::endl;
 
@@ -106,11 +108,13 @@ PersistentStreamingLabelImageToOGRDataFilter<TImageType, TSegmentationFilter>
   chrono1.Stop();
   std::cout<< "segmentation took " << chrono1.GetTotal() << " sec"<<std::endl;
   
+  
   itk::TimeProbe chrono2;
   chrono2.Start();
 
   labelImageToOGRDataFilter->SetInput(dynamic_cast<LabelImageType *>(m_SegmentationFilter->GetOutputs().at(labelImageIndex).GetPointer()));
   labelImageToOGRDataFilter->SetFieldName(this->GetFieldName());
+  labelImageToOGRDataFilter->SetUse8Connected(m_Use8Connected);
   labelImageToOGRDataFilter->Update();
   
   chrono2.Stop();
diff --git a/Testing/Code/OBIA/otbLabelImageToVectorDataFilter.cxx b/Testing/Code/OBIA/otbLabelImageToVectorDataFilter.cxx
index b522a7f0d3..7478798357 100644
--- a/Testing/Code/OBIA/otbLabelImageToVectorDataFilter.cxx
+++ b/Testing/Code/OBIA/otbLabelImageToVectorDataFilter.cxx
@@ -50,6 +50,7 @@ int otbLabelImageToVectorDataFilter(int argc, char * argv[])
   reader->SetFileName(infname);
 
   filter->SetInput(reader->GetOutput());
+  filter->SetUse8Connected(false);
 
   writer->SetFileName(outfname);
   writer->SetInput(filter->GetOutput());
diff --git a/Testing/Code/OBIA/otbStreamingVectorizedSegmentationOGR.cxx b/Testing/Code/OBIA/otbStreamingVectorizedSegmentationOGR.cxx
index 2b0892c0bc..c3d1ce7fc3 100644
--- a/Testing/Code/OBIA/otbStreamingVectorizedSegmentationOGR.cxx
+++ b/Testing/Code/OBIA/otbStreamingVectorizedSegmentationOGR.cxx
@@ -126,6 +126,7 @@ int otbStreamingVectorizedSegmentationOGR(int argc, char * argv[])
   filter->GetStreamer()->SetTileDimensionTiledStreaming(atoi(argv[3]));
   filter->SetFieldName(fieldName);
   filter->SetStartLabel(1);
+  filter->SetUse8Connected(true);
   filter->GetSegmentationFilter()->SetSpatialRadius(5);
   filter->GetSegmentationFilter()->SetRangeRadius(15);
   filter->GetSegmentationFilter()->SetMinimumRegionSize(100);
-- 
GitLab