forked from BRAINSia/BRAINSTools
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDWIConverter.h
279 lines (229 loc) · 8.76 KB
/
DWIConverter.h
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
/*=========================================================================
*
* Copyright SINAPSE: Scalable Informatics for Neuroscience, Processing and Software Engineering
* The University of Iowa
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0.txt
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*=========================================================================*/
#ifndef __DWIConverter_h
#define __DWIConverter_h
#include <vector>
#include <algorithm>
#include <iostream>
#include <string>
#include <sstream>
#include <cctype>
#include "itkMacro.h"
#include "itkIntTypes.h"
#include "itkDCMTKSeriesFileNames.h"
#include "itkDCMTKImageIO.h"
#include "itkRawImageIO.h"
#include "itkImage.h"
#include "itkImageRegionConstIterator.h"
#include "itkImageRegionIterator.h"
#include "itkImageRegionIteratorWithIndex.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itksys/Directory.hxx"
#include "itksys/SystemTools.hxx"
#include "itksys/Base64.h"
#include "itkDCMTKFileReader.h"
#include "itkMatrix.h"
#include "itkImageFileReader.h"
#include "itkImageFileWriter.h"
#include "itkImageSeriesReader.h"
#include "itkRawImageIO.h"
#include "itkImage.h"
#include "itkDCMTKFileReader.h"
#include "itkDCMTKImageIO.h"
#include "itkNumberToString.h"
#include "dcmtk/oflog/helpers/loglog.h"
#include "DWIConvertUtils.h"
#include "DWIMetaDataDictionaryValidator.h"
#include "StringContains.h"
/** the DWIConverter is a base class for all scanner-specific
* converters. It handles the tasks that are required for all
* scanners. In particular it loads the DICOM directory, and fills
* out various data fields needed by the DWIConvert program in order
* to write out NRRD and other files.
*/
class DWIConverter
{
public:
/* The internal format is an unwrapped 3D scalar image that is x,y,slices
* where slices is all the slices in both 3D and 4d directions.
* If each volume is 3DSlices, and their are NumGradients, then
* the last direction of the unwrapped direction is (3DSlices*NumGradients).
*/
using Volume3DUnwrappedType = itk::Image< PixelValueType, 3 >;
using SpacingType = Volume3DUnwrappedType::SpacingType;
using ReaderType = itk::ImageSeriesReader< Volume3DUnwrappedType >;
using FileNamesContainer = ReaderType::FileNamesContainer;
using VectorVolumeType = itk::VectorImage< PixelValueType, 3 >;
using SingleFileReaderType = itk::ImageFileReader< Volume3DUnwrappedType >;
using RotationMatrixType = itk::Matrix< double, 3, 3 >;
using PointType = itk::Vector< double, 3 >;
using CommonDicomFieldMapType = std::map< std::string, std::string >;
DWIConverter( const FileNamesContainer & inputFileNames );
virtual ~DWIConverter();
virtual void
LoadFromDisk() = 0;
RotationMatrixType
GetSpacingMatrix() const;
/** extract dwi data -- vendor specific so must happen in subclass
* implementing this method.
*/
virtual void
ExtractDWIData() = 0;
virtual CommonDicomFieldMapType
GetCommonDicomFieldsMap() const = 0;
/** access methods for image data */
const DWIMetaDataDictionaryValidator::GradientTableType &
GetDiffusionVectors() const;
/**
* @brief NRRD file format stores a single BValue, and sets all the BVectors to scaled version that represents
* the magnitude of the BValue offset.
*/
void
ConvertToSingleBValueScaledDiffusionVectors();
/**
* @brief FSL Format requires unit gradient directions and separate
* BValues for each gradient direction
*/
void
ConvertToMutipleBValuesUnitScaledBVectors();
/**
* @brief FSL orientation to allow for convenient display of images and
* conformance with conventions used by dcm2niix & fslview
* @param toFSL prefers FSL's internal data format layout, if false, prefer Dicom natural data layout
* FSL [1 0 0; 0 -1 0; 0 0 1] Dicom [1 0 0; 0 1 0; 0 0 1]
* @return Returns a 4D image pointer properly formatted
*/
Volume4DType::Pointer
OrientForFSLConventions( const bool toFSL = true );
const std::vector< double > &
GetBValues() const;
void
SetBValues( const std::vector< double > & inBValues );
double
GetMaxBValue() const;
Volume3DUnwrappedType::Pointer
GetDiffusionVolume() const;
SpacingType
GetSpacing() const;
double
GetThickness() const;
void
SetThicknessFromSpacing();
Volume3DUnwrappedType::PointType
GetOrigin() const;
void
SetOrigin( DWIConverter::Volume3DUnwrappedType::PointType origin );
RotationMatrixType
GetLPSDirCos() const;
RotationMatrixType
GetMeasurementFrame() const;
RotationMatrixType
GetNRRDSpaceDirection() const;
unsigned int
GetSlicesPerVolume() const;
unsigned int
GetNVolume() const;
std::string
GetNRRDSpaceDefinition() const;
unsigned short
GetRows() const;
unsigned short
GetCols() const;
/**
* @brief Force overwriting the gradient directions by inserting values read from specified file
* @param gradientVectorFile The file with gradients specified for overwriting
*/
void
ReadGradientInformation( const std::string & inputBValues, const std::string & inputBVectors,
const std::string & inputVolumeNameTemplate );
/**
* @brief ConvertBVectorsToIdentityMeasurementFrame, Convert the values of the gradients to
* use an identity measurement frame. This is required by FSL outputs.
*/
void
ConvertBVectorsToIdentityMeasurementFrame();
std::string
MakeFileComment( const std::string & version, bool useBMatrixGradientDirections, bool useIdentityMeaseurementFrame,
double smallGradientThreshold, const std::string conversionMode ) const;
void
ManualWriteNRRDFile( const std::string & outputVolumeHeaderName, const std::string commentstring ) const;
Volume4DType::Pointer
ThreeDToFourDImage( Volume3DUnwrappedType::Pointer img ) const;
Volume3DUnwrappedType::Pointer
FourDToThreeDImage( Volume4DType::Pointer img4D ) const;
/** the DICOM datasets are read as 3D volumes, but they need to be
* written as 4D volumes for image types other than NRRD.
*/
void
WriteFSLFormattedFileSet( const std::string & outputVolumeHeaderName, const std::string outputBValues,
const std::string outputBVectors, Volume4DType::Pointer img4D ) const;
/**
* @brief Choose if we are going to allow for lossy conversion by typecasting
* to the only internally supported format of short int
* @param allowLossyConvertsion (true = automatically convert to short int)
*/
void
SetAllowLossyConversion( const bool newValue );
// add by Hui Xie
Volume3DUnwrappedType::Pointer
getVolumePointer();
double
readThicknessFromDict();
protected:
double
ComputeMaxBvalue( const std::vector< double > & bValues ) const;
size_t
has_valid_nifti_extension( std::string outputVolumeHeaderName ) const;
/** add vendor-specific flags; */
virtual void
AddFlagsToDictionary() = 0;
/** the names of all the filenames, needed to use
* itk::ImageSeriesReader
*/
const FileNamesContainer m_InputFileNames;
bool m_allowLossyConversion; // Allow type-cast conversion from float to short storage format
/** double conversion instance, for optimal printing of numbers as text */
itk::NumberToString< double > m_DoubleConvert;
unsigned int m_SlicesPerVolume;
/** number of total slices */
unsigned int m_NSlice;
/** number of gradient volumes */
unsigned int m_NVolume;
double m_thickness;
/* The following variables make up the primary data model for diffusion weighted images
* in the most generic sense. These variables all need to be manipulated together in
* order to maintain a consistent data model.
*/
/** the image read from the DICOM dataset */
Volume3DUnwrappedType::Pointer m_Volume;
// this is always "left-posterior-superior" in all cases that we currently support
const std::string m_NRRDSpaceDefinition;
/** measurement from for gradients if different than patient
* reference frame.
*/
RotationMatrixType m_MeasurementFrame;
/** list of B Values for each volume */
std::vector< double > m_BValues;
/** list of gradient vectors */
DWIMetaDataDictionaryValidator::GradientTableType m_DiffusionVectors;
// A map of common dicom fields to be propagated to image
std::map< std::string, std::string > m_CommonDicomFieldsMap;
};
#endif // __DWIConverter_h