-
Notifications
You must be signed in to change notification settings - Fork 5
/
HistogramPeakSlider.m
540 lines (480 loc) · 19 KB
/
HistogramPeakSlider.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
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
classdef HistogramPeakSlider < handle
% HISTOGRAMPEAKSLIDER
%
% Syntax:
% obj = HistogramPeakSlider(oct)
%
% Input:
% oct OCT class or image
%
% Public Methods:
% obj.addParent Associate with a ChoroidApp instance
%
% History:
% 2Jan2018 - SSP
% ------------------------------------------------------------------------
properties (Access = private)
octImage
appHandle
end
properties (SetAccess = private)
chosenPeak
chosenTrough
calculatedMidpoint
storedPoints
end
properties (SetAccess = private)
figHandle
axHandle
imHandle
histHandle
histLine
sliderValue
end
properties (Dependent = true)
RPE
ILM
Choroid
end
methods
function obj = HistogramPeakSlider(im)
if nargin == 0
im = uigetfile({'*.png'; '*.tiff'; '*.jpeg'});
if ~ischar(im)
disp('No image selected!');
return
end
end
% Parse input file, get image
if isa(im, 'OCT')
im = im.octImage;
elseif ischar(im)
im = imread(im);
end
% Convert to grayscale if necessary
if numel(size(im)) == 3
im = rgb2gray(im);
end
obj.octImage = im;
obj.createUI();
end
function addParent(obj, appHandle)
% ADDPARENT Associate with a ChoroidApp instance
if ~isa(appHandle, 'ChoroidApp')
warning('Input must be ChoroidApp instance');
return;
end
obj.appHandle = appHandle;
if ~isempty(obj.RPE)
obj.plotLayers();
end
if ~isempty(obj.Choroid)
obj.plotChoroid();
end
if ~isempty(obj.appHandle.ControlPoints)
obj.storedPoints = [obj.appHandle.ControlPoints(:, 1),...
nan(size(obj.appHandle.ControlPoints, 1), 1),...
obj.appHandle.ControlPoints(:, 2)];
obj.plotStoredPoints();
end
addlistener(obj.appHandle, 'FitChoroid',...
@(src, evt)obj.onAppFitChoroid);
addlistener(obj.appHandle, 'SegmentedRetinalLayers',...
@(src,evt)obj.onAppSegmentedRetinalLayers);
addlistener(obj.appHandle, 'ClosedApp',...
@(src, evt)obj.onAppClosed);
end
end
% Dependent set/get methods
methods
function RPE = get.RPE(obj)
RPE = obj.appHandle.RPE;
end
function ILM = get.ILM(obj)
ILM = obj.appHandle.ILM;
end
function Choroid = get.Choroid(obj)
Choroid = obj.appHandle.Choroid;
end
end
% Listener callback methods
methods (Access = private)
function onAppFitChoroid(obj, ~, ~)
obj.plotChoroid();
end
function onAppSegmentedRetinalLayers(obj, ~, ~)
obj.plotLayers();
end
function onAppClosed(obj, ~, ~)
delete(obj.figHandle);
end
end
% Callback methods
methods (Access = private)
function onSlide(obj, ~, ~)
% ONSLIDE Continuously updated with slider movement
obj.plotHistogram(obj.sliderValue);
obj.plotLayers(obj.sliderValue);
obj.plotChoroid(obj.sliderValue);
delete(findall(obj.histHandle, 'Tag', 'Peak'));
delete(findall(obj.histHandle, 'Tag', 'Trough'));
delete(findall(obj.histHandle, 'Tag', 'Midpoint'));
set(obj.getByTag('SliderText'), 'String',...
sprintf('Index = %u', obj.sliderValue));
obj.plotSliderPoint();
end
function onSlideFinished(obj, ~, ~)
% ONSLIDEFINISHED Updated after slider is moved
set(obj.getByTag('PeakText'), 'String', 'Peak: ');
set(obj.getByTag('TroughText'), 'String', 'Trough: ');
set(obj.getByTag('MidpointText'), 'String', 'Midpoint: ');
obj.chosenPeak = []; obj.chosenTrough = [];
obj.calculatedMidpoint = [];
end
function onChangeColormap(obj, src, ~)
if strcmp(get(src, 'Tag'), 'gray')
colormap(obj.figHandle, pink);
set(src, 'Tag', 'pink');
else
colormap(obj.figHandle, gray);
set(src, 'Tag', 'gray');
end
end
function onFindPeaks(obj, ~, ~)
[pkInd, trInd] = histogramPeaks(...
obj.octImage(:, round(obj.sliderValue)), 'Thresh', 15);
if ~isempty(obj.RPE)
pkInd(pkInd(:, 1) > obj.RPE(obj.sliderValue, 2), :) = [];
trInd(trInd(:, 1) > obj.RPE(obj.sliderValue, 2), :) = [];
end
pkInd(pkInd(:, 2) == 0, :) = [];
trInd(trInd(:, 2) == 0, :) = [];
obj.plotPeaks(pkInd, trInd);
end
function onSelectPeak(obj, ~, ~)
[~, xSelect, ySelect] = selectdata2(...
'Axes', obj.histHandle,...
'Figure', obj.figHandle,...
'SelectionMode', 'closest',...
'Ignore', obj.histLine);
ind = cellfun(@isempty, xSelect);
obj.chosenPeak = [xSelect{~ind}, ySelect{~ind}];
set(obj.getByTag('PeakText'), 'String',...
sprintf('Peak: %u, %u', obj.chosenPeak));
end
function onSelectTrough(obj, ~, ~)
[~, xSelect, ySelect] = selectdata2(...
'Axes', obj.histHandle,...
'Figure', obj.figHandle,...
'SelectionMode', 'closest',...
'Ignore', obj.histLine);
ind = cellfun(@isempty, xSelect);
obj.chosenTrough = [xSelect{~ind}, ySelect{~ind}];
set(obj.getByTag('TroughText'), 'String',...
sprintf('Trough: %u, %u', obj.chosenTrough));
end
function onFindMidpoint(obj, ~, ~)
if isempty(obj.chosenPeak) || isempty(obj.chosenTrough)
return;
end
histRange = sort([obj.chosenPeak(2), obj.chosenTrough(2)]);
histRange = histRange(1):histRange(2);
meanPt = mean(obj.histLine.XData(histRange));
[~, ind] = closest(meanPt, double(obj.histLine.XData(histRange)));
xRange = double(obj.histLine.YData(histRange));
ind = xRange(ind);
obj.calculatedMidpoint = double([...
obj.histLine.XData(ind), obj.histLine.YData(ind)]);
set(obj.getByTag('MidpointText'), 'String',...
sprintf('Midpoint: %u, %u', obj.calculatedMidpoint));
obj.plotMidpoint(obj.calculatedMidpoint);
end
function onStorePoints(obj, ~, ~)
if ~isempty(obj.calculatedMidpoint)
obj.storedPoints = cat(1, obj.storedPoints,...
[obj.sliderValue, obj.calculatedMidpoint]);
obj.plotStoredPoints();
end
end
function onDeleteLastPoint(obj, ~, ~)
obj.storedPoints(end, :) = [];
obj.plotStoredPoints();
end
function onSendPoints(obj, ~, ~)
if isempty(obj.appHandle)
warning('Need to set appHandle first!');
return;
end
obj.appHandle.addControlPoints(obj.storedPoints(:, [1, 3]));
end
function onKeyPress(obj, ~, evt)
switch evt.Key
case 'f'
obj.onFindPeaks();
case 'a'
obj.onSelectPeak();
obj.onSelectTrough();
case 'p'
obj.onSelectPeak();
case 't'
obj.onSelectTrough();
case 'm'
obj.onFindMidpoint();
case 'd'
obj.onDeleteLastPoint();
case 's'
if isempty(evt.Modifier)
obj.onStorePoints();
elseif strcmp(evt.Modifier{1}, 'shift')
obj.onSendPoints();
end
case 'rightarrow'
newValue = obj.sliderValue + obj.getSteps(evt);
if newValue < size(obj.octImage, 2)
obj.sliderValue = newValue;
obj.onSlide(); obj.onSlideFinished();
end
case 'leftarrow'
newValue = obj.sliderValue - obj.getSteps(evt);
if newValue > 1
obj.sliderValue = newValue;
obj.onSlide(); obj.onSlideFinished();
end
case 'h'
end
end
end
% User interface functions
methods (Access = private)
function h = getByTag(obj, tag)
h = findall(obj.figHandle, 'Tag', tag);
end
function plotHistogram(obj, xInd)
set(obj.histLine, 'XData', obj.octImage(:, round(xInd)));
end
function plotLayers(obj, xInd)
if isempty(obj.RPE)
return;
end
if nargin == 1
xInd = obj.sliderValue;
end
% Plot the RPE
h = findobj(obj.histHandle, 'Tag', 'RPE');
if ~isempty(h)
set(h, 'YData', [obj.RPE(xInd, 2), obj.RPE(xInd, 2)]);
end
line([1, 255], repmat(obj.RPE(obj.sliderValue, 2), [1, 2]),...
'Parent', obj.histHandle,...
'Color', rgb('sky blue'),...
'LineWidth', 1,...
'Tag', 'RPE');
% Plot the ILM
h = findobj(obj.histHandle, 'Tag', 'ILM');
if ~isempty(h)
set(h, 'YData', [obj.ILM(xInd, 2), obj.ILM(xInd, 2)]);
else
line([1, 255], repmat(obj.ILM(xInd, 2), [1, 2]),...
'Parent', obj.histHandle,...
'Color', rgb('sky blue'),...
'LineWidth', 1,...
'Tag', 'ILM');
end
end
function plotChoroid(obj, xInd)
if isempty(obj.Choroid)
return;
end
if nargin == 1
xInd = obj.sliderValue;
end
h = findobj(obj.histHandle, 'Tag', 'Choroid');
if ~isempty(h)
set(h, 'YData', [obj.Choroid(xInd, 2), obj.Choroid(xInd, 2)]);
else
line([1, 255], repmat(obj.Choroid(xInd, 2), [1, 2]),...
'Parent', obj.histHandle,...
'Color', rgb('peach'),...
'LineWidth', 1,...
'Tag', 'Choroid');
end
end
function plotStoredPoints(obj)
delete(obj.getByTag('ControlPoints'));
line(obj.storedPoints(:, 1), obj.storedPoints(:, 3),...
'Parent', obj.axHandle,...
'Color', 'c', 'Marker', 'x',...
'LineStyle', 'none',...
'Tag', 'ControlPoints');
end
function plotSliderPoint(obj)
% PLOTSLIDERPOINT Show slider position with arrows
set(obj.getByTag('SliderPoint'), 'XData', obj.sliderValue);
end
function plotPeaks(obj, pkInd, trInd)
% PLOTPEAKS Plot detected peaks and troughs
delete(findall(obj.histHandle, 'Tag', 'Peak'));
delete(findall(obj.histHandle, 'Tag', 'Trough'));
line(pkInd(:, 2), pkInd(:, 1),...
'Parent', obj.histHandle,...
'Color', hex2rgb('00cc4d'),...
'Marker', 'o', 'LineWidth', 1,...
'LineStyle', 'none',...
'Tag', 'Peak');
line(trInd(:, 2), trInd(:, 1),...
'Parent', obj.histHandle,...
'Color', hex2rgb('ff4040'),...
'Marker', 'o', 'LineWidth', 1,...
'LineStyle', 'none',...
'Tag', 'Trough');
end
function plotMidpoint(obj, midPt)
delete(obj.getByTag('Midpoint'));
line(midPt(1), midPt(2),...
'Parent', obj.histHandle,...
'Color', hex2rgb('334de6'),...
'Marker', 'o', 'LineWidth', 1,...
'LineStyle', 'none',...
'Tag', 'Midpoint');
end
end
% User interface setup functions
methods (Access = private)
function createUI(obj)
% CREATEUI Sets up the main user interface
obj.figHandle = figure('Name', 'HistogramSlider',...
'Color', 'w',...
'NumberTitle', 'off',...
'Menubar', 'none', 'Toolbar', 'none',...
'DefaultUicontrolBackgroundColor', 'w',...
'KeyPressFcn', @obj.onKeyPress);
figPos(obj.figHandle, 1.5, 0.7);
mainLayout = uix.HBox('Parent', obj.figHandle,...
'Spacing', 1, 'Padding', 2,...
'BackgroundColor', 'w');
obj.sliderValue = 1; % Start at the leftmost pixel
obj.createImage(mainLayout);
obj.createHistogram(mainLayout);
uiLayout = uix.VBox('Parent', mainLayout,...
'BackgroundColor', 'w');
uicontrol(uiLayout,...
'Style', 'text',...
'String', 'Index = 1',...
'TooltipString', 'X-axis pixel (change with arrow keys)',...
'Tag', 'SliderText');
uicontrol(uiLayout,...
'Style', 'push',...
'String', 'Change colormap',...
'TooltipString', 'Change from gray to pink colormap',...
'Tag', 'gray',...
'Callback', @obj.onChangeColormap);
uicontrol(uiLayout,...
'Style', 'push',...
'String', 'Find Extrema',...
'TooltipString', 'Find peaks of current histogram (''f'')',...
'Callback', @obj.onFindPeaks);
selectLayout = uix.HBox('Parent', uiLayout,...
'BackgroundColor', 'w');
uicontrol(selectLayout,...
'Style', 'text',...
'String', 'Pick:');
uicontrol(selectLayout,...
'Style', 'push',...
'String', 'Peak',...
'TooltipString', 'Select a green peak point (''p'')',...
'Callback', @obj.onSelectPeak);
uicontrol(selectLayout,...
'Style', 'push',...
'String', 'Trough',...
'TooltipString', 'Select a red trough point (''t'')',...
'Callback', @obj.onSelectTrough);
set(selectLayout, 'Widths', [-0.5, -1, -1]);
uicontrol(uiLayout,...
'Style', 'text',...
'String', 'Peak: ',...
'Tag', 'PeakText');
uicontrol(uiLayout,...
'Style', 'text',...
'String', 'Trough: ',...
'Tag', 'TroughText');
uicontrol(uiLayout,...
'Style', 'text',...
'String', 'Midpoint: ',...
'Tag', 'MidpointText');
uicontrol(uiLayout,...
'Style', 'push',...
'String', 'Find midpoint',...
'TooltipString', 'Find midpoint between peak and trough (''m'')',...
'Tag', 'FindMidpoint',...
'Callback', @obj.onFindMidpoint);
obj.createExportUI(uiLayout);
set(uiLayout, 'Heights', [-0.5, -1, -1, -1, -0.5, -0.5, -0.5, -1, -1.5])
set(mainLayout, 'Widths', [-2.3, -1, -0.6]);
end
function createImage(obj, parentHandle)
% CREATEIMAGE Plot OCT image
obj.axHandle = axes('Parent', parentHandle);
obj.imHandle = imagesc(obj.axHandle, obj.octImage);
colormap(obj.figHandle, gray);
axis(obj.axHandle, 'equal', 'tight', 'off');
hold(obj.axHandle, 'on');
% Mark pixel currently shown in 1D histogram
line(1, size(obj.imHandle.CData, 1),...
'Parent', obj.axHandle,...
'Marker', '^', 'LineStyle', 'none',...
'Color', rgb('peach'), 'MarkerFaceColor', rgb('peach'),...
'Tag', 'SliderPoint');
line(1, 1,...
'Parent', obj.axHandle,...
'Marker', 'v', 'LineStyle', 'none',...
'Color', rgb('peach'), 'MarkerFaceColor', rgb('peach'),...
'Tag', 'SliderPoint');
end
function createHistogram(obj, parentHandle)
% CREATEHISTOGRAM Set up the 1D x-axis histogram
axPanel = uipanel('Parent', parentHandle,...
'BackgroundColor', 'w');
obj.histHandle = axes('Parent', axPanel,...
'YLim', [1, size(obj.octImage, 1)],...
'XLim', [0, 255],...
'YDir', 'reverse',...
'Tag', 'HistogramAxes');
obj.histLine = line(obj.histHandle,...
'YData', 1:size(obj.octImage, 1),...
'XData', obj.octImage(:, 1),...
'LineWidth', 1, 'Color', 'k');
end
function createExportUI(obj, parentHandle)
% CREATEEXPORTUI User interface regarding export from app
exportLayout = uix.HBox('Parent', parentHandle,...
'BackgroundColor', 'w');
plotLayout = uix.VBox('Parent', exportLayout,...
'BackgroundColor', 'w');
uicontrol(plotLayout,...
'Style', 'push',...
'String', 'Show',...
'TooltipString', 'Show on image to the left (key = ''s''',...
'Callback', @obj.onStorePoints);
uicontrol(plotLayout,...
'Style', 'push',...
'String', 'Delete',...
'TooltipString', 'Delete recently added point (key = ''d'')',...
'Callback', @obj.onDeleteLastPoint);
uicontrol(exportLayout,...
'Style', 'push',...
'String', 'Send',...
'FontWeight', 'bold',...
'Callback', @obj.onSendPoints);
end
end
methods (Static)
function nSteps = getSteps(evt)
% GETSTEPS Determine steps by whether shift key was pressed
if ~isempty(evt.Modifier) && strcmp(evt.Modifier{1}, 'shift')
nSteps = 10;
else
nSteps = 1;
end
end
end
end