forked from ANTsX/ANTs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathAverageTensorImages.cxx
181 lines (158 loc) · 5.28 KB
/
AverageTensorImages.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
#include "antsUtilities.h"
#include <algorithm>
#include <algorithm>
#include "stdio.h"
#include "itkImage.h"
#include "itkImageRegionIteratorWithIndex.h"
#include "ReadWriteData.h"
#include "TensorFunctions.h"
namespace ants
{
template <unsigned int ImageDimension>
int AverageTensorImages(unsigned int argc, char *argv[])
{
// typedef itk::Vector<float,6> TensorType;
typedef itk::SymmetricSecondRankTensor<float, 3> TensorType;
typedef itk::Image<TensorType, ImageDimension> ImageType;
typedef itk::ImageRegionIteratorWithIndex<ImageType> IteratorType;
char * outputName = argv[2];
int mathtype = atoi(argv[3]);
float numberofimages = (float)argc - 4.0;
std::cout << "Averaging " << numberofimages << " images " << std::endl;
typename ImageType::Pointer averageimage = ITK_NULLPTR;
typename ImageType::Pointer image2 = ITK_NULLPTR;
typename ImageType::SizeType size;
size.Fill(0);
unsigned int bigimage = 0;
for( unsigned int j = 4; j < argc; j++ )
{
// Get the image dimension
std::string fn = std::string(argv[j]);
std::cout << " fn " << fn << std::endl;
typename itk::ImageIOBase::Pointer imageIO =
itk::ImageIOFactory::CreateImageIO(fn.c_str(), itk::ImageIOFactory::ReadMode);
imageIO->SetFileName(fn.c_str() );
imageIO->ReadImageInformation();
for( unsigned int i = 0; i < imageIO->GetNumberOfDimensions(); i++ )
{
if( imageIO->GetDimensions(i) > size[i] )
{
size[i] = imageIO->GetDimensions(i);
bigimage = j;
std::cout << " bigimage " << j << " size " << size << std::endl;
}
}
}
std::cout << " largest image " << size << std::endl;
bool logeuc = true;
if( mathtype == 1 )
{
logeuc = false;
}
TensorType nullTensor;
nullTensor[0] = nullTensor[1] = nullTensor[2] = nullTensor[3]
= nullTensor[4] = nullTensor[5] = 0;
ReadTensorImage<ImageType>(averageimage, argv[bigimage], logeuc);
averageimage->FillBuffer(nullTensor);
for( unsigned int j = 4; j < argc; j++ )
{
std::string fn = std::string(argv[j]);
ReadTensorImage<ImageType>(image2, fn.c_str(), logeuc);
IteratorType vfIter( image2, image2->GetLargestPossibleRegion() );
for( vfIter.GoToBegin(); !vfIter.IsAtEnd(); ++vfIter )
{
TensorType val = vfIter.Get() / numberofimages;
averageimage->SetPixel(vfIter.GetIndex(), val + averageimage->GetPixel(vfIter.GetIndex() ) );
}
}
WriteTensorImage<ImageType>(averageimage, outputName, logeuc);
return EXIT_SUCCESS;
}
// Main Program
// entry point for the library; parameter 'args' is equivalent to 'argv' in (argc,argv) of commandline parameters to
// 'main()'
int AverageTensorImages( std::vector<std::string> args, std::ostream* /*out_stream = NULL */)
{
// 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(), "AverageTensorImages" );
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] = ITK_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 );
try
{
if( argc - 4 < 1 )
{
std::cerr << "Basic useage ex: " << std::endl;
std::cerr << argv[0] << " ImageDimension average.nii mathtype list-of-files-via-wildcard " << std::endl;
std::cerr << " e.g. \n AverageTensorImages 3 average.nii 1 *registered.nii " << std::endl;
std::cerr << " mathtype=[0=log-euclidean, 1=euclidean] " << 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;
}
int dim = atoi(argv[1]);
// char * outputName = argv[2];
// int mathtype = atoi(argv[3]);
// int numberofimages = argc - 4;
// Get the image dimension
switch( dim )
{
case 2:
{
return AverageTensorImages<2>(argc, argv);
}
break;
case 3:
{
return AverageTensorImages<3>(argc, argv);
}
break;
default:
std::cerr << "Unsupported dimension" << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}
catch( itk::ExceptionObject & err )
{
std::cerr << "ExceptionObject caught !" << std::endl;
std::cerr << err << std::endl;
return EXIT_FAILURE;
}
}
} // namespace ants