Commit 021cdb57aa9726c2630ea6834a01db215519c034

Authored by Pierre Lassalle
1 parent 49220950
Exists in master

Remove Spring algorithm

code/CMakeLists.txt
... ... @@ -77,22 +77,6 @@ ADD_EXECUTABLE(
77 77 TARGET_LINK_LIBRARIES(TiledBaatzSegmentation OTBCommon OTBIO)
78 78  
79 79 ADD_EXECUTABLE(
80   - TiledSpringSegmentation
81   - apps/TiledSpringSegmentation.cxx
82   - ${SOURCES}
83   -)
84   -
85   -TARGET_LINK_LIBRARIES(TiledSpringSegmentation OTBCommon OTBIO)
86   -
87   -ADD_EXECUTABLE(
88   - SpringSegmentation
89   - apps/SpringSegmentation.cxx
90   - ${SOURCES}
91   -)
92   -
93   -TARGET_LINK_LIBRARIES(SpringSegmentation OTBCommon OTBIO)
94   -
95   -ADD_EXECUTABLE(
96 80 TiledFLSASegmentation
97 81 apps/TiledFLSASegmentation.cxx
98 82 ${SOURCES}
... ...
code/CMakeLists.txt~
... ... @@ -77,22 +77,6 @@ ADD_EXECUTABLE(
77 77 TARGET_LINK_LIBRARIES(TiledBaatzSegmentation OTBCommon OTBIO)
78 78  
79 79 ADD_EXECUTABLE(
80   - TiledSpringSegmentation
81   - apps/TiledSpringSegmentation.cxx
82   - ${SOURCES}
83   -)
84   -
85   -TARGET_LINK_LIBRARIES(TiledSpringSegmentation OTBCommon OTBIO)
86   -
87   -ADD_EXECUTABLE(
88   - SpringSegmentation
89   - apps/SpringSegmentation.cxx
90   - ${SOURCES}
91   -)
92   -
93   -TARGET_LINK_LIBRARIES(SpringSegmentation OTBCommon OTBIO)
94   -
95   -ADD_EXECUTABLE(
96 80 TiledFLSASegmentation
97 81 apps/TiledFLSASegmentation.cxx
98 82 ${SOURCES}
... ...
code/apps/BaatzSegmentation.cxx
... ... @@ -20,32 +20,81 @@
20 20 #include "lss_RegionBasedTileBasedIterativeReduction.h"
21 21 #include "lss_RegionBasedFinisher.h"
22 22  
  23 +// For the command line
  24 +#include <boost/program_options/cmdline.hpp>
  25 +#include <boost/program_options/parsers.hpp>
  26 +#include <boost/program_options/options_description.hpp>
  27 +#include <boost/program_options/variables_map.hpp>
  28 +
  29 +namespace po = boost::program_options;
  30 +bool init_args(int argc, char ** argv, po::options_description& desc, po::variables_map& vm);
  31 +
23 32 int main(int argc, char **argv)
24 33 {
25   - if(argc != 6)
26   - {
27   - std::cerr << "Usage: " << argv[0] << ": \n"
28   - << "[input image path (jpg, png, tiff, jpeg200)]\n"
29   - << "[output image path (jpg, png, tiff)]\n"
30   - << "[scale]\n"
31   - << "[color]\n"
32   - << "[compactness]\n";
33 34  
  35 + po::options_description desc("Baatz & Schäpe segmentation: usage");
  36 + po::variables_map vm;
  37 +
  38 + if(init_args(argc, argv, desc, vm))
  39 + {
  40 + BaatzParameters bparams = {(float)vm["color"].as<double>(), (float)vm["compact"].as<double>(), (float)pow(vm["scale"].as<double>(), 2)};
  41 + typedef otb::VectorImage<float, 2> ImageType;
  42 + typedef BaatzAlgorithm<ImageType> AlgorithmType;
  43 + AlgorithmType baatz_segmenter;
  44 + baatz_segmenter.SetParameters(bparams);
  45 + baatz_segmenter.InitFromTiffImage(vm["input"].as<std::string>());
  46 + baatz_segmenter.Segmentation();
  47 + baatz_segmenter.WriteResultingImage(vm["output"].as<std::string>());
  48 + return 0;
  49 + }else{
34 50 return 1;
35 51 }
36 52  
37   - std::string input = argv[1];
38   - std::string output = argv[2];
39   - BaatzParameters bparams = {(float)atof(argv[4]), (float)atof(argv[5]), (float)pow(atof(argv[3]), 2)};
  53 + return 0;
  54 +}
40 55  
41   - typedef otb::VectorImage<float, 2> ImageType;
42   - typedef BaatzAlgorithm<ImageType> AlgorithmType;
  56 +bool init_args(int argc, char ** argv, po::options_description& desc, po::variables_map& vm)
  57 +{
  58 + desc.add_options()
  59 + ("help", "Baatz & Schäpe parameters")
  60 + ("input", po::value<std::string>(), "set input image path")
  61 + ("output", po::value<std::string>(), "set output image path (contour image file (png))")
  62 + ("scale", po::value<double>(), "set the scale parameter which has to be positive (40 for example)")
  63 + ("color", po::value<double>(), "set the color weight, value between 0 and 1 (0.7 for example)")
  64 + ("compact", po::value<double>(), "set the compact weight, value between 0 and 1 (0.3 for example)");
43 65  
44   - AlgorithmType baatz_segmenter;
45   - baatz_segmenter.SetParameters(bparams);
46   - baatz_segmenter.InitFromTiffImage(input);
47   - baatz_segmenter.Segmentation();
48   - baatz_segmenter.WriteResultingImage(output);
  66 + po::store(po::parse_command_line(argc, argv, desc), vm);
  67 + po::notify(vm);
49 68  
50   - return 0;
  69 + if (vm.count("help")) {
  70 + std::cout << desc << "\n";
  71 + return false;
  72 + }
  73 +
  74 + if (!vm.count("input")) {
  75 + std::cout << "The input image path was not set (--input).\n";
  76 + return false;
  77 + }
  78 +
  79 + if (!vm.count("output")) {
  80 + std::cout << "The output image path was not set (--output).\n";
  81 + return false;
  82 + }
  83 +
  84 + if (!vm.count("scale")) {
  85 + std::cout << "The scale parameter was not set (--scale).\n";
  86 + return false;
  87 + }
  88 +
  89 + if(!vm.count("color")){
  90 + std::cout << "The color weight was not set (--color).\n";
  91 + return false;
  92 + }
  93 +
  94 + if(!vm.count("compact")){
  95 + std::cout << "The compact weight was not set (--compact)\n";
  96 + return false;
  97 + }
  98 +
  99 + return true;
51 100 }
... ...
code/apps/FLSASegmentation.cxx
... ... @@ -20,30 +20,66 @@
20 20 #include "lss_RegionBasedTileBasedIterativeReduction.h"
21 21 #include "lss_RegionBasedFinisher.h"
22 22  
  23 +// For the command line
  24 +#include <boost/program_options/cmdline.hpp>
  25 +#include <boost/program_options/parsers.hpp>
  26 +#include <boost/program_options/options_description.hpp>
  27 +#include <boost/program_options/variables_map.hpp>
  28 +
  29 +namespace po = boost::program_options;
  30 +bool init_args(int argc, char ** argv, po::options_description& desc, po::variables_map& vm);
  31 +
23 32 int main(int argc, char **argv)
24 33 {
25   - if(argc != 4)
26   - {
27   - std::cerr << "Usage: " << argv[0] << ": \n"
28   - << "[input image path (jpg, png, tiff, jpeg200)]\n"
29   - << "[output image path (jpg, png, tiff)]\n"
30   - << "[lambda]\n";
  34 + po::options_description desc("Baatz & Schäpe segmentation: usage");
  35 + po::variables_map vm;
31 36  
  37 + if(init_args(argc, argv, desc, vm))
  38 + {
  39 + FLSParameter fparam = vm["lambda"].as<double>();
  40 + typedef otb::VectorImage<float, 2> ImageType;
  41 + typedef FLSAlgorithm<ImageType> AlgorithmType;
  42 + AlgorithmType fls_segmenter;
  43 + fls_segmenter.SetParameters(fparam);
  44 + fls_segmenter.InitFromTiffImage(vm["input"].as<std::string>());
  45 + fls_segmenter.Segmentation();
  46 + fls_segmenter.WriteResultingImage(vm["output"].as<std::string>());
  47 + return 0;
  48 + }
  49 + else
32 50 return 1;
  51 +}
  52 +
  53 +bool init_args(int argc, char ** argv, po::options_description& desc, po::variables_map& vm)
  54 +{
  55 + desc.add_options()
  56 + ("help", "Full lambda schedule parameters")
  57 + ("input", po::value<std::string>(), "set input image path")
  58 + ("output", po::value<std::string>(), "set output image path (contour image file (png))")
  59 + ("lambda", po::value<double>(), "set the lambda parameter which has to be positive (60 for example)");
  60 +
  61 + po::store(po::parse_command_line(argc, argv, desc), vm);
  62 + po::notify(vm);
  63 +
  64 + if (vm.count("help")) {
  65 + std::cout << desc << "\n";
  66 + return false;
33 67 }
34 68  
35   - std::string input = argv[1];
36   - std::string output = argv[2];
37   - FLSParameter fparam = atof(argv[3]);
  69 + if (!vm.count("input")) {
  70 + std::cout << "The input image path was not set (--input).\n";
  71 + return false;
  72 + }
38 73  
39   - typedef otb::VectorImage<float, 2> ImageType;
40   - typedef FLSAlgorithm<ImageType> AlgorithmType;
  74 + if (!vm.count("output")) {
  75 + std::cout << "The output image path was not set (--output).\n";
  76 + return false;
  77 + }
41 78  
42   - AlgorithmType fls_segmenter;
43   - fls_segmenter.SetParameters(fparam);
44   - fls_segmenter.InitFromTiffImage(input);
45   - fls_segmenter.Segmentation();
46   - fls_segmenter.WriteResultingImage(output);
  79 + if (!vm.count("lambda")) {
  80 + std::cout << "The lambda parameter was not set (--lambda).\n";
  81 + return false;
  82 + }
47 83  
48   - return 0;
  84 + return true;
49 85 }
... ...
code/apps/SpringSegmentation.cxx
... ... @@ -1,49 +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   -#include <otbVectorImage.h>
18   -#include "SpringAlgorithm.h"
19   -#include "lss_RegionBasedTileBasedReduction.h"
20   -#include "lss_RegionBasedTileBasedIterativeReduction.h"
21   -#include "lss_RegionBasedFinisher.h"
22   -
23   -int main(int argc, char **argv)
24   -{
25   - if(argc != 4)
26   - {
27   - std::cerr << "Usage: " << argv[0] << ": \n"
28   - << "[input image path (jpg, png, tiff, jpeg200)]\n"
29   - << "[output image path (jpg, png, tiff)]\n"
30   - << "[euclidean distance threshold]\n";
31   -
32   - return 1;
33   - }
34   -
35   - std::string input = argv[1];
36   - std::string output = argv[2];
37   - SpringParameter sparams = (double)atof(argv[3]);
38   -
39   - typedef otb::VectorImage<float, 2> ImageType;
40   - typedef SpringAlgorithm<ImageType> AlgorithmType;
41   -
42   - AlgorithmType spring_segmenter;
43   - spring_segmenter.SetParameters(sparams);
44   - spring_segmenter.InitFromTiffImage(input);
45   - spring_segmenter.Segmentation();
46   - spring_segmenter.WriteResultingImage(output);
47   -
48   - return 0;
49   -}
code/apps/TiledSpringSegmentation.cxx
... ... @@ -1,92 +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   -#include <otbVectorImage.h>
18   -#include "SpringAlgorithm.h"
19   -#include "lss_RegionBasedTileBasedReduction.h"
20   -#include "lss_RegionBasedTileBasedIterativeReduction.h"
21   -#include "lss_RegionBasedFinisher.h"
22   -
23   -int main(int argc, char **argv)
24   -{
25   - if(argc != 9)
26   - {
27   - std::cerr << "Usage: " << argv[0] << ": \n"
28   - << "[input image path (jpg, png, tiff, jpeg200)]\n"
29   - << "[output image path (jpg, png, tiff)]\n"
30   - << "[temporary directory path]\n"
31   - << "[tile size according to the width]\n"
32   - << "[tile size according to the height]\n"
33   - << "[number of iteration for Tile-Based reduction step]\n"
34   - << "[euclidean distance threshold]\n"
35   - << "[memory available (Mo)]\n";
36   -
37   - return 1;
38   - }
39   -
40   - std::string input = argv[1];
41   - std::string output = argv[2];
42   - std::string tmpdir = argv[3];
43   - unsigned int tilesizeX = atoi(argv[4]);
44   - unsigned int tilesizeY = atoi(argv[5]);
45   - unsigned int niter = atoi(argv[6]);
46   - SpringParameter sparams = (double)atof(argv[7]);
47   - long unsigned int memory = atoi(argv[8]);
48   - unsigned int margin = static_cast<unsigned int>(pow(2, niter+1) - 2);
49   -
50   - typedef otb::VectorImage<float, 2> ImageType;
51   - typedef SpringAlgorithm<ImageType> AlgorithmType;
52   - typedef lss::RegionBasedTileBasedReduction<ImageType, AlgorithmType> TileBasedReductionType;
53   - typedef lss::RegionBasedTileBasedIterativeReduction<AlgorithmType> TileBasedIterativeReductionType;
54   - typedef lss::RegionBasedFinisher<AlgorithmType> FinisherType;
55   -
56   - // Tile Based Reduction step
57   - std::cout << "***********************************\n";
58   - std::cout << "* Tile Based Reduction Step *\n";
59   - std::cout << "***********************************\n";
60   - TileBasedReductionType tb_reduction;
61   - tb_reduction.SetInputImagePath(input);
62   - tb_reduction.SetTemporaryDirectoryPath(tmpdir);
63   - tb_reduction.SetTileSizeX(tilesizeX);
64   - tb_reduction.SetTileSizeY(tilesizeY);
65   - tb_reduction.SetMarginOfStability(margin);
66   - tb_reduction.SetNumberOfIterations(niter);
67   - tb_reduction.SetSegmentationParameters(sparams);
68   - tb_reduction.GenerateData();
69   -
70   - // Tile Based Iterative reduction step
71   - std::cout << "*********************************************\n";
72   - std::cout << "* Tile Based Iterative Reduction Step *\n";
73   - std::cout << "*********************************************\n";
74   - TileBasedIterativeReductionType tb_itreduction;
75   - tb_itreduction.SetObserver(tb_reduction.GetObserver());
76   - tb_itreduction.SetSegmentationParameters(sparams);
77   - tb_itreduction.SetAvailableMemory(memory);
78   - bool DoSegmentation = tb_itreduction.GenerateData();
79   -
80   - std::cout << "*********************************************\n";
81   - std::cout << "* Aggregating and completion step *\n";
82   - std::cout << "*********************************************\n";
83   - FinisherType finisher;
84   - finisher.SetOutputPath(output);
85   - finisher.SetObserver(tb_itreduction.GetObserver());
86   - finisher.SetDoSegmentation(DoSegmentation);
87   - finisher.SetTiles(tb_itreduction.GetTiles());
88   - finisher.SetSegmentationParameters(sparams);
89   - finisher.GenerateData();
90   -
91   - return 0;
92   -}
code/src/algo/BaatzAlgorithm.txx~
... ... @@ -1,338 +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   - auto cw = m_Parameters.m_ColorWeight;
169   - auto sc = m_Parameters.m_Scale;
170   - m_ComputeCostFunction = [&](RegionPointerType r1, RegionPointerType r2)->double
171   - {
172   - double spectral_h;
173   - double spatial_h;
174   - double cost = 0;
175   -
176   - spectral_h = ColorComponentCostFusion(r1, r2);
177   - cost += cw * spectral_h;
178   -
179   - if(cost < sc)
180   - {
181   - spatial_h = CompactnessComponentCostFusion(r1, r2);
182   - cost += (1-cw)*spatial_h;
183   - return cost;
184   - }
185   - else
186   - return cost;
187   - };
188   -
189   - while(curr_step < max_iter && prev_merged)
190   - {
191   - prev_merged = false;
192   - std::cout << curr_step << std::endl;
193   - m_RMHandler.UpdateMergingCost(m_RegionList, m_ComputeCostFunction);
194   -
195   - for(auto& r: m_RegionList)
196   - {
197   - if(m_RMHandler.IsLMBF(r, m_Parameters.m_Scale))
198   - {
199   - auto res = m_RMHandler.GetRegionsToMerge(r);
200   - //std::cout << "Merge " << res.first->m_Id << " with " << res.second->m_Id << std::endl;
201   - // User contribution
202   - UpdateAttribute(res.first, res.second);
203   - m_RMHandler.Update(res);
204   - prev_merged = true;
205   - }
206   - }
207   - m_RMHandler.RemoveExpiredVertices(m_RegionList);
208   - curr_step++;
209   - m_PreviousMerge = prev_merged;
210   - }
211   -}
212   -
213   -template<class TInputImage>
214   -void
215   -BaatzAlgorithm<TInputImage>::UpdateAttribute(RegionPointerType r1, RegionPointerType r2)
216   -{
217   - // Update the spectral attributes
218   -
219   - unsigned int bands = r1->m_Avg_Color.size();
220   - float mean[bands], colorSum[bands];
221   - float squarePixels[bands];
222   - float a_current, a_neighbor, a_sum;
223   - a_current = r1->m_Area;
224   - a_neighbor = r2->m_Area;
225   - a_sum = a_current+a_neighbor;
226   -
227   - for (unsigned int b = 0; b < bands; b++)
228   - {
229   - mean[b] = ((r1->m_Avg_Color[b]*a_current)+(r2->m_Avg_Color[b]*a_neighbor))/a_sum;
230   - squarePixels[b] = (r1->m_Avg_Color_Square[b])+(r2->m_Avg_Color_Square[b]);
231   - colorSum[b] = r1->m_Color_Sum[b] + r2->m_Color_Sum[b];
232   - }
233   -
234   - for(unsigned int b = 0; b < bands; b++)
235   - {
236   - r1->m_Avg_Color[b] = mean[b];
237   - r1->m_Avg_Color_Square[b] = squarePixels[b];
238   - r1->m_Color_Sum[b] = colorSum[b];
239   - r1->m_Std_Color[b] = sqrt((squarePixels[b] - 2*mean[b]*colorSum[b] + a_sum*mean[b]*mean[b])/a_sum);
240   - }
241   -
242   - // Update spatial attributes
243   - r1->m_Area += r2->m_Area;
244   - r1->m_Perimeter += r2->m_Perimeter - 2*m_RMHandler.GetConnections(r1,r2);
245   -}
246   -
247   -template<class TInputImage>
248   -void
249   -BaatzAlgorithm<TInputImage>::WriteLabelImage(const std::string& ofname)
250   -{
251   - typename InputImageType::IndexType index;
252   - typename InputImageType::SizeType size;
253   - typename InputImageType::RegionType region;
254   - auto label_image = LabelImageType::New();
255   -
256   - index[0] = 0;
257   - index[1] = 0;
258   - size[0] = m_RMHandler.m_Encoder.GetCols();
259   - size[1] = m_RMHandler.m_Encoder.GetRows();
260   - region.SetIndex(index);
261   - region.SetSize(size);
262   -
263   - label_image->SetRegions(region);
264   - label_image->Allocate();
265   -
266   - unsigned int label = 1;
267   - for(auto& v : m_RegionList)
268   - {
269   - auto pixels = m_RMHandler.m_Encoder.GenerateAllPixels(v->m_Id, v->m_Contour, v->m_Bbox);
270   - for(auto& pix: pixels)
271   - {
272   - index[0] = pix % size[0];
273   - index[1] = pix / size[0];
274   - auto label_pixel = label_image->GetPixel(index);
275   - label_image->SetPixel(index, label);
276   - }
277   - label++;
278   - }
279   -
280   - auto label_writer = LabelWriterType::New();
281   - label_writer->SetFileName(ofname);
282   - label_writer->SetInput(label_image);
283   - label_writer->Update();
284   -}
285   -
286   -template<class TInputImage>
287   -void
288   -BaatzAlgorithm<TInputImage>::WriteResultingImage(const std::string& ofname)
289   -{
290   - auto out_image = InputImageType::New();
291   - typename InputImageType::IndexType index;
292   - typename InputImageType::SizeType size;
293   - typename InputImageType::RegionType region;
294   -
295   - index[0] = 0;
296   - index[1] = 0;
297   - size[0] = m_RMHandler.m_Encoder.GetCols();
298   - size[1] = m_RMHandler.m_Encoder.GetRows();
299   - region.SetIndex(index);
300   - region.SetSize(size);
301   - out_image->SetRegions(region);
302   - out_image->SetNumberOfComponentsPerPixel(3);
303   - out_image->Allocate();
304   -
305   - for(unsigned int r = 0; r< size[1]; r++)
306   - {
307   - for(unsigned int c = 0; c<size[0]; c++)
308   - {
309   - index[0] = c;
310   - index[1] = r;
311   - auto pixel_value = out_image->GetPixel(index);
312   - for(int b=0; b<3; b++)
313   - pixel_value[b] = 0;
314   - out_image->SetPixel(index, pixel_value);
315   - }
316   - }
317   - unsigned int label = 1;
318   - for(auto& v : m_RegionList)
319   - {
320   - auto pixels = m_RMHandler.m_Encoder.GeneratePixels(v->m_Id, v->m_Contour);
321   - for(auto& pix: pixels)
322   - {
323   - index[0] = pix % size[0];
324   - index[1] = pix / size[0];
325   - auto pixel_value = out_image->GetPixel(index);
326   - for(int b=0; b<3; b++)
327   - pixel_value[b] = v->m_Avg_Color[b];
328   - out_image->SetPixel(index, pixel_value);
329   - }
330   - }
331   -
332   - auto writer = WriterType::New();
333   - writer->SetFileName(ofname);
334   - writer->SetInput(out_image);
335   - writer->Update();
336   -}
337   -
338   -#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   - 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   - return cost;
108   - };
109   -
110   - while(curr_step < max_iter && prev_merged)
111   - {
112   - prev_merged = false;
113   - std::cout << curr_step << std::endl;
114   - m_RMHandler.UpdateMergingCost(m_RegionList, m_ComputeCostFunction);
115   -
116   - for(auto& r: m_RegionList)
117   - {
118   - if(m_RMHandler.IsLMBF(r, threshold))
119   - {
120   - auto res = m_RMHandler.GetRegionsToMerge(r);
121   - //std::cout << "Merge " << res.first->m_Id << " with " << res.second->m_Id << std::endl;
122   - // User contribution
123   - UpdateAttribute(res.first, res.second);
124   - m_RMHandler.Update(res);
125   - prev_merged = true;
126   - }
127   - }
128   - m_RMHandler.RemoveExpiredVertices(m_RegionList);
129   - curr_step++;
130   - m_PreviousMerge = prev_merged;
131   - }
132   -}
133   -
134   -template<class TInputImage>
135   -void
136   -SpringAlgorithm<TInputImage>::WriteLabelImage(const std::string& ofname)
137   -{
138   - typename InputImageType::IndexType index;
139   - typename InputImageType::SizeType size;
140   - typename InputImageType::RegionType region;
141   - auto label_image = LabelImageType::New();
142   -
143   - index[0] = 0;
144   - index[1] = 0;
145   - size[0] = m_RMHandler.m_Encoder.GetCols();
146   - size[1] = m_RMHandler.m_Encoder.GetRows();
147   - region.SetIndex(index);
148   - region.SetSize(size);
149   -
150   - label_image->SetRegions(region);
151   - label_image->Allocate();
152   -
153   - unsigned int label = 1;
154   - for(auto& v : m_RegionList)
155   - {
156   - auto pixels = m_RMHandler.m_Encoder.GenerateAllPixels(v->m_Id, v->m_Contour, v->m_Bbox);
157   - for(auto& pix: pixels)
158   - {
159   - index[0] = pix % size[0];
160   - index[1] = pix / size[0];
161   - auto label_pixel = label_image->GetPixel(index);
162   - label_image->SetPixel(index, label);
163   - }
164   - label++;
165   - }
166   -
167   - auto label_writer = LabelWriterType::New();
168   - label_writer->SetFileName(ofname);
169   - label_writer->SetInput(label_image);
170   - label_writer->Update();
171   -}
172   -
173   -template<class TInputImage>
174   -void
175   -SpringAlgorithm<TInputImage>::WriteResultingImage(const std::string& ofname)
176   -{
177   - auto out_image = InputImageType::New();
178   - typename InputImageType::IndexType index;
179   - typename InputImageType::SizeType size;
180   - typename InputImageType::RegionType region;
181   -
182   - index[0] = 0;
183   - index[1] = 0;
184   - size[0] = m_RMHandler.m_Encoder.GetCols();
185   - size[1] = m_RMHandler.m_Encoder.GetRows();
186   - region.SetIndex(index);
187   - region.SetSize(size);
188   - out_image->SetRegions(region);
189   - out_image->SetNumberOfComponentsPerPixel(3);
190   - out_image->Allocate();
191   -
192   - for(unsigned int r = 0; r< size[1]; r++)
193   - {
194   - for(unsigned int c = 0; c<size[0]; c++)
195   - {
196   - index[0] = c;
197   - index[1] = r;
198   - auto pixel_value = out_image->GetPixel(index);
199   - for(int b=0; b<3; b++)
200   - pixel_value[b] = 0;