-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathOCT_GUI.m
352 lines (308 loc) · 15.2 KB
/
OCT_GUI.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
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
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
function OCT_GUI
% opens a GUI where the user can segment OCT images using dynamic
% programming algorithm
figure_dimensions = get(0, 'ScreenSize');
GUI.Fig = figure('position', figure_dimensions, 'menu', 'none');
background_color = get(GUI.Fig, 'color');
set(gcf, 'toolbar' , 'figure')
cameratoolbar('Show')
aspect_ratio = figure_dimensions(1)/figure_dimensions(2);
rect = [0.35 0.2*aspect_ratio];
% first axis for displaying cartesian view
GUI.Ax1 = axes('Parent', GUI.Fig, 'Units', 'normalized', 'position', ...
[0.05 0.1 2.25*rect(2) 2.25*rect(1)]);
GUI.Ax1.XTick=0;
GUI.Ax1.YTick=0;
% second axis for displaying polar view
GUI.Ax2 = axes('Parent', GUI.Fig, 'Units', 'normalized', 'position', ...
[0.6 0.5 1.7*rect(2) 1.25*rect(1)]);
GUI.Ax2.XTick=0;
GUI.Ax2.YTick=0;
% buttons for segmentation
uicontrol('Parent', GUI.Fig, 'style','pushbutton','units', 'normalized', ...
'Position',[0.6 0.325 0.1 0.05], 'String', 'Load pullback',...
'Callback',@load_images, 'fontsize', 10);
uicontrol('Parent', GUI.Fig, 'style','pushbutton','units', 'normalized', ...
'Position',[0.6 0.275 0.1 0.05], 'String', 'Segment guidewire',...
'Callback',@segment_gw, 'fontsize', 10);
uicontrol('Parent', GUI.Fig, 'style','pushbutton','units', 'normalized', ...
'Position',[0.6 0.225 0.1 0.05], 'String', 'Segment sheath',...
'Callback',@segment_sheath, 'fontsize', 10);
uicontrol('Parent', GUI.Fig, 'style','pushbutton','units', 'normalized', ...
'Position',[0.6 0.175 0.1 0.05], 'String', 'Segment lumen',...
'TooltipString', ['Segment all images with dynamic programming'], ...
'Callback',@segment_lumen, 'fontsize', 10);
uicontrol('Parent', GUI.Fig, 'style','pushbutton','units', 'normalized', ...
'Position',[0.6 0.125 0.1 0.05], 'String', 'Threshold lumen',...
'TooltipString', ['Segment current image with threshold'], ...
'Callback',@threshold_lumen, 'fontsize', 10);
uicontrol('Parent', GUI.Fig, 'style','pushbutton','units', 'normalized', ...
'Position',[0.85 0.325 0.1 0.05], 'String', 'Edit lumen',...
'Callback',@edit_seg_spline, 'fontsize', 10);
uicontrol('Parent', GUI.Fig, 'style','pushbutton','units', 'normalized', ...
'Position',[0.85 0.275 0.1 0.05], 'String', 'Copy lumen',...
'Callback',@copy_seg, 'fontsize', 10, 'ToolTipString', ['Copy lumen contour from previous image']);
uicontrol('Parent', GUI.Fig, 'style','pushbutton','units', 'normalized', ...
'Position',[0.85 0.225 0.1 0.05], 'String', 'Load lumen', ...
'Callback',@load_seg, 'fontsize', 10, 'ToolTipString', ['Load saved lumen contour']);
uicontrol('Parent', GUI.Fig, 'style','pushbutton','units', 'normalized', ...
'Position',[0.85 0.175 0.1 0.05], 'String', 'Save', ...
'Callback',@save_seg, 'fontsize', 10);
% initialize first image
GUI.OCT_image = 1;
GUI.Txt = uicontrol('Style', 'text', 'Units', 'Normalized', 'FontSize', 14, 'FontWeight', 'bold', ...
'Position', [0.05 0.9 0.125 0.025], 'String',strcat('Current Frame :', num2str(GUI.OCT_image)));
% edit and text boxes for guidewire thresholds
img_cath_threshold = 0.2;
gw_threshold = 0.6;
GUI.s2 = uicontrol('Parent', GUI.Fig, 'style','edit','units', 'normalized', ...
'Position',[0.725 0.325 0.025 0.05], 'String', string(img_cath_threshold), ...
'ToolTipString', ['Enter a value for thresholding the catheter, use a greater value if more than the catheter is removed']);
GUI.s3 = uicontrol('Parent', GUI.Fig, 'style','edit','units', 'normalized', ...
'Position',[0.775 0.325 0.025 0.05], 'String', string(gw_threshold), ...
'ToolTipString', ['Enter a value for thresholding the guidewire']);
GUI.Txt1 = uicontrol('Style', 'text', 'Units', 'Normalized', 'FontSize', 8, 'Position', [0.72 0.375 0.035 0.025], 'String','Catheter');
GUI.Txt2 = uicontrol('Style', 'text', 'Units', 'Normalized', 'FontSize', 8, 'Position', [0.77 0.375 0.035 0.025], 'String','Guidewire');
% checkbox for including stent struts in segmentation
GUI.s4 = uicontrol('Parent', GUI.Fig, 'style','checkbox','units', 'normalized', ...
'Position',[0.725 0.125 0.1 0.05], 'String', 'Include struts', 'fontsize', 10, ...
'ToolTipString', ['Check this to include the stent struts inside the lumen mask']);
GUI.s5 = uicontrol('Parent', GUI.Fig, 'style','checkbox','units', 'normalized', ...
'Position',[0.80 0.125 0.1 0.05], 'String', 'Threshold all', 'fontsize', 10, ...
'ToolTipString', ['Check this to threshold all images, otherwise just the current image will be processed']);
OCT_polar = [];
OCT_cart = [];
OCT_gw = [];
OCT_sheath = [];
output_size = 1024;
OCT_lumen_contour = [];
OCT_contour_cart = {};
OCT_spline = [];
function slide_images(hObj, eventdata)
GUI.OCT_image = round(get(hObj, 'value'));
update_display()
end
function update_display()
cla(GUI.Ax1)
cla(GUI.Ax2)
imshow(OCT_cart(:,:,GUI.OCT_image), [0 250], 'Parent', GUI.Ax1)
imshow(OCT_gw(:,:,GUI.OCT_image), [0 250], 'Parent', GUI.Ax2)
set(GUI.Txt, 'string', strcat('Current Frame :', num2str(GUI.OCT_image)));
plot(OCT_sheath(:,GUI.OCT_image), 1:length(OCT_gw(:, 1, 1)), 'Parent', GUI.Ax2)
if ~isempty(OCT_contour_cart{GUI.OCT_image})
plot(OCT_contour_cart{GUI.OCT_image}(:, 1), OCT_contour_cart{GUI.OCT_image}(:, 2), 'Parent', GUI.Ax1)
plot(OCT_lumen_contour(:, GUI.OCT_image), 1:length(OCT_gw(:, 1, 1)), 'Parent', GUI.Ax2)
end
end
function load_images(hObj, eventdata)
% load image files in dcm or raw format
format = questdlg('Choose format', 'Select format of OCT files', 'raw (.oct)', 'dicom', 'raw (.oct)');
output_size = 1024;
if strcmp(format, 'dicom')
[OCT_polar OCT_cart] = OCT_cart2pol();
GUI.s2.String = string(0.5);
GUI.s3.String = string(0.8);
elseif strcmp(format, 'raw (.oct)')
[OCT_polar OCT_cart] = OCT_raw2cart(output_size);
GUI.s2.String = string(0.1);
GUI.s3.String = string(0.25);
end
OCT_gw = OCT_polar;
OCT_contour_cart = cell(length(OCT_cart(1,1,:)), 1);
OCT_sheath = zeros(length(OCT_polar(:,1,1)), length(OCT_polar(1,1,:)));
create_slider()
end
function create_slider()
% creates slider
GUI.s1 = uicontrol('Parent', GUI.Fig, 'Style', 'slider', 'units', 'normalized',...
'Min', 1, 'Max',length(OCT_cart(1,1,:)),'Value',1,'Position', [0.2 0.9 0.3 0.05],...
'sliderstep', [1/length(OCT_cart(1,1,:)) 10/length(OCT_cart(1,1,:))], 'Callback', @slide_images);
imshow(OCT_cart(:,:,GUI.OCT_image), [0 250], 'Parent', GUI.Ax1)
imshow(OCT_gw(:,:,GUI.OCT_image), [0 250], 'Parent', GUI.Ax2)
hold(GUI.Ax1, 'on')
hold(GUI.Ax2, 'on')
end
function segment_gw(hObj, eventdata)
% segments the guidewire
img_cath_threshold = str2num(get(GUI.s2, 'string'));
gw_threshold = str2num(get(GUI.s3, 'string'));
OCT_cath = OCT_img_cath(OCT_polar, img_cath_threshold);
OCT_gw = OCT_guidewire(OCT_cath, gw_threshold);
end
function segment_sheath(hObj, eventdata)
% segments sheath
OCT_sheath = OCT_sheath_segmentation(OCT_polar, output_size);
% plot sheath in polar image
plot(OCT_sheath(:,GUI.OCT_image), 1:length(OCT_polar(:, 1, 1)), 'Parent', GUI.Ax2)
end
function segment_lumen(hObj, eventdata)
% segments sheath
OCT_lumen_contour = OCT_lumen(OCT_gw, OCT_sheath);
% plot in polar image
plot(OCT_lumen_contour(:, GUI.OCT_image), 1:length(OCT_polar(:, 1, 1)), 'Parent', GUI.Ax2)
% convert to cartesian
OCT_lumen_contour1 = circshift(OCT_lumen_contour, length(OCT_lumen_contour(:,1))/2);
OCT_contour_cart = contour2cart(OCT_lumen_contour1, OCT_polar, round(length(OCT_cart(:,1,1,1))/2)*2, 0);
% if the contour doesn't match the image try reverse x and y
% for i = 1:length(OCT_contour_cart)
% OCT_contour_cart{i} = [OCT_contour_cart{i}(:,2) OCT_contour_cart{i}(:, 1)];
% end
plot(OCT_contour_cart{GUI.OCT_image}(:, 1), OCT_contour_cart{GUI.OCT_image}(:, 2), 'Parent', GUI.Ax1)
end
function threshold_lumen(hObj, eventdata)
% thresholds the current image to produce lumen segmentation
include_struts = get(GUI.s4, 'value');
if get(GUI.s5, 'value') == 1
h = waitbar(0, 'processing images');
for i =1:length(OCT_gw(1,1,:))
waitbar(i/length(OCT_gw(1, 1, :)));
OCT_lumen_contour1 = OCT_threshold(OCT_gw(:, :, i), OCT_sheath(:, i), include_struts);
OCT_lumen_contour(:, i) = OCT_lumen_contour1;
OCT_lumen_contour1 = circshift(OCT_lumen_contour1, length(OCT_lumen_contour1(:,1))/2);
OCT_contour_cart(i) = contour2cart(OCT_lumen_contour1, OCT_polar, round(length(OCT_cart(:,1,1,1))/2)*2, 0);
end
close(h);
else
OCT_lumen_contour1 = OCT_threshold(OCT_gw(:, :, GUI.OCT_image), OCT_sheath(:, GUI.OCT_image), include_struts);
OCT_lumen_contour(:, GUI.OCT_image) = OCT_lumen_contour1;
% convert to cartesian
OCT_lumen_contour1 = circshift(OCT_lumen_contour1, length(OCT_lumen_contour1(:,1))/2);
OCT_contour_cart(GUI.OCT_image) = contour2cart(OCT_lumen_contour1, OCT_polar, round(length(OCT_cart(:,1,1,1))/2)*2, 0);
end
plot(OCT_contour_cart{GUI.OCT_image}(:, 1), OCT_contour_cart{GUI.OCT_image}(:, 2), 'Parent', GUI.Ax1)
plot(OCT_lumen_contour(:, GUI.OCT_image), 1:length(OCT_gw(:, 1, 1)), 'Parent', GUI.Ax2)
end
function edit_seg(hObj, eventdata)
% manually edit the segmentation
% user clicks once to select the point and a second time to place
% the point, the plot is then updated after the user presses return
% downsample contour
if length(OCT_contour_cart{GUI.OCT_image}(:, 1)) > 30
% for i = 1:length(OCT_contour_cart)
% determine how much undersampling is required
i=GUI.OCT_image;
under_sample = round(length(OCT_contour_cart{i}(:, 1))/30);
OCT_contour_cart2{i}(:, 1) = OCT_contour_cart{i}(1:under_sample:end, 1);
OCT_contour_cart2{i}(:, 2) = OCT_contour_cart{i}(1:under_sample:end, 2);
% end
end
lumen = [OCT_contour_cart2{GUI.OCT_image}(:,1), OCT_contour_cart2{GUI.OCT_image}(:,2)];
cla(GUI.Ax1)
imshow(OCT_cart(:,:,GUI.OCT_image), [0 250], 'Parent', GUI.Ax1)
plot(lumen(:, 1), lumen(:, 2), 'r.-', 'Parent', GUI.Ax1)
resume= 0;
while resume == 0
[x y] = ginput(2);
if ~isempty(x)
find_x = lumen(:, 1) - x(1);
find_y = lumen(:, 2) - y(1);
% the point will be the minimum added distance from the x and y
find_point = sum([abs(find_x) abs(find_y)], 2);
find_point = find(find_point == min(find_point));
lumen(find_point, :) = [x(2) y(2)];
cla(GUI.Ax1)
imshow(OCT_cart(:,:,GUI.OCT_image), [0 250], 'Parent', GUI.Ax1)
plot(lumen(:, 1), lumen(:, 2), 'r.-', 'Parent', GUI.Ax1)
else
% re-interpolate back to desired resolution
% contour_polar{1} = [lumen; lumen; lumen];
contour_polar{1} = [lumen];
contour_polar = contour2pol(contour_polar, OCT_polar, output_size);
% contour_polar = contour_polar(length(OCT_polar(:,1,1))+1:length(OCT_polar(:,1,1))*2);
% convert back to cartesian
OCT_lumen_contour1 = circshift(contour_polar, length(OCT_lumen_contour(:,1))/2);
contour_cart = contour2cart(OCT_lumen_contour1, OCT_polar, round(length(OCT_cart(:,1,1,1))/2)*2, 0);
OCT_contour_cart{GUI.OCT_image} = contour_cart{1};
OCT_lumen_contour(:, GUI.OCT_image) = contour_polar;
% OCT_contour_cart{GUI.OCT_image} = lumen;
resume = 1;
end
end
end
function edit_seg_spline(hObj, eventdata)
% manually edit the segmentation
% user drags sline points around
% downsample contour
if length(OCT_contour_cart{GUI.OCT_image}(:, 1)) > 30
% for i = 1:length(OCT_contour_cart)
% determine how much undersampling is required
i=GUI.OCT_image;
under_sample = round(length(OCT_contour_cart{i}(:, 1))/30);
OCT_contour_cart2{i}(:, 1) = OCT_contour_cart{i}(1:under_sample:end, 1);
OCT_contour_cart2{i}(:, 2) = OCT_contour_cart{i}(1:under_sample:end, 2);
% end
end
lumen = [OCT_contour_cart2{GUI.OCT_image}(:,1), OCT_contour_cart2{GUI.OCT_image}(:,2)];
cla(GUI.Ax1)
imshow(OCT_cart(:,:,GUI.OCT_image), [0 250], 'Parent', GUI.Ax1)
sp = plot(lumen(:, 1), lumen(:, 2), 'r-', 'Parent', GUI.Ax1);
OCT_spline = splineroi();
OCT_spline.parent = GUI.Ax1;
OCT_spline.hSpline = sp;
OCT_spline.addNode(lumen);
GUI.compute_button = uicontrol('Parent', GUI.Fig, 'style','pushbutton','units', 'normalized', ...
'Position',[0.6 0.075 0.1 0.05], 'String', 'Recompute',...
'Callback',@recompute_lumen, 'fontsize', 10);
end
function recompute_lumen(hObj, eventdata)
% stores the edited lumen contour
contour_polar{1} = OCT_spline.calcSpline';
contour_polar = contour2pol(contour_polar, OCT_polar, output_size);
OCT_lumen_contour1 = circshift(contour_polar, length(OCT_lumen_contour(:,1))/2);
contour_cart = contour2cart(OCT_lumen_contour1, OCT_polar, round(length(OCT_cart(:,1,1,1))/2)*2, 0);
OCT_contour_cart{GUI.OCT_image} = contour_cart{1};
OCT_lumen_contour(:, GUI.OCT_image) = contour_polar;
delete(GUI.compute_button);
delete(OCT_spline.hSpline);
delete(OCT_spline.hPoint);
OCT_spline = [];
update_display();
end
function load_seg(hObj, eventdata)
% load a previously saved contour segmentation
[fname pname] = uigetfile('*.mat', 'Load mat file containing saved contour');
data = load(strcat(pname, fname));
if isempty(OCT_cart) && ~isfield(data, 'OCT_cart')
errordlg('Please load OCT pullback first', 'Load Error')
elseif isempty(OCT_cart)
OCT_cart = data.OCT_cart;
end
if isempty(OCT_polar) && isfield(data, 'OCT_polar')
OCT_polar = double(data.OCT_polar);
if isempty(OCT_gw)
OCT_gw = OCT_polar;
end
end
if isfield(data, 'OCT_contour_cart')
OCT_contour_cart = data.OCT_contour_cart;
OCT_lumen_contour = contour2pol(OCT_contour_cart, OCT_polar, output_size);
end
if isfield(data, 'OCT_sheath')
OCT_sheath = data.OCT_sheath;
end
if ~isfield(GUI, 's1')
create_slider()
end
update_display();
end
function save_seg(hObj, eventdata)
% saves data
fname = inputdlg('Enter filename');
fname = strcat(fname, '_OCT_segmentation.mat');
OCT_polar = uint16(OCT_polar);
save(fname{1}, 'OCT_cart', 'OCT_polar', 'OCT_contour_cart', 'OCT_sheath', 'OCT_lumen_contour')
end
function copy_seg (hObj ,eventdata)
% copy previous contour to the next image to save editing time
OCT_contour_cart{GUI.OCT_image}=OCT_contour_cart{GUI.OCT_image - 1};
OCT_lumen_contour(:, GUI.OCT_image) = OCT_lumen_contour(:, GUI.OCT_image - 1);
cla(GUI.Ax1)
cla(GUI.Ax2)
imshow(OCT_cart(:,:,GUI.OCT_image), [0 250], 'Parent', GUI.Ax1)
imshow(OCT_gw(:,:,GUI.OCT_image), [0 250], 'Parent', GUI.Ax2)
set(GUI.Txt, 'string', strcat('Current Frame :', num2str(GUI.OCT_image)));
plot(OCT_sheath(:,GUI.OCT_image), 1:length(OCT_polar(:, 1, 1)), 'Parent', GUI.Ax2)
plot(OCT_contour_cart{GUI.OCT_image}(:, 1), OCT_contour_cart{GUI.OCT_image}(:, 2), 'Parent', GUI.Ax1)
plot(OCT_lumen_contour(:, GUI.OCT_image), 1:length(OCT_polar(:, 1, 1)), 'Parent', GUI.Ax2)
end
end