examples/rp/Segmenter.cpp
Go to the documentation of this file.
1 #include "RPExamples.h"
2 
3 // TerraLib
4 #include <terralib/raster.h>
5 #include <terralib/dataaccess.h>
6 #include <terralib/geometry.h>
7 #include <terralib/memory.h>
8 #include <terralib/rp.h>
9 
10 // STL
11 #include <string>
12 #include <map>
13 
14 void Segmenter()
15 {
16  // -------------------------------------------------------------------------------------------------------------
17  // Region growing segmenter (with mean features) example
18 
19  try
20  {
21  std::cout << "Region growing Segmentation example using Raster Processing module." << std::endl << std::endl;
22 
23 // open input raster
24  std::string data_dir = TERRALIB_DATA_DIR;
25  std::string aux("");
26  std::cout << "Inform your input image (ENTER to accept default \'" << (data_dir + "/geotiff/cbers2b_rgb342_crop.tif") << "\'): ";
27  std::getline (std::cin, aux);
28  std::map<std::string, std::string> rinfo;
29  if (!aux.empty())
30  rinfo["URI"] = aux;
31  else
32  rinfo["URI"] = TERRALIB_DATA_DIR "/geotiff/cbers2b_rgb342_crop.tif";
33 
35 
36 // create output raster info
37  std::cout << "Inform your output image (ENTER to accept default \'" << (data_dir + "/geotiff/cbers2b_rgb342_crop_segmentedMean.tif") << "\'): ";
38  std::getline (std::cin, aux);
39  std::map<std::string, std::string> orinfo;
40  if (!aux.empty())
41  orinfo["URI"] = aux;
42  else
43  orinfo["URI"] = TERRALIB_DATA_DIR "/geotiff/cbers2b_rgb342_crop_segmentedMean.tif";
44 
45 // define segmentation parameters
46 
47 // input parameters
48  te::rp::Segmenter::InputParameters algoInputParameters;
49  algoInputParameters.m_inputRasterPtr = rin;
50  algoInputParameters.m_inputRasterBands.push_back( 0 );
51  algoInputParameters.m_inputRasterBands.push_back( 1 );
52  algoInputParameters.m_inputRasterBands.push_back( 2 );
53 
54 // link specific parameters with chosen implementation
55 // strategy specific parameters (m_minSegmentSize: size of the smallest segment to be created; m_segmentsSimilarityThreshold: similarity between neighboring segments to merge them or not)
57  segparameters.m_minSegmentSize = 50;
58  segparameters.m_segmentsSimilarityThreshold = 0.03;
59 
60  algoInputParameters.m_strategyName = "RegionGrowingMean";
61  algoInputParameters.setSegStrategyParams(segparameters);
62 
63 // output parameters
64 // the output can be a previously created raster (in this case, rout)
65 
66  te::rp::Segmenter::OutputParameters algoOutputParameters;
67  algoOutputParameters.m_rInfo = orinfo;
68  algoOutputParameters.m_rType = "GDAL";
69 
70 // execute the algorithm
71  te::rp::Segmenter seginstance;
72 
73  if( !seginstance.initialize( algoInputParameters ) ) throw;
74  if( !seginstance.execute( algoOutputParameters ) ) throw;
75 
76 // clean up
77  delete rin;
78 
79  std::cout << "Done!" << std::endl << std::endl;
80  }
81  catch(const std::exception& e)
82  {
83  std::cout << std::endl << "An exception has occurred in Segmenter(): " << e.what() << std::endl;
84  }
85  catch(...)
86  {
87  std::cout << std::endl << "An unexpected exception has occurred in Segmenter()!" << std::endl;
88  }
89 
90  // -------------------------------------------------------------------------------------------------------------
91  // Region growing segmenter (with Baatz features) example
92 
93  try
94  {
95  std::cout << "Region growing Segmentation (Baatz features) example using Raster Processing module." << std::endl << std::endl;
96 
97  // open input raster
98  std::string data_dir = TERRALIB_DATA_DIR;
99  std::string aux("");
100  std::cout << "Inform your input image (ENTER to accept default \'" << (data_dir + "/geotiff/cbers2b_rgb342_crop.tif") << "\'): ";
101  std::getline (std::cin, aux);
102  std::map<std::string, std::string> rinfo;
103  if (!aux.empty())
104  rinfo["URI"] = aux;
105  else
106  rinfo["URI"] = TERRALIB_DATA_DIR "/geotiff/cbers2b_rgb342_crop.tif";
107 
109 
110  // create output raster info
111  std::cout << "Inform your output image (ENTER to accept default \'" << (data_dir + "/geotiff/cbers2b_rgb342_crop_segmentedBaatz.tif") << "\'): ";
112  std::getline (std::cin, aux);
113  std::map<std::string, std::string> orinfo;
114  if (!aux.empty())
115  orinfo["URI"] = aux;
116  else
117  orinfo["URI"] = TERRALIB_DATA_DIR "/geotiff/cbers2b_rgb342_crop_segmentedBaatz.tif";
118 
119 // define segmentation parameters
120 
121 // input parameters
122  te::rp::Segmenter::InputParameters algoInputParameters;
123  algoInputParameters.m_inputRasterPtr = rin;
124  algoInputParameters.m_inputRasterBands.push_back( 0 );
125  algoInputParameters.m_inputRasterBands.push_back( 1 );
126  algoInputParameters.m_inputRasterBands.push_back( 2 );
127 
128 // link specific parameters with chosen implementation
129 // strategy specific parameters (m_minSegmentSize: size of the smallest segment to be created; m_segmentsSimilarityThreshold: similarity between neighboring segments to merge them or not)
131  segparameters.m_minSegmentSize = 50;
132  segparameters.m_segmentsSimilarityThreshold = 0.03;
133  segparameters.m_bandsWeights.clear();
134  segparameters.m_colorWeight = 0.9;
135  segparameters.m_compactnessWeight = 0.5;
136 
137  algoInputParameters.m_strategyName = "RegionGrowingBaatz";
138  algoInputParameters.setSegStrategyParams(segparameters);
139 
140 // output parameters
141 // the output can be a previously created raster (in this case, rout)
142 
143  te::rp::Segmenter::OutputParameters algoOutputParameters;
144  algoOutputParameters.m_rInfo = orinfo;
145  algoOutputParameters.m_rType = "GDAL";
146 
147 // execute the algorithm
148  te::rp::Segmenter seginstance;
149 
150  if( ! seginstance.initialize(algoInputParameters) ) throw;
151  if( ! seginstance.execute( algoOutputParameters ) ) throw;
152 
153 // clean up
154  delete rin;
155 
156  std::cout << "Done!" << std::endl << std::endl;
157  }
158  catch(const std::exception& e)
159  {
160  std::cout << std::endl << "An exception has occurred in Segmenter(): " << e.what() << std::endl;
161  }
162  catch(...)
163  {
164  std::cout << std::endl << "An unexpected exception has occurred in Segmenter()!" << std::endl;
165  }
166 
167 }
168 
std::string m_strategyName
The segmenter strategy name see each te::rp::SegmenterStrategyFactory inherited classes documentation...
This file contains include headers for the memory data source of TerraLib.
Raster segmentation.
double m_segmentsSimilarityThreshold
Segments similarity treshold - Use lower values to merge only those segments that are more similar - ...
These routines show how to use the RP (raster processing) module.
std::vector< double > m_bandsWeights
The weight given to each band, when applicable (note: the bands weights sum must always be 1) or an e...
bool initialize(const AlgorithmInputParameters &inputParams)
Initialize the algorithm instance making it ready for execution.
double m_compactnessWeight
The weight given to the compactness component, deafult:0.5, valid range: [0,1].
double m_colorWeight
The weight given to the color component, deafult:0.9, valid range: [0,1].
std::string m_rType
Output raster data source type (as described in te::raster::RasterFactory ).
void Segmenter()
unsigned int m_minSegmentSize
A positive minimum segment size (pixels number - default: 100).
void setSegStrategyParams(const SegmenterStrategyParameters &segStratParams)
Set specific segmenter strategy parameters.
An abstract class for raster data strucutures.
te::rst::Raster const * m_inputRasterPtr
Input raster.
This file contains include headers for the TerraLib Raster Processing module.
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).
std::map< std::string, std::string > m_rInfo
The necessary information to create the raster (as described in te::raster::RasterFactory).
This file contains include headers for the Vector Geometry model of TerraLib.
bool execute(AlgorithmOutputParameters &outputParams)
Executes the algorithm using the supplied parameters.
This file contains include headers for the Data Access module of TerraLib.
std::vector< unsigned int > m_inputRasterBands
Bands to be processed from the input raster.
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.