-
Notifications
You must be signed in to change notification settings - Fork 0
/
annotate3d.m
164 lines (146 loc) · 6.24 KB
/
annotate3d.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
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
SMALLDATA = true;
APPEND = true;
avoidEdgeDistance = 10;
minSlice = 1;
maxSlice = 150;
halfEdgeSize = 0;
filterRadius = 2;
maxIter = 3;
maxDistance = 100;
noMarks = 3;
filenamePattern = '*v7.3_double.mat';
annotationsPrefix = fullfile('.'); % Annotation file prefix (input)
if SMALLDATA
scaleFactor = 1; % scaling factor used in the analysis fase w.r.t. annotation file
analysisPrefix = fullfile('smallData'); % Analysis files prefix (input)
else
scaleFactor = 2; % scaling factor used in the analysis fase w.r.t. annotation file
analysisPrefix = fullfile('halfSizeData'); % Analysis files prefix (input)
end
% filenames= {'5.05_ID1662_769_v7.3_double', ...
% '5.05_ID1662_770_v7.3_double', ...
% '5.05_ID1662_771_v7.3_double', ...
% '5.05_ID1662_772_v7.3_double', ...
% '5.05_ID1662_773_v7.3_double', ...
% '5.05_ID1684_806_v7.3_double', ...
% '5.05_ID1684_809_v7.3_double', ...
% '5.05_ID1689_805_v7.3_double', ...
% '5.05_ID1689_807_v7.3_double', ...
% '5.05_ID1689_808_v7.3_double'};
%filenames = {'5.05_ID1689_805_pag_v7.3_double.mat', '5.05_ID1662_773_pag_v7.3_double.mat', '5.05_ID1689_807_pag_v7.3_double.mat', ...
%'5.05_ID1662_771_pag_v7.3_double.mat', '5.05_ID1684_809_pag_v7.3_double.mat', '5.05_ID1689_808_pag_v7.3_double.mat', '5.05_ID1662_769_pag_v7.3_double.mat', ...
%'5.05_ID1662_770_pag_v7.3_double.mat', '5.05_ID1662_772_pag_v7.3_double.mat', '5.05_ID1684_806_pag_v7.3_double.mat'};
tmp = dir(fullfile(analysisPrefix,filenamePattern));
filenames = {tmp(:).name};
figure(1);
if APPEND
load(fullfile(annotationsPrefix,'annotations.mat'), 'p');
else
p = [];
end
skipAll = false;
for i = 1:length(filenames)
clf;
fprintf('%d/%d: %s\n', i,length(filenames),filenames{i});
nameID = regexpi(filenames{i}, 'ID.+?(?=_v[\d.]+)', 'match');
nameID = nameID{1};
if(isfield(p,nameID))
if ~skipAll
choice = questdlg(sprintf('Annotations on "%s" already exists.',nameID), 'Question', 'Overwrite','Skip','Skip All','Skip');
skipAll = strcmp(choice,'Skip All');
if(~strcmp(choice,'Overwrite'))
fprintf(' Updating %s\n', nameID);
end
end
else
fprintf(' Creating %s\n', nameID);
choice = 'Overwrite';
end
if(strcmp(choice,'Overwrite'))
fprintf(' Reading\n');
inputFilename = fullfile(analysisPrefix,filenames{i});
load(inputFilename);
fprintf(' Annotating\n')
showSlice = round(size(newVol,3)/2);
imagesc(newVol(:,:,showSlice));
title(sprintf('Original image slice %d/%d', showSlice, size(newVol,3))); colormap(gray); axis image tight;
fprintf(' Select Implant\n');
title('Select Implant');
x = fliplr(ginput(1));
implantSamples = round([x,showSlice]);
fprintf(' Select Cavity\n');
title('Select Cavity');
x = fliplr(ginput(1));
cavitySamples = round([x,showSlice]);
fprintf(' We need at least 10 examples of bone spread out over the volume for 2. degree polynomial bias correction in 3D\n')
title('Select a bone samples near implant');
x = fliplr(ginput(1));
boneSamples = round([x,showSlice]);
title('Select 4 Bone samples near edges');
x = fliplr(ginput(4));
boneSamples = [boneSamples;round([x,showSlice*ones(4,1)])];
showSlice = round(size(newVol,1)/2);
imagesc(squeeze(newVol(showSlice,:,:))); axis image tight;
title('Select 4 Bone samples near edges');
x = fliplr(ginput(4));
boneSamples = [boneSamples;round([showSlice*ones(4,1),x])];
showSlice = round(size(newVol,2)/2);
imagesc(squeeze(newVol(:,showSlice,:))); axis image tight;
title('Select 4 Bone samples near edges');
x = fliplr(ginput(4));
boneSamples = [boneSamples;round([x(:,1),showSlice*ones(4,1),x(:,2)])];
fprintf(' Segmenting implant\n')
implantThreshold = (newVol(implantSamples(1), implantSamples(2), ...
implantSamples(3)) + newVol(boneSamples(1,1), ...
boneSamples(1,2), boneSamples(1,3)))/2;
implant = segmentImplant3d(newVol, implantThreshold);
fprintf(' Annotating implant marks\n')
xMax = round(size(implant)/2);
x1 = 0;
x2 = -(xMax(2)-1):xMax(2);
x3 = -(xMax(3)-1):xMax(3);
accepted = false;
[origo, R, D] = getAxes3d(implant);
while ~accepted
slice = squeeze(sample3d(newVol, origo, R, x1, x2, x3));
imagesc(slice); title('Saggital slice'); colormap(gray); axis image tight;
marks = zeros(noMarks, 3);
for j = 1:size(marks, 1)
title(sprintf('Select mark %d/%d', j, noMarks));
x = fliplr(ginput(1));
marks(j,:) = [showSlice, x];
end
v = marks(end,:)-marks(1,:);
v = v'/sqrt(sum(v.^2));
%disp(abs(dot(v,[0,0,1])));
if dot(v,[0,0,1]) > 0.9998 % 1 degree
accepted = true;
else
disp('Orientation corrected, repeat input');
v = R*v;
R(:,3) = v;
R(:,2) = R(:,2) - dot(R(:,2),R(:,3))*R(:,3);
R(:,2) = R(:,2)/sqrt(sum(R(:,2).^2));
R(:,1) = R(:,1) - dot(R(:,1),R(:,2))*R(:,2) - dot(R(:,1),R(:,3))*R(:,3);
R(:,1) = R(:,1)/sqrt(sum(R(:,1).^2));
end
end
p.(nameID).marks = marks;
p.(nameID).inputFilename = inputFilename;
p.(nameID).origo = origo;
p.(nameID).aBoneExample = boneSamples;
p.(nameID).aCavityExample = cavitySamples;
p.(nameID).anImplantExample = implantSamples;
p.(nameID).R = R;
p.(nameID).size = size(newVol);
p.(nameID).avoidEdgeDistance = avoidEdgeDistance;
p.(nameID).minSlice = minSlice;
p.(nameID).maxSlice = maxSlice;
p.(nameID).halfEdgeSize = halfEdgeSize;
p.(nameID).filterRadius = filterRadius;
p.(nameID).maxIter = maxIter;
p.(nameID).maxDistance = maxDistance;
p.(nameID).scaleFactor = scaleFactor;
save(fullfile(annotationsPrefix,'annotations.mat'), 'p');
end
end