-
Notifications
You must be signed in to change notification settings - Fork 177
/
mapper.cpp
242 lines (190 loc) · 9.02 KB
/
mapper.cpp
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
/* Copyright (c) 2008-2017 the MRtrix3 contributors.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, you can obtain one at http://mozilla.org/MPL/2.0/.
*
* MRtrix is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty
* of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
*
* For more details, see http://www.mrtrix.org/.
*/
#include "header.h"
#include "phase_encoding.h"
#include "image_io/default.h"
#include "image_io/mosaic.h"
#include "file/dicom/mapper.h"
#include "file/dicom/image.h"
#include "file/dicom/series.h"
#include "file/dicom/study.h"
#include "file/dicom/patient.h"
#include "file/dicom/tree.h"
namespace MR {
namespace File {
namespace Dicom {
std::unique_ptr<MR::ImageIO::Base> dicom_to_mapper (MR::Header& H, vector<std::shared_ptr<Series>>& series)
{
assert (series.size() > 0);
std::unique_ptr<MR::ImageIO::Base> io_handler;
Patient* patient (series[0]->study->patient);
std::string sbuf = ( patient->name.size() ? patient->name : "unnamed" );
sbuf += " " + format_ID (patient->ID);
if (series[0]->modality.size())
sbuf += std::string (" [") + series[0]->modality + "]";
if (series[0]->name.size())
sbuf += std::string (" ") + series[0]->name;
add_line (H.keyval()["comments"], sbuf);
H.name() = sbuf;
// build up sorted list of frames:
vector<Frame*> frames;
// loop over series list:
for (const auto series_it : series) {
try {
series_it->read();
}
catch (Exception& E) {
E.display();
throw Exception ("error reading series " + str (series_it->number) + " of DICOM image \"" + H.name() + "\"");
}
std::sort (series_it->begin(), series_it->end(), compare_ptr_contents());
// loop over images in each series:
for (auto image_it : *series_it) {
if (!image_it->transfer_syntax_supported) {
Exception E ("unsupported transfer syntax found in DICOM data");
E.push_back ("consider using third-party tools to convert your data to standard uncompressed encoding");
E.push_back ("See the MRtrix3 documentation on DICOM handling for details:");
E.push_back (" http://mrtrix.readthedocs.io/en/latest/tips_and_tricks/dicom_handling.html#error-unsupported-transfer-syntax");
throw E;
}
// if multi-frame, loop over frames in image:
if (image_it->frames.size()) {
std::sort (image_it->frames.begin(), image_it->frames.end(), compare_ptr_contents());
for (auto frame_it : image_it->frames)
if (frame_it->image_type == series_it->image_type)
frames.push_back (frame_it.get());
}
// otherwise add image frame:
else
frames.push_back (image_it.get());
}
}
auto dim = Frame::count (frames);
if (dim[0]*dim[1]*dim[2] < frames.size())
throw Exception ("dimensions mismatch in DICOM series");
if (dim[0]*dim[1]*dim[2] > frames.size())
throw Exception ("missing image frames for DICOM image \"" + H.name() + "\"");
if (dim[0] > 1) { // switch axes so slice dim is inner-most:
vector<Frame*> list (frames);
vector<Frame*>::iterator it = frames.begin();
for (size_t k = 0; k < dim[2]; ++k)
for (size_t i = 0; i < dim[0]; ++i)
for (size_t j = 0; j < dim[1]; ++j)
*(it++) = list[i+dim[0]*(j+dim[1]*k)];
}
default_type slice_separation = Frame::get_slice_separation (frames, dim[1]);
if (series[0]->study->name.size())
add_line (H.keyval()["comments"], std::string ("study: " + series[0]->study->name + " [ " + series[0]->image_type + " ]"));
if (patient->DOB.size())
add_line (H.keyval()["comments"], std::string ("DOB: " + format_date (patient->DOB)));
if (series[0]->date.size()) {
sbuf = "DOS: " + format_date (series[0]->date);
if (series[0]->time.size())
sbuf += " " + format_time (series[0]->time);
add_line (H.keyval()["comments"], sbuf);
}
const Image& image (*(*series[0])[0]);
const Frame& frame (*frames[0]);
if (std::isfinite (frame.echo_time))
H.keyval()["EchoTime"] = str (0.001 * frame.echo_time, 6);
size_t nchannels = frames.size() ? 1 : frame.data_size / (frame.dim[0] * frame.dim[1] * (frame.bits_alloc/8));
if (nchannels > 1)
INFO ("data segment is larger than expected from image dimensions - interpreting as multi-channel data");
H.ndim() = 3 + (dim[0]*dim[2]>1) + (nchannels>1);
size_t current_axis = 0;
if (nchannels > 1) {
H.stride(3) = 1;
H.size(3) = nchannels;
++current_axis;
}
H.stride(0) = ++current_axis;
H.size(0) = frame.dim[0];
H.spacing(0) = frame.pixel_size[0];
H.stride(1) = ++current_axis;
H.size(1) = frame.dim[1];
H.spacing(1) = frame.pixel_size[1];
H.stride(2) = ++current_axis;
H.size(2) = dim[1];
H.spacing(2) = slice_separation;
if (dim[0]*dim[2] > 1) {
H.stride(current_axis) = current_axis+1;
H.size(current_axis) = dim[0]*dim[2];
++current_axis;
}
if (frame.bits_alloc == 8)
H.datatype() = DataType::UInt8;
else if (frame.bits_alloc == 16) {
H.datatype() = DataType::UInt16;
if (image.is_BE)
H.datatype() = DataType::UInt16 | DataType::BigEndian;
else
H.datatype() = DataType::UInt16 | DataType::LittleEndian;
}
else throw Exception ("unexpected number of allocated bits per pixel (" + str (frame.bits_alloc)
+ ") in file \"" + H.name() + "\"");
H.set_intensity_scaling (frame.scale_slope, frame.scale_intercept);
// If multi-frame, take the transform information from the sorted frames; the first entry in the
// vector should be the first slice of the first volume
{
transform_type M;
M(0,0) = -frame.orientation_x[0];
M(1,0) = -frame.orientation_x[1];
M(2,0) = +frame.orientation_x[2];
M(0,1) = -frame.orientation_y[0];
M(1,1) = -frame.orientation_y[1];
M(2,1) = +frame.orientation_y[2];
M(0,2) = -frame.orientation_z[0];
M(1,2) = -frame.orientation_z[1];
M(2,2) = +frame.orientation_z[2];
M(0,3) = -frame.position_vector[0];
M(1,3) = -frame.position_vector[1];
M(2,3) = +frame.position_vector[2];
H.transform() = M;
std::string dw_scheme = Frame::get_DW_scheme (frames, dim[1], M);
if (dw_scheme.size())
H.keyval()["dw_scheme"] = dw_scheme;
}
PhaseEncoding::set_scheme (H, Frame::get_PE_scheme (frames, dim[1]));
for (size_t n = 1; n < frames.size(); ++n) // check consistency of data scaling:
if (frames[n]->scale_intercept != frames[n-1]->scale_intercept ||
frames[n]->scale_slope != frames[n-1]->scale_slope)
throw Exception ("unable to load series due to inconsistent data scaling between DICOM images");
if (image.images_in_mosaic) {
INFO ("DICOM image \"" + H.name() + "\" is in mosaic format");
if (H.size (2) != 1)
throw Exception ("DICOM mosaic contains multiple slices in image \"" + H.name() + "\"");
H.size(0) = frame.acq_dim[0];
H.size(1) = frame.acq_dim[1];
H.size(2) = image.images_in_mosaic;
if (frame.dim[0] % frame.acq_dim[0] || frame.dim[1] % frame.acq_dim[1]) {
WARN ("acquisition matrix [ " + str (frame.acq_dim[0]) + " " + str (frame.acq_dim[1])
+ " ] does not fit into DICOM mosaic [ " + str (frame.dim[0]) + " " + str (frame.dim[1])
+ " ] - adjusting matrix size to suit");
H.size(0) = frame.dim[0] / size_t (float(frame.dim[0]) / float(frame.acq_dim[0]));
H.size(1) = frame.dim[1] / size_t (float(frame.dim[1]) / float(frame.acq_dim[1]));
}
float xinc = H.spacing(0) * (frame.dim[0] - H.size(0)) / 2.0;
float yinc = H.spacing(1) * (frame.dim[1] - H.size(1)) / 2.0;
for (size_t i = 0; i < 3; i++)
H.transform()(i,3) += xinc * H.transform()(i,0) + yinc * H.transform()(i,1);
io_handler.reset (new MR::ImageIO::Mosaic (H, frame.dim[0], frame.dim[1], H.size (0), H.size (1), H.size (2)));
}
else
io_handler.reset (new MR::ImageIO::Default (H));
for (size_t n = 0; n < frames.size(); ++n)
io_handler->files.push_back (File::Entry (frames[n]->filename, frames[n]->data));
return io_handler;
}
}
}
}