OTB  9.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  */
86  virtual float ComputeMergingCost(NodePointerType n1, NodePointerType n2) = 0;
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);
128  GRMSetMacro(ParamType, Param);
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
grmMacroGenerator.h
grmGraphToOtbImage.h
grm::GraphToOtbImage
Definition: grmGraphToOtbImage.h:35
grm
Definition: grmBaatzSegmenter.h:22
grm::Segmenter::Self
Segmenter< TImage, TNode, TParam > Self
Definition: grmSegmenter.h:33
grm::Segmenter::m_NumberOfComponentsPerPixel
unsigned int m_NumberOfComponentsPerPixel
Definition: grmSegmenter.h:166
grm::Segmenter::GetLabeledClusteredOutput
LabelImageType::Pointer GetLabeledClusteredOutput()
Definition: grmSegmenter.h:110
grm::GraphOperations::NodePointerType
GraphType::NodePointerType NodePointerType
Definition: grmGraphOperations.h:44
grm::Segmenter::ClusteredImageType
IOType::ClusteredImageType ClusteredImageType
Definition: grmSegmenter.h:43
otb::VectorImage::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbVectorImage.h:53
grm::Segmenter
Definition: grmSegmenter.h:27
grm::Segmenter::NodeType
TNode NodeType
Definition: grmSegmenter.h:35
grm::Segmenter::Segmenter
Segmenter()
Definition: grmSegmenter.h:47
grm::Graph< NodeType >::EdgeType
NodeType::CRPTNeighborType EdgeType
Definition: grmGraph.h:94
grmGraphOperations.h
grm::Segmenter::Update
virtual void Update()
Definition: grmSegmenter.h:57
grm::GraphOperations::InitNodes
static void InitNodes(ImageType *inputImg, SegmenterType &seg, CONNECTIVITY mask)
otb::Image
Creation of an "otb" image which contains metadata.
Definition: otbImage.h:89
grm::Segmenter::GraphType
Graph< NodeType > GraphType
Definition: grmSegmenter.h:37
grm::Segmenter::~Segmenter
~Segmenter()
Definition: grmSegmenter.h:52
grm::Segmenter::m_ImageWidth
unsigned int m_ImageWidth
Definition: grmSegmenter.h:164
grm::Segmenter::m_InputImage
TImage * m_InputImage
Definition: grmSegmenter.h:169
grm::Segmenter::m_Complete
bool m_Complete
Definition: grmSegmenter.h:149
grm::Segmenter::ComputeMergingCost
virtual float ComputeMergingCost(NodePointerType n1, NodePointerType n2)=0
grm::Segmenter::EdgeType
GraphType::EdgeType EdgeType
Definition: grmSegmenter.h:38
grm::BaatzParam
Definition: grmBaatzSegmenter.h:32
grm::GraphOperations::PerfomAllDitheredIterationsWithBF
static bool PerfomAllDitheredIterationsWithBF(SegmenterType &seg)
grm::Segmenter::NodePointerType
GraphOperatorType::NodePointerType NodePointerType
Definition: grmSegmenter.h:40
grm::Segmenter::GRMGetMacro
GRMGetMacro(float, Threshold)
grm::GraphToOtbImage::GetClusteredOutput
ClusteredImageType::Pointer GetClusteredOutput(const GraphType &graph, const unsigned int width, const unsigned int height)
grm::Segmenter::m_NumberOfIterations
unsigned int m_NumberOfIterations
Definition: grmSegmenter.h:155
grm::Segmenter::LabelImageType
IOType::LabelImageType LabelImageType
Definition: grmSegmenter.h:42
grm::Segmenter::GetClusteredImageOutput
ClusteredImageType::Pointer GetClusteredImageOutput()
Definition: grmSegmenter.h:117
grm::Segmenter::GRMSetMacro
GRMSetMacro(bool, DoFastSegmentation)
grm::Segmenter::m_Param
ParamType m_Param
Definition: grmSegmenter.h:161
grm::Segmenter::GetComplete
bool GetComplete()
Definition: grmSegmenter.h:133
grm::GraphOperations::PerfomAllIterationsWithLMBFAndConstThreshold
static bool PerfomAllIterationsWithLMBFAndConstThreshold(SegmenterType &seg)
grm::Segmenter::m_Threshold
float m_Threshold
Definition: grmSegmenter.h:158
grm::Segmenter::GraphOperatorType
GraphOperations< Self > GraphOperatorType
Definition: grmSegmenter.h:39
grm::Graph< NodeType >
grm::Segmenter::m_Graph
GraphType m_Graph
Definition: grmSegmenter.h:143
grm::GraphOperations
Definition: grmGraphOperations.h:34
grm::Segmenter::IOType
GraphToOtbImage< GraphType > IOType
Definition: grmSegmenter.h:41
grm::GraphToOtbImage::GetLabelImage
LabelImageType::Pointer GetLabelImage(const GraphType &graph, const unsigned int width, const unsigned int height)
otb::Image::Pointer
itk::SmartPointer< Self > Pointer
Definition: otbImage.h:97
grm::Segmenter::UpdateSpecificAttributes
virtual void UpdateSpecificAttributes(NodePointerType n1, NodePointerType n2)=0
FOUR
@ FOUR
Definition: grmNeighborhood.h:21
grm::Segmenter::m_DoFastSegmentation
bool m_DoFastSegmentation
Definition: grmSegmenter.h:152
grm::Segmenter::m_ImageHeight
unsigned int m_ImageHeight
Definition: grmSegmenter.h:165
grm::Segmenter::InitFromImage
virtual void InitFromImage()=0
grm::Segmenter::ParamType
TParam ParamType
Definition: grmSegmenter.h:36
grm::Segmenter::SetInput
void SetInput(TImage *in)
Definition: grmSegmenter.h:132
grm::Segmenter::ImageType
TImage ImageType
Definition: grmSegmenter.h:34
otb::VectorImage
Creation of an "otb" vector image which contains metadata.
Definition: otbVectorImage.h:45