From 273d9b6aaac1e30b27e8abfbbdfb9c665cd166b5 Mon Sep 17 00:00:00 2001
From: remi cresson <remi.cresson@teledetection.fr>
Date: Fri, 24 Mar 2017 10:11:05 +0000
Subject: [PATCH] ENH: raffale patch for no-data

---
 include/grmBaatzSegmenter.txx  | 33 ++++++++++++++++++++++-----------
 include/grmGraphOperations.txx | 10 +++++++---
 2 files changed, 29 insertions(+), 14 deletions(-)

diff --git a/include/grmBaatzSegmenter.txx b/include/grmBaatzSegmenter.txx
index bc1aca8..b5d61c3 100644
--- a/include/grmBaatzSegmenter.txx
+++ b/include/grmBaatzSegmenter.txx
@@ -20,6 +20,7 @@
 #include <otbImageFileReader.h>
 #include <itkImageRegionIterator.h>
 #include "grmBaatzSegmenter.h"
+#include "otbNoDataHelper.h"
 
 namespace grm
 {
@@ -33,22 +34,32 @@ namespace grm
 		this->m_ImageWidth = this->m_InputImage->GetLargestPossibleRegion().GetSize()[0];
 		this->m_ImageHeight =this->m_InputImage->GetLargestPossibleRegion().GetSize()[1];
 		this->m_NumberOfComponentsPerPixel = this->m_InputImage->GetNumberOfComponentsPerPixel();
+		
+		std::vector<bool> noDataFlags;
+		std::vector<double> noDataValues;
+		bool noDataPresent = otb::ReadNoDataFlags(this->m_InputImage->GetMetaDataDictionary(),noDataFlags,noDataValues);
 
 		std::size_t idx = 0;
 		ImageIterator it(this->m_InputImage, this->m_InputImage->GetLargestPossibleRegion());
 		for(it.GoToBegin(); !it.IsAtEnd(); ++it)
 		{
-			this->m_Graph.m_Nodes[idx]->m_Means.reserve(this->m_NumberOfComponentsPerPixel);
-			this->m_Graph.m_Nodes[idx]->m_SquareMeans.reserve(this->m_NumberOfComponentsPerPixel);
-			this->m_Graph.m_Nodes[idx]->m_SpectralSum.reserve(this->m_NumberOfComponentsPerPixel);
-			this->m_Graph.m_Nodes[idx]->m_Std.assign(this->m_NumberOfComponentsPerPixel, 0.0f);
-
-			for(std::size_t b = 0; b < this->m_NumberOfComponentsPerPixel; ++b)
-			{
-				this->m_Graph.m_Nodes[idx]->m_Means.push_back(it.Get()[b]);
-				this->m_Graph.m_Nodes[idx]->m_SquareMeans.push_back((it.Get()[b])*(it.Get()[b]));
-				this->m_Graph.m_Nodes[idx]->m_SpectralSum.push_back(it.Get()[b]);
-			}	
+		
+			if (noDataPresent && otb::IsNoData<double>(it.Get(),noDataFlags,noDataValues)) {
+				this->m_Graph.m_Nodes[idx]->m_Expired = true;
+			} else {
+		
+				this->m_Graph.m_Nodes[idx]->m_Means.reserve(this->m_NumberOfComponentsPerPixel);
+				this->m_Graph.m_Nodes[idx]->m_SquareMeans.reserve(this->m_NumberOfComponentsPerPixel);
+				this->m_Graph.m_Nodes[idx]->m_SpectralSum.reserve(this->m_NumberOfComponentsPerPixel);
+				this->m_Graph.m_Nodes[idx]->m_Std.assign(this->m_NumberOfComponentsPerPixel, 0.0f);
+
+				for(std::size_t b = 0; b < this->m_NumberOfComponentsPerPixel; ++b)
+				{
+					this->m_Graph.m_Nodes[idx]->m_Means.push_back(it.Get()[b]);
+					this->m_Graph.m_Nodes[idx]->m_SquareMeans.push_back((it.Get()[b])*(it.Get()[b]));
+					this->m_Graph.m_Nodes[idx]->m_SpectralSum.push_back(it.Get()[b]);
+				}
+			}
 			++idx;
 		}
 	}
diff --git a/include/grmGraphOperations.txx b/include/grmGraphOperations.txx
index bd394eb..9f29696 100644
--- a/include/grmGraphOperations.txx
+++ b/include/grmGraphOperations.txx
@@ -62,6 +62,8 @@ namespace grm
 			
 			seg.m_Graph.m_Nodes.push_back(n);
 		}
+		
+		seg.InitFromImage();
 
 		if(mask == FOUR)
 		{
@@ -71,7 +73,7 @@ namespace grm
 				FOURNeighborhood(neighborhood, r->m_Id, width, height);
 				for(short j = 0; j < 4; ++j)
 				{
-					if(neighborhood[j] > -1)
+					if(neighborhood[j] > -1 && !seg.m_Graph.m_Nodes[neighborhood[j]]->m_Expired)
 						r->m_Edges.push_back(EdgeType( seg.m_Graph.m_Nodes[neighborhood[j]], 0, 1));
 				}
 			}
@@ -84,7 +86,7 @@ namespace grm
 				EIGHTNeighborhood(neighborhood, r->m_Id, width, height);
 				for(short j = 0; j < 8; ++j)
 				{
-					if(neighborhood[j] > -1)
+					if(neighborhood[j] > -1  && !seg.m_Graph.m_Nodes[neighborhood[j]]->m_Expired)
 					{
 						if(j % 2 > 0)
 							r->m_Edges.push_back(EdgeType( seg.m_Graph.m_Nodes[neighborhood[j]], 0, 0));
@@ -94,7 +96,9 @@ namespace grm
 				}
 			}
 		}
-		seg.InitFromImage();
+		
+		RemoveExpiredNodes(seg.m_Graph);
+		
 	}
 
 	template<class TSegmenter>
-- 
GitLab