Commit 4f606e0713b1740697628dd030ca0e51f0a49bd8

Authored by Rémi Cresson (IRSTEA)
1 parent 5367de74
Exists in master

ENH: implement Raffaele Gaetano's patch and refactorization

include/grmGraphOperations.txx
... ... @@ -28,114 +28,51 @@ namespace grm
28 28 SegmenterType& seg,
29 29 CONNECTIVITY mask)
30 30 {
31   - unsigned int width, height;
32   -
33   - {
34   - width = inputImg->GetLargestPossibleRegion().GetSize()[0];
35   - height = inputImg->GetLargestPossibleRegion().GetSize()[1];
36   - }
37   -
38   - const long unsigned int num_nodes = width * height;
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;
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 31  
74   - /*
75   - for(long unsigned int i = 0;
76   - i < num_nodes;
77   - ++i)
78   - {
79   - NodePointerType n(new NodeType);
80   - n->m_Id = i;
81   - n->m_Valid = true;
82   - n->m_Expired = false;
83   - n->m_IsMerged = true; // force to compute costs for the first iteration
84   - n->m_Perimeter = 4;
85   - n->m_Area = 1;
86   - n->m_Bbox.m_UX = i % width;
87   - n->m_Bbox.m_UY = i / width;
88   - n->m_Bbox.m_W = 1;
89   - n->m_Bbox.m_H = 1;
90   -
91   - // An initial contour is the one aroun a pixel
92   - ContourOperator::Push1(n->m_Contour);
93   - ContourOperator::Push2(n->m_Contour);
94   - ContourOperator::Push3(n->m_Contour);
95   - ContourOperator::Push0(n->m_Contour);
96   -
97   - seg.m_Graph.m_Nodes.push_back(n);
98   - }
99   - */
  32 + // Initialize nodes
  33 + seg.InitFromImage();
  34 +
  35 + // Compute the neighborhood for each node
  36 + const unsigned int width = inputImg->GetLargestPossibleRegion().GetSize()[0];
  37 + const unsigned int height = inputImg->GetLargestPossibleRegion().GetSize()[1];
  38 + if(mask == FOUR)
  39 + {
  40 + for(auto& r : seg.m_Graph.m_Nodes)
  41 + {
  42 + long int neighborhood[4];
  43 + FOURNeighborhood(neighborhood, r->m_Id, width, height);
  44 + for(short j = 0; j < 4; ++j)
  45 + {
  46 + if(neighborhood[j] > -1 && !seg.m_Graph.m_Nodes[neighborhood[j]]->m_Expired)
  47 + r->m_Edges.push_back(EdgeType( seg.m_Graph.m_Nodes[neighborhood[j]], 0, 1));
  48 + }
  49 + r->m_Valid = (r->m_Edges.size() > 0);
  50 + }
  51 + }
  52 + else
  53 + {
  54 + for(auto& r : seg.m_Graph.m_Nodes)
  55 + {
  56 + long int neighborhood[8];
  57 + EIGHTNeighborhood(neighborhood, r->m_Id, width, height);
  58 + bool haveNeighbors = false;
  59 + for(short j = 0; j < 8; ++j)
  60 + {
  61 + if(neighborhood[j] > -1 && !seg.m_Graph.m_Nodes[neighborhood[j]]->m_Expired)
  62 + {
  63 + if(j % 2 > 0)
  64 + r->m_Edges.push_back(EdgeType( seg.m_Graph.m_Nodes[neighborhood[j]], 0, 0));
  65 + else
  66 + r->m_Edges.push_back(EdgeType( seg.m_Graph.m_Nodes[neighborhood[j]], 0, 1));
  67 + }
  68 + }
  69 + r->m_Valid = (r->m_Edges.size() > 0);
  70 + }
  71 + }
  72 +
  73 + // Remove no-data nodes
  74 + RemoveExpiredNodes(seg.m_Graph);
100 75  
101   - if(mask == FOUR)
102   - {
103   - for(auto& r : seg.m_Graph.m_Nodes)
104   - {
105   - long int neighborhood[4];
106   - FOURNeighborhood(neighborhood, r->m_Id, width, height);
107   - for(short j = 0; j < 4; ++j)
108   - {
109   - if(neighborhood[j] > -1 && !seg.m_Graph.m_Nodes[neighborhood[j]]->m_Expired)
110   - r->m_Edges.push_back(EdgeType( seg.m_Graph.m_Nodes[neighborhood[j]], 0, 1));
111   - }
112   - r->m_Valid = (r->m_Edges.size() > 0);
113   - }
114   - }
115   - else
116   - {
117   - for(auto& r : seg.m_Graph.m_Nodes)
118   - {
119   - long int neighborhood[8];
120   - EIGHTNeighborhood(neighborhood, r->m_Id, width, height);
121   - bool haveNeighbors = false;
122   - for(short j = 0; j < 8; ++j)
123   - {
124   - if(neighborhood[j] > -1 && !seg.m_Graph.m_Nodes[neighborhood[j]]->m_Expired)
125   - {
126   - if(j % 2 > 0)
127   - r->m_Edges.push_back(EdgeType( seg.m_Graph.m_Nodes[neighborhood[j]], 0, 0));
128   - else
129   - r->m_Edges.push_back(EdgeType( seg.m_Graph.m_Nodes[neighborhood[j]], 0, 1));
130   - }
131   - }
132   - r->m_Valid = (r->m_Edges.size() > 0);
133   - }
134   - }
135   -
136   - seg.InitFromImage();
137   - RemoveExpiredNodes(seg.m_Graph);
138   -
139 76 }
140 77  
141 78 template<class TSegmenter>
... ... @@ -592,4 +529,4 @@ namespace grm
592 529 }
593 530 } // end of namespace grm
594 531  
595   -#endif
596 532 \ No newline at end of file
  533 +#endif
... ...
include/grmSegmenter.h
... ... @@ -20,6 +20,9 @@
20 20 #include "grmMacroGenerator.h"
21 21 #include "grmGraphOperations.h"
22 22 #include "grmGraphToOtbImage.h"
  23 +#include "grmNeighborhood.h"
  24 +#include "grmGraphOperations.h"
  25 +
23 26 #include <otbImageFileReader.h>
24 27 #include <itkImageRegionIterator.h>
25 28 #include "otbNoDataHelper.h"
... ... @@ -126,6 +129,9 @@ namespace grm
126 129 this->m_ImageHeight =this->m_InputImage->GetLargestPossibleRegion().GetSize()[1];
127 130 this->m_NumberOfComponentsPerPixel = this->m_InputImage->GetNumberOfComponentsPerPixel();
128 131  
  132 + const long unsigned int num_nodes = this->m_ImageWidth * this->m_ImageHeight;
  133 + this->m_Graph.m_Nodes.reserve(num_nodes);
  134 +
129 135 std::vector<bool> noDataFlags;
130 136 std::vector<double> noDataValues;
131 137 bool noDataPresent = otb::ReadNoDataFlags(this->m_InputImage->GetMetaDataDictionary(),noDataFlags,noDataValues);
... ... @@ -134,16 +140,30 @@ namespace grm
134 140 ImageIterator it(this->m_InputImage, this->m_InputImage->GetLargestPossibleRegion());
135 141 for(it.GoToBegin(); !it.IsAtEnd(); ++it)
136 142 {
137   -
138   - if (noDataPresent && otb::IsNoData<double>(it.Get(),noDataFlags,noDataValues))
139   - {
140   - this->m_Graph.m_Nodes[idx]->m_Expired = true;
141   - }
142   - else
143   - {
144   - UpdateSpecificAttributes(this->m_Graph.m_Nodes[idx], it.Get());
145   - }
  143 + NodePointerType n(new NodeType);
  144 + n->m_Id = idx;
  145 + n->m_Valid = true;
  146 + n->m_Expired = (noDataPresent && otb::IsNoData<double>(it.Get(),noDataFlags,noDataValues));
  147 + n->m_IsMerged = true; // force to compute costs for the first iteration
  148 + n->m_Perimeter = 4;
  149 + n->m_Area = 1;
  150 + n->m_Bbox.m_UX = it.GetIndex()[0];
  151 + n->m_Bbox.m_UY = it.GetIndex()[1];
  152 + n->m_Bbox.m_W = 1;
  153 + n->m_Bbox.m_H = 1;
  154 +
  155 + // An initial contour is the one aroun a pixel
  156 + lp::ContourOperations::Push1(n->m_Contour);
  157 + lp::ContourOperations::Push2(n->m_Contour);
  158 + lp::ContourOperations::Push3(n->m_Contour);
  159 + lp::ContourOperations::Push0(n->m_Contour);
  160 +
  161 + // Update attributes
  162 + UpdateSpecificAttributes(n, it.Get());
  163 +
  164 + this->m_Graph.m_Nodes.push_back(n);
146 165 ++idx;
  166 +
147 167 }
148 168 }
149 169  
... ...