-
Notifications
You must be signed in to change notification settings - Fork 1
/
phenocamimageprocessor.m
1313 lines (1002 loc) · 47.1 KB
/
phenocamimageprocessor.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
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
function varargout = phenocamimageprocessor(varargin)
% Written by Koen Hufkens at Boston University, Sept. 2011
% This code is published under a GPLv2 license and is free
% to redistribute.
% please reference the necessary publications when using the
% the 90th percentile method:
% Sonnentag et al. 2011 (Agricultural and Forest Management)
% PHENOCAMIMAGEPROCESSOR M-file for phenocamimageprocessor.fig
% PHENOCAMIMAGEPROCESSOR, by itself, creates a new PHENOCAMIMAGEPROCESSOR or raises the existing
% singleton*.
%
% H = PHENOCAMIMAGEPROCESSOR returns the handle to a new PHENOCAMIMAGEPROCESSOR or the handle to
% the existing singleton*.
%
% PHENOCAMIMAGEPROCESSOR('CALLBACK',hObject,eventData,handles,...) calls the local
% function named CALLBACK in PHENOCAMIMAGEPROCESSOR.M with the given input arguments.
%
% PHENOCAMIMAGEPROCESSOR('Property','Value',...) creates a new PHENOCAMIMAGEPROCESSOR or raises the
% existing singleton*. Starting from the left, property value pairs are
% applied to the GUI before phenocamimageprocessor_OpeningFcn gets called. An
% unrecognized property name or invalid value makes property application
% stop. All inputs are passed to phenocamimageprocessor_OpeningFcn via varargin.
%
% *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one
% instance to run (singleton)".
%
% See also: GUIDE, GUIDATA, GUIHANDLES
% Edit the above text to modify the response to help phenocamimageprocessor
% Last Modified by GUIDE v2.5 28-Sep-2011 18:21:52
% Begin initialization code - DO NOT EDIT
gui_Singleton = 1;
gui_State = struct('gui_Name', mfilename, ...
'gui_Singleton', gui_Singleton, ...
'gui_OpeningFcn', @phenocamimageprocessor_OpeningFcn, ...
'gui_OutputFcn', @phenocamimageprocessor_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 initialization code - DO NOT EDIT
% --- Executes just before phenocamimageprocessor is made visible.
function phenocamimageprocessor_OpeningFcn(hObject, eventdata, handles, varargin)
% This function has no output args, see OutputFcn.
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% varargin command line arguments to phenocamimageprocessor (see VARARGIN)
% determine system running and set the path / file separator to / or \
handles.OS = mexext;
if strcmp(handles.OS,'mexw32') || strcmp(handles.OS,'mexw64')
handles.file_split = '\'; % windows systems
handles.OS = 'windows';
handles.basedir = 'C:\';
else
handles.file_split = '/'; % unix systems
handles.OS = 'unix';
handles.basedir = '/';
end
% get overall figure name, this will be used to set axes
% and other plotting parameters etc...
handles.main_figure = gcf;
% default number of regions of interest ROI
% and colour map
handles.nroi = 1;
handles.cmap = ['b' 'r' 'y'];
handles.results=[];
handles.linetype=['- ';'--';': '];
% set directory name to empty
handles.dir_name = [];
% set dark threshold value default
% this is the % below which pictures will be considered
% to dark e.g. nighttime pictures and are excluded
% from the analysis
handles.threshold = 15;
set(handles.darkthreshold, 'Value',4); % update popout list
% set window size default
set(handles.popupmenuwindowsize,'Value',2); % set the moving window size default to 3 (pos. 2)
% initialize empty center value
%handles.center(1).X = []; % used to check for existence of ROI selections
handles.xcenter =[];
handles.ycenter = [];
% disable all buttons and stuff
% this prevents use of buttons in a
% disorderly fashion == errors
set(handles.setroibutton,'Enable','off');
set(handles.clearroibutton,'Enable','off');
set(handles.calculatebutton,'Enable','off');
set(handles.gcccheckbox,'Enable','off');
set(handles.gccsmoothcheckbox,'Enable','off');
set(handles.redcheckbox,'Enable','off');
set(handles.greencheckbox,'Enable','off');
set(handles.bluecheckbox,'Enable','off');
set(handles.frompopup,'Enable','off');
set(handles.topopup,'Enable','off');
set(handles.popuproi,'Enable','off');
set(handles.checkboxROI1,'Enable','Off');
set(handles.checkboxROI2,'Enable','Off');
set(handles.checkboxROI3,'Enable','Off');
set(handles.checkbox90th,'Enable','Off');
set(handles.checkboxmean,'Enable','Off');
set(handles.checkboxmedian,'Enable','Off');
set(handles.popupmenuwindowsize,'Enable','Off');
set(handles.pushbuttonupdateplot,'Enable','Off');
set(handles.darkthreshold,'Enable','off');
% set load/save ROI menu item to off
set(handles.menuloadroi,'Enable','off');
set(handles.menusaveroi,'Enable','off');
% set load/save time series data to off
set(handles.menusavetsdata,'Enable','off');
% set logo in first axes
handles.current_image = imread('logo.jpg');
set(handles.main_figure,'CurrentAxes',handles.axes1);
image(handles.current_image);
axis off;
% set fake time series in second axes / empty time series
set(handles.main_figure,'CurrentAxes',handles.axes2);
plot([]);
xlabel('Day of Year (DOY)');
ylabel('Index');
% Choose default command line output for phenocamimageprocessor
handles.output = hObject;
% Update handles structure
guidata(hObject, handles);
% UIWAIT makes phenocamimageprocessor wait for user response (see UIRESUME)
% uiwait(handles.figure1);
% --- Outputs from this function are returned to the command line.
function varargout = phenocamimageprocessor_OutputFcn(hObject, eventdata, handles)
% varargout cell array for returning output args (see VARARGOUT);
% hObject handle to figure
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% Get default command line output from handles structure
varargout{1} = handles.output;
% --- Executes on button press in setdirbutton.
function setdirbutton_Callback(hObject, eventdata, handles)
% hObject handle to setdirbutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
handles.dir_name = uigetdir(handles.basedir,'Select Image Directory');
if handles.dir_name == 0 % if no valid dir is set do nothing, else do...
else % akward construction... change
% list all jpeg files in valid directory
% and get the number of jpegs in the directory
handles.jpeg_files = dir(strcat(handles.dir_name,handles.file_split,'*.jpg'));
handles.nrjpegs = size(handles.jpeg_files,1);
% check if there are pictures in the folder if not display error
% else continue
if isempty(handles.jpeg_files)
errordlg('Contains no valid images, please select another directory')
else
% define containing matrices for year/month/day/hour/min variables
handles.year = zeros(handles.nrjpegs,1);
handles.month = zeros(handles.nrjpegs,1);
handles.day = zeros(handles.nrjpegs,1);
handles.hour = zeros(handles.nrjpegs,1);
handles.minutes = zeros(handles.nrjpegs,1);
% extract date/time values from filename using string manipulation
for i=1:handles.nrjpegs
parts = regexp(handles.jpeg_files(i,1).name,'_','split');
handles.year(i) = str2double(char(parts(2)));
handles.month(i) = str2double(char(parts(3)));
handles.day(i) = str2double(char(parts(4)));
time = char(parts(5));
handles.hour(i) = str2double(time(1:2));
handles.minutes(i) = str2double(time(3:4));
end
% calculate the range of hours of the images (min / max)
min_hour = min(handles.hour);
max_hour = max(handles.hour);
mean_hour = round((min_hour + max_hour) / 2);
AM = min_hour : (mean_hour-1);
PM = mean_hour : max_hour;
% set popup hour menu items using min max ranges as calculated above
set(handles.frompopup, 'String',AM);
set(handles.topopup, 'String',PM);
% unlock ROI button and nr ROI popup menu (you are now allowed to use these items)
set(handles.setroibutton,'Enable','on');
set(handles.popuproi,'Enable','on');
% calculate midday images
handles.midday_images = char(handles.jpeg_files.name);
handles.midday_images = handles.midday_images(handles.hour > 11 & handles.hour < 13,:);
% read random image and plot to axes 1
handles.current_image = imread(strcat(handles.dir_name,handles.file_split,handles.midday_images(1,:)));
set(handles.main_figure,'CurrentAxes',handles.axes1);
image(handles.current_image);
axis off;
% activate load roi menu item in the menu list
set(handles.menuloadroi,'Enable','on');
% update the listbox listing all files
set(handles.imagelistbox,'String',handles.midday_images);
end
end
% pass all handles objects to the root / figure
% if you don't do this the handles won't update
guidata(hObject, handles);
% --- Executes on button press in setroibutton.
function setroibutton_Callback(hObject, eventdata, handles)
% hObject handle to setroibutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% set focus to axes 1
set(handles.main_figure,'CurrentAxes',handles.axes1);
% hold is on for overplotting
% getline grabbed coordinates and polygons
hold on;
% get image size
[nrows, ncols, ncolors] = size(handles.current_image);
% define mask output size
% based upon image size and nroi
handles.mask = zeros(nrows,ncols,handles.nroi);
% for the number of nroi do
for i=1:handles.nroi;
% grab polygon coordinates and dump in structure
[handles.xcoordinates(:,i).poly,handles.ycoordinates(:,i).poly] = getline('closed');
% define binary mask using poly2mask
handles.mask(:,:,i) = poly2mask(handles.xcoordinates(:,i).poly,handles.ycoordinates(:,i).poly,nrows,ncols);
% get polygon center for plotting labels
handles.xcenter(:,i) = mean(handles.xcoordinates(:,i).poly);
handles.ycenter(:,i) = mean(handles.ycoordinates(:,i).poly);
% overplot the polygon (see hold on above)
% and text label
plot(handles.xcoordinates(:,i).poly,handles.ycoordinates(:,i).poly,char(handles.linetype(i,:)),'Color',handles.cmap(i),'LineWidth',2);
text(handles.xcenter(:,i), handles.ycenter(:,i), num2str(i),'Color',handles.cmap(i), 'FontWeight','Bold');
end
% activate clear ROI button
set(handles.clearroibutton,'Enable','on');
% activate from popup dialogues
set(handles.frompopup,'Enable','on');
% disable Set ROI button until reset
set(handles.setroibutton,'Enable','off');
% disable Set ROI button until reset
set(handles.setdirbutton,'Enable','off');
% disable Set ROI button until reset
set(handles.popuproi,'Enable','off');
% hold is off, will overplot with next plot command
hold off;
% set load/save ROI menu item
set(handles.menuloadroi,'Enable','off');
set(handles.menusaveroi,'Enable','on');
% update handles
guidata(hObject, handles);
% --- Executes on button press in clearroibutton.
function clearroibutton_Callback(hObject, eventdata, handles)
% hObject handle to clearroibutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% clear every previously set variable to reassign a new
% region of interest etc.
% delete content of mask
handles.mask = [];
% reset the handles.nroi to 1 / default
handles.nroi = 1;
set(handles.popuproi,'Value',1);
% delete content polygon coordinates
%clear handles.coordinates.X
handles.xcoordinates = rmfield(handles.xcoordinates,'poly');
handles.ycoordinates = rmfield(handles.ycoordinates,'poly');
handles.xcenter = [];
handles.ycenter = [];
% set current axes to axes 1 (top plot)
% and reprint current image without polygons
set(handles.main_figure,'CurrentAxes',handles.axes1);
image(handles.current_image);
axis off;
% deactivate popup / clear ROI / calculate buttons / etc
set(handles.clearroibutton,'Enable','off');
set(handles.frompopup,'Enable','off');
set(handles.topopup,'Enable','off');
set(handles.calculatebutton,'Enable','off');
set(handles.gcccheckbox,'Enable','off');
set(handles.gccsmoothcheckbox,'Enable','off');
set(handles.redcheckbox,'Enable','off');
set(handles.greencheckbox,'Enable','off');
set(handles.bluecheckbox,'Enable','off');
set(handles.gcccheckbox,'Value',0);
set(handles.gccsmoothcheckbox,'Value',0);
set(handles.redcheckbox,'Value',0);
set(handles.greencheckbox,'Value',0);
set(handles.bluecheckbox,'Value',0);
set(handles.checkboxROI1,'Value',0);
set(handles.checkboxROI2,'Value',0);
set(handles.checkboxROI3,'Value',0);
set(handles.checkboxROI1,'Enable','off');
set(handles.checkboxROI2,'Enable','off');
set(handles.checkboxROI3,'Enable','off');
set(handles.pushbuttonupdateplot,'Enable','Off');
set(handles.menusavetsdata,'Enable','off');
set(handles.menusaveroi,'Enable','off');
set(handles.setroibutton,'Enable','on');
set(handles.setdirbutton,'Enable','on');
set(handles.popuproi,'Enable','on');
set(handles.checkbox90th,'Enable','Off');
set(handles.checkboxmean,'Enable','Off');
set(handles.checkboxmedian,'Enable','Off');
set(handles.popupmenuwindowsize,'Enable','Off');
set(handles.darkthreshold,'Enable','off');
% clear results
handles.results = [];
% clear axes 2
set(handles.main_figure,'CurrentAxes',handles.axes2);
ylabel('');
cla(handles.axes2);
% return focus to axes 1
set(handles.main_figure,'CurrentAxes',handles.axes1);
% update handles
guidata(hObject, handles);
% --- Executes on selection change in imagelistbox.
function imagelistbox_Callback(hObject, eventdata, handles)
% hObject handle to imagelistbox (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 imagelistbox contents as cell array
% contents{get(hObject,'Value')} returns selected item from imagelistbox
% update preview image when selecting a new image in the listbox
% get selected image
selected_image = get(handles.imagelistbox,'Value');
% plot selected image
handles.current_image = imread(strcat(handles.dir_name,handles.file_split,handles.midday_images(selected_image,:)));
set(handles.main_figure,'CurrentAxes',handles.axes1);
hold on;
image(handles.current_image);
axis off;
% overplot the image with a polygon
% and text label
if isempty(handles.xcenter)
% dont try to plot polygons just refresh axes
else
for i=1:handles.nroi
plot(handles.xcoordinates(:,i).poly,handles.ycoordinates(:,i).poly,'Color',handles.cmap(i));
text(handles.xcenter(:,i), handles.ycenter(:,i), num2str(i), 'Color',handles.cmap(i), 'FontWeight','Bold');
end
end
hold off;
% update handles
guidata(hObject, handles);
% --- Executes during object creation, after setting all properties.
function imagelistbox_CreateFcn(hObject, eventdata, handles)
% hObject handle to imagelistbox (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles empty - handles not created until after all CreateFcns called
% Hint: listbox 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
% --- Executes on selection change in frompopup.
function frompopup_Callback(hObject, eventdata, handles)
% hObject handle to frompopup (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 frompopup contents as cell array
% contents{get(hObject,'Value')} returns selected item from frompopup
val = get(handles.frompopup,'Value'); % get value / location
str = get(handles.frompopup,'String'); % get all strings in popup
handles.start_time = str2double(strcat(str(val,1),str(val,2))); % concat strings and convert to double
% if set enable topopup
set(handles.topopup,'Enable','on');
% update handles
guidata(hObject, handles);
% --- Executes during object creation, after setting all properties.
function frompopup_CreateFcn(hObject, eventdata, handles)
% hObject handle to frompopup (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
% --- Executes on selection change in topopup.
function topopup_Callback(hObject, eventdata, handles)
% hObject handle to topopup (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 topopup contents as cell array
% contents{get(hObject,'Value')} returns selected item from topopup
val = get(handles.topopup,'Value'); % get value / location
str = get(handles.topopup,'String'); % get all strings in popup
handles.end_time = str2double(strcat(str(val,1),str(val,2))); % concat strings and convert to double
% activate calculation options
set(handles.checkbox90th,'Enable','On');
set(handles.checkbox90th,'Value',1);
set(handles.checkboxmean,'Enable','Off');
set(handles.checkboxmedian,'Enable','Off');
set(handles.popupmenuwindowsize,'Enable','On');
set(handles.calculatebutton,'Enable','On');
set(handles.darkthreshold,'Enable','on');
% update handles
guidata(hObject, handles);
% --- Executes during object creation, after setting all properties.
function topopup_CreateFcn(hObject, eventdata, handles)
% hObject handle to topopup (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
% --- Executes on button press in calculatebutton.
function calculatebutton_Callback(hObject, eventdata, handles)
% hObject handle to calculatebutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
if isempty(handles.results);
% make a list of subsetted jpegs to be processed
handles.subset_images = char(handles.jpeg_files.name);
handles.subset_images = handles.subset_images(handles.hour >= handles.start_time & handles.hour <= handles.end_time,:);
% get the length of this list
size_subset_images = size(handles.subset_images,1);
% subset year / month / day / hour / min
subset_year = handles.year(handles.hour >= handles.start_time & handles.hour <= handles.end_time,:);
subset_month = handles.month(handles.hour >= handles.start_time & handles.hour <= handles.end_time,:);
subset_day = handles.day(handles.hour >= handles.start_time & handles.hour <= handles.end_time,:);
subset_hour = handles.hour(handles.hour >= handles.start_time & handles.hour <= handles.end_time,:);
subset_min = handles.minutes(handles.hour >= handles.start_time & handles.hour <= handles.end_time,:);
% convert year / month / day / hour / min ... sec to matlab date
subset_year = unique(subset_year');
subset_month = subset_month';
subset_day = subset_day';
subset_hour = subset_hour';
subset_min = subset_min';
% calculate doy from year / month / ...
handles.subset_doy = date2jd(subset_year,subset_month,subset_day,subset_hour,subset_min);
handles.max_doy = max(unique(handles.subset_doy));
handles.min_doy = min(unique(handles.subset_doy));
% make a martrix to contain the results (length list, indices - 5)
handles.results = zeros(size_subset_images,6,handles.nroi);
% fill year column
handles.results(:,1,:) = subset_year;
% waitbar settings
h = waitbar(0,'Please wait...','Name','Processing Images...',...
'CreateCancelBtn',...
'setappdata(gcbf,''canceling'',1)');
for i=1:size_subset_images;
% this part needs vectorization as it slows down the whole
% calculation to much
% calculate gcc ./ mask and for every layer grab the mean
% Check for Cancel button press
if getappdata(h,'canceling') % grab cancel button hit
%delete(h); % delete wait bar handle
break % interupt the processing
end
% Report current estimate in the waitbar's message field
waitbar(i/size_subset_images);
% calculate DOY
handles.results(i,2,:) = date2jd(subset_year, subset_month(i), subset_day(i), subset_hour(i), subset_min(i));
% read in image
img = imread(strcat(handles.dir_name,handles.file_split,handles.subset_images(i,:)));
% split image in its components
red = img(:,:,1);
green = img(:,:,2);
blue = img(:,:,3);
% calculate green chromatic coordinates
for j=1:handles.nroi
mask = handles.mask(:,:,j);
handles.results(i,3,j) = mean(mean(red(mask == 1)));
handles.results(i,4,j) = mean(mean(green(mask == 1)));
handles.results(i,5,j) = mean(mean(blue(mask == 1)));
% calculate green chromatic coordinates
gcc = handles.results(i,4,j) ./ (handles.results(i,3,j) + handles.results(i,4,j) + handles.results(i,5,j));
% put gcc values in results
handles.results(i,6,j) = gcc;
end
end
% delete waitbar handle when done
delete(h);
end
% set window size
val = get(handles.popupmenuwindowsize,'Value'); % get value / location
str = get(handles.popupmenuwindowsize,'String'); % get all strings in popup
windowsize = floor(str2double(str(val,1)));
windowsize = floor(windowsize/2);
% smooth the data using the moving quantile method
l = round(handles.max_doy - windowsize);
% make matrix to dump smoothed results in
handles.gccsmooth = zeros(round(handles.max_doy),1,handles.nroi);
% set threshold (dark images)
threshold = 255*(handles.threshold/100);
% smooth time series
for i=1:handles.nroi;
DOY = round(handles.results(:,2,i));
for n=windowsize+1:l;
handles.gccsmooth(n,1,i) = n;
if windowsize == 0;
subset = handles.results(DOY == n & handles.results(:,3,i) > threshold & handles.results(:,4,i) > threshold & handles.results(:,5,i) > threshold,6,i);
else
subset = handles.results(DOY >= n-windowsize & DOY <= n+windowsize & handles.results(:,3,i) > threshold & handles.results(:,4,i) > threshold & handles.results(:,5,i) > threshold,6,i);
end
% 90th percentile
if get(handles.checkbox90th,'Value') == 1
handles.gccsmooth(n,2,i)=myquantile(subset,0.9);
end
% mean
if get(handles.checkboxmean,'Value') == 1
handles.gccsmooth(n,2,i)=nanmean(subset);
end
% median
if get(handles.checkboxmedian,'Value') == 1
handles.gccsmooth(n,2,i)=nanmedian(subset);
end
end
end
% make plotting options available
set(handles.gcccheckbox,'Enable','on');
set(handles.gccsmoothcheckbox,'Enable','on');
set(handles.redcheckbox,'Enable','off');
set(handles.greencheckbox,'Enable','off');
set(handles.bluecheckbox,'Enable','off');
% check the gcc box
set(handles.gcccheckbox,'Value',1);
set(handles.gccsmoothcheckbox,'Value',1);
set(handles.redcheckbox,'Value',0);
set(handles.greencheckbox,'Value',0);
set(handles.bluecheckbox,'Value',0);
% set ROI checkboxes
if handles.nroi == 1;
set(handles.checkboxROI1,'Value',1);
end
if handles.nroi == 2;
set(handles.checkboxROI1,'Enable','On');
set(handles.checkboxROI1,'Value',1);
set(handles.checkboxROI2,'Enable','On');
set(handles.checkboxROI2,'Value',1);
end
if handles.nroi == 3;
set(handles.checkboxROI1,'Enable','On');
set(handles.checkboxROI1,'Value',1);
set(handles.checkboxROI2,'Enable','On');
set(handles.checkboxROI2,'Value',1);
set(handles.checkboxROI3,'Enable','On');
set(handles.checkboxROI3,'Value',1);
end
% clear axes 2
set(handles.main_figure,'CurrentAxes',handles.axes2);
ylabel('');
xlabel('Day of Year (DOY)');
cla(handles.axes2);
hold on;
% set axes parameters
set(handles.main_figure,'CurrentAxes',handles.axes2);
% remove 0's
handles.gccsmooth = handles.gccsmooth(handles.gccsmooth(:,1,1) ~= 0,:,:);
% trim data (every 3 samples)
handles.gccsmooth = handles.gccsmooth(1:(windowsize*2)+1:end,:,:);
%handles.gccsubset = handles.results(handles.results(:,3,i) > threshold & handles.results(:,4,i) > threshold & handles.results(:,5,i) > threshold,6,i);
%handles.doygccsubset = handles.results(handles.results(:,3,i) > threshold & handles.results(:,4,i) > threshold & handles.results(:,5,i) > threshold,2,i);
% plot gccsmooth graphs for ROI 1
for i=1:handles.nroi;
handles.gccsubset = handles.results(handles.results(:,3,i) > threshold & handles.results(:,4,i) > threshold & handles.results(:,5,i) > threshold,6,i);
handles.doygccsubset = handles.results(handles.results(:,3,i) > threshold & handles.results(:,4,i) > threshold & handles.results(:,5,i) > threshold,2,i);
ylabel('GCC');
plot(handles.gccsmooth(:,1,i),handles.gccsmooth(:,2,i),'-','Color',handles.cmap(i),'LineWidth',2);
plot(handles.doygccsubset,handles.gccsubset,'.','Color',handles.cmap(i));
end
hold off;
% enable save menu items
set(handles.menusavetsdata,'Enable','On');
% set update button
set(handles.pushbuttonupdateplot,'Enable','On');
% update handles
guidata(hObject, handles);
% --- Executes on button press in gcccheckbox.
function gcccheckbox_Callback(hObject, eventdata, handles)
% hObject handle to gcccheckbox (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 gcccheckbox
% get checkbox status (both gcc's)
gccstatus = get(handles.gcccheckbox,'Value');
gccsmoothstatus = get(handles.gccsmoothcheckbox,'Value');
if gccstatus + gccsmoothstatus >= 1;
% clear rgb checkboxes (should be mutually exclusive)
set(handles.redcheckbox,'Value',0);
set(handles.greencheckbox,'Value',0);
set(handles.bluecheckbox,'Value',0);
set(handles.redcheckbox,'Enable','off');
set(handles.greencheckbox,'Enable','off');
set(handles.bluecheckbox,'Enable','off');
else
hold off;
ylabel('');
cla(handles.axes2);
set(handles.redcheckbox,'Enable','on');
set(handles.greencheckbox,'Enable','on');
set(handles.bluecheckbox,'Enable','on');
end
% --- Executes on button press in gccsmoothcheckbox.
function gccsmoothcheckbox_Callback(hObject, eventdata, handles)
% hObject handle to gccsmoothcheckbox (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 gccsmoothcheckbox
% get checkbox status (both gcc's)
gccstatus = get(handles.gcccheckbox,'Value');
gccsmoothstatus = get(handles.gccsmoothcheckbox,'Value');
if gccstatus + gccsmoothstatus >= 1;
% clear rgb checkboxes (should be mutually exclusive)
set(handles.redcheckbox,'Value',0);
set(handles.greencheckbox,'Value',0);
set(handles.bluecheckbox,'Value',0);
set(handles.redcheckbox,'Enable','off');
set(handles.greencheckbox,'Enable','off');
set(handles.bluecheckbox,'Enable','off');
else
hold off;
ylabel('');
cla(handles.axes2);
set(handles.redcheckbox,'Enable','on');
set(handles.greencheckbox,'Enable','on');
set(handles.bluecheckbox,'Enable','on');
end
% --- Executes on button press in redcheckbox.
function redcheckbox_Callback(hObject, eventdata, handles)
% hObject handle to redcheckbox (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 redcheckbox
% overplot if it is another RGB colour (see below)
%hold on;
% set plotting axes
%set(handles.main_figure,'CurrentAxes',handles.axes2);
% get checkbox status (RGB)
redstatus = get(handles.redcheckbox,'Value');
greenstatus = get(handles.greencheckbox,'Value');
bluestatus = get(handles.bluecheckbox,'Value');
if redstatus + greenstatus + bluestatus >= 1;
% clear axes and plot everything again
cla(handles.axes2) ;
% clear rgb checkboxes (should be mutually exclusive)
set(handles.gcccheckbox,'Value',0);
set(handles.gccsmoothcheckbox,'Value',0);
set(handles.gcccheckbox,'Enable','off');
set(handles.gccsmoothcheckbox,'Enable','off');
else
hold off;
ylabel('');
cla(handles.axes2);
set(handles.gcccheckbox,'Enable','on');
set(handles.gccsmoothcheckbox,'Enable','on');
end
% --- Executes on button press in greencheckbox.
function greencheckbox_Callback(hObject, eventdata, handles)
% hObject handle to greencheckbox (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 greencheckbox
% set plotting axes
set(handles.main_figure,'CurrentAxes',handles.axes2);
% get checkbox status (RGB)
redstatus = get(handles.redcheckbox,'Value');
greenstatus = get(handles.greencheckbox,'Value');
bluestatus = get(handles.bluecheckbox,'Value');
if redstatus + greenstatus + bluestatus >= 1
% clear rgb checkboxes (should be mutually exclusive)
set(handles.gcccheckbox,'Value',0);
set(handles.gccsmoothcheckbox,'Value',0);
set(handles.gcccheckbox,'Enable','off');
set(handles.gccsmoothcheckbox,'Enable','off');
else
hold off;
ylabel('');
cla(handles.axes2);
set(handles.gcccheckbox,'Enable','on');
set(handles.gccsmoothcheckbox,'Enable','on');
end
% --- Executes on button press in bluecheckbox.
function bluecheckbox_Callback(hObject, eventdata, handles)
% hObject handle to bluecheckbox (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 bluecheckbox
% overplot if it is another RGB colour (see below)
%hold on;
% set plotting axes
set(handles.main_figure,'CurrentAxes',handles.axes2);
% get checkbox status (RGB)
redstatus = get(handles.redcheckbox,'Value');
greenstatus = get(handles.greencheckbox,'Value');
bluestatus = get(handles.bluecheckbox,'Value');
if redstatus + greenstatus + bluestatus >= 1;
% clear rgb checkboxes (should be mutually exclusive)
set(handles.gcccheckbox,'Value',0);
set(handles.gccsmoothcheckbox,'Value',0);
set(handles.gcccheckbox,'Enable','off');
set(handles.gccsmoothcheckbox,'Enable','off');
else
hold off;
ylabel('');
cla(handles.axes2);
set(handles.gcccheckbox,'Enable','on');
set(handles.gccsmoothcheckbox,'Enable','on');
end
% --- If Enable == 'on', executes on mouse press in 5 pixel border.
% --- Otherwise, executes on mouse press in 5 pixel border or over setroibutton.
function setroibutton_ButtonDownFcn(hObject, eventdata, handles)
% hObject handle to setroibutton (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function menufile_Callback(hObject, eventdata, handles)
% hObject handle to menufile (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
% --------------------------------------------------------------------
function menuloadroi_Callback(hObject, eventdata, handles)
% hObject handle to menuloadroi (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[file,path] = uigetfile('*.mat','Load ROI...');
filename = strcat(path,'/',file);
% save data
mask = load(filename);
mask = int8(mask.roiimg);
% hold is on for overplotting
% getline grabbed coordinates and polygons
hold on;
% get image size / nr ROI
handles.nroi = size(mask,3);
% for the number of nroi do
for i=1:handles.nroi;
% get polygon from mask
loc = bwboundaries(mask(:,:,i));
loc = cell2mat(loc);
% grab polygon coordinates and dump in structure
handles.xcoordinates(:,i).poly = loc(:,2);
handles.ycoordinates(:,i).poly = loc(:,1);
% get polygon center for plotting labels
handles.xcenter(:,i) = mean(handles.xcoordinates(:,i).poly);
handles.ycenter(:,i) = mean(handles.ycoordinates(:,i).poly);
% overplot the polygon (see hold on above)
% and text label
plot(handles.xcoordinates(:,i).poly,handles.ycoordinates(:,i).poly,'Color',handles.cmap(i));
text(handles.xcenter(:,i), handles.ycenter(:,i), num2str(i), 'Color',handles.cmap(i), 'FontWeight','Bold');
end
% activate clear ROI button
set(handles.clearroibutton,'Enable','on');
% activate from popup dialogues
set(handles.frompopup,'Enable','on');
% disable Set ROI button until reset
set(handles.setroibutton,'Enable','off');
% disable Set ROI button until reset
set(handles.setdirbutton,'Enable','off');
% disable Set ROI button until reset
set(handles.popuproi,'Enable','off');
hold off;
% pass all handles objects to the root / figure
% if you don't do this the handles won't update
guidata(hObject, handles);
% --------------------------------------------------------------------
function menusaveroi_Callback(hObject, eventdata, handles)
% hObject handle to menusaveroi (see GCBO)
% eventdata reserved - to be defined in a future version of MATLAB
% handles structure with handles and user data (see GUIDATA)
[file,path] = uiputfile('*.mat','Save ROI...');
filename = strcat(path,'/',file);
roiimg = handles.mask;
% save data