-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathimtool3D_nii_crop.m
82 lines (69 loc) · 2.92 KB
/
imtool3D_nii_crop.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
function imtool3D_nii_crop(filename)
% imtool3D_nii_crop crop a nifti image
% imtool3D_nii_crop(filename) opens a .nii(.gz) file in a imtool3D_3plane
% instance with rectangles in each plane. User can manipulate the
% rectangles. When set, user close the figure and a croped .nii file
% is created with the suffix _crop.
% It requires the dicm2nii package https://github.com/xiangruili/dicm2nii
if ~isdeployed
if ~exist('filename','var'), help imtool3D_nii_crop; return; end
if ~exist(filename,'file'), error(['can''t find nifti file ' filename]); end
A = which('nii_tool');
if isempty(A)
error('Dependency to Xiangrui Li NIFTI tools is missing. http://www.mathworks.com/matlabcentral/fileexchange/42997');
end
end
% open image in imtool3D
nii = nii_tool('load',filename);
tool = imtool3D_nii_3planes(nii);
% add synchronized rectangles
RECTS = imtool3D_3planes_rect(tool.getTool);
% wait for user to close figure
waitfor(tool.getHandles.fig);
% get Rectrangles positions and crop
pos = RECTS(1).getPosition();
cut_from_L = floor(pos(2)-pos(4)/2);
cut_from_R = floor(pos(2)+pos(4)/2);
cut_from_P = floor(pos(1)-pos(3)/2);
cut_from_A = floor(pos(1)+pos(3)/2);
pos = RECTS(2).getPosition();
cut_from_I = floor(pos(1)-pos(3)/2);
cut_from_S = floor(pos(1)+pos(3)/2);
nii.img = nii.img( max(1,cut_from_L+1) : min(end,cut_from_R), ...
max(1,cut_from_P+1) : min(end,cut_from_A), ...
max(1,cut_from_I+1) : min(end,cut_from_S), ...
:,:,:,:,:);
% adapt the quaternion
b = nii.hdr.quatern_b;
c = nii.hdr.quatern_c;
d = nii.hdr.quatern_d;
if 1.0-(b*b+c*c+d*d) < 0
if abs(1.0-(b*b+c*c+d*d)) < 1e-5
a = 0;
else
error('Incorrect quaternion values in this NIFTI data.');
end
else
a = sqrt(1.0-(b*b+c*c+d*d));
end
R = [a*a+b*b-c*c-d*d 2*b*c-2*a*d 2*b*d+2*a*c
2*b*c+2*a*d a*a+c*c-b*b-d*d 2*c*d-2*a*b
2*b*d-2*a*c 2*c*d+2*a*b a*a+d*d-c*c-b*b];
qmod = R*[cut_from_L*nii.hdr.pixdim(2);cut_from_P*nii.hdr.pixdim(3);cut_from_I*nii.hdr.pixdim(4)*nii.hdr.pixdim(1)];
nii.hdr.qoffset_x = nii.hdr.qoffset_x + qmod(1);
nii.hdr.qoffset_y = nii.hdr.qoffset_y + qmod(2);
nii.hdr.qoffset_z = nii.hdr.qoffset_z + qmod(3);
nii.hdr.srow_x(4) = nii.hdr.srow_x(4) + ...
nii.hdr.srow_x(1)*cut_from_L + ...
nii.hdr.srow_x(2)*cut_from_P + ...
nii.hdr.srow_x(3)*cut_from_I;
nii.hdr.srow_y(4) = nii.hdr.srow_y(4) + ...
nii.hdr.srow_y(1)*cut_from_L + ...
nii.hdr.srow_y(2)*cut_from_P + ...
nii.hdr.srow_y(3)*cut_from_I;
nii.hdr.srow_z(4) = nii.hdr.srow_z(4) + ...
nii.hdr.srow_z(1)*cut_from_L + ...
nii.hdr.srow_z(2)*cut_from_P + ...
nii.hdr.srow_z(3)*cut_from_I;
% Save into NIFTI file with suffix _crop
nii_tool('save',nii,[strrep(strrep(filename,'.nii.gz',''),'.nii','') '_crop.nii'])