-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathOrcArmy.cxx
More file actions
348 lines (293 loc) · 11.6 KB
/
OrcArmy.cxx
File metadata and controls
348 lines (293 loc) · 11.6 KB
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
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
#include <iostream>
#include <time.h>
#include <itkBinaryThresholdImageFilter.h>
#include <itkCastImageFilter.h>
#include <itkFastMarchingImageFilter.h>
#include <itkImage.h>
#include <itkImageFileReader.h>
#include <itkImageFileWriter.h>
#include <itkImageRegionConstIterator.h>
#include <itkImageRegionIterator.h>
int main( int argc, char * argv[] )
{
//
//Create orc army image
//
//Classic typedefs
typedef unsigned char PixelType;
typedef itk::Image<PixelType, 2> ImageType;
typedef itk::Image<float, 2> FloatImageType;
//Start creat image
ImageType::Pointer orcArmy = ImageType::New();
ImageType::IndexType orcArmyStart = {0, 0};
ImageType::SizeType orcArmySize = {20000, 20000};
ImageType::RegionType orcArmyRegion;
orcArmyRegion.SetIndex( orcArmyStart );
orcArmyRegion.SetSize( orcArmySize );
orcArmy->SetRegions( orcArmyRegion );
//Put an origin
ImageType::PointType orcArmyOrigin;
orcArmyOrigin[0] = 42;//Orc haz weird or'gin
orcArmyOrigin[1] = -42;
orcArmy->SetOrigin( orcArmyOrigin );
//Put a spacing
ImageType::SpacingType orcArmySpacing;
orcArmySpacing[0] = 0.3; //Orc army not squared
orcArmySpacing[1] = 0.9;
orcArmy->SetSpacing( orcArmySpacing );
//Finally allocate
orcArmy->Allocate();
//Fill the image
int bigOrcRadius = 2000; //Orc army formation circus
int smallOrcRadius = 3900; //Orc haz big army
int gobelinRadius = 4000; //Orc army haz gobelins 2
ImageType::IndexType orcArmyCenter = { 10000, 10000 };
typedef itk::ImageRegionIterator<ImageType> IteratorType;
IteratorType orcGenerator( orcArmy, orcArmy->GetLargestPossibleRegion() );
for (orcGenerator.GoToBegin(); !orcGenerator.IsAtEnd(); ++orcGenerator)
{
ImageType::IndexType currentIndex = orcGenerator.GetIndex();
int xDist = currentIndex[0] - orcArmyCenter[0];
int yDist = currentIndex[1] - orcArmyCenter[1];
int distance = xDist*xDist + yDist*yDist;
if (distance < bigOrcRadius*bigOrcRadius)
{
orcGenerator.Set( 255 );
}
else if (distance < smallOrcRadius*smallOrcRadius)
{
orcGenerator.Set( 200 ); //small orc not so strong
}
else if (distance < gobelinRadius*gobelinRadius)
{
orcGenerator.Set( 100 ); //Gobelin halve value of orc
//Gobelin go dye first !
// niark ! niark ! niark !
}
}
//Take it out
typedef itk::ImageFileWriter<ImageType> WriterType;
WriterType::Pointer mordor = WriterType::New();
mordor->SetInput( orcArmy );
mordor->SetFileName( "./OrcArmy.png" );
mordor->Update();
//
//Now Fast march dilate it
// (No alive seeds)
//
typedef itk::FastMarchingImageFilter<FloatImageType, FloatImageType> FastMarchingFilterType;
typedef itk::BinaryThresholdImageFilter<FloatImageType, ImageType> ThresholdingFilterType;
typedef FastMarchingFilterType::NodeContainer NodeContainer;
typedef FastMarchingFilterType::NodeType NodeType;
NodeType node;
typedef itk::ImageRegionConstIterator<ImageType> ConstIteratorType;
ConstIteratorType it( orcArmy, orcArmy->GetLargestPossibleRegion() );
if (true) //Astuce to delete the fast marching so we can study the memory consumption
{
//Start Chrono
time_t t = time(NULL); //Get Start Time
//Instantiations
FastMarchingFilterType::Pointer growOrcs = FastMarchingFilterType::New();
NodeContainer::Pointer seeds = NodeContainer::New();
seeds->Initialize();
node.SetValue( 0.0 ); // seed value is 0 for all of them
//because these are all starting nodes
unsigned int numberOfSeeds = 0;
for ( it.GoToBegin(); !it.IsAtEnd(); ++it )
{
if ( it.Get() > 0 )
{
node.SetIndex( it.GetIndex() );
seeds->InsertElement( numberOfSeeds++, node );
}
}
//Only Trial points the first time
growOrcs->SetTrialPoints( seeds );
growOrcs->SetInput( NULL );
growOrcs->SetSpeedConstant( 1.0 ); // to solve a simple Eikonal equation
growOrcs->SetOutputSize( orcArmy->GetBufferedRegion().GetSize() );
growOrcs->SetOutputRegion( orcArmy->GetBufferedRegion() );
growOrcs->SetOutputSpacing( orcArmy->GetSpacing() );
growOrcs->SetOutputOrigin( orcArmy->GetOrigin() );
growOrcs->SetStoppingValue( 800 );
try
{
growOrcs->Update();
}
catch ( itk::ExceptionObject & excep )
{
std::cerr << "Exception caught !" << std::endl;
std::cerr << excep << std::endl;
}
ThresholdingFilterType::Pointer threshold = ThresholdingFilterType::New();
threshold->SetInput( growOrcs->GetOutput() );
threshold->SetLowerThreshold( 0.0 );
threshold->SetUpperThreshold( 700.0 );
threshold->SetOutsideValue( 0 );
threshold->SetInsideValue( 255 );
threshold->Update();
//Stop timer and print info
time_t orcArmyGrowthTime = time(NULL) - t;
std::cout<< "The orc army grew in " << orcArmyGrowthTime <<std::endl;
std::cout<< "There was " << numberOfSeeds << " trials points" <<std::endl;
//Write out images
typedef itk::CastImageFilter<FloatImageType, ImageType> CastFilterType;
CastFilterType::Pointer cast = CastFilterType::New();
cast->SetInput( growOrcs->GetOutput() );
mordor->SetInput( cast->GetOutput() );
mordor->SetFileName( "./OrcArmyOnSteroids_CastToUnsignedChar.png" );
mordor->Update();
mordor->SetInput( threshold->GetOutput() );
mordor->SetFileName( "./OrcArmyOnSteroids.png" );
mordor->Update();
}
//
//Now Fast march dilate it
// (With alive seeds)
//
if (true) //First, separate big orcs and the rest
{
//Start Chrono
time_t t = time(NULL); //Get Start Time
//Instantiations
FastMarchingFilterType::Pointer growOrcsWithMagic = FastMarchingFilterType::New();
NodeContainer::Pointer trialSeeds = NodeContainer::New();
NodeContainer::Pointer aliveSeeds = NodeContainer::New();
trialSeeds->Initialize();
aliveSeeds->Initialize();
node.SetValue( 0.0 ); // seed value is 0 for all of them
//because these are all starting nodes
int numberOfAliveSeeds = 0;
int numberOfTrialSeeds = 0;
for ( it.GoToBegin(); !it.IsAtEnd(); ++it )
{
if (it.Get() > 0)
{
node.SetIndex( it.GetIndex() );
if (it.Get() > 200) //Yes
{
aliveSeeds->InsertElement( numberOfAliveSeeds++, node );
}
else //Nop
{
trialSeeds->InsertElement( numberOfTrialSeeds++, node );
}
}
}
//Trial And Alive seeds
growOrcsWithMagic->SetTrialPoints( trialSeeds );
growOrcsWithMagic->SetAlivePoints( aliveSeeds );
growOrcsWithMagic->SetInput( NULL );
growOrcsWithMagic->SetSpeedConstant( 1.0 ); // to solve a simple Eikonal equation
growOrcsWithMagic->SetOutputSize( orcArmy->GetBufferedRegion().GetSize() );
growOrcsWithMagic->SetOutputRegion( orcArmy->GetBufferedRegion() );
growOrcsWithMagic->SetOutputSpacing( orcArmy->GetSpacing() );
growOrcsWithMagic->SetOutputOrigin( orcArmy->GetOrigin() );
growOrcsWithMagic->SetStoppingValue( 800 );
try
{
growOrcsWithMagic->Update();
}
catch ( itk::ExceptionObject & excep )
{
std::cerr << "Exception caught !" << std::endl;
std::cerr << excep << std::endl;
}
ThresholdingFilterType::Pointer thresholdWithMagic = ThresholdingFilterType::New();
thresholdWithMagic->SetInput( growOrcsWithMagic->GetOutput() );
thresholdWithMagic->SetLowerThreshold( 0.0 );
thresholdWithMagic->SetUpperThreshold( 700.0 );
thresholdWithMagic->SetOutsideValue( 0 );
thresholdWithMagic->SetInsideValue( 255 );
thresholdWithMagic->Update();
//Stop timer and print info
time_t orcArmyGrowthTimeWithMagic = time(NULL) - t;
std::cout<< "The orc army grew in " << orcArmyGrowthTimeWithMagic
<< " (it had magic)" <<std::endl;
std::cout<< "There was " << numberOfTrialSeeds << " trials points" <<std::endl;
std::cout<< "And was " << numberOfAliveSeeds << " alive points" <<std::endl;
//Write out images
typedef itk::CastImageFilter<FloatImageType, ImageType> CastFilterType;
CastFilterType::Pointer cast = CastFilterType::New();
cast->SetInput( growOrcsWithMagic->GetOutput() );
mordor->SetInput( cast->GetOutput() );
mordor->SetFileName( "./OrcArmyOnSteroidsWithMagics_CastToUnsignedChar.png" );
mordor->Update();
mordor->SetInput( thresholdWithMagic->GetOutput() );
mordor->SetFileName( "./OrcArmyOnSteroidsWithMagic.png" );
mordor->Update();
}
if (true) //First, separate orcs from gobelin (ony outside layer
{
//Start Chrono
time_t t = time(NULL); //Get Start Time
//Instantiations
FastMarchingFilterType::Pointer growOrcsWithDarkMagic = FastMarchingFilterType::New();
NodeContainer::Pointer trialSeeds = NodeContainer::New();
NodeContainer::Pointer aliveSeeds = NodeContainer::New();
trialSeeds->Initialize();
aliveSeeds->Initialize();
node.SetValue( 0.0 ); // seed value is 0 for all of them
//because these are all starting nodes
int numberOfAliveSeeds = 0;
int numberOfTrialSeeds = 0;
for ( it.GoToBegin(); !it.IsAtEnd(); ++it )
{
if (it.Get() > 0)
{
node.SetIndex( it.GetIndex() );
if (it.Get() > 100) //Yes
{
aliveSeeds->InsertElement( numberOfAliveSeeds++, node );
}
else //Nop
{
trialSeeds->InsertElement( numberOfTrialSeeds++, node );
}
}
}
//Trial And Alive seeds
growOrcsWithDarkMagic->SetTrialPoints( trialSeeds );
growOrcsWithDarkMagic->SetAlivePoints( aliveSeeds );
growOrcsWithDarkMagic->SetInput( NULL );
growOrcsWithDarkMagic->SetSpeedConstant( 1.0 ); // to solve a simple Eikonal equation
growOrcsWithDarkMagic->SetOutputSize( orcArmy->GetBufferedRegion().GetSize() );
growOrcsWithDarkMagic->SetOutputRegion( orcArmy->GetBufferedRegion() );
growOrcsWithDarkMagic->SetOutputSpacing( orcArmy->GetSpacing() );
growOrcsWithDarkMagic->SetOutputOrigin( orcArmy->GetOrigin() );
growOrcsWithDarkMagic->SetStoppingValue( 800 );
try
{
growOrcsWithDarkMagic->Update();
}
catch ( itk::ExceptionObject & excep )
{
std::cerr << "Exception caught !" << std::endl;
std::cerr << excep << std::endl;
}
ThresholdingFilterType::Pointer thresholdWithDarkMagic = ThresholdingFilterType::New();
thresholdWithDarkMagic->SetInput( growOrcsWithDarkMagic->GetOutput() );
thresholdWithDarkMagic->SetLowerThreshold( 0.0 );
thresholdWithDarkMagic->SetUpperThreshold( 700.0 );
thresholdWithDarkMagic->SetOutsideValue( 0 );
thresholdWithDarkMagic->SetInsideValue( 255 );
thresholdWithDarkMagic->Update();
//Stop timer and print info
time_t orcArmyGrowthTimeWithMagic = time(NULL) - t;
std::cout<< "The orc army grew in " << orcArmyGrowthTimeWithMagic
<< " (it had DARK magic !!)" <<std::endl;
std::cout<< "There was " << numberOfTrialSeeds << " trials points" <<std::endl;
std::cout<< "And was " << numberOfAliveSeeds << " alive points" <<std::endl;
//Write out images
typedef itk::CastImageFilter<FloatImageType, ImageType> CastFilterType;
CastFilterType::Pointer cast = CastFilterType::New();
cast->SetInput( growOrcsWithDarkMagic->GetOutput() );
mordor->SetInput( cast->GetOutput() );
mordor->SetFileName( "./OrcArmyOnSteroidsWithDarkMagic_CastToUnsignedChar.png" );
mordor->Update();
mordor->SetInput( thresholdWithDarkMagic->GetOutput() );
mordor->SetFileName( "./OrcArmyOnSteroidsWithDarkMagic.png" );
mordor->Update();
}
return EXIT_SUCCESS;
}