-
Notifications
You must be signed in to change notification settings - Fork 5
/
rri_orient.m
95 lines (73 loc) · 2.03 KB
/
rri_orient.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
% Convert image of different orientations to standard Analyze orientation
%
% Usage: nii = rri_orient(nii);
% Jimmy Shen (jimmy@rotman-baycrest.on.ca), 26-APR-04
%___________________________________________________________________
function [nii, orient, pattern] = rri_orient(nii, varargin)
if nargin > 1
pattern = varargin{1};
else
pattern = [];
end
orient = [1 2 3];
dim = double(nii.hdr.dime.dim([2:4]));
if ~isempty(pattern) & ~isequal(length(pattern), prod(dim))
return;
end
% get orient of the current image
%
orient = rri_orient_ui;
pause(.1);
% no need for conversion
%
if isequal(orient, [1 2 3])
return;
end
if isempty(pattern)
pattern = 1:prod(dim);
end
pattern = reshape(pattern, dim);
img = nii.img;
% calculate after flip orient
%
rot_orient = mod(orient + 2, 3) + 1;
% do flip:
%
flip_orient = orient - rot_orient;
for i = 1:3
if flip_orient(i)
pattern = flipdim(pattern, i);
img = flipdim(img, i);
end
end
% get index of orient (do inverse)
%
[tmp rot_orient] = sort(rot_orient);
% do rotation:
%
pattern = permute(pattern, rot_orient);
img = permute(img, [rot_orient 4 5 6]);
% rotate resolution, or 'dim'
%
new_dim = nii.hdr.dime.dim([2:4]);
new_dim = new_dim(rot_orient);
nii.hdr.dime.dim([2:4]) = new_dim;
% rotate voxel_size, or 'pixdim'
%
tmp = nii.hdr.dime.pixdim([2:4]);
tmp = tmp(rot_orient);
nii.hdr.dime.pixdim([2:4]) = tmp;
% re-calculate originator
%
tmp = nii.hdr.hist.originator([1:3]);
tmp = tmp(rot_orient);
flip_orient = flip_orient(rot_orient);
for i = 1:3
if flip_orient(i) & ~isequal(double(tmp(i)), 0)
tmp(i) = int16(double(new_dim(i)) - double(tmp(i)) + 1);
end
end
nii.hdr.hist.originator([1:3]) = tmp;
nii.img = img;
pattern = pattern(:);
return; % rri_orient