-
Notifications
You must be signed in to change notification settings - Fork 2
/
CombineDWIs.cxx
404 lines (387 loc) · 15.3 KB
/
CombineDWIs.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
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
#include "CombineDWIsCLP.h"
#include <itkImage.h>
#include <itkImageFileReader.h>
#include <itkImageFileWriter.h>
#include <itkImageAlgorithm.h>
#include <itkMetaDataObject.h>
#include <cmath>
#define EXIT_SUCCESS 0
#define EXIT_FAILURE 1
// Separate the vector image into a vector of images
template <class PixelType>
int SeparateImages( const typename itk::VectorImage<PixelType, 3>
::Pointer & imagePile,
std::vector<typename itk::Image<PixelType, 3>::Pointer> & vectorImage
)
{
typedef itk::Image<PixelType, 3> ImageType;
typedef itk::VectorImage<PixelType, 3> VectorImageType;
typename itk::VectorImage<PixelType, 3>::SizeType size;
typename itk::VectorImage<PixelType, 3>::DirectionType direction;
typename itk::VectorImage<PixelType, 3>::PointType origin;
typename itk::VectorImage<PixelType, 3>::SpacingType spacing;
size = imagePile->GetLargestPossibleRegion().GetSize();
direction = imagePile->GetDirection();
origin = imagePile->GetOrigin();
spacing = imagePile->GetSpacing();
typename itk::ImageRegionIterator<VectorImageType> in( imagePile,
imagePile->GetLargestPossibleRegion() );
typedef typename itk::ImageRegionIterator<ImageType> IteratorImageType;
std::vector<IteratorImageType> out;
for( unsigned int i = 0; i < imagePile->GetVectorLength(); i++ )
{
typename ImageType::Pointer imageTemp = ImageType::New();
imageTemp->SetRegions( size );
imageTemp->SetOrigin( origin );
imageTemp->SetDirection( direction );
imageTemp->SetSpacing( spacing );
imageTemp->Allocate();
vectorImage.push_back( imageTemp );
IteratorImageType outtemp( imageTemp, imageTemp->GetLargestPossibleRegion() );
outtemp.GoToBegin();
out.push_back( outtemp );
}
for( in.GoToBegin(); !in.IsAtEnd(); ++in )
{
itk::VariableLengthVector<PixelType> value = in.Get();
for( unsigned int i = 0; i < imagePile->GetVectorLength(); i++ )
{
out[i].Set( value[i] );
++out[i];
}
}
return EXIT_SUCCESS;
}
template<class PixelType>
void
ReadInputImage( std::string fileName ,
std::vector< typename itk::Image< PixelType , 3 >::Pointer > &vectorOfImage ,
itk::MetaDataDictionary &dict
)
{
// open image file
typedef itk::VectorImage< PixelType , 3 > VectorImageType ;
typename itk::ImageFileReader< VectorImageType >::Pointer reader ;
reader = itk::ImageFileReader< VectorImageType >::New() ;
reader->SetFileName( fileName.c_str() );
reader->Update() ;
// Save metadata dictionary
dict = reader->GetOutput()->GetMetaDataDictionary();
//Separate the vector image into a vector of images
SeparateImages<PixelType>( reader->GetOutput(), vectorOfImage );
}
// Write back the vector of images into a image vector
template <class PixelType>
int AddImage( typename itk::VectorImage<PixelType, 3>
::Pointer & imagePile,
const std::vector<typename itk::Image<PixelType, 3>::Pointer> & vectorImage
)
{
typedef itk::Image<PixelType, 3> ImageType;
imagePile->SetRegions( vectorImage.at( 0 )->GetLargestPossibleRegion().GetSize() );
imagePile->SetOrigin( vectorImage.at( 0 )->GetOrigin() );
imagePile->SetDirection( vectorImage.at( 0 )->GetDirection() );
imagePile->SetSpacing( vectorImage.at( 0 )->GetSpacing() );
imagePile->SetVectorLength( vectorImage.size() );
imagePile->Allocate();
typename itk::ImageRegionIterator<itk::VectorImage<PixelType, 3> > out( imagePile,
imagePile->GetLargestPossibleRegion()
);
typedef typename itk::ImageRegionIterator<ImageType> IteratorImageType;
std::vector<IteratorImageType> in;
for( unsigned int i = 0; i < imagePile->GetVectorLength(); i++ )
{
IteratorImageType intemp( vectorImage.at( i ), vectorImage.at( i )->GetLargestPossibleRegion() );
intemp.GoToBegin();
in.push_back( intemp );
}
itk::VariableLengthVector<PixelType> value;
value.SetSize( vectorImage.size() );
for( out.GoToBegin(); !out.IsAtEnd(); ++out )
{
for( unsigned int i = 0; i < imagePile->GetVectorLength(); i++ )
{
value.SetElement( i, in.at( i ).Get() );
++in[i];
}
out.Set( value );
}
return EXIT_SUCCESS;
}
int GetBValueFromMetaDataDictionary( const itk::MetaDataDictionary &dict , double &bval )
{
std::string bvalueString ;
itk::ExposeMetaData<std::string>( dict , "DWMRI_b-value" , bvalueString ) ;
try
{
bval = std::stod( bvalueString ) ;
}
catch(...)
{
std::cerr << "Cannot convert b-value to double" << std::endl ;
std::cerr << "b-value: " << bvalueString << std::endl ;
return 1 ;
}
return 0 ;
}
int FindLastGradient( const itk::MetaDataDictionary &dict )
{
typedef itk::MetaDataObject<std::string> MetaDataStringType;
itk::MetaDataDictionary::ConstIterator itr = dict.Begin();
itk::MetaDataDictionary::ConstIterator end = dict.End();
int maxVal = -1 ;
while( itr != end )
{
itk::MetaDataObjectBase::Pointer entry = itr->second;
MetaDataStringType::Pointer entryvalue
= dynamic_cast<MetaDataStringType *>( entry.GetPointer() );
if( entryvalue )
{
// get the gradient directions
int pos = itr->first.find( "DWMRI_gradient" );
if( pos != -1 )
{
std::string gradientNumberString = itr->first.substr( 15 ) ; //15 characters in "DWMRI_gradient_"
int gradientNumber = std::stoi( gradientNumberString ) ;
if( gradientNumber > maxVal )
{
maxVal = gradientNumber ;
}
}
}
++itr;
}
return maxVal ;
}
std::vector< itk::Vector< double, 3> > ExtractGradientsFromMetaDataDictionary( const itk::MetaDataDictionary &dict )
{
std::vector< itk::Vector< double, 3> > gradients ;
typedef itk::MetaDataObject<std::string> MetaDataStringType;
itk::MetaDataDictionary::ConstIterator itr = dict.Begin();
itk::MetaDataDictionary::ConstIterator end = dict.End();
while( itr != end )
{
itk::MetaDataObjectBase::Pointer entry = itr->second;
MetaDataStringType::Pointer entryvalue
= dynamic_cast<MetaDataStringType *>( entry.GetPointer() );
if( entryvalue )
{
// get the gradient directions
int pos = itr->first.find( "DWMRI_gradient" );
if( pos != -1 )
{
std::string tagvalue = entryvalue->GetMetaDataObjectValue();
itk::Vector<double, 3> vec;
std::istringstream iss( tagvalue );
iss >> vec[0] >> vec[1] >> vec[2]; // we copy the metavalue in an itk::vector
if( iss.fail() )
{
iss.str( tagvalue );
iss.clear();
std::string trash;
iss >> vec[0] >> trash >> vec[1] >> trash >> vec[2]; // in case the separator between the values is
// something else than spaces
if( iss.fail() ) // problem reading the gradient values
{
std::cerr << "Error reading a DWMRI gradient value - skipping" << itr->first << std::endl ;
}
}
gradients.push_back( vec ) ;
}
}
++itr;
}
return gradients ;
}
void
AppendDictionary( itk::MetaDataDictionary &dictAll , const itk::MetaDataDictionary ¤tDict , bool verbose )
{
double bvalueRef ;
GetBValueFromMetaDataDictionary( dictAll , bvalueRef ) ;
double currentbval ;
GetBValueFromMetaDataDictionary( currentDict , currentbval ) ;
double ratio = std::sqrt( currentbval / bvalueRef ) ;//multiply each component by sqrt of ratio!
if( verbose )
{
std::cout << "Reference b-value: " << bvalueRef << std::endl ;
std::cout << "Current image b-value: " << currentbval <<std::endl ;
std::cout << "Ratio (sqrt):" << ratio << std::endl ;
}
int lastGradient = FindLastGradient( dictAll ) ;
if( verbose )
{
std::cout << "Last gradient in appended dictionary: " << lastGradient << std::endl ;
}
std::vector< itk::Vector< double, 3> > currentGradientsVec ;
currentGradientsVec = ExtractGradientsFromMetaDataDictionary( currentDict ) ;
for( size_t i = 0 ; i < currentGradientsVec.size() ; i++ )
{
std::ostringstream oss;
if( verbose )
{
std::cout << "Current image gradient " << i << std::endl ;
std::cout << currentGradientsVec[ i ][ 0 ] << " " << currentGradientsVec[ i ][ 1 ] << " " << currentGradientsVec[ i ][ 2 ] << std::endl ;
}
// write the new gradient values (after transformation) in the metadatadictionary
if( currentGradientsVec[i].GetNorm() > .00001 ) //we do not multiply by the ratio if it is a b0 image
{
for( int j = 0 ; j < 3 ; j++ )
{
currentGradientsVec[i][j] *= ratio ;
}
}
if( verbose )
{
std::cout << "Current image gradient (with ratio applied): " << i << std::endl ;
std::cout << currentGradientsVec[ i ][ 0 ] << " " << currentGradientsVec[ i ][ 1 ] << " " << currentGradientsVec[ i ][ 2 ] << std::endl ;
}
oss << currentGradientsVec[i][0] << " "
<< currentGradientsVec[i][1] << " "
<< currentGradientsVec[i][2] ;
unsigned long long currentGradientIndex = lastGradient + 1 + i ;
std::ostringstream sstagkey ;
sstagkey << std::setw( 4 ) << std::setfill( '0' ) << currentGradientIndex;
std::string tagkey = "DWMRI_gradient_" + sstagkey.str() ;
if( verbose )
{
std::cout << "Adding following key to dictionary: " << tagkey << ":=" << oss.str() << std::endl ;
}
itk::EncapsulateMetaData<std::string>( dictAll , tagkey, oss.str() ) ;
}
}
template<class PixelType>
int DoIt( int argc, char * argv[] )
{
///////////////// Get arguments using GenerateCLP parser to get all the arguments ///////////////////////
PARSE_ARGS ;
typedef itk::Image<PixelType, 3> ImageType;
typedef itk::VectorImage<PixelType, 3> VectorImageType;
std::vector<typename ImageType::Pointer> vectorOfImages;
itk::MetaDataDictionary dictAll;
if( verbose )
{
std::cout << "Number of input volumes: " << inputVolumes.size() << std::endl ;
std::cout << "Reading reference volume" << std::endl ;
std::cout << "Reference Volume: " << inputVolumes[ 0] << std::endl ;
}
ReadInputImage<PixelType>( inputVolumes[0] , vectorOfImages , dictAll ) ;
if( verbose )
{
std::cout << "Number of Images in reference DWI: " << vectorOfImages.size() << std::endl ;
}
for( unsigned int i = 1 ; i < inputVolumes.size() ; i++ )
{
if( verbose )
{
std::cout << "Processing volume: " << inputVolumes[ i ] << std::endl ;
}
std::vector<typename ImageType::Pointer> currentVectorOfImages;
itk::MetaDataDictionary currentDict;
ReadInputImage<PixelType>( inputVolumes[ i ] , currentVectorOfImages , currentDict ) ;
vectorOfImages.insert( vectorOfImages.end(), currentVectorOfImages.begin(), currentVectorOfImages.end()) ;
AppendDictionary( dictAll , currentDict , verbose ) ;
}
typename itk::VectorImage< PixelType, 3 >::Pointer outputImage ;
outputImage = itk::VectorImage<PixelType, 3>::New() ;
if( verbose )
{
std::cout << "Converting std::vector of itk::Images, to itk::VectorImage" << std::endl ;
}
AddImage<PixelType>( outputImage, vectorOfImages ) ;
outputImage->SetMetaDataDictionary( dictAll );
vectorOfImages.clear() ;
if( verbose )
{
std::cout << "Output Volume name: " << outputVolume << std::endl ;
std::cout << "Writing output volume" << std::endl ;
}
typedef itk::ImageFileWriter<VectorImageType> WriterType ;
try
{
typename WriterType::Pointer writer = WriterType::New() ;
writer->SetInput( outputImage ) ;
writer->SetFileName( outputVolume.c_str() ) ;
writer->UseCompressionOn() ;
writer->Update() ;
}
catch( itk::ExceptionObject exception )
{
std::cerr << exception << std::endl ;
return EXIT_FAILURE ;
}
return EXIT_SUCCESS ;
}
void GetImageType( std::string fileName,
itk::ImageIOBase::IOPixelType &pixelType ,
itk::ImageIOBase::IOComponentType &componentType
)
{
typedef itk::Image< unsigned char , 3 > ImageType ;
itk::ImageFileReader< ImageType >::Pointer imageReader
= itk::ImageFileReader< ImageType >::New() ;
imageReader->SetFileName( fileName.c_str() ) ;
imageReader->UpdateOutputInformation() ;
pixelType = imageReader->GetImageIO()->GetPixelType() ;
componentType = imageReader->GetImageIO()->GetComponentType() ;
}
int main( int argc, char * argv[] )
{
///////////////// Get arguments using GenerateCLP
//// parser to get the input volume filename ///////////////////////
PARSE_ARGS;
if( inputVolumes.empty() )
{
std::cerr << "At least one input image need to be specified" << std::endl ;
return EXIT_FAILURE ;
}
if( outputVolume.empty() )
{
std::cerr << "An output file name needs to be specified" << std::endl ;
return EXIT_FAILURE ;
}
///////////////// Read input volume data type and instantiate
//// the corresponding templated filter function ////////////////////
itk::ImageIOBase::IOPixelType pixelType ;
itk::ImageIOBase::IOComponentType componentType ;
try
{
GetImageType ( inputVolumes[0] , pixelType , componentType ) ;
// This filter handles all image component types
switch( componentType )
{
case itk::ImageIOBase::UCHAR:
return DoIt< unsigned char >( argc , argv ) ;
case itk::ImageIOBase::CHAR:
return DoIt< char >( argc , argv ) ;
case itk::ImageIOBase::USHORT:
return DoIt< unsigned short >( argc , argv ) ;
case itk::ImageIOBase::SHORT:
return DoIt< short >( argc , argv ) ;
case itk::ImageIOBase::UINT:
return DoIt< unsigned int >( argc , argv ) ;
case itk::ImageIOBase::INT:
return DoIt< int >( argc , argv ) ;
case itk::ImageIOBase::ULONG:
return DoIt< unsigned long >( argc , argv ) ;
case itk::ImageIOBase::LONG:
return DoIt< long >( argc , argv ) ;
case itk::ImageIOBase::FLOAT:
return DoIt< float >( argc , argv ) ;
case itk::ImageIOBase::DOUBLE:
return DoIt< double >( argc , argv ) ;
case itk::ImageIOBase::UNKNOWNCOMPONENTTYPE:
default:
std::cout << "unknown component type" << std::endl ;
return EXIT_FAILURE ;
break ;
}
}
catch( itk::ExceptionObject &excep )
{
std::cerr << argv[0] << ": exception caught !" << std::endl ;
std::cerr << excep << std::endl ;
return EXIT_FAILURE ;
}
//should never arrive here
return EXIT_FAILURE ;
}