-
Notifications
You must be signed in to change notification settings - Fork 3
/
NormalizeDWIGradientVectors.cxx
211 lines (203 loc) · 6.86 KB
/
NormalizeDWIGradientVectors.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
#include <itkImageFileReader.h>
#include <itkImageFileWriter.h>
#include <itkVectorImage.h>
#include <cstring>
#include <itkMatrix.h>
#include <itkMetaDataObject.h>
#define NORMALIZEDWIGRADIENTVECTORS_VERSION "1.0.0"
//To check the image voxel type
void GetImageType( const char* fileName ,
itk::ImageIOBase::IOPixelType &pixelType ,
itk::ImageIOBase::IOComponentType &componentType
)
{
typedef itk::Image< unsigned char , 3 > ImageType ;
itk::ImageFileReader< ImageType >::Pointer imageReader ;
imageReader = itk::ImageFileReader< ImageType >::New() ;
imageReader->SetFileName( fileName ) ;
imageReader->UpdateOutputInformation() ;
pixelType = imageReader->GetImageIO()->GetPixelType() ;
componentType = imageReader->GetImageIO()->GetComponentType() ;
}
//Transform the gradient vectors
int NormalizeGradients( itk::MetaDataDictionary &dic )
{
typedef itk::MetaDataObject< std::string > MetaDataStringType ;
itk::MetaDataDictionary::ConstIterator itr = dic.Begin() ;
itk::MetaDataDictionary::ConstIterator end = dic.End() ;
int dtmri = 0 ;
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 )
{
dtmri = true ;
std::string tagvalue = entryvalue->GetMetaDataObjectValue() ;
itk::Vector< double , 3 > vec ;
itk::Vector< double , 3 > transformedVector ;
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" << std::endl ;
return 1 ;
}
}
//gradient null
if( vec.GetNorm() > .00001 )
{
vec /= vec.GetNorm() ;
}
std::ostringstream oss ;
//write the new gradient values (after transformation) in the metadatadictionary
oss << vec[ 0 ] << " " <<
vec[ 1 ] << " " <<
vec[ 2 ] << std::ends ;
entryvalue->SetMetaDataObjectValue( oss.str() ) ;
}
}
++itr ;
}
return dtmri ;
}
int CheckIfDWMRI( itk::MetaDataDictionary &dic )
{
typedef itk::MetaDataObject< std::string > MetaDataStringType ;
itk::MetaDataDictionary::ConstIterator itr = dic.Begin() ;
itk::MetaDataDictionary::ConstIterator end = dic.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( "modality" ) ;
if( pos != -1 )
{
std::string tagvalue = entryvalue->GetMetaDataObjectValue() ;
if( !tagvalue.compare( "DWMRI" ) )
{
return 1 ;
}
}
}
++itr ;
}
return 0 ;
}
template< class PixelType > int Normalize( char* input , char* output )
{
typedef itk::VectorImage< PixelType , 3 > ImageType ;
typedef itk::ImageFileReader< ImageType > ReaderType ;
typedef itk::ImageFileWriter< ImageType > WriterType ;
itk::MetaDataDictionary dic ;
typename ReaderType::Pointer reader = ReaderType::New() ;
reader->SetFileName( input ) ;
try
{
//open image file
reader->Update() ;
//Save metadata dictionary
dic = reader->GetOutput()->GetMetaDataDictionary() ;
}
catch( itk::ExceptionObject exception )
{
std::cerr << exception << std::endl ;
return 1 ;
}
if( !CheckIfDWMRI( dic ) )
{
std::cerr << "This image is not a DWMRI" << std::endl ;
return 1;
}
if( !NormalizeGradients( dic ) )
{
std::cerr << "Error while reading the gradients" << std::endl ;
return 1 ;
}
reader->GetOutput()->SetMetaDataDictionary( dic ) ;
typename WriterType::Pointer writer = WriterType::New() ;
writer->SetInput( reader->GetOutput() ) ;
writer->SetFileName( output ) ;
writer->UseCompressionOn() ;
try
{
writer->Update() ;
}
catch( itk::ExceptionObject exception )
{
std::cerr << exception << std::endl ;
return 1 ;
}
return 0 ;
}
int main( int argc , char* argv[] )
{
if( argc == 2 && !strcmp(argv[ 1 ],"--version") )
{
std::cout << "NormalizeDWIGradientVectors version: " << NORMALIZEDWIGRADIENTVECTORS_VERSION << std::endl ;
return 0 ;
}
if( argc != 3 )
{
std::cerr << argv[ 0 ] << " inputDWI outputDWI" << std::endl ;
std::cerr << argv[ 0 ] << " --version -> Prints version " << std::endl ;
return 1 ;
}
itk::ImageIOBase::IOPixelType pixelType ;
itk::ImageIOBase::IOComponentType componentType ;
GetImageType( argv[ 1 ] , pixelType , componentType ) ;
//templated over the input image voxel type
switch( componentType )
{
case itk::ImageIOBase::UCHAR:
return Normalize< unsigned char >( argv[ 1 ] , argv[ 2 ] ) ;
break ;
case itk::ImageIOBase::CHAR:
return Normalize< char >( argv[ 1 ] , argv[ 2 ] ) ;
break ;
case itk::ImageIOBase::USHORT:
return Normalize< unsigned short >( argv[ 1 ] , argv[ 2 ] ) ;
break ;
case itk::ImageIOBase::SHORT:
return Normalize< short >( argv[ 1 ] , argv[ 2 ] ) ;
break ;
case itk::ImageIOBase::UINT:
return Normalize< unsigned int >( argv[ 1 ] , argv[ 2 ] ) ;
break ;
case itk::ImageIOBase::INT:
return Normalize< int >( argv[ 1 ] , argv[ 2 ] ) ;
break ;
case itk::ImageIOBase::ULONG:
return Normalize< unsigned long >( argv[ 1 ] , argv[ 2 ] ) ;
break ;
case itk::ImageIOBase::LONG:
return Normalize< long >( argv[ 1 ] , argv[ 2 ] ) ;
break ;
case itk::ImageIOBase::FLOAT:
return Normalize< float >( argv[ 1 ] , argv[ 2 ] ) ;
break ;
case itk::ImageIOBase::DOUBLE:
return Normalize< double >( argv[ 1 ] , argv[ 2 ] ) ;
break ;
case itk::ImageIOBase::UNKNOWNCOMPONENTTYPE:
default:
std::cerr << "unknown component type" << std::endl ;
break ;
}
return 1 ;
}