File: TePDIClassification_test.cpp

package info (click to toggle)
libterralib 4.3.0%2Bdfsg.2-10
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 61,560 kB
  • ctags: 37,110
  • sloc: cpp: 225,052; ansic: 31,562; makefile: 807; sh: 80; xml: 37
file content (278 lines) | stat: -rw-r--r-- 8,911 bytes parent folder | download | duplicates (8)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
#define TEAGN_ENABLE_STDOUT_LOG

#include <TePDIExamplesBase.hpp>
#include <TePDISAMClassifier.hpp>
#include <TePDIIsosegClas.hpp>
#include <TePDIKMeansClas.hpp>
#include <TePDIEMClas.hpp>
#include <TePDIRegGrowSeg.hpp>
#include <TePDIUtils.hpp>

#include <TeInitRasterDecoders.h>
#include <TeProgress.h>
#include <TeStdIOProgress.h>
#include <TeAgnostic.h>

#include <vector>


void buildData( TePolygonSet& polset, TePDITypes::TePDIRasterPtrType& raster )
{
  TePDIParameters params;

  params.SetParameter( "input_image", raster );

  TePDITypes::TePDIPolSetMapPtrType output_polsets( 
    new TePDITypes::TePDIPolSetMapType );
  params.SetParameter( "output_polsets", output_polsets );
  
  params.SetParameter( "euc_treshold", (double)20 );
  params.SetParameter( "area_min", (int)15 );  
  
  TePDIRegGrowSeg segmenter;
  
  TEAGN_TRUE_OR_THROW( segmenter.Reset(params), "Reset failed" );
  
  TEAGN_TRUE_OR_THROW( segmenter.Apply(), "Apply error" );    
  
  TePDITypes::TePDIPolSetMapType::iterator psmap_it = 
    output_polsets->begin();
  TePDITypes::TePDIPolSetMapType::iterator psmap_it_end = 
    output_polsets->end();
    
  polset.clear();
    
  while( psmap_it != psmap_it_end ) {
    TePolygonSet::iterator ps_it = psmap_it->second.begin();
    TePolygonSet::iterator ps_it_end = psmap_it->second.end();
    
    while( ps_it != ps_it_end ) {
      polset.add( *ps_it );
      ++ps_it;  
    }
    
    ++psmap_it;
  }
}

void TePDISAMClassifier_test( TePDITypes::TePDIRasterPtrType& raster)
{ 
  // Initiating the output RAM raster
  
  TePDITypes::TePDIRasterPtrType output_raster;
  TEAGN_TRUE_OR_THROW(TePDIUtils::TeAllocRAMRaster( output_raster, 1, 1, 1, 
    false, TeUNSIGNEDCHAR, 0), "output_raster Alloc error");
    
  // Creating the vector of input raster used bands
  
  std::vector< unsigned int > bands;
  bands.push_back(0);
  bands.push_back(1);
  bands.push_back(2);
    
  // Creating the spectral samples vector
  
  TePDISAMClassifier::ClassSpectralSamples class1Samples;
  class1Samples.classId_ = 80;
  class1Samples.maxAngularDist_ = 0.2;
  class1Samples.samplesMatrix_.Reset( 1, 3 ); // one sample, 3 bands
  class1Samples.samplesMatrix_( 0, 0) = 166;
  class1Samples.samplesMatrix_( 0, 1) = 255;
  class1Samples.samplesMatrix_( 0, 2) = 255;
  
  TePDISAMClassifier::ClassSpectralSamples class2Samples;
  class2Samples.classId_ = 155;
  class2Samples.maxAngularDist_ = 0.2;
  class2Samples.samplesMatrix_.Reset( 1, 3 ); // one sample, 3 bands
  class2Samples.samplesMatrix_( 0, 0) = 36;
  class2Samples.samplesMatrix_( 0, 1) = 255;
  class2Samples.samplesMatrix_( 0, 2) = 76;  
  
  TePDISAMClassifier::ClassSpectralSamples class3Samples;
  class3Samples.classId_ = 255;
  class2Samples.maxAngularDist_ = 0.2;
  class3Samples.samplesMatrix_.Reset( 1, 3 ); // one sample, 3 bands
  class3Samples.samplesMatrix_( 0, 0) = 36;
  class3Samples.samplesMatrix_( 0, 1) = 36;
  class3Samples.samplesMatrix_( 0, 2) = 76;    
  
  TePDISAMClassifier::SpectralSamplesVecPtrT spectral_samples( new
    TePDISAMClassifier::SpectralSamplesVectorT );
  spectral_samples->push_back( class1Samples );
  spectral_samples->push_back( class2Samples );
  spectral_samples->push_back( class3Samples );
  
  TePDIParameters params;
  params.SetParameter("input_raster", raster);
  params.SetParameter("bands", bands);
  params.SetParameter("output_raster", output_raster);
  params.SetParameter("spectral_samples", spectral_samples);
  params.SetParameter("output_raster", output_raster);
  params.SetParameter("enable_multi_thread", (int)1);

  TePDISAMClassifier classification;
  
  TEAGN_TRUE_OR_THROW(classification.Reset(params), "Reset failed");
  
  TEAGN_TRUE_OR_THROW(classification.Apply(), "Apply error");
  
  TEAGN_TRUE_OR_THROW(TePDIUtils::TeRaster2Geotiff(output_raster, 
    TEPDIEXAMPLESBINPATH "TePDISAMClassifier_test.tif"), 
    "GeoTIF generation error");
}

void IsosegClassification_test( TePolygonSet& polset, 
  TePDITypes::TePDIRasterPtrType& raster)
{
  TePDIParameters params;

  vector<int> bands;
  bands.push_back(0);
  bands.push_back(1);
  bands.push_back(2);
  params.SetParameter("bands", bands);

  TePDITypes::TePDIRasterVectorType input_rasters;
  input_rasters.push_back( raster );
  input_rasters.push_back( raster );
  input_rasters.push_back( raster );
  params.SetParameter("input_rasters", input_rasters);

  TePDITypes::TePDIPolygonSetPtrType input_polygonset(  &polset, true );
  params.SetParameter("input_polygonset", input_polygonset);

  TePDITypes::TePDIRasterPtrType output_raster;
  TEAGN_TRUE_OR_THROW(TePDIUtils::TeAllocRAMRaster( output_raster, 1, 1, 1, 
    false, TeDOUBLE, 0), "output_raster Alloc error");
  params.SetParameter("output_raster", output_raster);
  
  params.SetParameter("acceptance_limiar", (double)90.0);

  TePDIIsosegClas classification;
  
  TEAGN_TRUE_OR_THROW(classification.Reset(params), "Reset failed");
  
  TEAGN_TRUE_OR_THROW(classification.Apply(), "Apply error");
  
  TEAGN_TRUE_OR_THROW(TePDIUtils::TeRaster2Geotiff(output_raster, 
    TEPDIEXAMPLESBINPATH "Classification_Isoseg_test.tif"), "GeoTIF generation error");
}


void KMeansClassification_test( TePDITypes::TePDIRasterPtrType& raster)
{ 
  TePDIParameters params;

  params.SetParameter("input_raster", raster);

  TePDITypes::TePDIRasterPtrType output_raster;
  TEAGN_TRUE_OR_THROW(TePDIUtils::TeAllocRAMRaster( output_raster, 1, 1, 1, 
    false, TeDOUBLE, 0), "output_raster Alloc error");
  params.SetParameter("output_raster", output_raster);
  
  vector<int> bands;
  bands.push_back(0);
  bands.push_back(1);
  bands.push_back(2);
  params.SetParameter("bands", bands);  
  
  params.SetParameter("classes_number", (int)10);
  params.SetParameter("sample", (int)1);
  params.SetParameter("iterations_number", (int)10);
  params.SetParameter("line_begin", (int)0);
  params.SetParameter("line_end", (int)( raster->params().nlines_ - 1 ) );
  params.SetParameter("column_begin", (int)0);
  params.SetParameter("column_end", (int)( raster->params().ncols_ - 1 ) );

  TePDIKMeansClas classification;
  
  TEAGN_TRUE_OR_THROW(classification.Reset(params), "Reset failed");
  
  TEAGN_TRUE_OR_THROW(classification.Apply(), "Apply error");
  
  TEAGN_TRUE_OR_THROW(TePDIUtils::TeRaster2Geotiff(output_raster, 
    TEPDIEXAMPLESBINPATH "KMeansClassification_test.tif"), "GeoTIF generation error");
}


void EMClassification_test( TePDITypes::TePDIRasterPtrType& raster)
{
  TePDIParameters params;

  TePDITypes::TePDIRasterVectorType input_rasters;
  input_rasters.push_back( raster );
  input_rasters.push_back( raster );
  input_rasters.push_back( raster );
  params.SetParameter("input_rasters", input_rasters);
  
  vector<int> bands;
  bands.push_back(0);
  bands.push_back(1);
  bands.push_back(2);
  params.SetParameter("bands", bands);
  
  params.SetParameter("classes_to_find", (int)5);

  TePDITypes::TePDIRasterPtrType output_raster;
  TEAGN_TRUE_OR_THROW(TePDIUtils::TeAllocRAMRaster( output_raster, 1, 1, 1, 
    false, TeDOUBLE, 0), "output_raster Alloc error");
  params.SetParameter("output_raster", output_raster);
  
  params.SetParameter("sx", (int)5);
  params.SetParameter("sy", (int)5);
  params.SetParameter("max_iterations", (int)3);
  params.SetParameter("epsilon", (double)3.5);
  params.SetParameter("shift_threshold", (double)35);

  TePDIEMClas classification;
  
  TEAGN_TRUE_OR_THROW(classification.Reset(params), "Factory make failed");
  
  TEAGN_TRUE_OR_THROW(classification.Apply(), "Apply error");
  
  TEAGN_TRUE_OR_THROW(TePDIUtils::TeRaster2Geotiff(output_raster, 
    TEPDIEXAMPLESBINPATH "EMClassification_test.tif"), "GeoTIF generation error");
}

int main()
{
  TEAGN_LOGMSG("Test started.");
  
  try
  {
    TeInitRasterDecoders();
    
    TeStdIOProgress pi;
    TeProgress::setProgressInterf(dynamic_cast< TeProgressBase* >(&pi));
    
    /* Opening image */
    
    TePDITypes::TePDIRasterPtrType raster;
    TEAGN_TRUE_OR_THROW( TePDIUtils::loadRaster( 
      std::string( TEPDIEXAMPLESRESPATH "cbers_rgb342_crop1.tif" ), 
      raster, true ), "Error loading raster" );    
    
    /* Polygons independent algorithms */
    
    TePDISAMClassifier_test( raster );
    EMClassification_test( raster );
    KMeansClassification_test( raster );
    
    /* Generating polygons */
    
    TePolygonSet polset;
    buildData( polset, raster );
    
    /* Polygons dependent algorithms */
    
    IsosegClassification_test( polset, raster );
  }
  catch( const TeException& excpt ){
    TEAGN_LOGERR( excpt.message() )
    return EXIT_FAILURE;
  }
  
  TEAGN_LOGMSG("Test OK.");
  return EXIT_SUCCESS;
}