File: TePDIParallelSegmenter_test.cpp

package info (click to toggle)
libterralib 4.3.0%2Bdfsg.2-14
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 61,564 kB
  • sloc: cpp: 225,052; ansic: 31,562; makefile: 807; sh: 80; xml: 37
file content (199 lines) | stat: -rw-r--r-- 7,140 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
#define TEAGN_ENABLE_STDOUT_LOG

#include <TePDIExamplesBase.hpp>

#include <TeAgnostic.h>
#include <TePDIUtils.hpp>
#include <TePDIParallelSegmenter.hpp>
#include <TePDIBaatz.hpp>
#include <TeProgress.h>
#include <TeStdIOProgress.h>

void RegionGrowing_Strategy_pattern1_test()
{
  // Input data
  
  TePDITypes::TePDIRasterPtrType input_image_ptr( new TeRaster(
    std::string( TEPDIEXAMPLESRESPATH "pattern1.tif" ), 'r' ) );
  TEAGN_TRUE_OR_THROW( input_image_ptr->init(), "Unable to init inRaster" );
  
  std::vector< unsigned int > input_channels;
  input_channels.push_back( 0 );
  
  TeRasterParams outputImageParams;
  outputImageParams.nBands( 1 );
  outputImageParams.setNLinesNColumns( 1, 1 );
  outputImageParams.setDataType( TeUNSIGNEDLONG, -1 );
  
  TePDITypes::TePDIRasterPtrType output_image_ptr;
  TEAGN_TRUE_OR_THROW( TePDIUtils::TeAllocRAMRaster( outputImageParams,
    output_image_ptr ), "RAM Raster Alloc error" );    
    
  // Creating segmenter startegy specific parameters 
  // TePDIParaSegRegGrowStrategy
  
  TePDIParameters strategy_params;
  strategy_params.SetParameter( "euc_treshold", (double)1 );
  strategy_params.SetParameter( "area_min", (int)5 );
  
  // Creating segmenter parameters
  
  TePDIParameters segmenterParams;
  segmenterParams.SetParameter( "input_image_ptr", input_image_ptr );
  segmenterParams.SetParameter( "input_channels", input_channels );
  segmenterParams.SetParameter( "strategy_name", 
    std::string( "RegionGrowing") );
  segmenterParams.SetParameter( "output_image_ptr", output_image_ptr );
  segmenterParams.SetParameter( "strategy_params", strategy_params );
  segmenterParams.SetParameter( "max_seg_threads", (unsigned int)0 );
  segmenterParams.SetParameter( "max_block_size", (unsigned int)128 );
  //segmenterParams.SetParameter( "merge_adjacent_blocks", (bool)false );
  
  // Running segmenter
  
  TePDIParallelSegmenter segmenter;
  
  TEAGN_TRUE_OR_THROW( segmenter.Apply( segmenterParams ), "Apply error" );
    
  // Saving result to disk
  
  TEAGN_TRUE_OR_THROW( TePDIUtils::TeRaster2Geotiff( output_image_ptr,
    TEPDIEXAMPLESBINPATH "TePDIParallelSegmenter_RG_pattern1_test.tif" ), 
    "GeoTIF generation error" );   
}


void RegionGrowing_Strategy_test()
{
  // Input data
  
  TePDITypes::TePDIRasterPtrType input_image_ptr( new TeRaster(
    std::string( TEPDIEXAMPLESRESPATH "cbers2b_rgb342_crop.tif" ), 'r' ) );
  TEAGN_TRUE_OR_THROW( input_image_ptr->init(), "Unable to init inRaster" );
  
  std::vector< unsigned int > input_channels;
  input_channels.push_back( 0 );
  input_channels.push_back( 1 );
  input_channels.push_back( 2 );
  
  TeRasterParams outputImageParams;
  outputImageParams.nBands( 1 );
  outputImageParams.setNLinesNColumns( 1, 1 );
  outputImageParams.setDataType( TeUNSIGNEDLONG, -1 );
  
  TePDITypes::TePDIRasterPtrType output_image_ptr;
  TEAGN_TRUE_OR_THROW( TePDIUtils::TeAllocRAMRaster( outputImageParams,
    output_image_ptr ), "RAM Raster Alloc error" );    
    
  // Creating segmenter startegy specific parameters 
  // TePDIParaSegRegGrowStrategy
  
  TePDIParameters strategy_params;
  strategy_params.SetParameter( "euc_treshold", (double)20 );
  strategy_params.SetParameter( "area_min", (int)5 );
  
  // Creating segmenter parameters
  
  TePDIParameters segmenterParams;
  segmenterParams.SetParameter( "input_image_ptr", input_image_ptr );
  segmenterParams.SetParameter( "input_channels", input_channels );
  segmenterParams.SetParameter( "strategy_name", 
    std::string( "RegionGrowing") );
  segmenterParams.SetParameter( "output_image_ptr", output_image_ptr );
  segmenterParams.SetParameter( "strategy_params", strategy_params );
  //segmenterParams.SetParameter( "max_seg_threads", (unsigned int)1 );
  //segmenterParams.SetParameter( "max_block_size", (unsigned int)100 );
  //segmenterParams.SetParameter( "merge_adjacent_blocks", (bool)false );
  
  // Running segmenter
  
  TePDIParallelSegmenter segmenter;
  
  TEAGN_TRUE_OR_THROW( segmenter.Apply( segmenterParams ), "Apply error" );
    
  // Saving result to disk
  
  TEAGN_TRUE_OR_THROW( TePDIUtils::TeRaster2Geotiff( output_image_ptr,
    TEPDIEXAMPLESBINPATH "TePDIParallelSegmenter_RG_test.tif" ), 
    "GeoTIF generation error" );   
}

void Baatz_Strategy_test()
{
  // Input data
  
  TePDITypes::TePDIRasterPtrType input_image_ptr( new TeRaster(
    std::string( TEPDIEXAMPLESRESPATH "cbers2b_rgb342_crop.tif" ), 'r' ) );
  TEAGN_TRUE_OR_THROW( input_image_ptr->init(), "Unable to init inRaster" );
  
  std::vector< unsigned int > input_channels;
  input_channels.push_back( 0 );
  input_channels.push_back( 1 );
  input_channels.push_back( 2 );
  
  TeRasterParams outputImageParams;
  outputImageParams.nBands( 1 );
  outputImageParams.setNLinesNColumns( 1, 1 );
  outputImageParams.setDataType( TeUNSIGNEDLONG, -1 );
  
  TePDITypes::TePDIRasterPtrType output_image_ptr;
  TEAGN_TRUE_OR_THROW( TePDIUtils::TeAllocRAMRaster( outputImageParams,
    output_image_ptr ), "RAM Raster Alloc error" );    
    
  // Creating segmenter startegy specific parameters 
  // TePDIParaSegBaatzStrategy
  
  TePDIParameters strategy_params;
  strategy_params.SetParameter( "scale", (float)20 );
  strategy_params.SetParameter( "compactness", (float)0.5 );
  strategy_params.SetParameter( "color", (float)0.5 );
  strategy_params.SetParameter( "euc_treshold", (double)20 );
  
  std::vector<float> input_weights;
  input_weights.push_back( (float) 0.33 );
  input_weights.push_back( (float) 0.33 );
  input_weights.push_back( (float) 0.33 );
  strategy_params.SetParameter( "input_weights", input_weights );  
  
  // Creating segmenter parameters
  
  TePDIParameters segmenterParams;
  segmenterParams.SetParameter( "input_image_ptr", input_image_ptr );
  segmenterParams.SetParameter( "input_channels", input_channels );
  segmenterParams.SetParameter( "strategy_name", 
    std::string( "Baatz") );
  segmenterParams.SetParameter( "output_image_ptr", output_image_ptr );
  segmenterParams.SetParameter( "strategy_params", strategy_params );
  //segmenterParams.SetParameter( "max_seg_threads", (unsigned int)0 );
  //segmenterParams.SetParameter( "max_block_size", (unsigned int)100 );
  //segmenterParams.SetParameter( "merge_adjacent_blocks", (bool)false );
  
  // Running segmenter
  
  TePDIParallelSegmenter segmenter;
  
  TEAGN_TRUE_OR_THROW( segmenter.Apply( segmenterParams ), "Apply error" );
    
  // Saving result to disk
  
  TEAGN_TRUE_OR_THROW( TePDIUtils::TeRaster2Geotiff( output_image_ptr,
    TEPDIEXAMPLESBINPATH "TePDIParallelSegmenter_Baatz_test.tif" ), 
    "GeoTIF generation error" );   
}


int main()
{
  TEAGN_LOGMSG( "Test started." );

  TeStdIOProgress pi;
  TeProgress::setProgressInterf( dynamic_cast< TeProgressBase* >( &pi ) );     
  
  RegionGrowing_Strategy_pattern1_test();
  Baatz_Strategy_test();
  RegionGrowing_Strategy_test();

  TEAGN_LOGMSG( "Test OK." );
  return EXIT_SUCCESS;
}