TsSegmenter.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/segmenter/TsSegmenter.cpp
22 
23  \brief A test suit for Segmenter interface.
24 */
25 
26 // TerraLib
27 #include "../Config.h"
28 #include <terralib/rp.h>
29 #include <terralib/raster.h>
30 #include <terralib/common.h>
31 #include <terralib/dataaccess.h>
32 
33 // STL
34 #include <cmath>
35 
36 // Boost
37 #define BOOST_TEST_NO_MAIN
38 #include <boost/test/unit_test.hpp>
39 #include <boost/shared_ptr.hpp>
40 
41 BOOST_AUTO_TEST_SUITE (segmenter_tests)
42 
43 BOOST_AUTO_TEST_CASE(dummyStrategy_test)
44 {
45  /* Progress interface */
46 
47  te::common::ConsoleProgressViewer progressViewerInstance;
48 
49  /* Open input raster */
50 
51  std::map<std::string, std::string> inputRasterInfo;
52  inputRasterInfo["URI"] = TERRALIB_DATA_DIR "/geotiff/cbers2b_rgb342_crop.tif";
53 
54  boost::shared_ptr< te::rst::Raster > inputRasterPointer ( te::rst::RasterFactory::open(
55  inputRasterInfo ) );
56  BOOST_CHECK( inputRasterPointer.get() );
57 
58  /* Access a raster datasource to create the output raster */
59 
60  std::map<std::string, std::string> outputRasterInfo;
61  outputRasterInfo["URI"] = "terralib_unittest_rp_Segmenter_DummyStrategy_Test.tif";
62 
63  /* Creating the algorithm parameters */
64 
66  strategyParameters.m_minSegmentSize = 50;
67  strategyParameters.m_segmentsSimilarityThreshold = 0.1;
68 
69  te::rp::Segmenter::InputParameters algoInputParams;
70  algoInputParams.m_inputRasterPtr = inputRasterPointer.get();
71  algoInputParams.m_inputRasterBands.push_back( 0 );
72  algoInputParams.m_inputRasterBands.push_back( 1 );
73  algoInputParams.m_inputRasterBands.push_back( 2 );
74  algoInputParams.m_enableThreadedProcessing = false;
75  algoInputParams.m_maxSegThreads = 0;
76  algoInputParams.m_enableBlockProcessing = false;
77  algoInputParams.m_blocksOverlapPercent = 0;
78  algoInputParams.m_maxBlockSize = 0;
79  algoInputParams.m_strategyName = "Dummy";
80  algoInputParams.setSegStrategyParams( strategyParameters );
81  algoInputParams.m_enableProgress = true;
82 
83  te::rp::Segmenter::OutputParameters algoOutputParams;
84  algoOutputParams.m_rInfo = outputRasterInfo;
85  algoOutputParams.m_rType = "GDAL";
86 
87  /* Executing the algorithm */
88 
89  te::rp::Segmenter algorithmInstance;
90 
91  BOOST_CHECK( algorithmInstance.initialize( algoInputParams ) );
92  BOOST_CHECK( algorithmInstance.execute( algoOutputParams ) );
93 }
94 
95 BOOST_AUTO_TEST_CASE(blockProcessingWithoutMerging_test)
96 {
97  /* Progress interface */
98 
99  te::common::ConsoleProgressViewer progressViewerInstance;
100 
101  /* Open input raster */
102 
103  std::map<std::string, std::string> inputRasterInfo;
104  inputRasterInfo["URI"] = TERRALIB_DATA_DIR "/geotiff/cbers2b_rgb342_crop.tif";
105 
106  boost::shared_ptr< te::rst::Raster > inputRasterPointer ( te::rst::RasterFactory::open(
107  inputRasterInfo ) );
108  BOOST_CHECK( inputRasterPointer.get() );
109 
110  /* Access a raster datasource to create the output raster */
111 
112  std::map<std::string, std::string> outputRasterInfo;
113  outputRasterInfo["URI"] = "terralib_unittest_rp_Segmenter_BlockProcessingWithoutMerging_Test.tif";
114 
115  /* Creating the algorithm parameters */
116 
118  strategyParameters.m_minSegmentSize = 50;
119  strategyParameters.m_segmentsSimilarityThreshold = 0.1;
120 
121  te::rp::Segmenter::InputParameters algoInputParams;
122  algoInputParams.m_inputRasterPtr = inputRasterPointer.get();
123  algoInputParams.m_inputRasterBands.push_back( 0 );
124  algoInputParams.m_inputRasterBands.push_back( 1 );
125  algoInputParams.m_inputRasterBands.push_back( 2 );
126  algoInputParams.m_enableThreadedProcessing = false;
127  algoInputParams.m_maxSegThreads = 0;
128  algoInputParams.m_enableBlockProcessing = true;
129  algoInputParams.m_blocksOverlapPercent = 0;
130  algoInputParams.m_maxBlockSize = 100;
131  algoInputParams.m_strategyName = "Dummy";
132  algoInputParams.setSegStrategyParams( strategyParameters );
133  algoInputParams.m_enableProgress = true;
134 
135  te::rp::Segmenter::OutputParameters algoOutputParams;
136  algoOutputParams.m_rInfo = outputRasterInfo;
137  algoOutputParams.m_rType = "GDAL";
138 
139  /* Executing the algorithm */
140 
141  te::rp::Segmenter algorithmInstance;
142 
143  BOOST_CHECK( algorithmInstance.initialize( algoInputParams ) );
144  BOOST_CHECK( algorithmInstance.execute( algoOutputParams ) );
145 }
146 
147 BOOST_AUTO_TEST_CASE(blockProcessingWithMerging_test)
148 {
149  /* Progress interface */
150 
151  te::common::ConsoleProgressViewer progressViewerInstance;
152 
153  /* Open input raster */
154 
155  std::map<std::string, std::string> inputRasterInfo;
156  inputRasterInfo["URI"] = TERRALIB_DATA_DIR "/geotiff/cbers2b_rgb342_crop.tif";
157 
158  boost::shared_ptr< te::rst::Raster > inputRasterPointer ( te::rst::RasterFactory::open(
159  inputRasterInfo ) );
160  BOOST_CHECK( inputRasterPointer.get() );
161 
162  /* Access a raster datasource to create the output raster */
163 
164  std::map<std::string, std::string> outputRasterInfo;
165  outputRasterInfo["URI"] = "terralib_unittest_rp_Segmenter_BlockProcessingWithMerging_Test.tif";
166 
167  /* Creating the algorithm parameters */
168 
170  strategyParameters.m_minSegmentSize = 50;
171  strategyParameters.m_segmentsSimilarityThreshold = 0.1;
172 
173  te::rp::Segmenter::InputParameters algoInputParams;
174  algoInputParams.m_inputRasterPtr = inputRasterPointer.get();
175  algoInputParams.m_inputRasterBands.push_back( 0 );
176  algoInputParams.m_inputRasterBands.push_back( 1 );
177  algoInputParams.m_inputRasterBands.push_back( 2 );
178  algoInputParams.m_enableThreadedProcessing = false;
179  algoInputParams.m_maxSegThreads = 0;
180  algoInputParams.m_enableBlockProcessing = true;
181  algoInputParams.m_blocksOverlapPercent = 20;
182  algoInputParams.m_maxBlockSize = 100;
183  algoInputParams.m_strategyName = "Dummy";
184  algoInputParams.setSegStrategyParams( strategyParameters );
185  algoInputParams.m_enableProgress = true;
186 
187  te::rp::Segmenter::OutputParameters algoOutputParams;
188  algoOutputParams.m_rInfo = outputRasterInfo;
189  algoOutputParams.m_rType = "GDAL";
190 
191  /* Executing the algorithm */
192 
193  te::rp::Segmenter algorithmInstance;
194 
195  BOOST_CHECK( algorithmInstance.initialize( algoInputParams ) );
196  BOOST_CHECK( algorithmInstance.execute( algoOutputParams ) );
197 }
198 
199 BOOST_AUTO_TEST_CASE(threadedProcessing_test)
200 {
201  /* Progress interface */
202 
203  te::common::ConsoleProgressViewer progressViewerInstance;
204 
205  /* Open input raster */
206 
207  std::map<std::string, std::string> inputRasterInfo;
208  inputRasterInfo["URI"] = TERRALIB_DATA_DIR "/geotiff/cbers2b_rgb342_crop.tif";
209 
210  boost::shared_ptr< te::rst::Raster > inputRasterPointer ( te::rst::RasterFactory::open(
211  inputRasterInfo ) );
212  BOOST_CHECK( inputRasterPointer.get() );
213 
214  /* Access a raster datasource to create the output raster */
215 
216  std::map<std::string, std::string> outputRasterInfo;
217  outputRasterInfo["URI"] = "terralib_unittest_rp_Segmenter_ThreadedProcessing_Test.tif";
218 
219  /* Creating the algorithm parameters */
220 
222  strategyParameters.m_minSegmentSize = 50;
223  strategyParameters.m_segmentsSimilarityThreshold = 0.1;
224 
225  te::rp::Segmenter::InputParameters algoInputParams;
226  algoInputParams.m_inputRasterPtr = inputRasterPointer.get();
227  algoInputParams.m_inputRasterBands.push_back( 0 );
228  algoInputParams.m_inputRasterBands.push_back( 1 );
229  algoInputParams.m_inputRasterBands.push_back( 2 );
230  algoInputParams.m_enableThreadedProcessing = true;
231  algoInputParams.m_maxSegThreads = 4;
232  algoInputParams.m_enableBlockProcessing = true;
233  algoInputParams.m_blocksOverlapPercent = 20;
234  algoInputParams.m_maxBlockSize = 100;
235  algoInputParams.m_strategyName = "Dummy";
236  algoInputParams.setSegStrategyParams( strategyParameters );
237  algoInputParams.m_enableProgress = true;
238 
239  te::rp::Segmenter::OutputParameters algoOutputParams;
240  algoOutputParams.m_rInfo = outputRasterInfo;
241  algoOutputParams.m_rType = "GDAL";
242 
243  /* Executing the algorithm */
244 
245  te::rp::Segmenter algorithmInstance;
246 
247  BOOST_CHECK( algorithmInstance.initialize( algoInputParams ) );
248  BOOST_CHECK( algorithmInstance.execute( algoOutputParams ) );
249 }
250 
251 BOOST_AUTO_TEST_CASE(regionGrowingMeanStrategy_test)
252 {
253  /* Progress interface */
254 
255  te::common::ConsoleProgressViewer progressViewerInstance;
256 
257  /* Open input raster */
258 
259  std::map<std::string, std::string> inputRasterInfo;
260  inputRasterInfo["URI"] = TERRALIB_DATA_DIR "/geotiff/cbers2b_rgb342_crop.tif";
261 
262  boost::shared_ptr< te::rst::Raster > inputRasterPointer ( te::rst::RasterFactory::open(
263  inputRasterInfo ) );
264  BOOST_CHECK( inputRasterPointer.get() );
265 
266  /* Access a raster datasource to create the output raster */
267 
268  std::map<std::string, std::string> outputRasterInfo;
269  outputRasterInfo["URI"] = "terralib_unittest_rp_Segmenter_RegionGrowingMeanStrategy_Test.tif";
270 
271  /* Creating the algorithm parameters */
272 
274  strategyParameters.m_minSegmentSize = 100;
275  strategyParameters.m_segmentsSimilarityThreshold = 0.03;
276 
277  te::rp::Segmenter::InputParameters algoInputParams;
278  algoInputParams.m_inputRasterPtr = inputRasterPointer.get();
279  algoInputParams.m_inputRasterBands.push_back( 0 );
280  algoInputParams.m_inputRasterBands.push_back( 1 );
281  algoInputParams.m_inputRasterBands.push_back( 2 );
282  algoInputParams.m_enableThreadedProcessing = false;
283  algoInputParams.m_maxSegThreads = 0;
284  algoInputParams.m_enableBlockProcessing = false;
285  algoInputParams.m_blocksOverlapPercent = 0;
286  algoInputParams.m_maxBlockSize = 0;
287  algoInputParams.m_strategyName = "RegionGrowingMean";
288  algoInputParams.setSegStrategyParams( strategyParameters );
289  algoInputParams.m_enableProgress = true;
290 
291  te::rp::Segmenter::OutputParameters algoOutputParams;
292  algoOutputParams.m_rInfo = outputRasterInfo;
293  algoOutputParams.m_rType = "GDAL";
294 
295  /* Executing the algorithm */
296 
297  te::rp::Segmenter algorithmInstance;
298 
299  BOOST_CHECK( algorithmInstance.initialize( algoInputParams ) );
300  BOOST_CHECK( algorithmInstance.execute( algoOutputParams ) );
301 }
302 
303 BOOST_AUTO_TEST_CASE(regionGrowingMeanStrategyBlockProcessing_test)
304 {
305  /* Progress interface */
306 
307  te::common::ConsoleProgressViewer progressViewerInstance;
308 
309  /* Open input raster */
310 
311  std::map<std::string, std::string> inputRasterInfo;
312  inputRasterInfo["URI"] = TERRALIB_DATA_DIR "/geotiff/cbers2b_rgb342_crop.tif";
313 
314  boost::shared_ptr< te::rst::Raster > inputRasterPointer ( te::rst::RasterFactory::open(
315  inputRasterInfo ) );
316  BOOST_CHECK( inputRasterPointer.get() );
317 
318  /* Access a raster datasource to create the output raster */
319 
320  std::map<std::string, std::string> outputRasterInfo;
321  outputRasterInfo["URI"] = "terralib_unittest_rp_Segmenter_RegionGrowingMeanStrategyBlockProcessing_Test.tif";
322 
323  /* Creating the algorithm parameters */
324 
326  strategyParameters.m_minSegmentSize = 100;
327  strategyParameters.m_segmentsSimilarityThreshold = 0.1;
328 
329  te::rp::Segmenter::InputParameters algoInputParams;
330  algoInputParams.m_inputRasterPtr = inputRasterPointer.get();
331  algoInputParams.m_inputRasterBands.push_back( 0 );
332  algoInputParams.m_inputRasterBands.push_back( 1 );
333  algoInputParams.m_inputRasterBands.push_back( 2 );
334  algoInputParams.m_enableThreadedProcessing = false;
335  algoInputParams.m_maxSegThreads = 0;
336  algoInputParams.m_enableBlockProcessing = true;
337  algoInputParams.m_blocksOverlapPercent = 20;
338  algoInputParams.m_maxBlockSize = 100;
339  algoInputParams.m_strategyName = "RegionGrowingMean";
340  algoInputParams.setSegStrategyParams( strategyParameters );
341  algoInputParams.m_enableProgress = true;
342 
343  te::rp::Segmenter::OutputParameters algoOutputParams;
344  algoOutputParams.m_rInfo = outputRasterInfo;
345  algoOutputParams.m_rType = "GDAL";
346 
347  /* Executing the algorithm */
348 
349  te::rp::Segmenter algorithmInstance;
350 
351  BOOST_CHECK( algorithmInstance.initialize( algoInputParams ) );
352  BOOST_CHECK( algorithmInstance.execute( algoOutputParams ) );
353 }
354 
355 BOOST_AUTO_TEST_CASE(regionGrowingBaatzStrategy_test)
356 {
357  /* Progress interface */
358 
359  te::common::ConsoleProgressViewer progressViewerInstance;
360 
361  /* Open input raster */
362 
363  std::map<std::string, std::string> inputRasterInfo;
364  inputRasterInfo["URI"] = TERRALIB_DATA_DIR "/geotiff/cbers2b_rgb342_crop.tif";
365 
366  boost::shared_ptr< te::rst::Raster > inputRasterPointer ( te::rst::RasterFactory::open(
367  inputRasterInfo ) );
368  BOOST_CHECK( inputRasterPointer.get() );
369 
370  /* Access a raster datasource to create the output raster */
371 
372  std::map<std::string, std::string> outputRasterInfo;
373  outputRasterInfo["URI"] = "terralib_unittest_rp_Segmenter_RegionGrowingBaatzStrategy_Test.tif";
374 
375  /* Creating the algorithm parameters */
376 
378  strategyParameters.m_minSegmentSize = 100;
379  strategyParameters.m_segmentsSimilarityThreshold = 0.5;
380  strategyParameters.m_bandsWeights.resize(
381  (unsigned int)inputRasterPointer->getNumberOfBands(),
382  1.0 / ((double)inputRasterPointer->getNumberOfBands()) );
383  strategyParameters.m_colorWeight = 0.9;
384  strategyParameters.m_compactnessWeight = 0.5;
385  strategyParameters.m_segmentsSimIncreaseSteps = 10;
386 
387  te::rp::Segmenter::InputParameters algoInputParams;
388  algoInputParams.m_inputRasterPtr = inputRasterPointer.get();
389  algoInputParams.m_inputRasterBands.push_back( 0 );
390  algoInputParams.m_inputRasterBands.push_back( 1 );
391  algoInputParams.m_inputRasterBands.push_back( 2 );
392  algoInputParams.m_enableThreadedProcessing = false;
393  algoInputParams.m_maxSegThreads = 0;
394  algoInputParams.m_enableBlockProcessing = false;
395  algoInputParams.m_blocksOverlapPercent = 0;
396  algoInputParams.m_maxBlockSize = 0;
397  algoInputParams.m_strategyName = "RegionGrowingBaatz";
398  algoInputParams.setSegStrategyParams( strategyParameters );
399  algoInputParams.m_enableProgress = true;
400 
401  te::rp::Segmenter::OutputParameters algoOutputParams;
402  algoOutputParams.m_rInfo = outputRasterInfo;
403  algoOutputParams.m_rType = "GDAL";
404 
405  /* Executing the algorithm */
406 
407  te::rp::Segmenter algorithmInstance;
408 
409  BOOST_CHECK( algorithmInstance.initialize( algoInputParams ) );
410  BOOST_CHECK( algorithmInstance.execute( algoOutputParams ) );
411 }
412 
413 BOOST_AUTO_TEST_CASE(regionGrowingBaatzStrategyBlockProcessing_test)
414 {
415  /* Progress interface */
416 
417  te::common::ConsoleProgressViewer progressViewerInstance;
418 
419  /* Open input raster */
420 
421  std::map<std::string, std::string> inputRasterInfo;
422  inputRasterInfo["URI"] = TERRALIB_DATA_DIR "/geotiff/cbers2b_rgb342_crop.tif";
423 
424  boost::shared_ptr< te::rst::Raster > inputRasterPointer ( te::rst::RasterFactory::open(
425  inputRasterInfo ) );
426  BOOST_CHECK( inputRasterPointer.get() );
427 
428  /* Access a raster datasource to create the output raster */
429 
430  std::map<std::string, std::string> outputRasterInfo;
431  outputRasterInfo["URI"] = "terralib_unittest_rp_Segmenter_RegionGrowingBaatzStrategyBlockProcessing_Test.tif";
432 
433  /* Creating the algorithm parameters */
434 
436  strategyParameters.m_minSegmentSize = 100;
437  strategyParameters.m_segmentsSimilarityThreshold = 0.03;
438  strategyParameters.m_bandsWeights.resize(
439  (unsigned int)inputRasterPointer->getNumberOfBands(),
440  1.0 / ((double)inputRasterPointer->getNumberOfBands()) );
441  strategyParameters.m_colorWeight = 1.0;
442  strategyParameters.m_compactnessWeight = 0.5;
443  strategyParameters.m_segmentsSimIncreaseSteps = 10;
444 
445  te::rp::Segmenter::InputParameters algoInputParams;
446  algoInputParams.m_inputRasterPtr = inputRasterPointer.get();
447  algoInputParams.m_inputRasterBands.push_back( 0 );
448  algoInputParams.m_inputRasterBands.push_back( 1 );
449  algoInputParams.m_inputRasterBands.push_back( 2 );
450  algoInputParams.m_enableThreadedProcessing = false;
451  algoInputParams.m_maxSegThreads = 0;
452  algoInputParams.m_enableBlockProcessing = true;
453  algoInputParams.m_blocksOverlapPercent = 20;
454  algoInputParams.m_maxBlockSize = 100;
455  algoInputParams.m_strategyName = "RegionGrowingBaatz";
456  algoInputParams.setSegStrategyParams( strategyParameters );
457  algoInputParams.m_enableProgress = true;
458 
459  te::rp::Segmenter::OutputParameters algoOutputParams;
460  algoOutputParams.m_rInfo = outputRasterInfo;
461  algoOutputParams.m_rType = "GDAL";
462 
463  /* Executing the algorithm */
464 
465  te::rp::Segmenter algorithmInstance;
466 
467  BOOST_CHECK( algorithmInstance.initialize( algoInputParams ) );
468  BOOST_CHECK( algorithmInstance.execute( algoOutputParams ) );
469 }
470 
471 BOOST_AUTO_TEST_SUITE_END()
std::string m_strategyName
The segmenter strategy name see each te::rp::SegmenterStrategyFactory inherited classes documentation...
unsigned char m_blocksOverlapPercent
The percentage of blocks overlapped area (valid range:0-25, defaul:10).
Raster segmentation.
double m_segmentsSimilarityThreshold
Segments similarity treshold - Use lower values to merge only those segments that are more similar - ...
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.
BOOST_AUTO_TEST_CASE(blockProcessingWithoutMerging_test)
Definition: TsSegmenter.cpp:95
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 ).
unsigned int m_minSegmentSize
A positive minimum segment size (pixels number - default: 100).
unsigned int m_maxBlockSize
The input image will be split into blocks with this width for processing, this parameter tells the ma...
bool m_enableProgress
Enable/Disable the progress interface (default:false).
void setSegStrategyParams(const SegmenterStrategyParameters &segStratParams)
Set specific segmenter strategy parameters.
te::rst::Raster const * m_inputRasterPtr
Input raster.
BOOST_AUTO_TEST_SUITE(segmenter_tests) BOOST_AUTO_TEST_CASE(dummyStrategy_test)
Definition: TsSegmenter.cpp:41
This file contains include headers for the TerraLib Raster Processing module.
unsigned int m_segmentsSimIncreaseSteps
The maximum number of steps to increment the similarity threshold value for the cases where no segmen...
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 TerraLib Common Runtime module.
bool m_enableThreadedProcessing
If true, threaded processing will be performed (best with multi-core or multi-processor systems (defa...
bool execute(AlgorithmOutputParameters &outputParams)
Executes the algorithm using the supplied parameters.
unsigned int m_maxSegThreads
The maximum number of concurrent segmenter threads (default:0 - automatically found).
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.
bool m_enableBlockProcessing
If true, the original raster will be splitted into small blocks, each one will be segmented independe...