Commit 5367de746da80f0e7d82412c9d23e3591c701a0f

Authored by Rémi Cresson (IRSTEA)
1 parent 45a3be1c
Exists in master

FIX: add raffaele's patch

Showing 1 changed file with 48 additions and 7 deletions   Show diff stats
include/grmGraphOperations.txx
... ... @@ -18,6 +18,7 @@
18 18 #ifndef GRM_GRAPH_OPERATIONS_TXX
19 19 #define GRM_GRAPH_OPERATIONS_TXX
20 20 #include <otbImageFileReader.h>
  21 +#include <otbNoDataHelper.h>
21 22 #include "grmGraphOperations.h"
22 23  
23 24 namespace grm
... ... @@ -37,7 +38,40 @@ namespace grm
37 38 const long unsigned int num_nodes = width * height;
38 39  
39 40 seg.m_Graph.m_Nodes.reserve(num_nodes);
  41 +
  42 + std::vector<bool> noDataFlags;
  43 + std::vector<double> noDataValues;
  44 + bool noDataPresent = otb::ReadNoDataFlags(inputImg->GetMetaDataDictionary(), noDataFlags, noDataValues);
  45 +
  46 + typedef itk::ImageRegionIterator<ImageType> ImageIterator;
  47 + ImageIterator it(inputImg, inputImg->GetLargestPossibleRegion());
  48 + unsigned int i = 0;
  49 + for(it.GoToBegin(); !it.IsAtEnd(); ++it) {
  50 +
  51 + NodePointerType n(new NodeType);
  52 + n->m_Id = i;
  53 + n->m_Valid = true;
  54 + n->m_Expired = (noDataPresent && otb::IsNoData<double>(it.Get(), noDataFlags, noDataValues));
  55 + n->m_IsMerged = true; // force to compute costs for the first iteration
  56 + n->m_Perimeter = 4;
  57 + n->m_Area = 1;
  58 + n->m_Bbox.m_UX = i % width;
  59 + n->m_Bbox.m_UY = i / width;
  60 + n->m_Bbox.m_W = 1;
  61 + n->m_Bbox.m_H = 1;
40 62  
  63 + // An initial contour is the one aroun a pixel
  64 + ContourOperator::Push1(n->m_Contour);
  65 + ContourOperator::Push2(n->m_Contour);
  66 + ContourOperator::Push3(n->m_Contour);
  67 + ContourOperator::Push0(n->m_Contour);
  68 +
  69 + seg.m_Graph.m_Nodes.push_back(n);
  70 + ++i;
  71 +
  72 + }
  73 +
  74 + /*
41 75 for(long unsigned int i = 0;
42 76 i < num_nodes;
43 77 ++i)
... ... @@ -62,8 +96,7 @@ namespace grm
62 96  
63 97 seg.m_Graph.m_Nodes.push_back(n);
64 98 }
65   -
66   - seg.InitFromImage();
  99 + */
67 100  
68 101 if(mask == FOUR)
69 102 {
... ... @@ -73,9 +106,10 @@ namespace grm
73 106 FOURNeighborhood(neighborhood, r->m_Id, width, height);
74 107 for(short j = 0; j < 4; ++j)
75 108 {
76   - if(neighborhood[j] > -1 && !seg.m_Graph.m_Nodes[neighborhood[j]]->m_Expired)
  109 + if(neighborhood[j] > -1 && !seg.m_Graph.m_Nodes[neighborhood[j]]->m_Expired)
77 110 r->m_Edges.push_back(EdgeType( seg.m_Graph.m_Nodes[neighborhood[j]], 0, 1));
78 111 }
  112 + r->m_Valid = (r->m_Edges.size() > 0);
79 113 }
80 114 }
81 115 else
... ... @@ -84,19 +118,22 @@ namespace grm
84 118 {
85 119 long int neighborhood[8];
86 120 EIGHTNeighborhood(neighborhood, r->m_Id, width, height);
  121 + bool haveNeighbors = false;
87 122 for(short j = 0; j < 8; ++j)
88 123 {
89   - if(neighborhood[j] > -1 && !seg.m_Graph.m_Nodes[neighborhood[j]]->m_Expired)
  124 + if(neighborhood[j] > -1 && !seg.m_Graph.m_Nodes[neighborhood[j]]->m_Expired)
90 125 {
91   - if(j % 2 > 0)
  126 + if(j % 2 > 0)
92 127 r->m_Edges.push_back(EdgeType( seg.m_Graph.m_Nodes[neighborhood[j]], 0, 0));
93 128 else
94 129 r->m_Edges.push_back(EdgeType( seg.m_Graph.m_Nodes[neighborhood[j]], 0, 1));
95 130 }
96 131 }
  132 + r->m_Valid = (r->m_Edges.size() > 0);
97 133 }
98 134 }
99 135  
  136 + seg.InitFromImage();
100 137 RemoveExpiredNodes(seg.m_Graph);
101 138  
102 139 }
... ... @@ -116,6 +153,8 @@ namespace grm
116 153  
117 154 for(auto& r : seg.m_Graph.m_Nodes)
118 155 {
  156 + if (r->m_Edges.size() == 0) continue;
  157 +
119 158 min_cost = std::numeric_limits<float>::max();
120 159 idx = 0;
121 160 min_idx = 0;
... ... @@ -356,7 +395,7 @@ namespace grm
356 395  
357 396 for(auto& region : seg.m_Graph.m_Nodes)
358 397 {
359   -
  398 +
360 399 auto res_node = CheckLMBF(region, seg.GetThreshold());
361 400  
362 401 if(res_node)
... ... @@ -506,7 +545,7 @@ namespace grm
506 545 return false;
507 546  
508 547 for(auto& r : seg.m_Graph.m_Nodes)
509   - r->m_Valid = true;
  548 + r->m_Valid = (r->m_Edges.size() > 0);
510 549  
511 550 return merged;
512 551 }
... ... @@ -546,8 +585,10 @@ namespace grm
546 585 ++idx;
547 586 }
548 587 }
  588 +
549 589 assert(min_idx < r->m_Edges.size());
550 590 std::swap(r->m_Edges[0], r->m_Edges[min_idx]);
  591 +
551 592 }
552 593 } // end of namespace grm
553 594  
... ...