Commit bc9957b6c916a24ba05b5f65714a85513b1609b3

Authored by Pierre Lassalle
1 parent 9369f8d1
Exists in master

remove useless files

code/src/algo/BaatzAlgorithm.h~
... ... @@ -1,138 +0,0 @@
1   -/*=========================================================================
2   -
3   - Program: Large Scale Segmentation (LSS)
4   - Language: C++
5   - author: Lassalle Pierre
6   -
7   -
8   -
9   - Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
10   -
11   -
12   - This software is distributed WITHOUT ANY WARRANTY; without even
13   - the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14   - PURPOSE. See the above copyright notices for more information.
15   -
16   -=========================================================================*/
17   -#ifndef __BaatzAlgorithm_h
18   -#define __BaatzAlgorithm_h
19   -
20   -#include <otbImage.h>
21   -#include <otbVectorImage.h>
22   -#include <otbImageFileReader.h>
23   -#include <otbImageFileWriter.h>
24   -#include <itkImageRegionIterator.h>
25   -#include "lss_GenericLMBFRegionMergingHandler.h"
26   -#include "lss_MacroGenerator.h"
27   -
28   -struct BaatzRegion : public lss::CRPT_Region<BaatzRegion>
29   -{
30   - // Specific attributes for Baatz & Shape criterion
31   - float m_Area;
32   - float m_Perimeter;
33   - std::vector<float> m_Avg_Color;
34   - std::vector<float> m_Avg_Color_Square;
35   - std::vector<float> m_Color_Sum;
36   - std::vector<float> m_Std_Color;
37   -
38   - // Serialization for the storage on the disk (information on the number of bands is considered)
39   - unsigned int GetSizeOfData()
40   - {
41   - return (3 + 4*m_Avg_Color.size());
42   - }
43   -
44   - // store elements in a contiguous way in the memory
45   - void SerializeData(float * data_block)
46   - {
47   - data_block[0] = m_Area;
48   - data_block[1] = m_Perimeter;
49   - data_block[2] = static_cast<float>(m_Avg_Color.size());
50   - for(unsigned int b =0; b<m_Avg_Color.size(); b++)
51   - {
52   - data_block[3+b] = m_Avg_Color[b];
53   - data_block[3 + m_Avg_Color.size() + b] = m_Avg_Color_Square[b];
54   - data_block[3 + 2*m_Avg_Color.size() + b] = m_Color_Sum[b];
55   - data_block[3 + 3*m_Avg_Color.size() + b] = m_Std_Color[b];
56   - }
57   - }
58   -
59   - // Read from the contiguous block of memory returned
60   - void InitAttributesFromSerializeData(float * data_block)
61   - {
62   - m_Area = data_block[0];
63   - m_Perimeter = data_block[1];
64   - unsigned int bands = static_cast<unsigned int>(data_block[2]);
65   - m_Avg_Color.reserve(bands);
66   - m_Avg_Color_Square.reserve(bands);
67   - m_Color_Sum.reserve(bands);
68   - m_Std_Color.reserve(bands);
69   - for(unsigned int b=0; b<bands; b++)
70   - {
71   - m_Avg_Color.push_back(data_block[3+b]);
72   - m_Avg_Color_Square.push_back(data_block[3 + bands + b]);
73   - m_Color_Sum.push_back(data_block[3 + 2*bands + b]);
74   - m_Std_Color.push_back(data_block[3 + 3*bands + b]);
75   - }
76   - }
77   -};
78   -
79   -struct BaatzParameters
80   -{
81   - float m_ColorWeight;
82   - float m_CompactnessWeight;
83   - float m_Scale;
84   -};
85   -
86   -template<class TInputImage>
87   -class BaatzAlgorithm
88   -{
89   - public:
90   -
91   - // Mandatory typedef
92   - typedef BaatzRegion RegionType;
93   - typedef BaatzParameters ParameterType;
94   - typedef std::shared_ptr<BaatzRegion> RegionPointerType;
95   - typedef std::vector<RegionPointerType> RegionListType;
96   - typedef TInputImage InputImageType;
97   - typedef otb::ImageFileReader<InputImageType> ReaderType;
98   - typedef otb::ImageFileWriter<InputImageType> WriterType;
99   - typedef itk::ImageRegionIterator<InputImageType> IteratorType;
100   - typedef itk::ImageRegion<2>::IndexType BboxIndexType;
101   - typedef itk::ImageRegion<2>::SizeType BboxSizeType;
102   -
103   - // Optional: Label Image
104   - typedef unsigned int LabelPixelType;
105   - typedef otb::Image<LabelPixelType,2> LabelImageType;
106   - typedef itk::ImageRegionIterator<LabelImageType> LabelIteratorType;
107   - typedef otb::ImageFileWriter<LabelImageType> LabelWriterType;
108   -
109   - SetMacro(ParameterType, Parameters)
110   - SetMacro(unsigned int, NumberOfIterations);
111   - GetMacro(bool, PreviousMerge);
112   - GetMacro(RegionListType, RegionList);
113   - SetMacro(RegionListType, RegionList);
114   - void SetDimensionForEncoder(unsigned int, unsigned int);
115   -
116   - void InitFromTiffImage(const std::string& input_file);
117   - void Segmentation();
118   - void WriteResultingImage(const std::string& ofname);
119   - void WriteLabelImage(const std::string& ofname);
120   -
121   - private:
122   -
123   - // Specific methods to compute the fusion cost
124   - double ColorComponentCostFusion(RegionPointerType r1, RegionPointerType r2);
125   - double CompactnessComponentCostFusion(RegionPointerType r1, RegionPointerType r2);
126   - void UpdateAttribute(RegionPointerType r1, RegionPointerType r2);
127   -
128   - bool m_PreviousMerge;
129   - ParameterType m_Parameters;
130   - unsigned int m_NumberOfIterations;
131   - RegionListType m_RegionList;
132   - lss::GenericLMBFRegionMergingHandler<BaatzRegion> m_RMHandler;
133   - std::function<double(RegionPointerType, RegionPointerType)> m_ComputeCostFunction;
134   -};
135   -
136   -#include "BaatzAlgorithm.txx"
137   -
138   -#endif
code/src/algo/BaatzAlgorithm.txx~
... ... @@ -1,336 +0,0 @@
1   -/*=========================================================================
2   -
3   - Program: Large Scale Segmentation (LSS)
4   - Language: C++
5   - author: Lassalle Pierre
6   -
7   -
8   -
9   - Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
10   -
11   -
12   - This software is distributed WITHOUT ANY WARRANTY; without even
13   - the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14   - PURPOSE. See the above copyright notices for more information.
15   -
16   -=========================================================================*/
17   -#ifndef __BaatzAlgorithm_txx
18   -#define __BaatzAlgorithm_txx
19   -
20   -template<class TInputImage>
21   -void
22   -BaatzAlgorithm<TInputImage>::SetDimensionForEncoder(unsigned int cols, unsigned int rows)
23   -{
24   - m_RMHandler.m_Encoder.SetCols(cols);
25   - m_RMHandler.m_Encoder.SetRows(rows);
26   -}
27   -
28   -template<class TInputImage>
29   -void
30   -BaatzAlgorithm<TInputImage>::InitFromTiffImage(const std::string& input_file)
31   -{
32   - auto reader = ReaderType::New();
33   - reader->SetFileName(input_file);
34   - reader->Update();
35   -
36   - auto regionToProcess = reader->GetOutput()->GetLargestPossibleRegion();
37   - unsigned int cols = regionToProcess.GetSize()[0];
38   - unsigned int rows = regionToProcess.GetSize()[1];
39   - unsigned int bands = reader->GetOutput()->GetNumberOfComponentsPerPixel();
40   -
41   - m_RMHandler.m_Encoder.SetRows(rows); // Penser à n'utiliser qu'une seule méthode (SetRegionToProcess)
42   - m_RMHandler.m_Encoder.SetCols(cols);
43   -
44   - m_RegionList.reserve(cols*rows);
45   -
46   - IteratorType it(reader->GetOutput(), regionToProcess);
47   -
48   - // Create the vertices
49   - long unsigned int idx = 0;
50   - for(it.GoToBegin(); !it.IsAtEnd(); ++it)
51   - {
52   - auto r = std::make_shared<BaatzRegion>();
53   - m_RMHandler.InitGenericRegion(r, idx);
54   - r->m_Area = 1;
55   - r->m_Perimeter = 4;
56   - r->m_Avg_Color.reserve(bands);
57   - r->m_Avg_Color_Square.reserve(bands);
58   - r->m_Color_Sum.reserve(bands);
59   - r->m_Std_Color.reserve(bands);
60   -
61   - for(unsigned int b = 0; b < bands; b++)
62   - {
63   - r->m_Avg_Color.push_back(it.Get()[b]);
64   - r->m_Avg_Color_Square.push_back(pow(it.Get()[b], 2));
65   - r->m_Color_Sum.push_back(it.Get()[b]);
66   - r->m_Std_Color.push_back(0);
67   - }
68   -
69   - m_RegionList.push_back(r);
70   - ++idx;
71   - }
72   - m_RMHandler.InitNeighborhood(m_RegionList);
73   -}
74   -
75   -template<class TInputImage>
76   -double
77   -BaatzAlgorithm<TInputImage>::ColorComponentCostFusion(RegionPointerType r1, RegionPointerType r2)
78   -{
79   - unsigned int bands = r1->m_Avg_Color.size();
80   - float mean[bands], colorSum[bands];
81   - float squarePixels[bands];
82   - float stddev[bands];
83   - float stddevNew[bands];
84   - double color_f[bands];
85   - double color_h;
86   - float a_current, a_neighbor, a_sum;
87   - a_current = r1->m_Area;
88   - a_neighbor = r2->m_Area;
89   - a_sum = a_current+a_neighbor;
90   -
91   - for (unsigned int b = 0; b < bands; b++)
92   - {
93   - mean[b] = ((r1->m_Avg_Color[b]*a_current)+(r2->m_Avg_Color[b]*a_neighbor))/a_sum;
94   - squarePixels[b] = (r1->m_Avg_Color_Square[b])+(r2->m_Avg_Color_Square[b]);
95   - colorSum[b] = r1->m_Color_Sum[b] + r2->m_Color_Sum[b];
96   - stddev[b] = 0;
97   - stddevNew[b] = 0;
98   - }
99   -
100   - for(unsigned int b = 0; b < bands; b++)
101   - {
102   - stddevNew[b] = squarePixels[b] - 2*mean[b]*colorSum[b] + a_sum*mean[b]*mean[b];
103   - }
104   -
105   - /* calculates color factor per band and total */
106   - color_h = 0;
107   - for (unsigned int b = 0; b < bands; b++)
108   - {
109   - stddev[b] = sqrt(stddevNew[b]/a_sum);
110   - color_f[b] = (a_current*r1->m_Std_Color[b]) + (a_neighbor*r2->m_Std_Color[b]);
111   - color_f[b] = (a_sum*stddev[b])- color_f[b];
112   - color_h += color_f[b];
113   - }
114   - return color_h;
115   -}
116   -
117   -template<class TInputImage>
118   -double
119   -BaatzAlgorithm<TInputImage>::CompactnessComponentCostFusion(RegionPointerType r1, RegionPointerType r2)
120   -{
121   - double spatial_h, smooth_f, compact_f;
122   - float area[3], perimeter[3], b_box_len[3]; /* 0-current segment; 1-neighbor segment; 2-merged (new) segment */
123   -
124   - /* area */
125   - area[0] = r1->m_Area;
126   - area[1] = r2->m_Area;
127   - area[2] = area[0]+area[1];
128   -
129   - /* perimeter */
130   - perimeter[0] = r1->m_Perimeter;
131   - perimeter[1] = r2->m_Perimeter;
132   - perimeter[2] = r1->m_Perimeter + r2->m_Perimeter - 2*m_RMHandler.GetConnections(r1, r2);
133   -
134   - /* bounding box lenght */
135   - auto mbbox = m_RMHandler.GetResultingBbox(r1, r2);
136   - b_box_len[0] = (r1->m_Bbox.GetSize(0))*2 + (r1->m_Bbox.GetSize(1))*2;
137   - b_box_len[1] = (r2->m_Bbox.GetSize(0))*2 + (r2->m_Bbox.GetSize(1))*2;
138   - b_box_len[2] = (mbbox.GetSize(0))*2 + (mbbox.GetSize(1))*2;
139   -
140   - /* smoothness factor */
141   - smooth_f = (area[2]*perimeter[2]/b_box_len[2] -
142   - (area[1]*perimeter[1]/b_box_len[1] + area[0]*perimeter[0]/b_box_len[0]));
143   -
144   - /* compactness factor */
145   - compact_f = (area[2]*perimeter[2]/sqrt(area[2]) -
146   - (area[1]*perimeter[1]/sqrt(area[1]) + area[0]*perimeter[0]/sqrt(area[0])));
147   -
148   - /* spatial heterogeneity */
149   - spatial_h = m_Parameters.m_CompactnessWeight*compact_f + (1-m_Parameters.m_CompactnessWeight)*smooth_f;
150   -
151   - return spatial_h;
152   -}
153   -
154   -template<class TInputImage>
155   -void
156   -BaatzAlgorithm<TInputImage>::Segmentation()
157   -{
158   - m_PreviousMerge = false;
159   - unsigned int max_iter = 500;
160   - if(m_NumberOfIterations > 0)
161   - max_iter = m_NumberOfIterations;
162   -
163   - unsigned int curr_step = 0;
164   - double cost;
165   - RegionPointerType bestneigh = nullptr;
166   - bool prev_merged = true;
167   -
168   - m_ComputeCostFunction = [&](RegionPointerType r1, RegionPointerType r2)->double
169   - {
170   - double spectral_h;
171   - double spatial_h;
172   - double cost = 0;
173   -
174   - spectral_h = ColorComponentCostFusion(r1, r2);
175   - cost += m_Parameters.m_ColorWeight * spectral_h;
176   -
177   - if(cost < m_Parameters.m_Scale)
178   - {
179   - spatial_h = CompactnessComponentCostFusion(r1, r2);
180   - cost += (1-m_Parameters.m_ColorWeight)*spatial_h;
181   - return cost;
182   - }
183   - else
184   - return cost;
185   - };
186   -
187   - while(curr_step < max_iter && prev_merged)
188   - {
189   - prev_merged = false;
190   - std::cout << curr_step << std::endl;
191   - m_RMHandler.UpdateMergingCost(m_RegionList, m_ComputeCostFunction);
192   -
193   - for(auto& r: m_RegionList)
194   - {
195   - if(m_RMHandler.IsLMBF(r, m_Parameters.m_Scale))
196   - {
197   - auto res = m_RMHandler.GetRegionsToMerge(r);
198   - //std::cout << "Merge " << res.first->m_Id << " with " << res.second->m_Id << std::endl;
199   - // User contribution
200   - UpdateAttribute(res.first, res.second);
201   - m_RMHandler.Update(res);
202   - prev_merged = true;
203   - }
204   - }
205   - m_RMHandler.RemoveExpiredVertices(m_RegionList);
206   - curr_step++;
207   - m_PreviousMerge = prev_merged;
208   - }
209   -}
210   -
211   -template<class TInputImage>
212   -void
213   -BaatzAlgorithm<TInputImage>::UpdateAttribute(RegionPointerType r1, RegionPointerType r2)
214   -{
215   - // Update the spectral attributes
216   -
217   - unsigned int bands = r1->m_Avg_Color.size();
218   - float mean[bands], colorSum[bands];
219   - float squarePixels[bands];
220   - float a_current, a_neighbor, a_sum;
221   - a_current = r1->m_Area;
222   - a_neighbor = r2->m_Area;
223   - a_sum = a_current+a_neighbor;
224   -
225   - for (unsigned int b = 0; b < bands; b++)
226   - {
227   - mean[b] = ((r1->m_Avg_Color[b]*a_current)+(r2->m_Avg_Color[b]*a_neighbor))/a_sum;
228   - squarePixels[b] = (r1->m_Avg_Color_Square[b])+(r2->m_Avg_Color_Square[b]);
229   - colorSum[b] = r1->m_Color_Sum[b] + r2->m_Color_Sum[b];
230   - }
231   -
232   - for(unsigned int b = 0; b < bands; b++)
233   - {
234   - r1->m_Avg_Color[b] = mean[b];
235   - r1->m_Avg_Color_Square[b] = squarePixels[b];
236   - r1->m_Color_Sum[b] = colorSum[b];
237   - r1->m_Std_Color[b] = sqrt((squarePixels[b] - 2*mean[b]*colorSum[b] + a_sum*mean[b]*mean[b])/a_sum);
238   - }
239   -
240   - // Update spatial attributes
241   - r1->m_Area += r2->m_Area;
242   - r1->m_Perimeter += r2->m_Perimeter - 2*m_RMHandler.GetConnections(r1,r2);
243   -}
244   -
245   -template<class TInputImage>
246   -void
247   -BaatzAlgorithm<TInputImage>::WriteLabelImage(const std::string& ofname)
248   -{
249   - typename InputImageType::IndexType index;
250   - typename InputImageType::SizeType size;
251   - typename InputImageType::RegionType region;
252   - auto label_image = LabelImageType::New();
253   -
254   - index[0] = 0;
255   - index[1] = 0;
256   - size[0] = m_RMHandler.m_Encoder.GetCols();
257   - size[1] = m_RMHandler.m_Encoder.GetRows();
258   - region.SetIndex(index);
259   - region.SetSize(size);
260   -
261   - label_image->SetRegions(region);
262   - label_image->Allocate();
263   -
264   - unsigned int label = 1;
265   - for(auto& v : m_RegionList)
266   - {
267   - auto pixels = m_RMHandler.m_Encoder.GenerateAllPixels(v->m_Id, v->m_Contour, v->m_Bbox);
268   - for(auto& pix: pixels)
269   - {
270   - index[0] = pix % size[0];
271   - index[1] = pix / size[0];
272   - auto label_pixel = label_image->GetPixel(index);
273   - label_image->SetPixel(index, label);
274   - }
275   - label++;
276   - }
277   -
278   - auto label_writer = LabelWriterType::New();
279   - label_writer->SetFileName(ofname);
280   - label_writer->SetInput(label_image);
281   - label_writer->Update();
282   -}
283   -
284   -template<class TInputImage>
285   -void
286   -BaatzAlgorithm<TInputImage>::WriteResultingImage(const std::string& ofname)
287   -{
288   - auto out_image = InputImageType::New();
289   - typename InputImageType::IndexType index;
290   - typename InputImageType::SizeType size;
291   - typename InputImageType::RegionType region;
292   -
293   - index[0] = 0;
294   - index[1] = 0;
295   - size[0] = m_RMHandler.m_Encoder.GetCols();
296   - size[1] = m_RMHandler.m_Encoder.GetRows();
297   - region.SetIndex(index);
298   - region.SetSize(size);
299   - out_image->SetRegions(region);
300   - out_image->SetNumberOfComponentsPerPixel(3);
301   - out_image->Allocate();
302   -
303   - for(unsigned int r = 0; r< size[1]; r++)
304   - {
305   - for(unsigned int c = 0; c<size[0]; c++)
306   - {
307   - index[0] = c;
308   - index[1] = r;
309   - auto pixel_value = out_image->GetPixel(index);
310   - for(int b=0; b<3; b++)
311   - pixel_value[b] = 0;
312   - out_image->SetPixel(index, pixel_value);
313   - }
314   - }
315   - unsigned int label = 1;
316   - for(auto& v : m_RegionList)
317   - {
318   - auto pixels = m_RMHandler.m_Encoder.GeneratePixels(v->m_Id, v->m_Contour);
319   - for(auto& pix: pixels)
320   - {
321   - index[0] = pix % size[0];
322   - index[1] = pix / size[0];
323   - auto pixel_value = out_image->GetPixel(index);
324   - for(int b=0; b<3; b++)
325   - pixel_value[b] = v->m_Avg_Color[b];
326   - out_image->SetPixel(index, pixel_value);
327   - }
328   - }
329   -
330   - auto writer = WriterType::New();
331   - writer->SetFileName(ofname);
332   - writer->SetInput(out_image);
333   - writer->Update();
334   -}
335   -
336   -#endif
code/src/algo/FLSAlgorithm.h~
... ... @@ -1,108 +0,0 @@
1   -/*=========================================================================
2   -
3   - Program: Large Scale Segmentation (LSS)
4   - Language: C++
5   - author: Lassalle Pierre
6   -
7   -
8   -
9   - Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
10   -
11   -
12   - This software is distributed WITHOUT ANY WARRANTY; without even
13   - the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14   - PURPOSE. See the above copyright notices for more information.
15   -
16   -=========================================================================*/
17   -#ifndef __FLSAlgorithm_h
18   -#define __FLSAlgorithm_h
19   -
20   -#include <otbImage.h>
21   -#include <otbVectorImage.h>
22   -#include <otbImageFileReader.h>
23   -#include <otbImageFileWriter.h>
24   -#include <itkImageRegionIterator.h>
25   -#include "lss_GenericLMBFRegionMergingHandler.h"
26   -#include "lss_MacroGenerator.h"
27   -
28   -struct FLSRegion : public lss::CRPT_Region<FLSRegion>
29   -{
30   - // Specific attributes for Spring criterion
31   - float m_Area;
32   - std::vector<float> m_Avg_Color;
33   -
34   - // Serialization for the storage on the disk (Information on the number of bands is considered)
35   - unsigned int GetSizeOfData()
36   - {
37   - return (2 + m_Avg_Color.size());
38   - }
39   -
40   - // Store elements in a contiguous way in the memory
41   - void SerializeData(float * data_block)
42   - {
43   - data_block[0] = m_Area;
44   - data_block[1] = static_cast<float>(m_Avg_Color.size());
45   - for(unsigned int b = 0; b<m_Avg_Color.size(); b++)
46   - data_block[2+b] = m_Avg_Color[b];
47   - }
48   -
49   - // Read from the contiguous block of memory returned
50   - void InitAttributesFromSerializeData(float * data_block)
51   - {
52   - m_Area = data_block[0];
53   - unsigned int bands = static_cast<unsigned int>(data_block[1]);
54   - m_Avg_Color.reserve(bands);
55   - for(unsigned int b = 0; b<bands; b++)
56   - m_Avg_Color.push_back(data_block[2+b]);
57   - }
58   -};
59   -
60   -typedef double FLSParameter;
61   -
62   -template<class TInputImage>
63   -class FLSAlgorithm
64   -{
65   - public:
66   - // Mandatory typedef
67   - typedef FLSRegion RegionType;
68   - typedef FLSParameter ParameterType;
69   - typedef std::shared_ptr<FLSRegion> RegionPointerType;
70   - typedef std::vector<RegionPointerType> RegionListType;
71   - typedef TInputImage InputImageType;
72   - typedef otb::ImageFileReader<InputImageType> ReaderType;
73   - typedef otb::ImageFileWriter<InputImageType> WriterType;
74   - typedef itk::ImageRegionIterator<InputImageType> IteratorType;
75   - typedef itk::ImageRegion<2>::IndexType BboxIndexType;
76   - typedef itk::ImageRegion<2>::SizeType BboxSizeType;
77   -
78   - // Optional: Label Image
79   - typedef unsigned int LabelPixelType;
80   - typedef otb::Image<LabelPixelType,2> LabelImageType;
81   - typedef itk::ImageRegionIterator<LabelImageType> LabelIteratorType;
82   - typedef otb::ImageFileWriter<LabelImageType> LabelWriterType;
83   -
84   - SetMacro(ParameterType, Parameters);
85   - SetMacro(unsigned int, NumberOfIterations);
86   - GetMacro(bool, PreviousMerge);
87   - GetMacro(RegionListType, RegionList);
88   - SetMacro(RegionListType, RegionList);
89   - void SetDimensionForEncoder(unsigned int, unsigned int);
90   -
91   - void InitFromTiffImage(const std::string& input_file);
92   - void Segmentation();
93   - void WriteResultingImage(const std::string& ofname);
94   -
95   - private:
96   - void UpdateAttribute(RegionPointerType r1, RegionPointerType r2);
97   -
98   - bool m_PreviousMerge;
99   - ParameterType m_Parameters;
100   - unsigned int m_NumberOfIterations;
101   - RegionListType m_RegionList;
102   - lss::GenericLMBFRegionMergingHandler<FLSRegion> m_RMHandler;
103   - std::function<double(RegionPointerType, RegionPointerType)> m_ComputeCostFunction;
104   -};
105   -
106   -#include "FLSAlgorithm.txx"
107   -
108   -#endif
code/src/algo/FLSAlgorithm.txx~
... ... @@ -1,226 +0,0 @@
1   -/*=========================================================================
2   -
3   - Program: Large Scale Segmentation (LSS)
4   - Language: C++
5   - author: Lassalle Pierre
6   -
7   -
8   -
9   - Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
10   -
11   -
12   - This software is distributed WITHOUT ANY WARRANTY; without even
13   - the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14   - PURPOSE. See the above copyright notices for more information.
15   -
16   -=========================================================================*/
17   -#ifndef __FLSAlgorithm_txx
18   -#define __FLSAlgorithm_txx
19   -
20   -template<class TInputImage>
21   -void
22   -FLSAlgorithm<TInputImage>::SetDimensionForEncoder(unsigned int cols, unsigned int rows)
23   -{
24   - m_RMHandler.m_Encoder.SetCols(cols);
25   - m_RMHandler.m_Encoder.SetRows(rows);
26   -}
27   -
28   -template<class TInputImage>
29   -void
30   -FLSAlgorithm<TInputImage>::UpdateAttribute(RegionPointerType r1, RegionPointerType r2)
31   -{
32   - // Update the spectral attributes
33   -
34   - unsigned int bands = r1->m_Avg_Color.size();
35   - float a_current, a_neighbor, a_sum;
36   - a_current = r1->m_Area;
37   - a_neighbor = r2->m_Area;
38   - a_sum = a_current+a_neighbor;
39   -
40   - for (unsigned int b = 0; b < bands; b++)
41   - r1->m_Avg_Color[b] = ((r1->m_Avg_Color[b]*a_current)+(r2->m_Avg_Color[b]*a_neighbor))/a_sum;
42   -
43   - // Update spatial attributes
44   - r1->m_Area += r2->m_Area;
45   -}
46   -
47   -
48   -template<class TInputImage>
49   -void
50   -FLSAlgorithm<TInputImage>::InitFromTiffImage(const std::string& input_file)
51   -{
52   - auto reader = ReaderType::New();
53   - reader->SetFileName(input_file);
54   - reader->Update();
55   -
56   - auto regionToProcess = reader->GetOutput()->GetLargestPossibleRegion();
57   - unsigned int cols = regionToProcess.GetSize()[0];
58   - unsigned int rows = regionToProcess.GetSize()[1];
59   - unsigned int bands = reader->GetOutput()->GetNumberOfComponentsPerPixel();
60   -
61   - m_RMHandler.m_Encoder.SetRows(rows); // Penser à n'utiliser qu'une seule méthode (SetRegionToProcess)
62   - m_RMHandler.m_Encoder.SetCols(cols);
63   -
64   - m_RegionList.reserve(cols*rows);
65   -
66   - IteratorType it(reader->GetOutput(), regionToProcess);
67   -
68   - // Create the vertices
69   - long unsigned int idx = 0;
70   - for(it.GoToBegin(); !it.IsAtEnd(); ++it)
71   - {
72   - auto r = std::make_shared<FLSRegion>();
73   - m_RMHandler.InitGenericRegion(r, idx);
74   - r->m_Area = 1;
75   - r->m_Avg_Color.reserve(bands);
76   -
77   - for(unsigned int b = 0; b < bands; b++)
78   - r->m_Avg_Color.push_back(it.Get()[b]);
79   -
80   - m_RegionList.push_back(r);
81   - ++idx;
82   - }
83   - m_RMHandler.InitNeighborhood(m_RegionList);
84   -}
85   -
86   -template<class TInputImage>
87   -void
88   -FLSAlgorithm<TInputImage>::Segmentation()
89   -{
90   - m_PreviousMerge = false;
91   - unsigned int max_iter = 500;
92   - if(m_NumberOfIterations > 0)
93   - max_iter = m_NumberOfIterations;
94   -
95   - unsigned int curr_step = 0;
96   - double cost;
97   - double threshold = m_Parameters * m_Parameters;
98   - RegionPointerType bestneigh = nullptr;
99   - bool prev_merged = true;
100   -
101   - m_ComputeCostFunction = [&](RegionPointerType r1, RegionPointerType r2)->double
102   - {
103   - double cost = 0.0;
104   - unsigned int bands = r1->m_Avg_Color.size();
105   - for(unsigned int b = 0; b < bands; b++)
106   - cost += ((r1->m_Avg_Color[b] - r2->m_Avg_Color[b])*(r1->m_Avg_Color[b] - r2->m_Avg_Color[b]));
107   - cost = (((r1->m_Area * r2->m_Area) / (r1->m_Area + r2->m_Area)) * cost) / (m_RMHandler.GetConnections(r1,r2));
108   - return cost;
109   - };
110   -
111   - while(curr_step < max_iter && prev_merged)
112   - {
113   - prev_merged = false;
114   - std::cout << curr_step << std::endl;
115   - m_RMHandler.UpdateMergingCost(m_RegionList, m_ComputeCostFunction);
116   -
117   - for(auto& r: m_RegionList)
118   - {
119   - if(m_RMHandler.IsLMBF(r, threshold))
120   - {
121   - auto res = m_RMHandler.GetRegionsToMerge(r);
122   - //std::cout << "Merge " << res.first->m_Id << " with " << res.second->m_Id << std::endl;
123   - // User contribution
124   - UpdateAttribute(res.first, res.second);
125   - m_RMHandler.Update(res);
126   - prev_merged = true;
127   - }
128   - }
129   - m_RMHandler.RemoveExpiredVertices(m_RegionList);
130   - curr_step++;
131   - m_PreviousMerge = prev_merged;
132   - }
133   -}
134   -
135   -template<class TInputImage>
136   -void
137   -FLSAlgorithm<TInputImage>::WriteLabelImage(const std::string& ofname)
138   -{
139   - typename InputImageType::IndexType index;
140   - typename InputImageType::SizeType size;
141   - typename InputImageType::RegionType region;
142   - auto label_image = LabelImageType::New();
143   -
144   - index[0] = 0;
145   - index[1] = 0;
146   - size[0] = m_RMHandler.m_Encoder.GetCols();
147   - size[1] = m_RMHandler.m_Encoder.GetRows();
148   - region.SetIndex(index);
149   - region.SetSize(size);
150   -
151   - label_image->SetRegions(region);
152   - label_image->Allocate();
153   -
154   - unsigned int label = 1;
155   - for(auto& v : m_RegionList)
156   - {
157   - auto pixels = m_RMHandler.m_Encoder.GenerateAllPixels(v->m_Id, v->m_Contour, v->m_Bbox);
158   - for(auto& pix: pixels)
159   - {
160   - index[0] = pix % size[0];
161   - index[1] = pix / size[0];
162   - auto label_pixel = label_image->GetPixel(index);
163   - label_image->SetPixel(index, label);
164   - }
165   - label++;
166   - }
167   -
168   - auto label_writer = LabelWriterType::New();
169   - label_writer->SetFileName(ofname);
170   - label_writer->SetInput(label_image);
171   - label_writer->Update();
172   -}
173   -
174   -template<class TInputImage>
175   -void
176   -FLSAlgorithm<TInputImage>::WriteResultingImage(const std::string& ofname)
177   -{
178   - auto out_image = InputImageType::New();
179   - typename InputImageType::IndexType index;
180   - typename InputImageType::SizeType size;
181   - typename InputImageType::RegionType region;
182   -
183   - index[0] = 0;
184   - index[1] = 0;
185   - size[0] = m_RMHandler.m_Encoder.GetCols();
186   - size[1] = m_RMHandler.m_Encoder.GetRows();
187   - region.SetIndex(index);
188   - region.SetSize(size);
189   - out_image->SetRegions(region);
190   - out_image->SetNumberOfComponentsPerPixel(3);
191   - out_image->Allocate();
192   -
193   - for(unsigned int r = 0; r< size[1]; r++)
194   - {
195   - for(unsigned int c = 0; c<size[0]; c++)
196   - {
197   - index[0] = c;
198   - index[1] = r;
199   - auto pixel_value = out_image->GetPixel(index);
200   - for(int b=0; b<3; b++)
201   - pixel_value[b] = 0;
202   - out_image->SetPixel(index, pixel_value);
203   - }
204   - }
205   - unsigned int label = 1;
206   - for(auto& v : m_RegionList)
207   - {
208   - auto pixels = m_RMHandler.m_Encoder.GeneratePixels(v->m_Id, v->m_Contour);
209   - for(auto& pix: pixels)
210   - {
211   - index[0] = pix % size[0];
212   - index[1] = pix / size[0];
213   - auto pixel_value = out_image->GetPixel(index);
214   - for(int b=0; b<3; b++)
215   - pixel_value[b] = v->m_Avg_Color[b];
216   - out_image->SetPixel(index, pixel_value);
217   - }
218   - }
219   -
220   - auto writer = WriterType::New();
221   - writer->SetFileName(ofname);
222   - writer->SetInput(out_image);
223   - writer->Update();
224   -}
225   -
226   -#endif
code/src/algo/SpringAlgorithm.h~
... ... @@ -1,109 +0,0 @@
1   -/*=========================================================================
2   -
3   - Program: Large Scale Segmentation (LSS)
4   - Language: C++
5   - author: Lassalle Pierre
6   -
7   -
8   -
9   - Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
10   -
11   -
12   - This software is distributed WITHOUT ANY WARRANTY; without even
13   - the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14   - PURPOSE. See the above copyright notices for more information.
15   -
16   -=========================================================================*/
17   -#ifndef __SpringAlgorithm_h
18   -#define __SpringAlgorithm_h
19   -
20   -#include <otbImage.h>
21   -#include <otbVectorImage.h>
22   -#include <otbImageFileReader.h>
23   -#include <otbImageFileWriter.h>
24   -#include <itkImageRegionIterator.h>
25   -#include "lss_GenericLMBFRegionMergingHandler.h"
26   -#include "lss_MacroGenerator.h"
27   -
28   -struct SpringRegion : public lss::CRPT_Region<SpringRegion>
29   -{
30   - // Specific attributes for Spring criterion
31   - float m_Area;
32   - std::vector<float> m_Avg_Color;
33   -
34   - // Serialization for the storage on the disk (Information on the number of bands is considered)
35   - unsigned int GetSizeOfData()
36   - {
37   - return (2 + m_Avg_Color.size());
38   - }
39   -
40   - // Store elements in a contiguous way in the memory
41   - void SerializeData(float * data_block)
42   - {
43   - data_block[0] = m_Area;
44   - data_block[1] = static_cast<float>(m_Avg_Color.size());
45   - for(unsigned int b = 0; b<m_Avg_Color.size(); b++)
46   - data_block[2+b] = m_Avg_Color[b];
47   - }
48   -
49   - // Read from the contiguous block of memory returned
50   - void InitAttributesFromSerializeData(float * data_block)
51   - {
52   - m_Area = data_block[0];
53   - unsigned int bands = static_cast<unsigned int>(data_block[1]);
54   - m_Avg_Color.reserve(bands);
55   - for(unsigned int b = 0; b<bands; b++)
56   - m_Avg_Color.push_back(data_block[2+b]);
57   - }
58   -};
59   -
60   -typedef double SpringParameter;
61   -
62   -template<class TInputImage>
63   -class SpringAlgorithm
64   -{
65   - public:
66   - // Mandatory typedef
67   - typedef SpringRegion RegionType;
68   - typedef SpringParameter ParameterType;
69   - typedef std::shared_ptr<SpringRegion> RegionPointerType;
70   - typedef std::vector<RegionPointerType> RegionListType;
71   - typedef TInputImage InputImageType;
72   - typedef otb::ImageFileReader<InputImageType> ReaderType;
73   - typedef otb::ImageFileWriter<InputImageType> WriterType;
74   - typedef itk::ImageRegionIterator<InputImageType> IteratorType;
75   - typedef itk::ImageRegion<2>::IndexType BboxIndexType;
76   - typedef itk::ImageRegion<2>::SizeType BboxSizeType;
77   -
78   - // Optional: Label Image
79   - typedef unsigned int LabelPixelType;
80   - typedef otb::Image<LabelPixelType,2> LabelImageType;
81   - typedef itk::ImageRegionIterator<LabelImageType> LabelIteratorType;
82   - typedef otb::ImageFileWriter<LabelImageType> LabelWriterType;
83   -
84   - SetMacro(ParameterType, Parameters);
85   - SetMacro(unsigned int, NumberOfIterations);
86   - GetMacro(bool, PreviousMerge);
87   - GetMacro(RegionListType, RegionList);
88   - SetMacro(RegionListType, RegionList);
89   - void SetDimensionForEncoder(unsigned int, unsigned int);
90   -
91   - void InitFromTiffImage(const std::string& input_file);
92   - void Segmentation();
93   - void WriteResultingImage(const std::string& ofname);
94   - void WriteLabelImage(const std::string& ofname);
95   -
96   - private:
97   - void UpdateAttribute(RegionPointerType r1, RegionPointerType r2);
98   -
99   - bool m_PreviousMerge;
100   - ParameterType m_Parameters;
101   - unsigned int m_NumberOfIterations;
102   - RegionListType m_RegionList;
103   - lss::GenericLMBFRegionMergingHandler<SpringRegion> m_RMHandler;
104   - std::function<double(RegionPointerType, RegionPointerType)> m_ComputeCostFunction;
105   -};
106   -
107   -#include "SpringAlgorithm.txx"
108   -
109   -#endif
code/src/algo/SpringAlgorithm.txx~
... ... @@ -1,225 +0,0 @@
1   -/*=========================================================================
2   -
3   - Program: Large Scale Segmentation (LSS)
4   - Language: C++
5   - author: Lassalle Pierre
6   -
7   -
8   -
9   - Copyright (c) Centre National d'Etudes Spatiales. All rights reserved.
10   -
11   -
12   - This software is distributed WITHOUT ANY WARRANTY; without even
13   - the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR
14   - PURPOSE. See the above copyright notices for more information.
15   -
16   -=========================================================================*/
17   -#ifndef __SpringAlgorithm_txx
18   -#define __SpringAlgorithm_txx
19   -
20   -template<class TInputImage>
21   -void
22   -SpringAlgorithm<TInputImage>::SetDimensionForEncoder(unsigned int cols, unsigned int rows)
23   -{
24   - m_RMHandler.m_Encoder.SetCols(cols);
25   - m_RMHandler.m_Encoder.SetRows(rows);
26   -}
27   -
28   -template<class TInputImage>
29   -void
30   -SpringAlgorithm<TInputImage>::UpdateAttribute(RegionPointerType r1, RegionPointerType r2)
31   -{
32   - // Update the spectral attributes
33   -
34   - unsigned int bands = r1->m_Avg_Color.size();
35   - float a_current, a_neighbor, a_sum;
36   - a_current = r1->m_Area;
37   - a_neighbor = r2->m_Area;
38   - a_sum = a_current+a_neighbor;
39   -
40   - for (unsigned int b = 0; b < bands; b++)
41   - r1->m_Avg_Color[b] = ((r1->m_Avg_Color[b]*a_current)+(r2->m_Avg_Color[b]*a_neighbor))/a_sum;
42   -
43   - // Update spatial attributes
44   - r1->m_Area += r2->m_Area;
45   -}
46   -
47   -
48   -template<class TInputImage>
49   -void
50   -SpringAlgorithm<TInputImage>::InitFromTiffImage(const std::string& input_file)
51   -{
52   - auto reader = ReaderType::New();
53   - reader->SetFileName(input_file);
54   - reader->Update();
55   -
56   - auto regionToProcess = reader->GetOutput()->GetLargestPossibleRegion();
57   - unsigned int cols = regionToProcess.GetSize()[0];
58   - unsigned int rows = regionToProcess.GetSize()[1];
59   - unsigned int bands = reader->GetOutput()->GetNumberOfComponentsPerPixel();
60   -
61   - m_RMHandler.m_Encoder.SetRows(rows); // Penser à n'utiliser qu'une seule méthode (SetRegionToProcess)
62   - m_RMHandler.m_Encoder.SetCols(cols);
63   -
64   - m_RegionList.reserve(cols*rows);
65   -
66   - IteratorType it(reader->GetOutput(), regionToProcess);
67   -
68   - // Create the vertices
69   - long unsigned int idx = 0;
70   - for(it.GoToBegin(); !it.IsAtEnd(); ++it)
71   - {
72   - auto r = std::make_shared<SpringRegion>();
73   - m_RMHandler.InitGenericRegion(r, idx);
74   - r->m_Area = 1;
75   - r->m_Avg_Color.reserve(bands);
76   -
77   - for(unsigned int b = 0; b < bands; b++)
78   - r->m_Avg_Color.push_back(it.Get()[b]);
79   -
80   - m_RegionList.push_back(r);
81   - ++idx;
82   - }
83   - m_RMHandler.InitNeighborhood(m_RegionList);
84   -}
85   -
86   -template<class TInputImage>
87   -void
88   -SpringAlgorithm<TInputImage>::Segmentation()
89   -{
90   - m_PreviousMerge = false;
91   - unsigned int max_iter = 500;
92   - if(m_NumberOfIterations > 0)
93   - max_iter = m_NumberOfIterations;
94   -
95   - unsigned int curr_step = 0;
96   - double cost;
97   - double threshold = m_Parameters*m_Parameters;
98   - RegionPointerType bestneigh = nullptr;
99   - bool prev_merged = true;
100   -
101   - m_ComputeCostFunction = [&](RegionPointerType r1, RegionPointerType r2)->double
102   - {
103