forked from ANTsX/ANTs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
LabelGeometryMeasures.cxx
385 lines (339 loc) · 13.2 KB
/
LabelGeometryMeasures.cxx
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
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
#include "antsUtilities.h"
#include "antsAllocImage.h"
#include <algorithm>
#include "ReadWriteData.h"
#include "itkAffineTransform.h"
#include "itkCSVArray2DDataObject.h"
#include "itkCSVArray2DFileReader.h"
#include "itkCSVNumericObjectFileWriter.h"
#include "itkImage.h"
#include "itkLabelGeometryImageFilter.h"
#include "itkNearestNeighborInterpolateImageFunction.h"
#include "itkResampleImageFilter.h"
#include "itkTransformFileWriter.h"
#include "itkLabelPerimeterEstimationCalculator.h"
#include "itkLabelMap.h"
#include "itkLabelImageToShapeLabelMapFilter.h"
#include "itkShapeLabelMapFilter.h"
#include "itkShapeLabelObject.h"
#include <iostream>
#include <vector>
#include <cmath>
#include <iomanip>
#include <iostream>
#include <ostream>
#include <sstream>
namespace ants
{
template <unsigned int ImageDimension>
int LabelGeometryMeasures( int argc, char * argv[] )
{
using LabelType = unsigned int;
using LabelImageType = itk::Image<LabelType, ImageDimension>;
using RealType = float;
using RealImageType = itk::Image<RealType, ImageDimension>;
typename LabelImageType::Pointer labelImage = LabelImageType::New();
ReadImage<LabelImageType>( labelImage, argv[2] );
typename RealImageType::Pointer intensityImage = RealImageType::New();
bool intensityImageUsed = false;
if( argc > 3 )
{
try
{
ReadImage<RealImageType>( intensityImage, argv[3] );
intensityImageUsed = true;
}
catch( ... )
{
}
}
bool outputCSVFormat = false;
if( argc > 4 )
{
outputCSVFormat = true;
}
using FilterType = itk::LabelGeometryImageFilter<LabelImageType, RealImageType>;
typename FilterType::Pointer filter = FilterType::New();
filter->SetInput( labelImage );
if( intensityImageUsed )
{
filter->SetIntensityInput( intensityImage );
}
filter->CalculatePixelIndicesOff();
filter->CalculateOrientedBoundingBoxOff();
filter->CalculateOrientedLabelRegionsOff();
// These generate optional outputs.
// filter->CalculatePixelIndicesOn();
// filter->CalculateOrientedBoundingBoxOn();;
// filter->CalculateOrientedLabelRegionsOn();
filter->Update();
using LabelObjectType = itk::ShapeLabelObject<LabelType, ImageDimension>;
using LabelMapType = itk::LabelMap<LabelObjectType>;
// convert the image in a collection of objects
using ConverterType = itk::LabelImageToShapeLabelMapFilter<LabelImageType, LabelMapType>;
typename ConverterType::Pointer converter = ConverterType::New();
converter->SetInput( labelImage );
using ValuatorType = itk::ShapeLabelMapFilter<LabelMapType>;
typename ValuatorType::Pointer valuator = ValuatorType::New();
valuator->SetInput( converter->GetOutput() );
valuator->Update();
typename LabelMapType::Pointer labelMap = valuator->GetOutput();
// typedef itk::LabelPerimeterEstimationCalculator<LabelImageType> AreaFilterType;
// typename AreaFilterType::Pointer areafilter = AreaFilterType::New();
// areafilter->SetImage( labelImage );
// areafilter->SetFullyConnected( false );
// areafilter->Compute();
typename FilterType::LabelsType allLabels = filter->GetLabels();
std::sort( allLabels.begin(), allLabels.end() );
if( outputCSVFormat )
{
typename FilterType::LabelsType::iterator allLabelsIt;
std::vector<std::string> columnHeaders;
columnHeaders.emplace_back( "Label" );
columnHeaders.emplace_back( "VolumeInVoxels" );
columnHeaders.emplace_back( "SurfaceAreaInMillimetersSquared" );
columnHeaders.emplace_back( "Eccentricity" );
columnHeaders.emplace_back( "Elongation" );
columnHeaders.emplace_back( "Orientation" );
columnHeaders.emplace_back( "Centroid_x" );
columnHeaders.emplace_back( "Centroid_y" );
if( ImageDimension == 3 )
{
columnHeaders.emplace_back( "Centroid_z" );
}
columnHeaders.emplace_back( "AxesLength_x" );
columnHeaders.emplace_back( "AxesLength_y" );
if( ImageDimension == 3 )
{
columnHeaders.emplace_back( "AxesLength_z" );
}
columnHeaders.emplace_back( "BoundingBoxLower_x" );
columnHeaders.emplace_back( "BoundingBoxUpper_x" );
columnHeaders.emplace_back( "BoundingBoxLower_y" );
columnHeaders.emplace_back( "BoundingBoxUpper_y" );
if( ImageDimension == 3 )
{
columnHeaders.emplace_back( "BoundingBoxLower_z" );
columnHeaders.emplace_back( "BoundingBoxUpper_z" );
}
if( filter->GetIntensityInput() )
{
columnHeaders.emplace_back( "IntegratedIntensity" );
columnHeaders.emplace_back( "WeightedCentroid_x" );
columnHeaders.emplace_back( "WeightedCentroid_y" );
if( ImageDimension == 3 )
{
columnHeaders.emplace_back( "WeightedCentroid_z" );
}
}
std::vector<std::string> rowHeaders;
for( allLabelsIt = allLabels.begin(); allLabelsIt != allLabels.end(); allLabelsIt++ )
{
if( *allLabelsIt == 0 )
{
continue;
}
std::ostringstream convert;// stream used for the conversion
convert << *allLabelsIt; // insert the textual representation of 'Number' in the characters in the stream
rowHeaders.push_back( convert.str() ); // set 'Result' to the contents of the stream
}
vnl_matrix<double> measures( allLabels.size() - 1, columnHeaders.size() - 1 );
unsigned int rowIndex = 0;
for( allLabelsIt = allLabels.begin(); allLabelsIt != allLabels.end(); allLabelsIt++ )
{
if( *allLabelsIt == 0 )
{
continue;
}
unsigned int columnIndex = 0;
// measures( rowIndex, columnIndex ) = static_cast< double >( *allLabelsIt );
measures( rowIndex, columnIndex++ ) = filter->GetVolume( *allLabelsIt );
const LabelObjectType * labelObject = labelMap->GetLabelObject( *allLabelsIt );
measures( rowIndex, columnIndex++ ) = labelObject->GetPerimeter();
measures( rowIndex, columnIndex++ ) = filter->GetEccentricity( *allLabelsIt );
measures( rowIndex, columnIndex++ ) = filter->GetElongation( *allLabelsIt );
measures( rowIndex, columnIndex++ ) = filter->GetOrientation( *allLabelsIt );
measures( rowIndex, columnIndex++ ) = filter->GetCentroid( *allLabelsIt )[0];
measures( rowIndex, columnIndex++ ) = filter->GetCentroid( *allLabelsIt )[1];
if( ImageDimension == 3 )
{
measures( rowIndex, columnIndex++ ) = filter->GetCentroid( *allLabelsIt )[2];
}
measures( rowIndex, columnIndex++ ) = filter->GetAxesLength( *allLabelsIt )[0];
measures( rowIndex, columnIndex++ ) = filter->GetAxesLength( *allLabelsIt )[1];
if( ImageDimension == 3 )
{
measures( rowIndex, columnIndex++ ) = filter->GetAxesLength( *allLabelsIt )[2];
}
unsigned int arrayIndex = 0;
measures( rowIndex, columnIndex++ ) = filter->GetBoundingBox( *allLabelsIt )[arrayIndex++];
measures( rowIndex, columnIndex++ ) = filter->GetBoundingBox( *allLabelsIt )[arrayIndex++];
if( ImageDimension == 3 )
{
measures( rowIndex, columnIndex++ ) = filter->GetBoundingBox( *allLabelsIt )[arrayIndex++];
}
measures( rowIndex, columnIndex++ ) = filter->GetBoundingBox( *allLabelsIt )[arrayIndex++];
measures( rowIndex, columnIndex++ ) = filter->GetBoundingBox( *allLabelsIt )[arrayIndex++];
if( ImageDimension == 3 )
{
measures( rowIndex, columnIndex++ ) = filter->GetBoundingBox( *allLabelsIt )[arrayIndex++];
}
if( filter->GetIntensityInput() )
{
measures( rowIndex, columnIndex++ ) = filter->GetIntegratedIntensity( *allLabelsIt );
measures( rowIndex, columnIndex++ ) = filter->GetWeightedCentroid( *allLabelsIt )[0];
measures( rowIndex, columnIndex++ ) = filter->GetWeightedCentroid( *allLabelsIt )[1];
if( ImageDimension == 3 )
{
measures( rowIndex, columnIndex++ ) = filter->GetWeightedCentroid( *allLabelsIt )[2];
}
}
rowIndex++;
}
using WriterType = itk::CSVNumericObjectFileWriter<double, 1, 1>;
WriterType::Pointer writer = WriterType::New();
writer->SetFileName( argv[4] );
writer->SetColumnHeaders( columnHeaders );
writer->SetRowHeaders( rowHeaders );
writer->SetInput( &measures );
try
{
writer->Write();
}
catch( itk::ExceptionObject& exp )
{
std::cerr << "Exception caught!" << std::endl;
std::cerr << exp << std::endl;
return EXIT_FAILURE;
}
}
else
{
typename FilterType::LabelsType::iterator allLabelsIt;
// std::cout << "Number of labels: " << labelGeometryFilter->GetNumberOfLabels() << std::endl;
// std::cout << "Label geometry measures." << std::endl;
std::cout << std::left << std::setw( 7 ) << "Label"
<< std::left << std::setw( 10 ) << "Volume(voxels)"
<< std::left << std::setw( 15 ) << "SurfArea(mm^2)"
<< std::left << std::setw( 15 ) << "Eccentricity"
<< std::left << std::setw( 15 ) << "Elongation"
<< std::left << std::setw( 15 ) << "Orientation"
<< std::left << std::setw( 30 ) << "Centroid"
<< std::left << std::setw( 30 ) << "Axes Length"
<< std::left << std::setw( 30 ) << "Bounding Box";
if( filter->GetIntensityInput() )
{
std::cout << std::left << std::setw( 20 ) << "Integrated Int."
<< std::left << std::setw( 30 ) << "Weighted Centroid";
}
std::cout << std::endl;
for( allLabelsIt = allLabels.begin(); allLabelsIt != allLabels.end(); allLabelsIt++ )
{
if( *allLabelsIt == 0 )
{
continue;
}
std::cout << std::setw( 7 ) << *allLabelsIt;
std::cout << std::setw( 10 ) << filter->GetVolume( *allLabelsIt );
const LabelObjectType * labelObject = labelMap->GetLabelObject( *allLabelsIt );
std::cout << std::setw( 15 ) << labelObject->GetPerimeter();
std::cout << std::setw( 15 ) << filter->GetEccentricity( *allLabelsIt );
std::cout << std::setw( 15 ) << filter->GetElongation( *allLabelsIt );
std::cout << std::setw( 15 ) << filter->GetOrientation( *allLabelsIt );
std::stringstream oss;
oss << filter->GetCentroid( *allLabelsIt );
std::cout << std::setw( 30 ) << ( oss.str() ).c_str();
oss.str( "" );
oss << filter->GetAxesLength( *allLabelsIt );
std::cout << std::setw( 30 ) << ( oss.str() ).c_str();
oss.str( "" );
oss << filter->GetBoundingBox( *allLabelsIt );
std::cout << std::setw( 30 ) << ( oss.str() ).c_str();
oss.str( "" );
// std::cout << filter->GetMajorAxisLength( *allLabelsIt ) << "\t";
// std::cout << filter->GetMinorAxisLength( *allLabelsIt ) << "\t";
if( filter->GetIntensityInput() )
{
oss << filter->GetIntegratedIntensity( *allLabelsIt );
std::cout << std::setw( 20 ) << ( oss.str() ).c_str();
oss.str( "" );
oss << filter->GetWeightedCentroid( *allLabelsIt );
std::cout << std::setw( 30 ) << ( oss.str() ).c_str();
oss.str( "" );
}
std::cout << std::endl;
}
}
return EXIT_SUCCESS;
}
// entry point for the library; parameter 'args' is equivalent to 'argv' in (argc,argv) of commandline parameters to
// 'main()'
int LabelGeometryMeasures( std::vector<std::string> args, std::ostream* itkNotUsed( out_stream ) )
{
// put the arguments coming in as 'args' into standard (argc,argv) format;
// 'args' doesn't have the command name as first, argument, so add it manually;
// 'args' may have adjacent arguments concatenated into one argument,
// which the parser should handle
args.insert( args.begin(), "LabelGeometryMeasures" );
int argc = args.size();
char* * argv = new char *[args.size() + 1];
for( unsigned int i = 0; i < args.size(); ++i )
{
// allocate space for the string plus a null character
argv[i] = new char[args[i].length() + 1];
std::strncpy( argv[i], args[i].c_str(), args[i].length() );
// place the null character in the end
argv[i][args[i].length()] = '\0';
}
argv[argc] = nullptr;
// class to automatically cleanup argv upon destruction
class Cleanup_argv
{
public:
Cleanup_argv( char* * argv_, int argc_plus_one_ ) : argv( argv_ ), argc_plus_one( argc_plus_one_ )
{
}
~Cleanup_argv()
{
for( unsigned int i = 0; i < argc_plus_one; ++i )
{
delete[] argv[i];
}
delete[] argv;
}
private:
char* * argv;
unsigned int argc_plus_one;
};
Cleanup_argv cleanup_argv( argv, argc + 1 );
// antscout->set_stream( out_stream );
if( argc < 3 )
{
std::cout << "Usage 1: " << argv[0] << " imageDimension labelImage [intensityImage=none] [csvFile]" << std::endl;
// std::cout << "Usage 2: " << argv[0] << " X singleLabelImage outputTransform <doReflection=1> <scaleFactor=1>" << std::endl;
if( argc >= 2 &&
( std::string( argv[1] ) == std::string("--help") || std::string( argv[1] ) == std::string("-h") ) )
{
return EXIT_SUCCESS;
}
return EXIT_FAILURE;
}
switch( std::stoi( argv[1] ) )
{
case 2:
{
LabelGeometryMeasures<2>( argc, argv );
}
break;
case 3:
{
LabelGeometryMeasures<3>( argc, argv );
}
break;
default:
std::cout << "Unsupported dimension" << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
} // namespace ants