-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgui.m
632 lines (461 loc) · 20.4 KB
/
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
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
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
function varargout = gui(varargin)
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @gui_OpeningFcn, ...
'gui_OutputFcn', @gui_OutputFcn, ...
'gui_LayoutFcn', [] , ...
'gui_Callback', []);
if nargin && ischar(varargin{1})
gui_State.gui_Callback = str2func(varargin{1});
end
if nargout
[varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
else
gui_mainfcn(gui_State, varargin{:});
end
end
% --- Executes just before gui is made visible.
function gui_OpeningFcn(hObject, eventdata, handles, varargin)
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes gui wait for user response (see UIRESUME)
% uiwait(handles.figure1);
end
% --- Outputs from this function are returned to the command line.
function varargout = gui_OutputFcn(hObject, eventdata, handles)
% Get default command line output from handles structure
varargout{1} = handles.output;
end
% ================================================================ %
% Aux Functions
% --- Shows image on Axis1 --- %
function ShowImageAxis1(im_now, hObject, handles)
% Get size of axes1
set(handles.axes1,'Units','pixels');
resizePos = get(handles.axes1,'Position');
% Check if transparency box is ticked
transparent = get(handles.transparencyBox, 'Value');
% Resize and show im_orig in axes1
im_now = imresize(im_now, [resizePos(3) resizePos(3)]);
size(im_now)
axes(handles.axes1);
imageHandle = imshow(im_now);
set(imageHandle, 'ButtonDownFcn', {@my_callback, handles})
if transparent
alpha(0.25)
end
set(handles.axes1,'Units','normalized');
handles.im_now = im_now;
guidata(hObject, handles);
end
function my_callback(dummy1, dummy2, handles)
cursorPoint = get(handles.axes1, 'CurrentPoint');
curX = cursorPoint(1,1);
curY = cursorPoint(1,2);
%disp(curX)
%disp(curY)
if isequal(get(handles.merge,'value'), 1)
%size(handles.im_labeled)
%rgbColor = impixel(handles.im_labeled, curX, curY);
%disp(rgbColor)
end
return;
end
function axes1_ButtonDownFcn(objectHandle, eventData, handles)
disp('clicked axes1')
end
function ShowImageAxis3(im_now, handles)
% Get size of axes1
set(handles.axes3,'Units','pixels');
resizePos = get(handles.axes3,'Position');
% Resize and show im_orig in axes1
im_now = imresize(im_now, [resizePos(3) resizePos(3)]);
axes(handles.axes3);
imshow(im_now);
set(handles.axes3,'Units','normalized');
end
function VisuButtons(value, handles)
set(handles.tgbtnOriginal, 'Value', 0);
set(handles.tgbtnFiltered, 'Value', 0);
set(handles.tgbtnBinarized, 'Value', 0);
set(handles.tgbtnSegmented, 'Value', 0);
switch value
case 1
set(handles.tgbtnOriginal, 'Value', 1);
case 2
set(handles.tgbtnFiltered, 'Value', 1);
case 3
set(handles.tgbtnBinarized, 'Value', 1);
case 4
set(handles.tgbtnSegmented, 'Value', 1);
end
end
% Transparency box Callback
function transparencyBox_Callback(hObject, eventdata, handles)
% Check if turned on or off
%transparent = get(handles.transparencyBox, 'Value');
% Check which toggle btn is on
if get(handles.tgbtnOriginal, 'Value')
ShowImageAxis1(handles.im_orig, hObject, handles)
elseif get(handles.tgbtnFiltered, 'Value')
ShowImageAxis1(handles.im_filtered, hObject, handles)
elseif get(handles.tgbtnBinarized, 'Value')
ShowImageAxis1(handles.im_morphed, hObject, handles)
elseif get(handles.tgbtnSegmented, 'Value')
ShowImageAxis1(handles.im_segmented, hObject, handles)
end
end
% ================================================================= %
% Push Buttons
% Push Button Select Image
function pbSelectImage_Callback(hObject, eventdata, handles)
[file,path] = uigetfile({'*.jpg';'*.png'}, 'File Selector');
if isequal(file,0)
disp('User selected Cancel');
else
disp(['User selected ', fullfile(path,file)]);
% Read image
im_orig = imread(fullfile(path,file));
% Assign the value if they didn't click cancel.
handles.im_orig = im_orig;
handles.im_now = im_orig;
handles.im_filtered = im_orig;
handles.im_segmented = im_orig;
ShowImageAxis1(im_orig, hObject, handles);
ShowImageAxis3(im_orig, handles);
% Update handles
guidata(hObject, handles);
end
% If someone selects another image, restart stuff
set(handles.treeCountBox,'string',num2str(0));
set(handles.contrastCheckBox,'value',0);
set(handles.atmCheckBox,'value',0);
VisuButtons(1, handles);
end
% Toggle Button Show Original Image
function tgbtnOriginal_Callback(hObject, eventdata, handles)
im_orig = handles.im_orig;
ShowImageAxis1(im_orig, hObject, handles);
end
% Toggle Button Show Filtered Image
function tgbtnFiltered_Callback(hObject, eventdata, handles)
im_filtered = handles.im_filtered;
ShowImageAxis1(im_filtered, hObject, handles);
end
% Toggle Button Show Binarized Image
function tgbtnBinarized_Callback(hObject, eventdata, handles)
im_morphed = handles.im_morphed;
ShowImageAxis1(im_morphed, hObject, handles);
end
% Toggle Button Show Segmented Image
function tgbtnSegmented_Callback(hObject, eventdata, handles)
im_segmented = handles.im_segmented;
ShowImageAxis1(im_segmented, hObject, handles);
end
% ================================================================= %
% Filtering
% --- Contrast Enhancement
function contrastCheckBox_Callback(hObject, eventdata, handles)
set(gcf,'Pointer','watch');
drawnow;
if isequal(get(handles.contrastCheckBox,'value'), 1)
im_now = handles.im_now;
im_now = contrastEnhancement(im_now, 0.002);
handles.im_filtered = im_now;
handles.im_now = im_now;
guidata(hObject, handles);
VisuButtons(2, handles);
ShowImageAxis1(im_now, hObject, handles);
else
if isequal(get(handles.atmCheckBox,'value'), 1)
im_now = handles.im_now;
im_now = imreducehaze(im_now);
handles.im_filtered = im_now;
handles.im_now = im_now;
guidata(hObject, handles);
VisuButtons(2, handles);
ShowImageAxis1(im_now, hObject, handles);
else
im_now = handles.im_orig;
handles.im_now = im_now;
handles.im_filtered = im_now;
guidata(hObject, handles);
VisuButtons(1, handles);
ShowImageAxis1(im_now, hObject, handles);
end
end
set(gcf,'Pointer','arrow');
drawnow;
end
% --- Reduce Atmosphere Haze
function atmCheckBox_Callback(hObject, eventdata, handles)
set(gcf,'Pointer','watch');
drawnow;
if isequal(get(handles.atmCheckBox,'value'), 1)
im_now = handles.im_now;
im_now = imreducehaze(im_now);
handles.im_filtered = im_now;
handles.im_now = im_now;
guidata(hObject, handles);
VisuButtons(2, handles);
ShowImageAxis1(im_now, hObject, handles);
else
if isequal(get(handles.contrastCheckBox,'value'), 1)
im_now = handles.im_now;
im_now = contrastEnhancement(im_now, 0.002);
handles.im_filtered = im_now;
handles.im_now = im_now;
guidata(hObject, handles);
VisuButtons(2, handles);
ShowImageAxis1(im_now, hObject, handles);
else
im_now = handles.im_orig;
handles.im_filtered = im_now;
handles.im_now = im_now;
guidata(hObject, handles);
VisuButtons(1, handles);
ShowImageAxis1(im_now, hObject, handles);
end
end
set(gcf,'Pointer','arrow');
drawnow;
end
% ================================================================= %
% Binarizing
% --- Apply Button
function pbtnApplyBin_Callback(hObject, eventdata, handles)
set(gcf,'Pointer','watch');
drawnow;
% Radio button kMeans is selected
if isequal(get(handles.rbtnKMeans, 'Value'), 1)
nColors = get(handles.kMeansMenu, 'Value') + 1;
im_filtered = handles.im_filtered;
labels = kmeansColor(im_filtered, nColors);
handles.im_labeled = labels;
% Turn layer1 white and the rest black
im_binarized = selectLayers(labels, 2:nColors, 1);
handles.im_binarized = im_binarized;
handles.im_morphed = im_binarized;
% Reload kMeans list
klist = 1:nColors;
set(handles.kMeansList, 'String', strtrim(cellstr(num2str(klist'))'));
% Save stuff
guidata(hObject, handles);
% Reload stuff
VisuButtons(3, handles);
ShowImageAxis1(im_binarized, hObject, handles);
end
% Radio button Otsu is selected
if isequal(get(handles.rbtnOtsu, 'Value'), 1)
im_filtered = handles.im_filtered;
im_binarized = otsuMorph(im_filtered, 200, 2, 4);
handles.im_binarized = im_binarized;
handles.im_morphed = im_binarized;
guidata(hObject, handles);
VisuButtons(3, handles);
ShowImageAxis1(im_binarized, hObject, handles);
end
% If someone selects another binarizing technique, zero count
set(handles.treeCountBox,'string',num2str(0));
set(gcf,'Pointer','arrow');
drawnow;
end
% --- Executes on selection change in kMeansList.
function kMeansList_Callback(hObject, eventdata, handles)
% Get selected items
contents = (get(hObject,'Value'));
str_contents = cellstr(get(hObject,'String'));
% list1 = selected items
% list0 = all items - selected items
list1 = str_contents(contents);
list0 = erase(str_contents, list1);
list0 = str2num(join(string(list0)));
list1 = str2num(join(string(list1)));
% Paint the selected with white and the rest with black
labeledImage = handles.im_labeled;
im_binarized = selectLayers(labeledImage, list0, list1);
handles.im_binarized = im_binarized;
handles.im_morphed = im_binarized;
% Save stuff
guidata(hObject, handles);
% Reload stuff
VisuButtons(3, handles);
ShowImageAxis1(im_binarized, hObject, handles);
% If someone selects another image, zero count
set(handles.treeCountBox,'string',num2str(0));
end
% --- Executes on selection change in kMeansList.
function kMeansMenu_Callback(hObject, eventdata, handles)
end
% ================================================================= %
% Morph Operations
% --- Executes on button press in applyMorph.
function applyMorph_Callback(hObject, eventdata, handles)
im_morphed = handles.im_morphed;
contents = get(handles.operMenu,'String');
operation = contents{get(handles.operMenu,'Value')};
display(operation)
contents = get(handles.seMenu,'String');
struct_elem = contents{get(handles.seMenu,'Value')};
display(operation)
r = str2double(get(handles.radiusEdit, 'String'));
display(r)
% Build structuring element
switch struct_elem
case 'Disk'
se = strel('disk', r);
case 'Diamond'
se = strel('diamond', r);
case 'Octagon'
se = strel('octagon', r);
case 'Square'
se = strel('square', r);
end
% Select morph operation
switch operation
case 'Closing'
im_morphed = imclose(im_morphed, se);
case 'Opening'
im_morphed = imopen(im_morphed, se);
case 'Filter CC'
end
handles.im_morphed = im_morphed;
% Save stuff
guidata(hObject, handles);
% Reload stuff
VisuButtons(3, handles);
ShowImageAxis1(im_morphed, hObject, handles);
end
% --- Executes on button press in restartMorph.
function restartMorph_Callback(hObject, eventdata, handles)
im_binarized = handles.im_binarized;
handles.im_morphed = im_binarized;
% Save stuff
guidata(hObject, handles);
% Reload stuff
VisuButtons(3, handles);
ShowImageAxis1(im_binarized, hObject, handles);
end
% ================================================================= %
% Dist. Transform and Watershed
% --- Executes on button press in btnWatershed.
function btnWatershed_Callback(hObject, eventdata, handles)
set(gcf,'Pointer','watch');
drawnow;
im_binarized = handles.im_morphed;
sensitivity = get(handles.distTMenu, 'Value');
[RGB_label, im_labeled, count] = dtWatershed(im_binarized, sensitivity);
handles.im_labeled = im_labeled;
handles.im_segmented = RGB_label;
guidata(hObject, handles);
ShowImageAxis1(RGB_label, hObject, handles);
VisuButtons(4, handles);
set(handles.treeCountBox,'string',num2str(count));
set(gcf,'Pointer','arrow');
drawnow;
end
% --- Executes on button press in merge.
function merge_Callback(hObject, eventdata, handles)
BW2 = bwselect(handles.im_segmented, 4)
end
% ================================================================= %
% --- Executes on selection change in distTMenu.
function distTMenu_Callback(hObject, eventdata, handles)
end
function distTMenu_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
end
% --- Executes during object creation, after setting all properties.
function kMeansMenu_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
end
% --- Executes during object creation, after setting all properties.
function kMeansList_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
end
function treeCountBox_Callback(hObject, eventdata, handles)
% hObject handle to treeCountBox (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of treeCountBox as text
% str2double(get(hObject,'String')) returns contents of treeCountBox as a double
end
% --- Executes during object creation, after setting all properties.
function treeCountBox_CreateFcn(hObject, eventdata, handles)
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
end
% ================================================================= %
% --- Executes on button press in transparencyBox.
function checkbox13_Callback(hObject, eventdata, handles)
% hObject handle to transparencyBox (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hint: get(hObject,'Value') returns toggle state of transparencyBox
end
% --- Executes on selection change in operMenu.
function operMenu_Callback(hObject, eventdata, handles)
% hObject handle to operMenu (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns operMenu contents as cell array
% contents{get(hObject,'Value')} returns selected item from operMenu
end
% --- Executes during object creation, after setting all properties.
function operMenu_CreateFcn(hObject, eventdata, handles)
% hObject handle to operMenu (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
end
% --- Executes on selection change in seMenu.
function seMenu_Callback(hObject, eventdata, handles)
% hObject handle to seMenu (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: contents = cellstr(get(hObject,'String')) returns seMenu contents as cell array
% contents{get(hObject,'Value')} returns selected item from seMenu
end
% --- Executes during object creation, after setting all properties.
function seMenu_CreateFcn(hObject, eventdata, handles)
% hObject handle to seMenu (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: popupmenu controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
end
function radiusEdit_Callback(hObject, eventdata, handles)
% hObject handle to radiusEdit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Hints: get(hObject,'String') returns contents of radiusEdit as text
% str2double(get(hObject,'String')) returns contents of radiusEdit as a double
end
% --- Executes during object creation, after setting all properties.
function radiusEdit_CreateFcn(hObject, eventdata, handles)
% hObject handle to radiusEdit (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: edit controls usually have a white background on Windows.
% See ISPC and COMPUTER.
if ispc && isequal(get(hObject,'BackgroundColor'), get(0,'defaultUicontrolBackgroundColor'))
set(hObject,'BackgroundColor','white');
end
end