-
Notifications
You must be signed in to change notification settings - Fork 5
/
ChoroidApp.m
511 lines (457 loc) · 18 KB
/
ChoroidApp.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
classdef ChoroidApp < handle
% CHOROIDAPP
%
% Constructor:
% obj = ChoroidApp(im);
%
% Input:
% im Image or file name or OCT class
% If empty will prompt to choose image from file
%
% Properties:
% RPE RPE-Choroid boundary
% ILM ILM boundary
% Choroid Choroid upper boundary
% ChoroidParams Parameters to create parabola/Choroid
% ControlPoints Points to fit parabola
%
% History:
% 2Aug2018 - SSP - original ChoroidIdentification
% 18Dec2018 - SSP - new framework, no edge detection
% ---------------------------------------------------------------------
events
FitChoroid
SegmentedRetinalLayers
ClosedApp
end
properties (SetAccess = private)
RPE
ILM
ChoroidParams
ControlPoints
end
properties (SetObservable, AbortSet)
Choroid
end
properties
waitingForPoint = false;
end
properties (SetAccess = private)
originalImage
imageName
imagePath
figHandle
axHandle
imHandle
end
methods
function obj = ChoroidApp(im)
if nargin == 0
im = uigetfile({'*.png'; '*.tiff'; '*.jpeg'; '*.jpg'});
if ~ischar(im)
disp('No image selected!');
return
end
end
obj.parseInput(im);
obj.createUI();
ChoroidRatioView(obj);
end
function addControlPoints(obj, newPoints)
for i = 1:size(newPoints, 1)
obj.ControlPoints = cat(1, obj.ControlPoints, newPoints(i,:));
obj.plotLastControlPoint();
end
obj.ControlPoints = unique(obj.ControlPoints, 'rows');
set(findobj(obj.figHandle, 'Tag', 'FitChoroid'),...
'Enable', 'on');
end
end
% Analysis functions
methods (Access = private)
function detectChoroid(obj)
% DETECTCHOROID Run choroid detection fit
[~, ind] = sort(obj.ControlPoints);
obj.ControlPoints = obj.ControlPoints(ind(:, 1), :);
% Fit parabola to control points
[~, obj.ChoroidParams] = parabola_leastsquares(...
obj.ControlPoints(:, 1), obj.ControlPoints(:, 2), false);
% Evaluate over entire image x-axis
xpts = 1:size(obj.imHandle.CData, 2);
obj.Choroid = [xpts; parabola(xpts, obj.ChoroidParams)]';
% Update the user interface accordingly
set(findobj(obj.figHandle, 'Tag', 'ShowChoroid'),...
'Enable', 'on', 'Value', 1);
obj.statusUpdate(sprintf('Fit Choroid: %.2f, %.2f, %.4f',...
obj.ChoroidParams));
notify(obj, 'FitChoroid');
end
function detectRetina(obj)
% DETECTRETINA Run retinal layer segmentation
[obj.ILM, obj.RPE] = simpleSegmentation(obj.originalImage);
obj.plotRetina();
% Make sure show segmented retina checkbox is checked
set(findobj(obj.figHandle, 'Tag', 'ShowRetina'), 'Value', 1);
notify(obj, 'SegmentedRetinalLayers');
end
end
% Plotting functions
methods (Access = private)
function plotChoroid(obj)
% PLOTCHOROID Plots fitted chroroid
delete(findobj(obj.figHandle, 'Tag', 'Choroid'));
line(obj.axHandle, obj.Choroid(:, 1), obj.Choroid(:, 2),...
'Color', 'r', 'LineWidth', 0.5,...
'Tag', 'Choroid');
set(findobj(obj.figHandle, 'Tag', 'ShowChoroid'),...
'Value', 1);
end
function plotRetina(obj)
% PLOTRETINA Plots segmented RPE and ILM
delete(findobj(obj.figHandle, 'Tag', 'RPE'));
delete(findobj(obj.figHandle, 'Tag', 'ILM'));
% Plot new segmentation
line(obj.RPE(:, 1), obj.RPE(:, 2),...
'Parent', obj.axHandle,...
'LineWidth', 1.25, 'Color', 'b',...
'Tag', 'RPE');
line(obj.ILM(:, 1), obj.ILM(:, 2),...
'Parent', obj.axHandle,...
'LineWidth', 1.25, 'Color', 'b',...
'Tag', 'ILM');
end
function plotLastControlPoint(obj)
% PLOTCONTROLPOINT Add most recent control point to plot
line(obj.ControlPoints(end, 1), obj.ControlPoints(end, 2),...
'Parent', obj.axHandle,...
'Marker', '+', 'MarkerSize', 5,...
'Color', rgb('orange'),...
'Tag', 'CtrlPt');
end
end
% Callback functions
methods (Access = private)
function onShowRetinaSegmentation(obj, src, ~)
if src.Value == 1
action = 'on';
else
action = 'off';
end
obj.showByTag('RPE', action);
obj.showByTag('ILM', action);
end
function onSegmentRetina(obj, ~, ~)
obj.statusUpdate('Segmenting Image...');
obj.detectRetina();
obj.statusUpdate('');
end
function onAddCtrlPoint(obj, ~, ~)
obj.waitingForPoint = true;
obj.statusUpdate('Waiting for control point placement');
end
function onClearCtrlPoints(obj, ~, ~)
% ONCLEARCTRLPTS Delete control points, remove from plot
obj.ControlPoints = [];
delete(findall(obj.axHandle, 'Tag', 'CtrlPt'));
end
function onFitChoroid(obj, ~, ~)
if ~isempty(obj.ControlPoints)
obj.detectChoroid();
obj.plotChoroid();
end
end
function onUseHistogramApp(obj, ~, ~)
x = HistogramPeakSlider(obj.originalImage);
x.addParent(obj);
end
function onShowChoroid(obj, src, ~)
if src.Value == 1
action = 'on';
else
action = 'off';
end
obj.showByTag('Choroid', action);
end
function onChangeCMap(obj, src, ~)
if strcmp(get(src, 'Tag'), 'gray')
colormap(obj.figHandle, pink);
set(src, 'Tag', 'parula');
else
colormap(obj.figHandle, gray);
set(src, 'Tag', 'gray');
end
end
function onReloadImage(obj, ~, ~)
x = OCT(str2double(obj.imageName(3:end)), obj.imagePath);
obj.originalImage = x.octImage;
if ndims(obj.originalImage) > 3
obj.originalImage = rgb2gray(obj.originalImage);
end
set(obj.imHandle, 'CData', obj.originalImage);
end
function onExportFigure(obj, ~, ~)
% ONEXPORTFIGURE Export image with any visible segmentation
newAxes = exportFigure(obj.axHandle);
[fname, fpath] = uiputfile('*.png');
print(newAxes, [fpath, filesep, fname], '-dpng', '-r600');
end
function onSaveAll(obj, ~, ~)
h = findobj(obj.figHandle, 'Tag', 'OCTName');
if ~isempty(h.String)
octName = h.String;
else
return
end
jsonPath = [uigetdir(), filesep, octName, '.json'];
if ~exist(jsonPath, 'file')
S = struct('ChoroidParams', obj.ChoroidParams,...
'ControlPoints', obj.ControlPoints,...
'Choroid', obj.Choroid,...
'RPE', obj.RPE, 'ILM', obj.ILM);
else
S = loadjson(jsonPath);
S.ControlPoints = obj.ControlPoints;
S.ChoroidParams = obj.ChoroidParams;
S.RPE = obj.RPE; S.ILM = obj.ILM;
S.Choroid = obj.Choroid;
end
savejson('', S, jsonPath);
fprintf('Saved as: %s\n', jsonPath);
end
end
% Misc user interface helper functions
methods (Access = private)
function statusUpdate(obj, str)
% STATUSUPDATE Update status text
if nargin < 2
str = '';
else
assert(ischar(str), 'Status updates must be char');
end
set(findobj(obj.figHandle, 'Tag', 'Status'), 'String', str);
drawnow;
end
function showByTag(obj, tag, action)
% SHOWBYTAG Toggle visibility of plot object by tag
set(findobj(obj.figHandle, 'Tag', tag),...
'Visible', action);
end
function flipByTag(obj, tag)
% FLIPBYTAG Set visibility to opposite current setting
h = findobj(obj.figHandle, 'Tag', tag);
if ~isempty(h)
set(h, 'Visible', obj.onOff(get(h, 'Visible')));
end
end
end
% User interaction functions
methods (Access = private)
function onWindowButtonUp(obj, src, ~)
if obj.waitingForPoint
newPoint = round(src.CurrentAxes.CurrentPoint(1, 1:2));
obj.ControlPoints = cat(1, obj.ControlPoints, newPoint);
obj.plotLastControlPoint();
set(findobj(obj.figHandle, 'Tag', 'FitChoroid'),...
'Enable', 'on');
obj.waitingForPoint = false;
obj.statusUpdate('');
end
end
function onKeyPress(obj, ~, evt)
% ONKEYPRESS Key controls for common actions
switch evt.Character
case 'a'
obj.waitingForPoint = true;
obj.statusUpdate('Waiting for control point placement');
case 'f'
if ~isempty(obj.ControlPoints)
obj.detectChoroid();
end
case 'c'
obj.flipByTag('Choroid');
case 'r'
obj.flipByTag('RPE');
obj.flipByTag('ILM');
end
end
function onClose(obj, ~, ~)
% ONCLOSE Runs when ChoroidApp figure closes
notify(obj, 'ClosedApp');
delete(obj.figHandle);
end
end
% User interface setup functions run at initialization
methods
function createUI(obj)
% CREATEUI Initialize the user interface
obj.figHandle = figure(...
'Name', 'ChoroidApp',...
'Color', 'w',...
'Menubar', 'none', 'Toolbar', 'none',...
'NumberTitle', 'off',...
'DefaultUicontrolBackgroundColor', 'w',...
'KeyPressFcn', @obj.onKeyPress,...
'WindowButtonUpFcn', @obj.onWindowButtonUp,...
'CloseRequestFcn', @obj.onClose);
figPos(obj.figHandle, 1.5, 0.8);
mainLayout = uix.HBoxFlex('Parent', obj.figHandle,...
'BackgroundColor', 'w');
octLayout = uix.VBox('Parent', mainLayout,...
'Padding', 0, 'Spacing', 0,...
'BackgroundColor', 'w');
% Status display
uicontrol(octLayout, 'Style', 'text',...
'String', '',...
'FontSize', 16,...
'FontWeight', 'bold',...
'Tag', 'Status');
% Setup the image display
obj.axHandle = axes(...
'Parent', uipanel(octLayout, 'BackgroundColor', 'w'));
obj.imHandle = imagesc(obj.axHandle, obj.originalImage);
colormap(obj.figHandle, gray);
axis(obj.axHandle, 'equal', 'tight', 'off');
hold(obj.axHandle, 'on');
% Set up UI controls
uiLayout = uix.VBox('Parent', mainLayout,...
'BackgroundColor', 'w');
% UI for image control
uix.Empty('Parent', uiLayout);
imageLayout = uix.HBox('Parent', uiLayout,...
'BackgroundColor', 'w');
uicontrol(imageLayout, 'Style', 'push',...
'String', 'Reload image',...
'Callback', @obj.onReloadImage);
uicontrol(imageLayout, 'Style', 'push',...
'String', 'Colormap',...
'Tag', 'gray',...
'Callback', @obj.onChangeCMap);
widths = [-0.25, -1];
% UI for retinal layer segmentation
uix.Empty('Parent', uiLayout);
uicontrol(uiLayout, 'Style', 'text',...
'String', 'Retinal Layers:',...
'FontWeight', 'bold');
retinaLayout = uix.HBox('Parent', uiLayout,...
'BackgroundColor', 'w');
uicontrol(retinaLayout, 'Style', 'push',...
'String', 'Segment',...
'Callback', @obj.onSegmentRetina);
uix.Empty('Parent', retinaLayout);
uicontrol(retinaLayout, 'Style', 'check',...
'String', 'Show',...
'TooltipString', 'Toggle display of layers (''r'')',...
'Tag', 'ShowRetina',...
'Callback', @obj.onShowRetinaSegmentation);
set(retinaLayout, 'Widths', [-1, -0.25, -0.75]);
widths = cat(2, widths, [-0.25, -0.5, -1]);
% UI for choroid parabola control points
uix.Empty('Parent', uiLayout);
uicontrol(uiLayout, 'Style', 'text',...
'String', 'Control Points:',...
'FontWeight', 'bold');
pointLayout = uix.HBox('Parent', uiLayout,...
'BackgroundColor', 'w');
uicontrol(pointLayout, 'Style', 'push',...
'String', 'Add Point',...
'Tag', 'AddPts',...
'Callback', @obj.onAddCtrlPoint);
uix.Empty('Parent', pointLayout);
uicontrol(pointLayout, 'Style', 'push',...
'String', 'Clear points',...
'Tag', 'ClearPts',...
'Callback', @obj.onClearCtrlPoints);
set(pointLayout, 'Widths', [-1, -0.25, -1]);
uicontrol(uiLayout, 'Style', 'push',...
'String', 'Use Histogram App',...
'Callback', @obj.onUseHistogramApp);
widths = cat(2, widths, [-0.5, -0.5, -1, -1]);
% UI for fitting choroid
uix.Empty('Parent', uiLayout);
uicontrol(uiLayout, 'Style', 'text',...
'String', 'Choroid:',...
'FontWeight', 'bold');
choroidLayout = uix.HBox('Parent', uiLayout,...
'BackgroundColor', 'w');
uicontrol(choroidLayout, 'Style', 'push',...
'String', 'Fit',...
'Tag', 'FitChoroid',...
'Callback', @obj.onFitChoroid);
uix.Empty('Parent', choroidLayout);
uicontrol(choroidLayout, 'Style', 'check',...
'String', 'Show',...
'TooltipString', 'Toggle display of choroid (''c'')',...
'Tag', 'ShowChoroid',...
'Callback', @obj.onShowChoroid);
set(choroidLayout, 'Widths', [-1, -0.25, -0.75]);
widths = cat(2, widths, [-0.5, -0.5, -1]);
% UI for saving and exporting analysis
uix.Empty('Parent', uiLayout);
uicontrol(uiLayout, 'Style', 'text',...
'String', 'Export:', 'FontWeight', 'bold');
nameLayout = uix.HBox('Parent', uiLayout,...
'BackgroundColor', 'w');
uicontrol(nameLayout, 'Style', 'text', 'String', 'OCT Name:');
uicontrol(nameLayout, 'Style', 'edit',...
'String', obj.imageName,...
'Tag', 'OCTName');
saveLayout = uix.HBox('Parent', uiLayout,...
'BackgroundColor', 'w');
uicontrol(saveLayout, 'Style', 'push',...
'String', 'Save Data',...
'Callback', @obj.onSaveAll);
uicontrol(saveLayout, 'Style', 'push',...
'String', 'Save image',...
'Callback', @obj.onExportFigure);
uix.Empty('Parent', uiLayout);
widths = cat(2, widths, [-0.5, -0.5, -0.6, -1, -0.25]);
set(uiLayout, 'Heights', widths);
set(octLayout, 'Heights', [-0.5, -6]);
set(mainLayout, 'Widths', [-6, -1.5]);
% Plot imported data, if applicable
if ~isempty(obj.ControlPoints)
set(findobj(obj.figHandle, 'Tag', 'FitChoroid'),...
'Enable', 'on');
obj.addControlPoints(obj.ControlPoints);
end
end
function tf = parseInput(obj, x)
% PARSEINPUT Get OCT image and run initial processing
tf = false;
obj.imageName = ''; % Blank unless provided by OCT class
obj.imagePath = '';
if isa(x, 'OCT')
if ~isempty(x.ControlPoints)
selection = questdlg('Import existing control points?');
if strcmp(selection, 'Yes')
obj.ControlPoints = x.ControlPoints;
end
end
im = x.octImage;
obj.imageName = x.imageName;
obj.imagePath = x.imagePath;
elseif ischar(x)
im = imread(x);
elseif isnumeric(x)
im = x;
else
error('CHOROIDAPP:InvalidInput',...
'Input must be an image or OCT class');
end
% Convert to grayscale if necessary
if ndims(im) == 3
im = rgb2gray(im);
end
obj.originalImage = im;
end
end
methods (Static)
function action = onOff(action)
if strcmp(action, 'on')
action = 'off';
else
action = 'on';
end
end
end
end