-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathReadMHDFile.m
103 lines (91 loc) · 2.79 KB
/
ReadMHDFile.m
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
function [I, ijk2ras]=ReadMHDFile(filespec)
% reads an metaheader type file
% returns an image volume in ijk coords and coord transform matrix
% returns int16 volume
% if [M,N,P]=size(image), then
% i is columns of image 1...N
% j is rows of image 1...M
% k is plane of image 1..P
display( ['Running ', mfilename ]);
disp(sprintf('Reading %s',filespec));
hdr_fname = [filespec, '.mhd'];
vol_fname = [filespec, '.raw'];
if ~exist(hdr_fname, 'file') || ~exist(vol_fname, 'file')
error( 'could not locate file ! ');
return;
end
hdr_fid = fopen( hdr_fname, 'r');
ijk2ras = eye(4);
offset = zeros(4,4);
scale = eye(4);
dims = [0,0,0];
dtype = 'MET_FLOAT';
I = [];
rai2ras = eye(4);
% % rai2ras(1,1) = -1; %convert into lps
% % rai2ras(2,2) = -1;
rai2ras(3,3) = 1;
rai2ras_flag = 0;
tline = fgetl(hdr_fid);
while ischar(tline)
%disp(tline);
[word, r] = strtok(tline);
switch word
case 'NDims'
[n, r] = strtok(r);
if strtok(r) ~= '3'
error( 'incorrect number of dimesions');
return;
end
case 'TransformMatrix'
[n, r] = strtok(r);
for j = 1:3
for i = 1:3
[n, r] = strtok(r);
ijk2ras(i,j) = str2num(n);
end
end
case 'Offset'
[n, r] = strtok(r);
for i = 1:3
[n, r] = strtok(r);
offset(i,4) = str2num(n);
end
case 'AnatomicalOrientation'
[n, r] = strtok(r);
if strtok(r) == 'RAI'
%error( 'not RAI system - consider adding code for flipping');
rai2ras_flag = 1;
end
case 'ElementSpacing'
[n, r] = strtok(r);
for i = 1:3
[n, r] = strtok(r);
scale(i,i) = str2num(n);
end
case 'DimSize'
[n, r] = strtok(r);
for i = 1:3
[n, r] = strtok(r);
dims(i) = str2num(n);
end
case 'ElementType'
[n, r] = strtok(r);
dtype = strtok(r)
if strcmpi(strtok(r), 'MET_FLOAT')
disp('floating datatype');
end
otherwise
%disp(tline);
end
tline = fgetl(hdr_fid);
end
ijk2ras = rai2ras*(ijk2ras*scale+ offset);
vol_fid = fopen( vol_fname, 'r');
switch (dtype)
case 'MET_FLOAT'
vol_data = fread(vol_fid, dims(1)*dims(2)*dims(3), 'float32');
case 'MET_SHORT'
vol_data = fread(vol_fid, dims(1)*dims(2)*dims(3), 'short');
end
I = int16(reshape(vol_data, dims));