OTB  10.0.0
Orfeo Toolbox
grmSegmenter.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_SEGMENTER_H
19 #define GRM_SEGMENTER_H
20 #include "grmMacroGenerator.h"
21 #include "grmGraphOperations.h"
22 #include "grmGraphToOtbImage.h"
23 
24 namespace grm
25 {
26  template<class TImage, class TNode, class TParam>
27  class Segmenter
28  {
29  public:
30 
31  /* Some convenient typedefs */
32 
34  typedef TImage ImageType;
35  typedef TNode NodeType;
36  typedef TParam ParamType;
38  typedef typename GraphType::EdgeType EdgeType;
44 
45  /* Default constructor and destructor */
46 
48  this->m_DoFastSegmentation = false;
49  this->m_NumberOfIterations = 0;
50  this->m_Complete = false;
51  };
53 
54  /*
55  * This method performs the region merging segmentation.
56  */
57  virtual void Update()
58  {
60  bool prev_merged = false;
61 
62  if(this->m_DoFastSegmentation)
63  {
65  }
66  else
67  {
69  }
70 
71  this->m_Complete = !prev_merged;
72  }
73 
74  /* methods to overload */
75 
76  /*
77  * Given 2 smart adjacent node pointers (boost::shared_ptr), this
78  * method has to compute the merging cost which is coded as a float.
79  *
80  * @params
81  * NodePointerType n1 : Smart pointer to node 1
82  * NodePointerType n2 : Smart pointer to node 2
83  *
84  * @return the merging cost.
85  */
87 
88  /*
89  * Given 2 smart adjacent node pointers (boost::shared_ptr), this
90  * method merges th node n2 into the node n1 by updating the customized
91  * attributes of the node n1.
92  *
93  * @params
94  * NodePointerType n1 : Smart pointer to node 1
95  * NodePointerType n2 : Smart pointer to node 2
96  *
97  */
99 
100  /*
101  * Given the input image, this method initializes the
102  * internal and specific attributes of the graph.
103  *
104  * @params
105  * const std::string& inputFileName : input image path
106  */
107  virtual void InitFromImage() = 0;
108 
109  /* Return the label image */
111  {
112  IOType io;
113  auto labelImg = io.GetLabelImage(this->m_Graph, this->m_ImageWidth, this->m_ImageHeight);
114  return labelImg;
115  }
116 
118  {
119  IOType io;
120  auto clusteredImg = io.GetClusteredOutput(this->m_Graph, this->m_ImageWidth, this->m_ImageHeight);
121  return clusteredImg;
122  }
123 
124  /* Set methods */
125  GRMSetMacro(bool, DoFastSegmentation);
126  GRMSetMacro(unsigned int, NumberOfIterations);
127  GRMSetMacro(float, Threshold);
129  GRMSetMacro(unsigned int, ImageWidth);
130  GRMSetMacro(unsigned int, ImageHeight);
131  GRMSetMacro(unsigned int, NumberOfComponentsPerPixel);
132  inline void SetInput(TImage * in){ m_InputImage = in;}
133  inline bool GetComplete(){ return this->m_Complete;}
134 
135  /* Get methods */
136  GRMGetMacro(float, Threshold);
137  GRMGetMacro(unsigned int, ImageWidth);
138  GRMGetMacro(unsigned int, ImageHeight);
139  GRMGetMacro(unsigned int, NumberOfComponentsPerPixel);
140  GRMGetMacro(unsigned int, NumberOfIterations);
141 
142  /* Graph */
144 
145 
146  protected:
147 
148  /* Boolean indicating if the segmentation procedure is achieved */
150 
151  /* Activate the fast segmentation */
153 
154  /* Number of iterations for the Local Mutual Best Fitting segmentation */
155  unsigned int m_NumberOfIterations;
156 
157  /* Limit threshold for the region merging criterion */
158  float m_Threshold;
159 
160  /* Specific parameters required for the region merging criterion */
162 
163  /* Image information (has to be initialized in the method InitFromImage) */
164  unsigned int m_ImageWidth; // Number of columns
165  unsigned int m_ImageHeight; // NUmber of rows
166  unsigned int m_NumberOfComponentsPerPixel; // Number of spectral bands
167 
168  /* Pointer to the input image to segment */
169  TImage * m_InputImage;
170  };
171 } // end of namespace grm
172 
173 #endif
GraphType::NodePointerType NodePointerType
static bool PerfomAllDitheredIterationsWithBF(SegmenterType &seg)
static void InitNodes(ImageType *inputImg, SegmenterType &seg, CONNECTIVITY mask)
static bool PerfomAllIterationsWithLMBFAndConstThreshold(SegmenterType &seg)
LabelImageType::Pointer GetLabelImage(const GraphType &graph, const unsigned int width, const unsigned int height)
ClusteredImageType::Pointer GetClusteredOutput(const GraphType &graph, const unsigned int width, const unsigned int height)
void SetInput(TImage *in)
Definition: grmSegmenter.h:132
GRMSetMacro(bool, DoFastSegmentation)
Graph< NodeType > GraphType
Definition: grmSegmenter.h:37
GRMSetMacro(ParamType, Param)
virtual void InitFromImage()=0
LabelImageType::Pointer GetLabeledClusteredOutput()
Definition: grmSegmenter.h:110
unsigned int m_ImageHeight
Definition: grmSegmenter.h:165
TImage ImageType
Definition: grmSegmenter.h:34
GRMSetMacro(unsigned int, ImageWidth)
unsigned int m_ImageWidth
Definition: grmSegmenter.h:164
GRMGetMacro(float, Threshold)
GraphType::EdgeType EdgeType
Definition: grmSegmenter.h:38
GRMGetMacro(unsigned int, ImageWidth)
unsigned int m_NumberOfComponentsPerPixel
Definition: grmSegmenter.h:166
virtual float ComputeMergingCost(NodePointerType n1, NodePointerType n2)=0
GraphOperations< Self > GraphOperatorType
Definition: grmSegmenter.h:39
GRMSetMacro(unsigned int, NumberOfIterations)
ClusteredImageType::Pointer GetClusteredImageOutput()
Definition: grmSegmenter.h:117
virtual void Update()
Definition: grmSegmenter.h:57
virtual void UpdateSpecificAttributes(NodePointerType n1, NodePointerType n2)=0
Segmenter< TImage, TNode, TParam > Self
Definition: grmSegmenter.h:33
GRMSetMacro(unsigned int, NumberOfComponentsPerPixel)
GraphOperatorType::NodePointerType NodePointerType
Definition: grmSegmenter.h:40
ParamType m_Param
Definition: grmSegmenter.h:161
GraphToOtbImage< GraphType > IOType
Definition: grmSegmenter.h:41
GRMGetMacro(unsigned int, NumberOfComponentsPerPixel)
bool m_DoFastSegmentation
Definition: grmSegmenter.h:152
GRMGetMacro(unsigned int, ImageHeight)
GRMGetMacro(unsigned int, NumberOfIterations)
IOType::LabelImageType LabelImageType
Definition: grmSegmenter.h:42
GraphType m_Graph
Definition: grmSegmenter.h:143
TParam ParamType
Definition: grmSegmenter.h:36
GRMSetMacro(unsigned int, ImageHeight)
bool GetComplete()
Definition: grmSegmenter.h:133
GRMSetMacro(float, Threshold)
IOType::ClusteredImageType ClusteredImageType
Definition: grmSegmenter.h:43
TImage * m_InputImage
Definition: grmSegmenter.h:169
unsigned int m_NumberOfIterations
Definition: grmSegmenter.h:155
Creation of an "otb" image which contains metadata.
Definition: otbImage.h:92
itk::SmartPointer< Self > Pointer
Definition: otbImage.h:97
Creation of an "otb" vector image which contains metadata.
itk::SmartPointer< Self > Pointer
@ FOUR
NodeType::CRPTNeighborType EdgeType
Definition: grmGraph.h:94