From 7242220f8e926cc38420ec4b1027ef4d45ac89ca Mon Sep 17 00:00:00 2001
From: Arnaud Jaen <arnaud.jaen@c-s.fr>
Date: Thu, 22 Mar 2012 17:36:49 +0100
Subject: [PATCH] ENH: Add a specialization template to LabeledOutputAccessor
 for the new mean shift filter

---
 .../otbStreamingVectorizedSegmentationOGR.h   | 12 ++++
 Testing/Code/OBIA/CMakeLists.txt              | 37 +++--------
 .../otbLabelImageToOGRDataSourceFilter.cxx    |  6 +-
 .../otbStreamingVectorizedSegmentationOGR.cxx | 66 ++++++++++++++++---
 4 files changed, 79 insertions(+), 42 deletions(-)

diff --git a/Code/OBIA/otbStreamingVectorizedSegmentationOGR.h b/Code/OBIA/otbStreamingVectorizedSegmentationOGR.h
index d01cd69858..8d0f97e273 100644
--- a/Code/OBIA/otbStreamingVectorizedSegmentationOGR.h
+++ b/Code/OBIA/otbStreamingVectorizedSegmentationOGR.h
@@ -39,6 +39,7 @@
 #include "otbPersistentImageToOGRDataFilter.h"
 
 #include "otbMeanShiftImageFilter.h"
+#include "otbMeanShiftImageFilter2.h"
 #include "otbMeanShiftVectorImageFilter.h"
 #include "itkMacro.h"
 
@@ -82,6 +83,17 @@ class LabeledOutputAccessor<MeanShiftVectorImageFilter<TInputImage, TOutputImage
       itkStaticConstMacro(LabeledOutputIndex, unsigned int, 2);
 };
 
+/**
+ * \class LabeledOutputAccessor
+ * \brief Specialized class to get the index of the labeled output image in mean shift filter (new version).
+ */
+template <class TInputImage, class TOutputImage, class TOutputImage2, class TKernelType>
+class LabeledOutputAccessor<MeanShiftImageFilter2<TInputImage, TOutputImage, TOutputImage2, TKernelType> >
+{
+   public:
+      typedef typename MeanShiftImageFilter2<TInputImage, TOutputImage, TOutputImage2, TKernelType>::LabeledOutputType    LabelImageType;
+      itkStaticConstMacro(LabeledOutputIndex, unsigned int, 0);
+};
 
 /** \class PersistentStreamingLabelImageToOGRDataFilter
  *  \brief this class uses GDALPolygonize method to transform a Label image into a VectorData.
diff --git a/Testing/Code/OBIA/CMakeLists.txt b/Testing/Code/OBIA/CMakeLists.txt
index f0f6e9d343..f01f44086f 100644
--- a/Testing/Code/OBIA/CMakeLists.txt
+++ b/Testing/Code/OBIA/CMakeLists.txt
@@ -167,16 +167,17 @@ ADD_TEST(obTuMeanShiftStreamingConnectedComponentSegmentationOBIAToVectorDataFil
 #TODO JGT add validation test
 ADD_TEST(obTuMeanShiftConnectedComponentSegmentationFilter ${OBIA_TESTS1}
       otbMeanShiftConnectedComponentSegmentationFilter
-      ${INPUTDATA}/ROI_QB_MUL_4.tif
+      /home2/arnaud/ORFEO-TOOLBOX/images/pleiade_extract.tif
+      #${INPUTDATA}/ROI_QB_MUL_4.tif
       ${TEMP}/obTuMeanShiftConnectedComponentSegmentationImage.png
       9 
       9 
       50 
       10 
-      0.001
+      0.01
       ""
       "distance<0.2"
-      20)
+      50)
       
 
 
@@ -235,33 +236,12 @@ ADD_TEST(obTvLabelImageToVectorDataFilter ${OBIA_TESTS1}
     )
 
 
-# -------            otb::StreamingVectorizedSegmentation   -------------
-ADD_TEST(obTuStreamingVectorizedSegmentationNew ${OBIA_TESTS1}
-    otbStreamingVectorizedSegmentationNew)
-
-ADD_TEST(obTvStreamingVectorizedSegmentation ${OBIA_TESTS1}
-     #--compare-ogr  ${EPSILON_8}
-     #${BASELINE_FILES}/obTvStreamingVectorizedSegmentationOutput.sqlite
-     #${TEMP}/obTvStreamingVectorizedSegmentationOutput.sqlite
-     otbStreamingVectorizedSegmentation
-     #${INPUTDATA}/QB_Toulouse_Ortho_PAN.tif
-     /home2/arnaud/ORFEO-TOOLBOX/images/TLSE_ORTHO_P_8bits/IMG_PHR1A_P_001/IMG_PHR1A_P_201006181052297_ORT_IPU_20111109_7807-001_R1C1.TIF
-     #/media/otbnas/otb/OTB-LargeInput/QUICKBIRD/TOULOUSE/000000128955_01_P001_MUL/02APR01105228-M1BS-000000128955_01_P001.TIF
-     ${TEMP}/obTvStreamingVectorizedSegmentationOutput2.shp
-     2048
-     #100
-     )
-
 ADD_TEST(obTuLabelImageToOGRDataSourceFilterNew ${OBIA_TESTS1}
     otbLabelImageToOGRDataSourceFilterNew)
     
 ADD_TEST(obTvLabelImageToOGRDataSourceFilter ${OBIA_TESTS1}
-    #--compare-ogr ${NOTOL}
-    #${BASELINE_FILES}/obTuLabelImageToOGRDataSourceFilter.shp
-    #${TEMP}/obTuLabelImageToOGRDataSourceFilter.shp
     otbLabelImageToOGRDataSourceFilter
     ${INPUTDATA}/labelImage_UnsignedChar.tif
-    ${TEMP}/obTuLabelImageToOGRDataSourceFilter.shp
     )
 
 # -------            otb::StreamingVectorizedSegmentation   -------------
@@ -273,12 +253,11 @@ ADD_TEST(obTvStreamingVectorizedSegmentationOGR ${OBIA_TESTS1}
      #${BASELINE_FILES}/obTvStreamingVectorizedSegmentationOutput.sqlite
      #${TEMP}/obTvStreamingVectorizedSegmentationOutput.sqlite
      otbStreamingVectorizedSegmentationOGR
-     #${INPUTDATA}/QB_Toulouse_Ortho_PAN.tif
-     /home2/arnaud/ORFEO-TOOLBOX/images/TLSE_ORTHO_P_8bits/IMG_PHR1A_P_001/IMG_PHR1A_P_201006181052297_ORT_IPU_20111109_7807-001_R1C1.TIF
-     #/media/otbnas/otb/OTB-LargeInput/QUICKBIRD/TOULOUSE/000000128955_01_P001_MUL/02APR01105228-M1BS-000000128955_01_P001.TIF
+     ${INPUTDATA}/QB_Toulouse_Ortho_PAN.tif
+     #/home2/arnaud/ORFEO-TOOLBOX/images/TLSE_ORTHO_P_8bits/IMG_PHR1A_P_001/IMG_PHR1A_P_201006181052297_ORT_IPU_20111109_7807-001_R1C1_undersample10.TIF
      ${TEMP}/obTvStreamingVectorizedSegmentationOutputOGR.shp
-     2048
-     #100
+     #2048
+     100
      )
 
 # OBIATests2 (need PQXX)
diff --git a/Testing/Code/OBIA/otbLabelImageToOGRDataSourceFilter.cxx b/Testing/Code/OBIA/otbLabelImageToOGRDataSourceFilter.cxx
index 14bc6a1ab2..04ea7bff01 100644
--- a/Testing/Code/OBIA/otbLabelImageToOGRDataSourceFilter.cxx
+++ b/Testing/Code/OBIA/otbLabelImageToOGRDataSourceFilter.cxx
@@ -39,10 +39,10 @@ int otbLabelImageToOGRDataSourceFilterNew(int argc, char * argv[])
 
 int otbLabelImageToOGRDataSourceFilter(int argc, char * argv[])
 {
-  if (argc != 3)
+  if (argc != 2)
     {
     std::cerr << "Usage: " << argv[0];
-    std::cerr << " inputLabelImageFile outputVectorfile(shp)" << std::endl;
+    std::cerr << " inputLabelImageFile " << std::endl;
     return EXIT_FAILURE;
     }
   const char * infname = argv[1];
@@ -65,8 +65,6 @@ int otbLabelImageToOGRDataSourceFilter(int argc, char * argv[])
   filter->SetInput(reader->GetOutput());
   filter->Update();
   
-  std::cout<<"layer name : "<<filter->GetOutput()->Get()->GetDataSource()->GetLayer(0)->GetName()<<std::endl;
-  std::cout<<"layer nb features : "<<filter->GetOutput()->Get()->GetDataSource()->GetLayer(0)->GetFeatureCount()<<std::endl;
 
   return EXIT_SUCCESS;
 }
diff --git a/Testing/Code/OBIA/otbStreamingVectorizedSegmentationOGR.cxx b/Testing/Code/OBIA/otbStreamingVectorizedSegmentationOGR.cxx
index 1a3bca51d7..7c9ba46cac 100644
--- a/Testing/Code/OBIA/otbStreamingVectorizedSegmentationOGR.cxx
+++ b/Testing/Code/OBIA/otbStreamingVectorizedSegmentationOGR.cxx
@@ -28,6 +28,9 @@
 #include "otbPersistentImageToOGRDataFilter.h"
 #include "otbPersistentFilterStreamingDecorator.h"
 
+#include "otbMeanShiftImageFilter2.h"
+#include "otbMeanShiftConnectedComponentSegmentationFilter.h"
+
 int otbStreamingVectorizedSegmentationOGRNew(int argc, char * argv[])
 {
   typedef float InputPixelType;
@@ -47,6 +50,18 @@ int otbStreamingVectorizedSegmentationOGRNew(int argc, char * argv[])
 
 int otbStreamingVectorizedSegmentationOGR(int argc, char * argv[])
 {
+  const unsigned int spatialRadius = 9;
+  const unsigned int spectralRadius = 9;
+  const double spectralbandwidth = 50;
+  const double spatialbandwidth = 10;
+  const double threshold = 0.001;
+
+  /* conencted component parameters */
+
+  const char * maskexpression = "";
+  const char * segmentationexpression = "distance<0.2";
+  unsigned int minobjectsize = 20;
+
 
   typedef float InputPixelType;
   const unsigned int Dimension = 2;
@@ -56,31 +71,64 @@ int otbStreamingVectorizedSegmentationOGR(int argc, char * argv[])
   typedef otb::VectorImage<InputPixelType,  Dimension>    ImageType;
   //typedef otb::Image<InputPixelType,  Dimension>          ImageType;
   typedef otb::Image<unsigned int, Dimension>             LabelImageType;
-  typedef otb::MeanShiftVectorImageFilter<ImageType, ImageType, LabelImageType> MeanShiftImageFilterType;
-  //typedef otb::MeanShiftImageFilter<ImageType, ImageType, LabelImageType> MeanShiftImageFilterType;
+  typedef double                                          KernelType;
   
-  typedef otb::Functor::ConnectedComponentMuParserFunctor<ImageType::PixelType>  FunctorType;
+  //old mean shift filter
+  typedef otb::MeanShiftVectorImageFilter<ImageType, ImageType, LabelImageType> SegmentationFilterType;
+  //typedef otb::MeanShiftImageFilter<ImageType, ImageType, LabelImageType> SegmentationFilterType;
+  
+  
+  //connectedComponnent filter
+  /*typedef otb::Functor::ConnectedComponentMuParserFunctor<ImageType::PixelType>  FunctorType;
   typedef itk::ConnectedComponentFunctorImageFilter<ImageType, LabelImageType, FunctorType, LabelImageType > SegmentationFilterType;
-  //typedef otb::StreamingVectorizedSegmentationOGR<ImageType, SegmentationFilterType> StreamingVectorizedSegmentationOGRType;
-  typedef otb::StreamingVectorizedSegmentationOGR<ImageType, MeanShiftImageFilterType> StreamingVectorizedSegmentationOGRType;
-
+  */
+  
+  //New mean shift filter
+  /*typedef otb::MeanShiftConnectedComponentSegmentationFilter
+  < ImageType, LabelImageType, LabelImageType >  SegmentationFilterType;*/
+  typedef otb::MeanShiftImageFilter2<ImageType,ImageType,ImageType,KernelType> MeanShiftFilterType;
+  
+  
+  typedef otb::StreamingVectorizedSegmentationOGR<ImageType, SegmentationFilterType> StreamingVectorizedSegmentationOGRType;
   typedef otb::ImageFileReader<ImageType>                      ReaderType;
 
-
   ReaderType::Pointer             reader = ReaderType::New();
   StreamingVectorizedSegmentationOGRType::Pointer filter = StreamingVectorizedSegmentationOGRType::New();
+  MeanShiftFilterType::Pointer meanShiftFilter = MeanShiftFilterType::New();
 
 
+  
   reader->SetFileName(argv[1]);
   reader->GenerateOutputInformation();
+  
+  
+  //New Filter mean shift
+  ImageType::SizeType radius;
+  radius[0] = spatialRadius;
+  radius[1] = spatialRadius;
+  meanShiftFilter->SetSpatialRadius(radius);
+  radius[0] = spectralRadius;
+  radius[1] = spectralRadius;
+  meanShiftFilter->SetRangeRadius(radius);
+  meanShiftFilter->SetSpectralBandwidth(spectralbandwidth);
+  meanShiftFilter->SetSpatialBandwidth(spatialbandwidth);
+  meanShiftFilter->SetThreshold(threshold);
+  meanShiftFilter->SetInput(reader->GetOutput());
+  meanShiftFilter->UpdateOutputInformation();
+
+  /*filter->GetSegmentationFilter()->SetMeanShiftFilter(meanShiftFilter);
+  filter->GetSegmentationFilter()->SetMaskExpression(maskexpression);
+  filter->GetSegmentationFilter()->SetConnectedComponentExpression(segmentationexpression);
+  filter->GetSegmentationFilter()->SetMinimumObjectSize(minobjectsize);*/
+  
   filter->SetInput(reader->GetOutput());
   //filter->GetStreamer()->SetNumberOfLinesStrippedStreaming(atoi(argv[3]));
   filter->GetStreamer()->SetTileDimensionTiledStreaming(atoi(argv[3]));
   filter->SetFieldName(fieldName);
   filter->SetStartLabel(1);
-  filter->GetSegmentationFilter()->SetSpatialRadius(10);
+  filter->GetSegmentationFilter()->SetSpatialRadius(5);
   filter->GetSegmentationFilter()->SetRangeRadius(15);
-  filter->GetSegmentationFilter()->SetMinimumRegionSize(400);
+  filter->GetSegmentationFilter()->SetMinimumRegionSize(100);
   //filter->GetSegmentationFilter()->GetFunctor().SetExpression("distance<15");
   
   filter->SetFileName(argv[2]);
-- 
GitLab