From 28db4fa6c20d32278132bc815b2cd0f6078306c6 Mon Sep 17 00:00:00 2001
From: Julien Michel <julien.michel@c-s.fr>
Date: Fri, 8 Dec 2006 17:41:57 +0000
Subject: [PATCH] =?UTF-8?q?-=20Impl=C3=A9mentation=20du=20calcul=20du=20gr?=
 =?UTF-8?q?aphe=20RCC8=20(otbImageMultiSegmentationToRCC8GraphFilter),=20s?=
 =?UTF-8?q?ans=20l'optimisation=20de=20la=20table=20de=20composition.=20-?=
 =?UTF-8?q?=20Correction=20d'une=20erreur=20dans=20l'otbBinaryImageMinimal?=
 =?UTF-8?q?BoudingRegionCalculator.?=
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit

---
 ...ryImageMinimalBoundingRegionCalculator.txx |  2 +-
 ...bImageMultiSegmentationToRCC8GraphFilter.h | 17 +---
 ...mageMultiSegmentationToRCC8GraphFilter.txx | 87 ++++++++++++++++++-
 .../otbImageToImageRCC8Calculator.txx         |  6 +-
 Testing/Code/BasicFilters/CMakeLists.txt      |  2 +-
 Testing/Code/SpatialReasoning/CMakeLists.txt  | 27 +++---
 ...mageMultiSegmentationToRCC8GraphFilter.cxx |  8 +-
 .../otbSpatialReasoningTests.cxx              |  2 +-
 8 files changed, 113 insertions(+), 38 deletions(-)

diff --git a/Code/BasicFilters/otbBinaryImageMinimalBoundingRegionCalculator.txx b/Code/BasicFilters/otbBinaryImageMinimalBoundingRegionCalculator.txx
index 46916ee001..b6b56cc814 100644
--- a/Code/BasicFilters/otbBinaryImageMinimalBoundingRegionCalculator.txx
+++ b/Code/BasicFilters/otbBinaryImageMinimalBoundingRegionCalculator.txx
@@ -131,7 +131,7 @@ BinaryImageMinimalBoundingRegionCalculator<TInputImage>
 	for(int i=0;i<InputImageType::ImageDimension;i++)
 	  {
 	    // If we are not on boundary case, we can do what we want
-	    if(min[i]> maxRegion.GetIndex()[i])
+	    if(min[i]-m_Pad> maxRegion.GetIndex()[i])
 	      {
 		index[i]= min[i]-m_Pad;
 	      }
diff --git a/Code/SpatialReasoning/otbImageMultiSegmentationToRCC8GraphFilter.h b/Code/SpatialReasoning/otbImageMultiSegmentationToRCC8GraphFilter.h
index d63618545b..76413ba7f8 100644
--- a/Code/SpatialReasoning/otbImageMultiSegmentationToRCC8GraphFilter.h
+++ b/Code/SpatialReasoning/otbImageMultiSegmentationToRCC8GraphFilter.h
@@ -43,22 +43,16 @@ public:
   itkTypeMacro(ImageMultiSegmentationToRCC8GraphFilter,ImageListToRCC8GraphFilter);
   /** Input related typedefs */
   typedef TInputImage InputImageType;
+  typedef typename InputImageType::PixelType PixelType;
   typedef typename InputImageType::Pointer InputImagePointerType;
+  typedef typename Superclass::InputImageListType InputImageListType;
+  typedef typename InputImageListType::Pointer InputImageListPointerType;
+  typedef typename InputImageListType::ConstIterator ConstListIteratorType;
   /** Output related typedefs */
   typedef TOutputGraph OutputGraphType;
   typedef typename OutputGraphType::Pointer OutputGraphPointerType;
   typedef typename OutputGraphType::VertexType VertexType;
   typedef typename VertexType::Pointer VertexPointerType;
-  
-/*   /// Get The statistics for the different relations */
-/*   itkGetConstMacro(TotalNumberOfRegions,int); */
-/*   itkGetConstMacro(NumberOfRelations,int); */
-/*   /// Get the number of regions by segmentation image */
-/*   std::vector<int> GetNumberOfRegions(void); */
-/*   /// Set a filter to not take in account relations whose index is */
-/*   /// under the threshold */
-/*   itkGetConstMacro(RelationFilter,int); */
-/*   itkSetMacro(RelationFilter,int);       */          
 
 protected:
   /** Constructor */
@@ -71,9 +65,6 @@ protected:
   void PrintSelf(std::ostream& os, itk::Indent indent) const;
 
 private:
- /*  int m_NumberOfRelations; */
-/*   int m_TotalNumberOfRegions; */
-/*   std::vector<unsigned int> m_NumberOfRegions; */
 };
 } // End namespace otb
 
diff --git a/Code/SpatialReasoning/otbImageMultiSegmentationToRCC8GraphFilter.txx b/Code/SpatialReasoning/otbImageMultiSegmentationToRCC8GraphFilter.txx
index 87901e3696..3b2ed96396 100644
--- a/Code/SpatialReasoning/otbImageMultiSegmentationToRCC8GraphFilter.txx
+++ b/Code/SpatialReasoning/otbImageMultiSegmentationToRCC8GraphFilter.txx
@@ -19,8 +19,9 @@
 #define _otbImageMultiSegmentationToRCC8GraphFilter_txx
 
 #include "otbImageMultiSegmentationToRCC8GraphFilter.h"
-#include "itkMinimumMaximumImageFilter.h"
+#include "itkMinimumMaximumImageCalculator.h"
 #include "otbImageToImageRCC8Calculator.h"
+#include "otbRCC8VertexIterator.h"
 
 namespace otb
 {
@@ -45,7 +46,89 @@ template <class TInputImage, class TOutputGraph>
 void
 ImageMultiSegmentationToRCC8GraphFilter<TInputImage, TOutputGraph>
 ::GenerateData()
-{}
+{
+  // Input image list pointer
+  InputImageListPointerType segList = this->GetInput();
+  
+  // Ouptut graph pointer 
+  OutputGraphPointerType graph = this->GetOutput();
+
+  // Some typedefs
+  typedef itk::MinimumMaximumImageCalculator<InputImageType> MinMaxCalculatorType;
+  typedef ImageToImageRCC8Calculator<InputImageType> RCC8CalculatorType;
+  typedef RCC8VertexIterator<OutputGraphType> VertexIteratorType; 
+
+    // Vector of label
+  std::vector<PixelType> maxLabelVector;
+  
+  // Vertex indexes
+  unsigned int vertexIndex = 0;
+  unsigned int segmentationImageIndex = 0;
+
+  // For each segmentation image
+  for(ConstListIteratorType it = segList->Begin();it!=segList->End();++it)
+    {
+      // Compute the maximum label
+      typename MinMaxCalculatorType::Pointer minMax = MinMaxCalculatorType::New();
+      minMax->SetImage(it.Get());
+      minMax->ComputeMaximum();
+      maxLabelVector.push_back(minMax->GetMaximum());
+      otbMsgDebugMacro(<<"Number of objects in image "<<segmentationImageIndex<<": "
+	       <<minMax->GetMaximum());
+      
+      // then for each region of the images
+      for(PixelType label=1; label<=maxLabelVector.back();++label)
+	{
+	  // Create a new vertex
+	  VertexPointerType vertex = VertexType::New();
+	  // Set its properties
+	  vertex->SetSegmentationImageIndex(segmentationImageIndex);
+	  vertex->SetObjectLabelInImage(label);
+	  // Put it in the graph
+	  graph->SetVertex(vertexIndex,vertex);
+	  vertexIndex++;
+	}
+      segmentationImageIndex++;
+    }
+
+  VertexIteratorType vIt1(graph);
+  VertexIteratorType vIt2(graph);
+
+  // For each couple of vertices
+  for(vIt1.GoToBegin();!vIt1.IsAtEnd();++vIt1)
+    {
+      for(vIt2.GoToBegin();!vIt2.IsAtEnd();++vIt2)
+	{
+	  // Get the segmentation images indexes 
+	  unsigned int source = vIt1.Get()->GetSegmentationImageIndex();
+	  unsigned int target = vIt2.Get()->GetSegmentationImageIndex();
+	 
+	  // We do not examine each couple because of the RCC8 simetry
+	  if(source<target)
+	    {
+	      // Get the labels of source and target
+	      PixelType label1 = vIt1.Get()->GetObjectLabelInImage();	 
+	      PixelType label2 = vIt2.Get()->GetObjectLabelInImage();
+
+	      // Compute the RCC8 relation
+	      typename RCC8CalculatorType::Pointer calc = RCC8CalculatorType::New();
+	      calc->SetInput1(segList->GetNthElement(source));
+	      calc->SetInsideValue1(label1);
+	      calc->SetInput2(segList->GetNthElement(target));
+	      calc->SetInsideValue2(label2);
+	      calc->Update();
+
+	      // If the vertices are connected
+	      if(calc->GetValue()>OTB_RCC8_DC)
+		{
+		  // Add the edge to the graph.
+		  otbMsgDebugMacro(<<"Adding edge: "<<source<<" -> "<<target<<": "<<calc->GetValue());
+		  graph->AddEdge(vIt1.GetIndex(),vIt2.GetIndex(),calc->GetValue());
+		}
+	    }
+	}
+    }
+}
 template <class TInputImage, class TOutputGraph>
 void
 ImageMultiSegmentationToRCC8GraphFilter<TInputImage, TOutputGraph>
diff --git a/Code/SpatialReasoning/otbImageToImageRCC8Calculator.txx b/Code/SpatialReasoning/otbImageToImageRCC8Calculator.txx
index 3fcb6deade..9ef3f6e21e 100644
--- a/Code/SpatialReasoning/otbImageToImageRCC8Calculator.txx
+++ b/Code/SpatialReasoning/otbImageToImageRCC8Calculator.txx
@@ -130,6 +130,8 @@ namespace otb
     rc->SetInsideValue(this->GetInsideValue2());
     rc->Update();
     region2=rc->GetRegion();
+    // otbMsgDebugMacro(<<"RCC8Calculator->ComputeMinimalRegion() Region1: index: "<<region1.GetIndex()<<" size: "<<region1.GetSize());
+    // otbMsgDebugMacro(<<"RCC8Calculator->ComputeMinimalRegion() Region2: index: "<<region2.GetIndex()<<" size: "<<region2.GetSize());
     typename ImageType::SizeType size;
     typename ImageType::IndexType index;
     
@@ -167,6 +169,8 @@ ImageToImageRCC8Calculator<TInputImage>
   typename BoolImageType::IndexType boolImageIndex;
   boolImageIndex[0]=m_MinimalROI.GetIndex()[0]-1;
   boolImageIndex[1]=m_MinimalROI.GetIndex()[1]-1;
+  //otbMsgDebugMacro(<<"RCC8Calculator->ConvertToBoolImage() size: "<<boolImageSize<<" index: "<<boolImageIndex);  
+
   typename BoolImageType::RegionType boolRegion;
   boolRegion.SetSize(boolImageSize);
   boolRegion.SetIndex(boolImageIndex);
@@ -184,7 +188,7 @@ ImageToImageRCC8Calculator<TInputImage>
       ++inputIt;
       ++outputIt;
     }
-  // otbMsgDebugMacro(<<"RCC8Calculator->ConvertToBoolImage() size: "<<output->GetLargestPossibleRegion().GetSize());
+  
   return output;
 }
   /**
diff --git a/Testing/Code/BasicFilters/CMakeLists.txt b/Testing/Code/BasicFilters/CMakeLists.txt
index 785448aa8e..df08e17639 100755
--- a/Testing/Code/BasicFilters/CMakeLists.txt
+++ b/Testing/Code/BasicFilters/CMakeLists.txt
@@ -97,7 +97,7 @@ ADD_TEST(bfTvBoundingRegionCalculator ${BASICFILTERS_TESTS}
  		   ${BASELINE_FILES}/bfBoundingRegionCalculatorOutput.txt
  		   ${TEMP}/bfBoundingRegionCalculatorOutput.txt
         otbBinaryImageMinimalBoundingRegionCalculator
-	4	
+	5
 	${TEMP}/bfBoundingRegionCalculatorOutput.txt
 	${INPUTDATA}/rcc8_mire1.png
 	${INPUTDATA}/rcc8_mire2.png
diff --git a/Testing/Code/SpatialReasoning/CMakeLists.txt b/Testing/Code/SpatialReasoning/CMakeLists.txt
index d0a4738441..50bef7ab0a 100644
--- a/Testing/Code/SpatialReasoning/CMakeLists.txt
+++ b/Testing/Code/SpatialReasoning/CMakeLists.txt
@@ -127,20 +127,17 @@ ADD_TEST(srTuMultiSegToRCC8GraphFilterNew ${SPATIALREASONING_TESTS}
          otbImageMultiSegmentationToRCC8GraphFilterNew)
 
 
-#ADD_TEST(srTvMultiSegToRCC8GraphFilter ${SPATIALREASONING_TESTS}
-#	--compare-ascii	${TOL}
-#			${BASELINE_FILES}/srRCC8GraphFilterOutput.dot
-#			${TEMP}/srRCC8GraphFilterOutput.dot
-#	 otbImageMultiSegmentationToRCC8GraphFilter
-#			${TEMP}/srRCC8GraphFilterOutput.dot
-#			4
-#			${INPUTDATA}/
-#			${INPUTDATA}/
-#			${INPUTDATA}/
-#			${INPUTDATA}/
-#)
-
-
+ADD_TEST(srTvMultiSegToRCC8GraphFilter ${SPATIALREASONING_TESTS}
+	--compare-ascii	${TOL}
+			${BASELINE_FILES}/srRCC8GraphFilterOutput.dot
+			${TEMP}/srRCC8GraphFilterOutput.dot
+	 otbImageMultiSegmentationToRCC8GraphFilter
+			${TEMP}/srRCC8GraphFilterOutput.dot
+			2
+			${INPUTDATA}/Seg1InputForRCC8Graph.tif
+			${INPUTDATA}/Seg2InputForRCC8Graph.tif
+			
+)
 
 # -------       Fichiers sources CXX -----------------------------------
 SET(BasicSpatialReasoning_SRCS
@@ -162,7 +159,7 @@ otbRCC8GraphFileReader.cxx
 otbRCC8GraphIOEndToEnd.cxx
 otbImageListToRCC8GraphFilterNew.cxx
 otbImageMultiSegmentationToRCC8GraphFilterNew.cxx
-#otbImageMultiSegmentationToRCC8GraphFilter.cxx
+otbImageMultiSegmentationToRCC8GraphFilter.cxx
 )
 
 INCLUDE_DIRECTORIES("${OTBTesting_BINARY_DIR}")
diff --git a/Testing/Code/SpatialReasoning/otbImageMultiSegmentationToRCC8GraphFilter.cxx b/Testing/Code/SpatialReasoning/otbImageMultiSegmentationToRCC8GraphFilter.cxx
index 1330962046..b3cba8bfbc 100644
--- a/Testing/Code/SpatialReasoning/otbImageMultiSegmentationToRCC8GraphFilter.cxx
+++ b/Testing/Code/SpatialReasoning/otbImageMultiSegmentationToRCC8GraphFilter.cxx
@@ -46,21 +46,21 @@ try
     ImageListType::Pointer inputList = ImageListType::New();
 
     // Reading input images
-    for(int i=3;i<nbImages+2;i++)
+    for(int i=0;i<nbImages;i++)
       {
 	ReaderType::Pointer reader = ReaderType::New();
-	reader->SetFilename(argv[i]);
+	reader->SetFileName(argv[3+i]);
 	reader->Update();
 	inputList->PushBack(reader->GetOutput());
       }
-
+    std::cout<<"Input image loaded into images list."<<std::endl;
     // Instanatiation
     RCC8GraphFilterType::Pointer filter = RCC8GraphFilterType::New();
     filter->SetInput(inputList);
     
     // Writing output graph
     GraphWriterType::Pointer writer = GraphWriterType::New();
-    writer->SetFilename(outputFilename);
+    writer->SetFileName(outputFilename);
     writer->SetInput(filter->GetOutput());
     writer->Update();
 
diff --git a/Testing/Code/SpatialReasoning/otbSpatialReasoningTests.cxx b/Testing/Code/SpatialReasoning/otbSpatialReasoningTests.cxx
index 12975ab849..7bbb9168b6 100644
--- a/Testing/Code/SpatialReasoning/otbSpatialReasoningTests.cxx
+++ b/Testing/Code/SpatialReasoning/otbSpatialReasoningTests.cxx
@@ -44,5 +44,5 @@ REGISTER_TEST(otbRCC8GraphFileReader);
 REGISTER_TEST(otbRCC8GraphIOEndToEnd);
 REGISTER_TEST(otbImageListToRCC8GraphFilterNew);
 REGISTER_TEST(otbImageMultiSegmentationToRCC8GraphFilterNew);
-//REGISTER_TEST(otbImageMultiSegmentationToRCC8GraphFilter);
+REGISTER_TEST(otbImageMultiSegmentationToRCC8GraphFilter);
 }
-- 
GitLab