OTB  9.0.0
Orfeo Toolbox
grmGraphOperations.h
Go to the documentation of this file.
1 /*=========================================================================
2 
3  Program: Generic Region Merging Library
4  Language: C++
5  author: Lassalle Pierre
6  contact: lassallepierre34@gmail.com
7 
8 
9 
10  Copyright (c) Centre National d'Etudes Spatiales. All rights reserved
11 
12 
13  This software is distributed WITHOUT ANY WARRANTY; without even
14  the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
15  PURPOSE. See the above copyright notices for more information.
16 
17 =========================================================================*/
18 #ifndef GRM_GRAPH_OPERATIONS_H
19 #define GRM_GRAPH_OPERATIONS_H
20 #include "grmGraph.h"
21 #include "grmNeighborhood.h"
22 #include <iostream>
23 #include <cassert>
24 #include <limits>
25 #include <map>
26 #include <utility>
27 #include <set>
28 #include <random>
29 #include <numeric>
30 
31 namespace grm
32 {
33  template<class TSegmenter>
35  {
36  public:
37 
38  /* Some convenient typedefs */
39  typedef TSegmenter SegmenterType;
40  typedef typename SegmenterType::ImageType ImageType;
41  typedef typename SegmenterType::GraphType GraphType;
42  typedef typename GraphType::NodeType NodeType;
43  typedef typename GraphType::EdgeType EdgeType;
44  typedef typename GraphType::NodePointerType NodePointerType;
45  typedef typename GraphType::NodeListType NodeList;
46  typedef typename GraphType::NodeIteratorType NodeIterator;
47  typedef typename GraphType::NodeConstIteratorType NodeConstIterator;
48  typedef typename GraphType::EdgeListType EdgeList;
49  typedef typename GraphType::EdgeIteratorType EdgeIterator;
50  typedef typename GraphType::EdgeConstIteratorType EdgeConstIterator;
51 
53 
54 
55  /*
56  * Given the size of the input image and the mask of the
57  * neighborhood, we initialize a new graph of nodes
58  *
59  * @params:
60  * GraphType& graph: reference to a graph of nodes
61  * const unsigned int width: width of the input image
62  * const unsigned int height: height of the input image
63  * CONNECTIVITY mask : mask of the neighborhood (4X4 or 8X8)
64  */
65  static void InitNodes(ImageType * inputImg,
66  SegmenterType& seg,
67  CONNECTIVITY mask);
68 
69  /*
70  * Given a graph of nodes, we explore all the nodes
71  * and for each node we compute his merging costs
72  * with all its neighboring nodes given a function
73  * to compute the merging cost between two nodes.
74  *
75  * @params:
76  * GraphType& graph: reference to the graph of nodes
77  * float(*fptr)(NodeType*, NodeType*): pointer to the function
78  * to compute the merging cost between two adjacent nodes.
79  */
80  static void UpdateMergingCosts(SegmenterType& seg);
81 
82  /*
83  * Given a node A, we analyse its best node B.
84  * If the node A is also node B's best node
85  * then it returns a pointer to node B if node A 's id
86  * is smaller or a pointer to node A if node B's id is
87  * smaller
88  * else it returns a null pointer.
89  * (Local Mutual Best Fitting Heuristic)
90  *
91  * @params:
92  * NodeType * a : Pointer to the node A
93  * float t : threshold of the criterion
94  */
95  static NodePointerType CheckLMBF(NodePointerType, float t);
96 
97  /*
98  * Given a node A, we analyse its best node B.
99  * If the criterion is checked and the node B
100  * is valid then it returns a pointer to the
101  * node A if node's A id is smaller or a pointer
102  * to node B if node B's id is smaller
103  * else it returns a null pointer.
104  *
105  * @params:
106  * NodeType * a : pointer to node A
107  * float t : threshold of the criterion
108  */
109  static NodePointerType CheckBF(NodePointerType a, float t);
110 
111  /*
112  * Given the current node and the target node, it returns
113  * the edge from the current node targeting to the target
114  * node.
115  *
116  * @params
117  * const NodeType * n : pointer to the current node.
118  * const NodeType * target : pointer to the target node.
119  * @return an iterator pointing to the candidate edge.
120  */
122 
123  /*
124  * Given a node a and the node b to be merged into node a,
125  * it updates the neighbors of node a with respect to the
126  * neighbors of node b.
127  *
128  * @params
129  * NodeType * a : pointer to node a.
130  * NodeType * b : pointer to node b.
131  */
133 
134  /*
135  * Given 2 nodes A and B (node B being merged into node A)
136  * we update the internal attributes of node A with respect
137  * to node B.
138  *
139  * @params:
140  * NodeType * a: pointer to node A.
141  * NodeType * b: pointer to node B.
142  */
144  NodePointerType b,
145  const unsigned int width);
146 
147  /*
148  * Given a graph, it removes all the expired nodes.
149  *
150  * @params
151  * GraphType& graph : reference to the graph.
152  */
153  static void RemoveExpiredNodes(GraphType& graph);
154 
155 
156  /*
157  * Given a graph, a region merging algorithm, a threshold
158  * and the dimension of the image, it performs one iteration
159  * of the merging process using the local mutual best fitting
160  * heuristic.
161  *
162  * @params
163  * GraphType& graph : reference to the graph
164  * SegmenterType& seg : reference to the region merging algorithm.
165  * const float threshold : threshold for this iteration.
166  * const unsigned int width : width of the image.
167  * const unsigned int height : height of the image.
168  *
169  * @return a boolean pointing out if there was at least a fusion
170  * of nodes.
171  */
172  static bool PerfomOneIterationWithLMBF(SegmenterType& seg);
173 
174  /*
175  * Given a graph, a region merging algorithm, a threshold,
176  * the number of iterations to apply and the dimension of the image,
177  * it performs all the iterations of the merging process using the
178  * local mutual best fitting heuristic.
179  * This method can be used when the threshold is constant during
180  * the region merging process.
181  *
182  * @params
183  * GraphType& graph : reference to the graph
184  * SegmenterType& seg : reference to the region merging algorithm.
185  * const float threshold : threshold for this iteration.
186  * const unsigned int numberOfIterations: number of iteration to perform.
187  * const unsigned int width : width of the image.
188  * const unsigned int height : height of the image.
189  *
190  * @return a boolean pointing out if there was at least a fusion
191  * of nodes.
192  */
194 
195 
197 
199 
201 
202  };
203 } // end of namespace lsrm
204 
205 #include "grmGraphOperations.txx"
206 #endif
grm
Definition: grmBaatzSegmenter.h:22
grm::GraphOperations::EdgeIterator
GraphType::EdgeIteratorType EdgeIterator
Definition: grmGraphOperations.h:49
grm::GraphOperations::NodePointerType
GraphType::NodePointerType NodePointerType
Definition: grmGraphOperations.h:44
grm::GraphOperations::CheckBF
static NodePointerType CheckBF(NodePointerType a, float t)
grm::GraphOperations::EdgeConstIterator
GraphType::EdgeConstIteratorType EdgeConstIterator
Definition: grmGraphOperations.h:50
grm::GraphOperations::InitNodes
static void InitNodes(ImageType *inputImg, SegmenterType &seg, CONNECTIVITY mask)
grm::GraphOperations::ComputeMergingCostsUsingDither
static void ComputeMergingCostsUsingDither(NodePointerType r, SegmenterType &seg)
grm::GraphOperations::NodeIterator
GraphType::NodeIteratorType NodeIterator
Definition: grmGraphOperations.h:46
grmNeighborhood.h
grm::GraphOperations::NodeConstIterator
GraphType::NodeConstIteratorType NodeConstIterator
Definition: grmGraphOperations.h:47
grm::GraphOperations::SegmenterType
TSegmenter SegmenterType
Definition: grmGraphOperations.h:39
grm::GraphOperations::PerfomAllDitheredIterationsWithBF
static bool PerfomAllDitheredIterationsWithBF(SegmenterType &seg)
grm::GraphOperations::EdgeType
GraphType::EdgeType EdgeType
Definition: grmGraphOperations.h:43
grm::GraphOperations::UpdateNeighbors
static void UpdateNeighbors(NodePointerType a, NodePointerType b)
grm::GraphOperations::UpdateMergingCosts
static void UpdateMergingCosts(SegmenterType &seg)
grm::GraphOperations::NodeType
GraphType::NodeType NodeType
Definition: grmGraphOperations.h:42
grm::GraphOperations::RemoveExpiredNodes
static void RemoveExpiredNodes(GraphType &graph)
otb::NodeType
NodeType
Definition: otbDataNode.h:39
grm::GraphOperations::PerfomOneIterationWithLMBF
static bool PerfomOneIterationWithLMBF(SegmenterType &seg)
grm::GraphOperations::CheckLMBF
static NodePointerType CheckLMBF(NodePointerType, float t)
grm::GraphOperations::GraphType
SegmenterType::GraphType GraphType
Definition: grmGraphOperations.h:41
CONNECTIVITY
CONNECTIVITY
Definition: grmNeighborhood.h:21
grm::GraphOperations::UpdateInternalAttributes
static void UpdateInternalAttributes(NodePointerType a, NodePointerType b, const unsigned int width)
grm::GraphOperations::ImageType
SegmenterType::ImageType ImageType
Definition: grmGraphOperations.h:40
grm::GraphOperations::PerfomAllIterationsWithLMBFAndConstThreshold
static bool PerfomAllIterationsWithLMBFAndConstThreshold(SegmenterType &seg)
grm::GraphOperations::EdgeList
GraphType::EdgeListType EdgeList
Definition: grmGraphOperations.h:48
grm::GraphOperations::FindEdge
static EdgeIterator FindEdge(NodePointerType n, NodePointerType target)
lp::ContourOperations
Definition: lpContour.h:55
grm::GraphOperations::NodeList
GraphType::NodeListType NodeList
Definition: grmGraphOperations.h:45
grm::GraphOperations
Definition: grmGraphOperations.h:34
grmGraph.h
grm::GraphOperations::PerfomOneDitheredIterationWithBF
static bool PerfomOneDitheredIterationWithBF(SegmenterType &seg)