Commit 45a3be1cee75d0948c866193fd50836d185a71f5

Authored by Rémi Cresson (IRSTEA)
1 parent 273d9b6a
Exists in master

REFAC: refactorisation segmenter

include/grmBaatzSegmenter.h
... ... @@ -43,6 +43,7 @@ namespace grm
43 43 /* Some convenient typedefs */
44 44 typedef Segmenter<TImage, BaatzNode, BaatzParam> Superclass;
45 45 typedef TImage ImageType;
  46 + typedef typename ImageType::PixelType PixelType;
46 47 typedef BaatzParam ParameterType;
47 48 typedef typename Superclass::GraphType GraphType;
48 49 typedef BaatzNode NodeType;
... ... @@ -53,8 +54,8 @@ namespace grm
53 54  
54 55 float ComputeMergingCost(NodePointerType n1, NodePointerType n2);
55 56 void UpdateSpecificAttributes(NodePointerType n1, NodePointerType n2);
56   - void InitFromImage();
  57 + void UpdateSpecificAttributes(NodePointerType n, PixelType p);
57 58 };
58 59 } // end of namespace grm
59 60 #include "grmBaatzSegmenter.txx"
60   -#endif
61 61 \ No newline at end of file
  62 +#endif
... ...
include/grmBaatzSegmenter.txx
... ... @@ -17,52 +17,28 @@
17 17 =========================================================================*/
18 18 #ifndef GRM_BAATZ_SEGMENTER_TXX
19 19 #define GRM_BAATZ_SEGMENTER_TXX
20   -#include <otbImageFileReader.h>
21   -#include <itkImageRegionIterator.h>
22 20 #include "grmBaatzSegmenter.h"
23   -#include "otbNoDataHelper.h"
24 21  
25 22 namespace grm
26 23 {
27 24  
28   - template<class TImage>
29   - void
30   - BaatzSegmenter<TImage>::InitFromImage()
31   - {
32   - typedef itk::ImageRegionIterator<TImage> ImageIterator;
33   -
34   - this->m_ImageWidth = this->m_InputImage->GetLargestPossibleRegion().GetSize()[0];
35   - this->m_ImageHeight =this->m_InputImage->GetLargestPossibleRegion().GetSize()[1];
36   - this->m_NumberOfComponentsPerPixel = this->m_InputImage->GetNumberOfComponentsPerPixel();
37   -
38   - std::vector<bool> noDataFlags;
39   - std::vector<double> noDataValues;
40   - bool noDataPresent = otb::ReadNoDataFlags(this->m_InputImage->GetMetaDataDictionary(),noDataFlags,noDataValues);
41   -
42   - std::size_t idx = 0;
43   - ImageIterator it(this->m_InputImage, this->m_InputImage->GetLargestPossibleRegion());
44   - for(it.GoToBegin(); !it.IsAtEnd(); ++it)
45   - {
46   -
47   - if (noDataPresent && otb::IsNoData<double>(it.Get(),noDataFlags,noDataValues)) {
48   - this->m_Graph.m_Nodes[idx]->m_Expired = true;
49   - } else {
50   -
51   - this->m_Graph.m_Nodes[idx]->m_Means.reserve(this->m_NumberOfComponentsPerPixel);
52   - this->m_Graph.m_Nodes[idx]->m_SquareMeans.reserve(this->m_NumberOfComponentsPerPixel);
53   - this->m_Graph.m_Nodes[idx]->m_SpectralSum.reserve(this->m_NumberOfComponentsPerPixel);
54   - this->m_Graph.m_Nodes[idx]->m_Std.assign(this->m_NumberOfComponentsPerPixel, 0.0f);
55   -
56   - for(std::size_t b = 0; b < this->m_NumberOfComponentsPerPixel; ++b)
57   - {
58   - this->m_Graph.m_Nodes[idx]->m_Means.push_back(it.Get()[b]);
59   - this->m_Graph.m_Nodes[idx]->m_SquareMeans.push_back((it.Get()[b])*(it.Get()[b]));
60   - this->m_Graph.m_Nodes[idx]->m_SpectralSum.push_back(it.Get()[b]);
61   - }
62   - }
63   - ++idx;
64   - }
65   - }
  25 +template<class TImage>
  26 +void
  27 +BaatzSegmenter<TImage>::UpdateSpecificAttributes(NodePointerType n, PixelType p)
  28 +{
  29 +
  30 + n->m_Means.reserve(p.GetSize());
  31 + n->m_SquareMeans.reserve(p.GetSize());
  32 + n->m_SpectralSum.reserve(p.GetSize());
  33 + n->m_Std.assign(p.GetSize(), 0.0f);
  34 +
  35 + for(std::size_t b = 0; b < p.GetSize(); ++b)
  36 + {
  37 + n->m_Means.push_back(p[b]);
  38 + n->m_SquareMeans.push_back((p[b])*(p[b]));
  39 + n->m_SpectralSum.push_back(p[b]);
  40 + }
  41 +}
66 42  
67 43 template<class TImage>
68 44 float
... ... @@ -128,4 +104,4 @@ namespace grm
128 104 }
129 105 } // end of namespace grm
130 106  
131   -#endif
132 107 \ No newline at end of file
  108 +#endif
... ...
include/grmFullLambdaScheduleSegmenter.h
... ... @@ -35,6 +35,8 @@ namespace grm
35 35 /* Some convenient typedefs */
36 36 typedef Segmenter<TImage, FLSNode, FLSParam> Superclass;
37 37 typedef TImage ImageType;
  38 + typedef typename ImageType::PixelType PixelType;
  39 +
38 40 typedef typename Superclass::GraphType GraphType;
39 41 typedef typename Superclass::NodePointerType NodePointerType;
40 42 typedef typename Superclass::GraphOperatorType GraphOperatorType;
... ... @@ -42,8 +44,8 @@ namespace grm
42 44  
43 45 float ComputeMergingCost(NodePointerType n1, NodePointerType n2);
44 46 void UpdateSpecificAttributes(NodePointerType n1, NodePointerType n2);
45   - void InitFromImage();
  47 + void UpdateSpecificAttributes(NodePointerType n, PixelType p);
46 48 };
47 49 } // end of namespace grm
48 50 #include "grmFullLambdaScheduleSegmenter.txx"
49   -#endif
50 51 \ No newline at end of file
  52 +#endif
... ...
include/grmFullLambdaScheduleSegmenter.txx
... ... @@ -22,29 +22,19 @@
22 22  
23 23 namespace grm
24 24 {
25   - template<class TImage>
26   - void
27   - FullLambdaScheduleSegmenter<TImage>::InitFromImage()
28   - {
29   - typedef itk::ImageRegionIterator<TImage> ImageIterator;
  25 +template<class TImage>
  26 +void
  27 +FullLambdaScheduleSegmenter<TImage>::UpdateSpecificAttributes(NodePointerType n, PixelType p)
  28 +{
30 29  
31   - this->m_ImageWidth = this->m_InputImage->GetLargestPossibleRegion().GetSize()[0];
32   - this->m_ImageHeight =this->m_InputImage->GetLargestPossibleRegion().GetSize()[1];
33   - this->m_NumberOfComponentsPerPixel = this->m_InputImage->GetNumberOfComponentsPerPixel();
  30 + n->m_Means.reserve(p.GetSize());
34 31  
35   - std::size_t idx = 0;
36   - ImageIterator it(this->m_InputImage, this->m_InputImage->GetLargestPossibleRegion());
37   - for(it.GoToBegin(); !it.IsAtEnd(); ++it)
38   - {
39   - this->m_Graph.m_Nodes[idx]->m_Means.reserve(this->m_NumberOfComponentsPerPixel);
  32 + for(std::size_t b = 0; b < p.GetSize(); ++b)
  33 + {
  34 + n->m_Means.push_back(p[b]);
  35 + }
40 36  
41   - for(std::size_t b = 0; b < this->m_NumberOfComponentsPerPixel; ++b)
42   - {
43   - this->m_Graph.m_Nodes[idx]->m_Means.push_back(it.Get()[b]);
44   - }
45   - ++idx;
46   - }
47   - }
  37 +}
48 38  
49 39 template<class TImage>
50 40 float
... ...
include/grmSegmenter.h
... ... @@ -20,6 +20,9 @@
20 20 #include "grmMacroGenerator.h"
21 21 #include "grmGraphOperations.h"
22 22 #include "grmGraphToOtbImage.h"
  23 +#include <otbImageFileReader.h>
  24 +#include <itkImageRegionIterator.h>
  25 +#include "otbNoDataHelper.h"
23 26  
24 27 namespace grm
25 28 {
... ... @@ -32,6 +35,7 @@ namespace grm
32 35  
33 36 typedef Segmenter<TImage, TNode, TParam> Self;
34 37 typedef TImage ImageType;
  38 + typedef typename ImageType::PixelType PixelType;
35 39 typedef TNode NodeType;
36 40 typedef TParam ParamType;
37 41 typedef Graph<NodeType> GraphType;
... ... @@ -98,13 +102,50 @@ namespace grm
98 102 virtual void UpdateSpecificAttributes(NodePointerType n1, NodePointerType n2) = 0;
99 103  
100 104 /*
  105 + * Given 1 smart adjacent node pointer (boost::shared_ptr), this
  106 + * method updates the customized attributes of the node according
  107 + * to the given pixel.
  108 + *
  109 + * @params
  110 + * NodePointerType n : Smart pointer to node
  111 + * PixelType p: pixel used for attributes computation
  112 + *
  113 + */
  114 + virtual void UpdateSpecificAttributes(NodePointerType n, PixelType p) = 0;
  115 +
  116 + /*
101 117 * Given the input image, this method initializes the
102 118 * internal and specific attributes of the graph.
103 119 *
104   - * @params
105   - * const std::string& inputFileName : input image path
106 120 */
107   - virtual void InitFromImage() = 0;
  121 + virtual void InitFromImage()
  122 + {
  123 + typedef itk::ImageRegionIterator<ImageType> ImageIterator;
  124 +
  125 + this->m_ImageWidth = this->m_InputImage->GetLargestPossibleRegion().GetSize()[0];
  126 + this->m_ImageHeight =this->m_InputImage->GetLargestPossibleRegion().GetSize()[1];
  127 + this->m_NumberOfComponentsPerPixel = this->m_InputImage->GetNumberOfComponentsPerPixel();
  128 +
  129 + std::vector<bool> noDataFlags;
  130 + std::vector<double> noDataValues;
  131 + bool noDataPresent = otb::ReadNoDataFlags(this->m_InputImage->GetMetaDataDictionary(),noDataFlags,noDataValues);
  132 +
  133 + std::size_t idx = 0;
  134 + ImageIterator it(this->m_InputImage, this->m_InputImage->GetLargestPossibleRegion());
  135 + for(it.GoToBegin(); !it.IsAtEnd(); ++it)
  136 + {
  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 + }
  146 + ++idx;
  147 + }
  148 + }
108 149  
109 150 /* Return the label image */
110 151 inline typename LabelImageType::Pointer GetLabeledClusteredOutput()
... ... @@ -170,4 +211,4 @@ namespace grm
170 211 };
171 212 } // end of namespace grm
172 213  
173   -#endif
174 214 \ No newline at end of file
  215 +#endif
... ...
include/grmSpringSegmenter.h
... ... @@ -35,6 +35,8 @@ namespace grm
35 35 /* Some convenient typedefs */
36 36 typedef Segmenter<TImage, SpringNode, SpringParam> Superclass;
37 37 typedef TImage ImageType;
  38 + typedef typename ImageType::PixelType PixelType;
  39 +
38 40 typedef typename Superclass::GraphType GraphType;
39 41 typedef typename Superclass::NodePointerType NodePointerType;
40 42 typedef typename Superclass::GraphOperatorType GraphOperatorType;
... ... @@ -42,7 +44,7 @@ namespace grm
42 44  
43 45 float ComputeMergingCost(NodePointerType n1, NodePointerType n2);
44 46 void UpdateSpecificAttributes(NodePointerType n1, NodePointerType n2);
45   - void InitFromImage();
  47 + void UpdateSpecificAttributes(NodePointerType n, PixelType p);
46 48 };
47 49 } // end of namespace grm
48 50 #include "grmSpringSegmenter.txx"
... ...
include/grmSpringSegmenter.txx
... ... @@ -26,26 +26,14 @@ namespace grm
26 26  
27 27 template<class TImage>
28 28 void
29   - SpringSegmenter<TImage>::InitFromImage()
  29 + SpringSegmenter<TImage>::UpdateSpecificAttributes(NodePointerType n, PixelType p)
30 30 {
31   - typedef itk::ImageRegionIterator<TImage> ImageIterator;
  31 + n->m_Means.reserve(p.GetSize());
32 32  
33   - this->m_ImageWidth = this->m_InputImage->GetLargestPossibleRegion().GetSize()[0];
34   - this->m_ImageHeight =this->m_InputImage->GetLargestPossibleRegion().GetSize()[1];
35   - this->m_NumberOfComponentsPerPixel = this->m_InputImage->GetNumberOfComponentsPerPixel();
36   -
37   - std::size_t idx = 0;
38   - ImageIterator it(this->m_InputImage, this->m_InputImage->GetLargestPossibleRegion());
39   - for(it.GoToBegin(); !it.IsAtEnd(); ++it)
40   - {
41   - this->m_Graph.m_Nodes[idx]->m_Means.reserve(this->m_NumberOfComponentsPerPixel);
42   -
43   - for(std::size_t b = 0; b < this->m_NumberOfComponentsPerPixel; ++b)
  33 + for(std::size_t b = 0; b < p.GetSize(); ++b)
44 34 {
45   - this->m_Graph.m_Nodes[idx]->m_Means.push_back(it.Get()[b]);
  35 + n->m_Means.push_back(p[b]);
46 36 }
47   - ++idx;
48   - }
49 37 }
50 38  
51 39 template<class TImage>
... ...