TsClassifier.cpp
Go to the documentation of this file.
1 /* Copyright (C) 2008 National Institute For Space Research (INPE) - Brazil.
2 
3  This file is part of the TerraLib - a Framework for building GIS enabled applications.
4 
5  TerraLib is free software: you can redistribute it and/or modify
6  it under the terms of the GNU Lesser General Public License as published by
7  the Free Software Foundation, either version 3 of the License,
8  or (at your option) any later version.
9 
10  TerraLib is distributed in the hope that it will be useful,
11  but WITHOUT ANY WARRANTY; without even the implied warranty of
12  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13  GNU Lesser General Public License for more details.
14 
15  You should have received a copy of the GNU Lesser General Public License
16  along with TerraLib. See COPYING. If not, write to
17  TerraLib Team at <terralib-team@terralib.org>.
18  */
19 
20 /*!
21  \file terralib/unittest/rp/classifier/TsClassifier.cpp
22 
23  \brief Main file of test suit for Classifier interface.
24 */
25 
26 // TerraLib
27 #include "../Config.h"
28 #include <terralib/dataaccess.h>
29 #include <terralib/rp.h>
30 #include <terralib/geometry.h>
31 #include <terralib/raster.h>
32 #include <terralib/memory.h>
33 #include <terralib/common.h>
34 
35 // Boost
36 #define BOOST_TEST_NO_MAIN
37 #include <boost/test/unit_test.hpp>
38 #include <boost/timer.hpp>
39 
40 BOOST_AUTO_TEST_SUITE (classifier_tests)
41 
42 /* This code is for image segmentation to be used with ISOSegClassifier function */
43 void SegmentImage(te::rst::Raster* rin, std::vector<te::gm::Polygon*>& polygons,
44  std::vector< double >& polsValues )
45 {
46  /* Define segmentation parameters */
47 
48  /* Create output raster info */
49  std::map<std::string, std::string> orinfo;
50  orinfo["URI"] = "cbers2b_rgb342_crop_segmented.tif";
51 
52  /* Input parameters */
53 
54  te::rp::Segmenter::InputParameters algoInputParameters;
55  algoInputParameters.m_inputRasterPtr = rin;
56  algoInputParameters.m_inputRasterBands.push_back(0);
57  algoInputParameters.m_inputRasterBands.push_back(1);
58  algoInputParameters.m_inputRasterBands.push_back(2);
59  algoInputParameters.m_enableBlockProcessing = false;
60 
61  /* Link specific parameters with chosen implementation */
63  segparameters.m_minSegmentSize = 50;
64  segparameters.m_segmentsSimilarityThreshold = 0.10;
65 
66  algoInputParameters.m_strategyName = "RegionGrowingMean";
67  algoInputParameters.setSegStrategyParams(segparameters);
68 
69  /* Output parameters */
70 
71  te::rp::Segmenter::OutputParameters algoOutputParameters;
72  algoOutputParameters.m_rInfo = orinfo;
73  algoOutputParameters.m_rType = "GDAL";
74 
75  /* Execute the algorithm */
76 
77  te::rp::Segmenter seginstance;
78 
79  if(!seginstance.initialize(algoInputParameters)) throw;
80  if(!seginstance.execute(algoOutputParameters )) throw;
81 
82  /* Export the segmentation into shapefile */
83  std::vector<te::gm::Geometry*> geometries;
84  algoOutputParameters.m_outputRasterPtr->vectorize( geometries, 0, 0, &polsValues );
85 
86  for (unsigned i = 0; i < geometries.size(); i++)
87  {
88  polygons.push_back(static_cast<te::gm::Polygon*> (geometries[i]));
89  }
90 }
91 
92 void saveToShp( std::vector<te::gm::Polygon*> polygons, std::vector< double >& polygonsValues,
93  const std::string& shpBaseFileName )
94 {
95  std::unique_ptr<te::da::DataSetType> dataSetTypePtr1(new te::da::DataSetType(shpBaseFileName));
96 
97  dataSetTypePtr1->add( new te::dt::SimpleProperty("value", te::dt::DOUBLE_TYPE, true) );
98  dataSetTypePtr1->add( new te::dt::SimpleProperty("id", te::dt::DOUBLE_TYPE, true) );
99  dataSetTypePtr1->add( new te::gm::GeometryProperty("polygon", polygons[ 0 ]->getSRID(),
100  te::gm::PolygonType, true) );
101 
102  std::unique_ptr<te::da::DataSetType> dataSetTypePtr2( new te::da::DataSetType( *dataSetTypePtr1 ) );
103 
104  std::unique_ptr< te::mem::DataSet > memDataSetPtr( new te::mem::DataSet( dataSetTypePtr1.get()) );
105 
106  for( unsigned int polygonsIdx = 0 ; polygonsIdx < polygons.size() ; ++polygonsIdx )
107  {
108  te::mem::DataSetItem* dsItemPtr = new te::mem::DataSetItem(memDataSetPtr.get());
109  dsItemPtr->setDouble( 0, polygonsValues[ polygonsIdx ] );
110  dsItemPtr->setDouble( 1, polygonsIdx );
111  dsItemPtr->setGeometry( 2, (te::gm::Geometry*)polygons[ polygonsIdx ]->clone() );
112 
113  memDataSetPtr->add( dsItemPtr );
114  }
115 
116  remove( ( shpBaseFileName + ".shx" ).c_str() );
117  remove( ( shpBaseFileName + ".shp" ).c_str() );
118  remove( ( shpBaseFileName + ".prj" ).c_str() );
119  remove( ( shpBaseFileName + ".dbf" ).c_str() );
120 
121  std::string connInfo("file://" + shpBaseFileName + ".shp");
122 
123  std::unique_ptr<te::da::DataSource> dsOGR( te::da::DataSourceFactory::make("OGR", connInfo) );
124  dsOGR->open();
125 
126  memDataSetPtr->moveBeforeFirst();
127 
128  te::da::Create(dsOGR.get(), dataSetTypePtr2.get(), memDataSetPtr.get());
129 
130  dsOGR->close();
131 }
132 
133 BOOST_AUTO_TEST_CASE(ISOSeg_single_thread_test)
134 {
135  te::common::ConsoleProgressViewer progressViewerInstance;
136 
137  /* First open the input image */
138 
139  std::map<std::string, std::string> rinfo;
140  rinfo["URI"] = TERRALIB_DATA_DIR"/geotiff/cbers_rgb342_crop3_EPSG_22522.tif";
141 
142  std::unique_ptr< te::rst::Raster > raster1Ptr( te::rst::RasterFactory::open(rinfo) );
143 
144  rinfo["URI"] = TERRALIB_DATA_DIR"/geotiff/cbers_rgb342_crop2.tif";
145 
146  std::unique_ptr< te::rst::Raster > raster2Ptr( te::rst::RasterFactory::open(rinfo) );
147 
148  /* To apply ISOSeg the image must be segmented */
149  std::vector<te::gm::Polygon*> pin;
150  std::vector< double > polsValues;
151  SegmentImage(raster1Ptr.get(), pin, polsValues );
152  saveToShp( pin, polsValues, "terralib_unittest_rp_Classifier_ISOSeg_Single_Thread_Test" );
153 
154  /* Define classification parameters */
155 
156  /* Input parameters */
157  te::rp::Classifier::InputParameters algoInputParameters;
158  algoInputParameters.m_inputRasterPtr = raster2Ptr.get();
159  algoInputParameters.m_inputRasterBands.push_back(0);
160  algoInputParameters.m_inputRasterBands.push_back(1);
161  algoInputParameters.m_inputRasterBands.push_back(2);
162  algoInputParameters.m_inputPolygons = pin;
163  algoInputParameters.m_createRasterPalette = true;
164  algoInputParameters.m_enableMultiThread = false;
165  algoInputParameters.m_enableProgressInterface = true;
166 
167  /* Link specific parameters with chosen implementation */
168 
170  classifierparameters.m_acceptanceThreshold = 99.0;
171  algoInputParameters.m_strategyName = "isoseg";
172 
173  // Mahalanobis distance test
174 
175  {
176  classifierparameters.m_distanceType =
178  algoInputParameters.setClassifierStrategyParams(classifierparameters);
179 
180  /* Output parameters */
181 
182  std::map<std::string, std::string> orinfo;
183  orinfo["URI"] =
184  "terralib_unittest_rp_Classifier_ISOSeg_Single_Thread_Mahalanobis_Test.tif";
185 
186  te::rp::Classifier::OutputParameters algoOutputParameters;
187  algoOutputParameters.m_rInfo = orinfo;
188  algoOutputParameters.m_rType = "GDAL";
189 
190  /* Execute the algorithm */
191 
192  te::rp::Classifier algorithmInstance;
193 
194  BOOST_CHECK( algorithmInstance.initialize(algoInputParameters) );
195 
196  boost::timer timer;
197  BOOST_CHECK( algorithmInstance.execute(algoOutputParameters) );
198  std::cout << std::endl << "ISOSeg Mahalanobis single thread execution time: " << timer.elapsed() << std::endl;
199  }
200 
201  // Bhattacharyya distance test
202 
203  {
204  classifierparameters.m_distanceType =
206  algoInputParameters.setClassifierStrategyParams(classifierparameters);
207 
208  /* Output parameters */
209 
210  std::map<std::string, std::string> orinfo;
211  orinfo["URI"] =
212  "terralib_unittest_rp_Classifier_ISOSeg_Single_Thread_Bhattacharyya_Test.tif";
213 
214  te::rp::Classifier::OutputParameters algoOutputParameters;
215  algoOutputParameters.m_rInfo = orinfo;
216  algoOutputParameters.m_rType = "GDAL";
217 
218  /* Execute the algorithm */
219 
220  te::rp::Classifier algorithmInstance;
221 
222  BOOST_CHECK( algorithmInstance.initialize(algoInputParameters) );
223  BOOST_CHECK( algorithmInstance.execute(algoOutputParameters) );
224  }
225 }
226 
227 BOOST_AUTO_TEST_CASE(ISOSeg_multi_thread_test)
228 {
229  te::common::ConsoleProgressViewer progressViewerInstance;
230 
231  /* First open the input image */
232 
233  std::map<std::string, std::string> rinfo;
234  rinfo["URI"] = TERRALIB_DATA_DIR"/geotiff/cbers_rgb342_crop3_EPSG_22522.tif";
235 
236  std::unique_ptr< te::rst::Raster > raster1Ptr( te::rst::RasterFactory::open(rinfo) );
237 
238  rinfo["URI"] = TERRALIB_DATA_DIR"/geotiff/cbers_rgb342_crop2.tif";
239 
240  std::unique_ptr< te::rst::Raster > raster2Ptr( te::rst::RasterFactory::open(rinfo) );
241 
242  /* To apply ISOSeg the image must be segmented */
243  std::vector<te::gm::Polygon*> pin;
244  std::vector< double > polsValues;
245  SegmentImage(raster1Ptr.get(), pin, polsValues );
246  saveToShp( pin, polsValues, "terralib_unittest_rp_Classifier_ISOSeg_Multi_Thread_Test" );
247 
248  /* Define classification parameters */
249 
250  /* Input parameters */
251  te::rp::Classifier::InputParameters algoInputParameters;
252  algoInputParameters.m_inputRasterPtr = raster2Ptr.get();
253  algoInputParameters.m_inputRasterBands.push_back(0);
254  algoInputParameters.m_inputRasterBands.push_back(1);
255  algoInputParameters.m_inputRasterBands.push_back(2);
256  algoInputParameters.m_inputPolygons = pin;
257  algoInputParameters.m_createRasterPalette = true;
258  algoInputParameters.m_enableMultiThread = true;
259  algoInputParameters.m_enableProgressInterface = true;
260 
261 
262  /* Link specific parameters with chosen implementation */
263 
265  classifierparameters.m_acceptanceThreshold = 99.0;
266  algoInputParameters.m_strategyName = "isoseg";
267 
268  // Mahalanobis distance test
269 
270  {
271  classifierparameters.m_distanceType =
273  algoInputParameters.setClassifierStrategyParams(classifierparameters);
274 
275  /* Output parameters */
276 
277  std::map<std::string, std::string> orinfo;
278  orinfo["URI"] =
279  "terralib_unittest_rp_Classifier_ISOSeg_Multi_Thread_Mahalanobis_Test.tif";
280 
281  te::rp::Classifier::OutputParameters algoOutputParameters;
282  algoOutputParameters.m_rInfo = orinfo;
283  algoOutputParameters.m_rType = "GDAL";
284 
285  /* Execute the algorithm */
286 
287  te::rp::Classifier algorithmInstance;
288 
289  BOOST_CHECK( algorithmInstance.initialize(algoInputParameters) );
290 
291  boost::timer timer;
292  BOOST_CHECK( algorithmInstance.execute(algoOutputParameters) );
293  std::cout << std::endl << "ISOSeg Mahalanobis multi thread execution time: " << timer.elapsed() << std::endl;
294  }
295 }
296 
298 {
299  te::common::ConsoleProgressViewer progressViewerInstance;
300 
301  /* First open the input image */
302 
303  std::map<std::string, std::string> rinfo;
304  rinfo["URI"] = TERRALIB_DATA_DIR"/geotiff/cbers_rgb342_crop1.tif";
305 
307 
308  /* Create output raster info */
309 
310  std::map<std::string, std::string> orinfo;
311  orinfo["URI"] = "terralib_unittest_rp_Classifier_MAP_Test.tif";
312 
313  /* Defining the classes samples */
314 
316  sampleC1_1.push_back( 41 );
317  sampleC1_1.push_back( 212 );
318  sampleC1_1.push_back( 81 );
319 
321  sampleC1_2.push_back( 38 );
322  sampleC1_2.push_back( 213 );
323  sampleC1_2.push_back( 76 );
324 
326  sampleC1_3.push_back( 42 );
327  sampleC1_3.push_back( 128 );
328  sampleC1_3.push_back( 70 );
329 
331  sampleC1_4.push_back( 31 );
332  sampleC1_4.push_back( 159 );
333  sampleC1_4.push_back( 59 );
334 
336  class1Samples.push_back( sampleC1_1 );
337  class1Samples.push_back( sampleC1_2 );
338  class1Samples.push_back( sampleC1_3 );
339  class1Samples.push_back( sampleC1_4 );
340 
342  sampleC2_1.push_back( 39 );
343  sampleC2_1.push_back( 36 );
344  sampleC2_1.push_back( 81 );
345 
347  sampleC2_2.push_back( 35 );
348  sampleC2_2.push_back( 41 );
349  sampleC2_2.push_back( 72 );
350 
352  sampleC2_3.push_back( 39 );
353  sampleC2_3.push_back( 42 );
354  sampleC2_3.push_back( 76 );
355 
357  sampleC2_4.push_back( 38 );
358  sampleC2_4.push_back( 38 );
359  sampleC2_4.push_back( 77 );
360 
362  class2Samples.push_back( sampleC2_1 );
363  class2Samples.push_back( sampleC2_2 );
364  class2Samples.push_back( sampleC2_3 );
365  class2Samples.push_back( sampleC2_4 );
366 
368  allClassesSamples->insert(te::rp::ClassifierMAPStrategy::Parameters::MClassesSamplesCT::value_type(1, class1Samples));
369  allClassesSamples->insert(te::rp::ClassifierMAPStrategy::Parameters::MClassesSamplesCT::value_type(2, class2Samples));
370 
371  /* Define classification parameters */
372 
373  /* Input parameters */
374 
375  te::rp::Classifier::InputParameters algoInputParameters;
376  algoInputParameters.m_inputRasterPtr = rin;
377  algoInputParameters.m_inputRasterBands.push_back(0);
378  algoInputParameters.m_inputRasterBands.push_back(1);
379  algoInputParameters.m_inputRasterBands.push_back(2);
380 
381  /* Link specific parameters with chosen implementation */
382 
383  te::rp::ClassifierMAPStrategy::Parameters classifierparameters;
384  classifierparameters.m_trainSamplesPtr = allClassesSamples;
385 
386  algoInputParameters.m_strategyName = "map";
387  algoInputParameters.setClassifierStrategyParams(classifierparameters);
388  algoInputParameters.m_createRasterPalette = true;
389  algoInputParameters.m_enableMultiThread = false;
390  algoInputParameters.m_enableProgressInterface = true;
391 
392 
393  /* Output parameters */
394 
395  te::rp::Classifier::OutputParameters algoOutputParameters;
396  algoOutputParameters.m_rInfo = orinfo;
397  algoOutputParameters.m_rType = "GDAL";
398 
399  /* Execute the algorithm */
400 
401  te::rp::Classifier algorithmInstance;
402 
403  BOOST_CHECK( algorithmInstance.initialize(algoInputParameters) );
404  BOOST_CHECK( algorithmInstance.execute(algoOutputParameters) );
405 
406  /* Clean up */
407  delete rin;
408 }
409 
411 {
412  te::common::ConsoleProgressViewer progressViewerInstance;
413 
414  /* First open the input image */
415 
416  std::map<std::string, std::string> rinfo;
417  rinfo["URI"] = TERRALIB_DATA_DIR"/geotiff/cbers2b_rgb342_crop.tif";
418 
420 
421  /* Create output raster info */
422 
423  std::map<std::string, std::string> orinfo;
424  orinfo["URI"] = "terralib_unittest_rp_Classifier_ED_Test.tif";
425 
426  /* Defining the classes samples */
427 
429  sampleC1_1.push_back( 20 );
430  sampleC1_1.push_back( 190 );
431  sampleC1_1.push_back( 50 );
432 
434  sampleC1_2.push_back( 143 );
435  sampleC1_2.push_back( 242 );
436  sampleC1_2.push_back( 174 );
437 
439  sampleC1_3.push_back( 81 );
440  sampleC1_3.push_back( 226 );
441  sampleC1_3.push_back( 112 );
442 
444  class1Samples.push_back( sampleC1_1 );
445  class1Samples.push_back( sampleC1_2 );
446  class1Samples.push_back( sampleC1_3 );
447 
449  sampleC2_1.push_back( 255 );
450  sampleC2_1.push_back( 226 );
451  sampleC2_1.push_back( 245 );
452 
454  sampleC2_2.push_back( 168 );
455  sampleC2_2.push_back( 138 );
456  sampleC2_2.push_back( 122 );
457 
459  sampleC2_3.push_back( 179 );
460  sampleC2_3.push_back( 167 );
461  sampleC2_3.push_back( 153 );
462 
464  class2Samples.push_back( sampleC2_1 );
465  class2Samples.push_back( sampleC2_2 );
466  class2Samples.push_back( sampleC2_3 );
467 
469  allClassesSamples->insert(te::rp::ClassifierEDStrategy::Parameters::MClassesSamplesCT::value_type(1, class1Samples));
470  allClassesSamples->insert(te::rp::ClassifierEDStrategy::Parameters::MClassesSamplesCT::value_type(2, class2Samples));
471 
472  /* Define classification parameters */
473 
474  /* Input parameters */
475 
476  te::rp::Classifier::InputParameters algoInputParameters;
477  algoInputParameters.m_inputRasterPtr = rin;
478  algoInputParameters.m_inputRasterBands.push_back(0);
479  algoInputParameters.m_inputRasterBands.push_back(1);
480  algoInputParameters.m_inputRasterBands.push_back(2);
481 
482  /* Link specific parameters with chosen implementation */
483 
484  te::rp::ClassifierEDStrategy::Parameters classifierparameters;
485  classifierparameters.m_trainSamplesPtr = allClassesSamples;
486 
487  algoInputParameters.m_strategyName = "ed";
488  algoInputParameters.setClassifierStrategyParams(classifierparameters);
489  algoInputParameters.m_createRasterPalette = true;
490  algoInputParameters.m_enableMultiThread = false;
491  algoInputParameters.m_enableProgressInterface = true;
492 
493 
494  /* Output parameters */
495 
496  te::rp::Classifier::OutputParameters algoOutputParameters;
497  algoOutputParameters.m_rInfo = orinfo;
498  algoOutputParameters.m_rType = "GDAL";
499 
500  /* Execute the algorithm */
501 
502  te::rp::Classifier algorithmInstance;
503 
504  BOOST_CHECK( algorithmInstance.initialize(algoInputParameters) );
505  BOOST_CHECK( algorithmInstance.execute(algoOutputParameters) );
506 
507  /* Clean up */
508  delete rin;
509 }
510 
512 {
513  te::common::ConsoleProgressViewer progressViewerInstance;
514 
515  /* First open the input image */
516 
517  std::map<std::string, std::string> rinfo;
518  rinfo["URI"] = TERRALIB_DATA_DIR"/geotiff/cbers2b_rgb342_crop.tif";
519 
521 
522  /* Create output raster info */
523 
524  std::map<std::string, std::string> orinfo;
525  orinfo["URI"] = "terralib_unittest_rp_Classifier_EM_Test.tif";
526 
527  /* Define classification parameters */
528 
529  /* Input parameters */
530 
531  te::rp::Classifier::InputParameters algoInputParameters;
532  algoInputParameters.m_inputRasterPtr = rin;
533  algoInputParameters.m_inputRasterBands.push_back(0);
534  algoInputParameters.m_inputRasterBands.push_back(1);
535  algoInputParameters.m_inputRasterBands.push_back(2);
536 
537  /* Link specific parameters with chosen implementation */
538 
539  te::rp::ClassifierEMStrategy::Parameters classifierparameters;
540  classifierparameters.m_numberOfClusters = 4;
541  classifierparameters.m_maxIterations = 100;
542  classifierparameters.m_maxInputPoints = 1000;
543  classifierparameters.m_epsilon = 15.0;
544  classifierparameters.m_clustersMeans = std::vector<std::vector<double> >();
545 
546  algoInputParameters.m_strategyName = "em";
547  algoInputParameters.setClassifierStrategyParams(classifierparameters);
548  algoInputParameters.m_createRasterPalette = true;
549  algoInputParameters.m_enableMultiThread = false;
550  algoInputParameters.m_enableProgressInterface = true;
551 
552 
553  /* Output parameters */
554 
555  te::rp::Classifier::OutputParameters algoOutputParameters;
556  algoOutputParameters.m_rInfo = orinfo;
557  algoOutputParameters.m_rType = "GDAL";
558 
559  /* Execute the algorithm */
560 
561  te::rp::Classifier algorithmInstance;
562 
563  BOOST_CHECK( algorithmInstance.initialize(algoInputParameters) );
564  BOOST_CHECK( algorithmInstance.execute(algoOutputParameters) );
565 
566  /* Clean up */
567  delete rin;
568 }
569 
571 {
572  te::common::ConsoleProgressViewer progressViewerInstance;
573 
574  /* First open the input image */
575 
576  std::map<std::string, std::string> rinfo;
577  rinfo["URI"] = TERRALIB_DATA_DIR"/geotiff/cbers2b_rgb342_crop.tif";
578 
580 
581  /* Create output raster info */
582 
583  std::map<std::string, std::string> orinfo;
584  orinfo["URI"] = "terralib_unittest_rp_Classifier_SAM_Test.tif";
585 
586  /* Defining the classes samples */
587 
589  sampleC1_1.push_back( 166 );
590  sampleC1_1.push_back( 255 );
591  sampleC1_1.push_back( 255 );
593  class1Samples.push_back( sampleC1_1 );
594 
596  sampleC2_1.push_back( 36 );
597  sampleC2_1.push_back( 255 );
598  sampleC2_1.push_back( 76 );
600  class2Samples.push_back( sampleC2_1 );
601 
603  sampleC3_1.push_back( 36 );
604  sampleC3_1.push_back( 36 );
605  sampleC3_1.push_back( 76 );
607  class3Samples.push_back( sampleC3_1 );
608 
610  allClassesSamples->insert(te::rp::ClassifierSAMStrategy::ClassesSamplesT::value_type(1, class1Samples));
611  allClassesSamples->insert(te::rp::ClassifierSAMStrategy::ClassesSamplesT::value_type(2, class2Samples));
612  allClassesSamples->insert(te::rp::ClassifierSAMStrategy::ClassesSamplesT::value_type(3, class3Samples));
613 
614  /* Define classification parameters */
615 
616  /* Input parameters */
617 
618  te::rp::Classifier::InputParameters algoInputParameters;
619  algoInputParameters.m_inputRasterPtr = rin;
620  algoInputParameters.m_inputRasterBands.push_back(0);
621  algoInputParameters.m_inputRasterBands.push_back(1);
622  algoInputParameters.m_inputRasterBands.push_back(2);
623 
624  /* Link specific parameters with chosen implementation */
625 
626  te::rp::ClassifierSAMStrategy::Parameters classifierparameters;
627  classifierparameters.m_trainSamplesPtr = allClassesSamples;
628  classifierparameters.m_maxAngularDistances.push_back( 0.2 );
629  classifierparameters.m_maxAngularDistances.push_back( 0.2 );
630  classifierparameters.m_maxAngularDistances.push_back( 0.2 );
631  algoInputParameters.m_strategyName = "sam";
632  algoInputParameters.setClassifierStrategyParams(classifierparameters);
633  algoInputParameters.m_createRasterPalette = true;
634  algoInputParameters.m_enableMultiThread = false;
635  algoInputParameters.m_enableProgressInterface = true;
636 
637 
638  /* Output parameters */
639 
640  te::rp::Classifier::OutputParameters algoOutputParameters;
641  algoOutputParameters.m_rInfo = orinfo;
642  algoOutputParameters.m_rType = "GDAL";
643 
644  /* Execute the algorithm */
645 
646  te::rp::Classifier algorithmInstance;
647 
648  BOOST_CHECK( algorithmInstance.initialize(algoInputParameters) );
649  BOOST_CHECK( algorithmInstance.execute(algoOutputParameters) );
650 
651  /* Clean up */
652  delete rin;
653 }
654 
656 {
657  te::common::ConsoleProgressViewer progressViewerInstance;
658 
659  /* First open the input image */
660 
661  std::map<std::string, std::string> rinfo;
662  rinfo["URI"] = TERRALIB_DATA_DIR"/geotiff/cbers2b_rgb342_crop.tif";
663 
665 
666  /* Create output raster info */
667 
668  std::map<std::string, std::string> orinfo;
669  orinfo["URI"] = "terralib_unittest_rp_Classifier_KMeans_Test.tif";
670 
671  /* Define classification parameters */
672 
673  /* Input parameters */
674 
675  te::rp::Classifier::InputParameters algoInputParameters;
676  algoInputParameters.m_inputRasterPtr = rin;
677  algoInputParameters.m_inputRasterBands.push_back(0);
678  algoInputParameters.m_inputRasterBands.push_back(1);
679  algoInputParameters.m_inputRasterBands.push_back(2);
680 
681  /* Link specific parameters with chosen implementation */
682 
684  classifierparameters.m_K = 4;
685  classifierparameters.m_maxIterations = 100;
686  classifierparameters.m_maxInputPoints = 1000;
687  classifierparameters.m_epsilon = 15.0;
688 
689  algoInputParameters.m_strategyName = "kmeans";
690  algoInputParameters.setClassifierStrategyParams(classifierparameters);
691  algoInputParameters.m_createRasterPalette = true;
692  algoInputParameters.m_enableMultiThread = false;
693  algoInputParameters.m_enableProgressInterface = true;
694 
695 
696  /* Output parameters */
697 
698  te::rp::Classifier::OutputParameters algoOutputParameters;
699  algoOutputParameters.m_rInfo = orinfo;
700  algoOutputParameters.m_rType = "GDAL";
701 
702  /* Execute the algorithm */
703 
704  te::rp::Classifier algorithmInstance;
705 
706  BOOST_CHECK( algorithmInstance.initialize(algoInputParameters) );
707  BOOST_CHECK( algorithmInstance.execute(algoOutputParameters) );
708 
709  /* Clean up */
710  delete rin;
711 }
712 
713 BOOST_AUTO_TEST_SUITE_END()
std::vector< te::gm::Polygon * > m_inputPolygons
The polygons to be classified when using object-based image analysis (OBIA).
Definition: Classifier.h:120
std::string m_strategyName
The segmenter strategy name see each te::rp::SegmenterStrategyFactory inherited classes documentation...
std::map< ClassIDT, SamplesT > ClassesSamplesT
Classes samples container type definition.
Raster classification.
Definition: Classifier.h:65
static std::unique_ptr< DataSource > make(const std::string &driver, const te::core::URI &connInfo)
Geometric property.
MClassesSamplesCTPtr m_trainSamplesPtr
A shared pointer to a always-valid structure where trainning samples are stored.
void setGeometry(std::size_t i, te::gm::Geometry *value)
It sets the value of the i-th property.
boost::shared_ptr< MClassesSamplesCT > MClassesSamplesCTPtr
A shared pointer to a multi classes samples container type definition.
boost::shared_ptr< MClassesSamplesCT > MClassesSamplesCTPtr
A shared pointer to a multi classes samples container type definition.
This file contains include headers for the memory data source of TerraLib.
te::rst::Raster const * m_inputRasterPtr
Input raster.
Definition: Classifier.h:114
bool m_enableProgressInterface
Enable (true) or disable (false) the use of a progress interface (default: disabled).
Definition: Classifier.h:115
void setDouble(std::size_t i, double value)
It sets the value of the i-th property.
An atomic property like an integer or double.
void saveToShp(std::vector< te::gm::Polygon * > polygons, std::vector< double > &polygonsValues, const std::string &shpBaseFileName)
BOOST_AUTO_TEST_CASE(ISOSeg_single_thread_test)
unsigned int m_numberOfClusters
The number of clusters (classes) to estimate in the image.
Raster segmentation.
std::string m_strategyName
The classifier strategy name see each te::rp::ClassifierStrategyFactory inherited classes documentati...
Definition: Classifier.h:121
bool execute(AlgorithmOutputParameters &outputParams)
Executes the algorithm using the supplied parameters.
A class that models the description of a dataset.
Definition: DataSetType.h:72
bool initialize(const AlgorithmInputParameters &inputParams)
Initialize the algorithm instance making it ready for execution.
std::map< ClassIDT, ClassSamplesContainerT > MClassesSamplesCT
Multi-classes samples container type definition.
std::string m_rType
Output raster data source type (as described in te::raster::RasterFactory ).
unsigned int m_maxInputPoints
The maximum number of points used to estimate the clusters (default = 1000).
Classifier output parameters.
Definition: Classifier.h:133
boost::shared_ptr< ClassesSamplesT > ClassesSamplesTPtr
A shared pointer to a multi classes samples container type definition.
ClassesSamplesTPtr m_trainSamplesPtr
A shared pointer to a always-valid structure where trainning samples are stored.
std::vector< double > SampleT
Class sample type definition.
MClassesSamplesCTPtr m_trainSamplesPtr
A shared pointer to a always-valid structure where trainning samples are stored.
std::vector< te::gm::Polygon * > SegmentImage(te::rst::Raster *rin)
std::map< ClassIDT, ClassSamplesContainerT > MClassesSamplesCT
Multi-classes samples container type definition.
unsigned int m_K
The number of clusters (means) to detect in image.
Implementation of a random-access dataset class for the TerraLib In-Memory Data Access driver...
std::vector< double > ClassSampleT
Class sample type definition.
void setSegStrategyParams(const SegmenterStrategyParameters &segStratParams)
Set specific segmenter strategy parameters.
An abstract class for raster data strucutures.
TEDATAACCESSEXPORT void Create(DataSource *ds, DataSetType *dt, DataSet *d, std::size_t limit=0)
It creates the dataset definition in a data source and then fill it with data from the input dataset...
te::rst::Raster const * m_inputRasterPtr
Input raster.
URI C++ Library.
Definition: Attributes.h:37
This file contains include headers for the TerraLib Raster Processing module.
std::vector< unsigned int > m_inputRasterBands
Bands to be processed from the input raster.
Definition: Classifier.h:119
double m_segmentsSimilarityThreshold
Segments similarity treshold - Use lower values to merge only those segments that are more similar - ...
unsigned int m_minSegmentSize
A positive minimum segment size (pixels number - default: 100).
unsigned int m_maxInputPoints
The maximum number of points used to estimate the clusters (default = 1000).
Geometry is the root class of the geometries hierarchy, it follows OGC and ISO standards.
double m_epsilon
The stop criteria. When the clusters change in a value smaller then epsilon, the convergence is achie...
An implementation of the DatasetItem class for the TerraLib In-Memory Data Access driver...
std::map< std::string, std::string > m_rInfo
The necessary information to create the raster (as described in te::raster::RasterFactory).
unsigned int m_maxIterations
The maximum of iterations (E/M steps) to perform if convergence is not achieved.
void setClassifierStrategyParams(const ClassifierStrategyParameters &p)
Set specific classifier strategy parameters.
bool initialize(const AlgorithmInputParameters &inputParams)
Initialize the algorithm instance making it ready for execution.
std::vector< SampleT > SamplesT
Class samples container type definition.
unsigned int m_maxIterations
The maximum of iterations to perform if convergence is not achieved.
This file contains include headers for the TerraLib Common Runtime module.
double m_acceptanceThreshold
The acceptance threshold (the closer to 100%, few clusters are created).
This file contains include headers for the Vector Geometry model of TerraLib.
bool execute(AlgorithmOutputParameters &outputParams)
Executes the algorithm using the supplied parameters.
BOOST_AUTO_TEST_SUITE(classifier_tests) void SegmentImage(te
std::map< std::string, std::string > m_rInfo
The necessary information to create the raster (as described in te::raster::RasterFactory).
Definition: Classifier.h:161
This file contains include headers for the Data Access module of TerraLib.
std::string m_rType
Output raster data source type (as described in te::raster::RasterFactory ).
Definition: Classifier.h:160
std::unique_ptr< te::rst::Raster > m_outputRasterPtr
A pointer the ge generated output raster (label image).
std::vector< ClassSampleT > ClassSamplesContainerT
Class samples container type definition.
std::vector< ClassSampleT > ClassSamplesContainerT
Class samples container type definition.
std::vector< double > m_maxAngularDistances
This is a vector of maximum acceptable angles (radians) between one pixel spectra and the reference s...
std::vector< std::vector< double > > m_clustersMeans
The previously estimated means of the clusters (optional).
std::vector< unsigned int > m_inputRasterBands
Bands to be processed from the input raster.
bool m_createRasterPalette
Enable (true) or disable (false) the creation of a paletted output raster (default:true).
Definition: Classifier.h:116
std::vector< double > ClassSampleT
Class sample type definition.
Classifier input parameters.
Definition: Classifier.h:75
double m_epsilon
The stop criteria. When the clusters change in a value smaller then epsilon, the convergence is achie...
bool m_enableMultiThread
Enable (true) or disable (false) the use multiple threads (default:true).
Definition: Classifier.h:118
static Raster * open(const std::map< std::string, std::string > &rinfo, te::common::AccessPolicy p=te::common::RAccess)
It opens a raster with the given parameters and default raster driver.
bool m_enableBlockProcessing
If true, the original raster will be splitted into small blocks, each one will be segmented independe...