-
Notifications
You must be signed in to change notification settings - Fork 0
/
SLMeasure_exported.m
1680 lines (1443 loc) · 72.2 KB
/
SLMeasure_exported.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
classdef SLMeasure_exported < matlab.apps.AppBase
% Properties that correspond to app components
properties (Access = public)
SLMeasureUIFigure matlab.ui.Figure
FileMenu matlab.ui.container.Menu
SaveMeasurementMenu matlab.ui.container.Menu
LoadMeasurementMenu matlab.ui.container.Menu
CameraPanel matlab.ui.container.Panel
ResolutionPanel matlab.ui.container.Panel
ResolutionDropDown matlab.ui.control.DropDown
ColorAdjustmentPanel matlab.ui.container.Panel
DefaultsButton matlab.ui.control.Button
GammaEditField matlab.ui.control.NumericEditField
GammaEditFieldLabel matlab.ui.control.Label
GammaSlider matlab.ui.control.Slider
ContrastEditField matlab.ui.control.NumericEditField
ContrastEditFieldLabel matlab.ui.control.Label
ContrastSlider matlab.ui.control.Slider
BrightnessEditField matlab.ui.control.NumericEditField
BrightnessEditFieldLabel matlab.ui.control.Label
BrightnessSlider matlab.ui.control.Slider
SaturationEditField matlab.ui.control.NumericEditField
SaturationEditFieldLabel matlab.ui.control.Label
SaturationSlider matlab.ui.control.Slider
HueSlider matlab.ui.control.Slider
HueEditField matlab.ui.control.NumericEditField
HueEditFieldLabel matlab.ui.control.Label
WhiteBalancePanel matlab.ui.container.Panel
WBDefaultsButton matlab.ui.control.Button
WhiteBalanceButton matlab.ui.control.Button
TintSlider matlab.ui.control.Slider
TintEditField matlab.ui.control.NumericEditField
TintEditFieldLabel matlab.ui.control.Label
ColorTempSlider matlab.ui.control.Slider
ColorTemperatureEditField matlab.ui.control.NumericEditField
ColorTemperatureEditFieldLabel matlab.ui.control.Label
ExposurePanel matlab.ui.container.Panel
ExposureSlider matlab.ui.control.Slider
ExposureTimemsEditField matlab.ui.control.NumericEditField
ExposureTimemsEditFieldLabel matlab.ui.control.Label
AutoExposureCheckBox matlab.ui.control.CheckBox
SnapshotRecordPanel matlab.ui.container.Panel
SaveImageButton matlab.ui.control.Button
DeviceList matlab.ui.control.ListBox
DeviceListPanel matlab.ui.container.Panel
LiveExperimentModeButton matlab.ui.control.StateButton
LiveMeasurementModeButton matlab.ui.control.StateButton
SarcomereLengthCalculationPanel matlab.ui.container.Panel
ExportSLTableButton matlab.ui.control.Button
UITable matlab.ui.control.Table
exp_axis matlab.ui.control.UIAxes
ac_axis matlab.ui.control.UIAxes
fft_axis matlab.ui.control.UIAxes
BrightfieldPanel matlab.ui.container.Panel
MicroscopeCalibrationDropDown matlab.ui.control.DropDown
MicroscopeCalibrationDropDownLabel matlab.ui.control.Label
BoxSelectionDropDown matlab.ui.control.DropDown
BoxSelectionDropDownLabel matlab.ui.control.Label
NewBoxButton matlab.ui.control.Button
LoadImageButton matlab.ui.control.Button
ROIRowSelectSpinner matlab.ui.control.Spinner
ROIRowSelectSpinnerLabel matlab.ui.control.Label
ROIWidthpxEditField matlab.ui.control.NumericEditField
ROIWidthpxEditFieldLabel matlab.ui.control.Label
ROIHeightpxEditField matlab.ui.control.NumericEditField
ROIHeightpxEditFieldLabel matlab.ui.control.Label
CalibrationFilePathEditField matlab.ui.control.EditField
CalibrationFilePathEditFieldLabel matlab.ui.control.Label
LoadMicroscopeCalibrationFileButton matlab.ui.control.Button
calculation_axis matlab.ui.control.UIAxes
px_intensity matlab.ui.control.UIAxes
inset_axis matlab.ui.control.UIAxes
image_axis matlab.ui.control.UIAxes
end
properties (Access = private)
end
properties (Access = public)
image_file
roi_pos
roi_rec
px_cal
roi_box
profile
sig
background
sl_data
P2
lags
X_acf
wavelength
y_fit
r_squared
mean_X_acf
mean_P2
live_image = []
ToupcamData
Devices
live_measurement = 0;
live_experiment = 0;
boxes
live_fig_handle
time
end
methods (Access = private)
function ClearDisplay(app)
cla(app.image_axis)
cla(app.inset_axis)
cla(app.px_intensity)
cla(app.calculation_axis)
cla(app.fft_axis)
cla(app.ac_axis)
app.ROIHeightpxEditField.Value = 0;
app.ROIWidthpxEditField.Value = 0;
end
end
methods (Access = public)
function SarcomereLength(app)
cla(app.px_intensity);
cla(app.inset_axis);
cla(app.calculation_axis);
cla(app.ac_axis);
cla(app.fft_axis);
app.profile = [];
app.roi_box = [];
box_no = str2num(app.BoxSelectionDropDown.Value);
% Box Resize if neccessary
% n = numel(app.sl_data.box_handle);
% for i=1:n
% p(i,1:4) = app.sl_data.box_handle(i).Position;
% end
% w = p(box_no,3);
% h = p(box_no,4);
% if ((w~=app.sl_data.old_width)|(h~=app.sl_data.old_height))
% for i=1:n
% p(i,3) = w;
% p(i,4) = h;
% app.sl_data.box_handle(i).Position = p(i,:);
% end
% end
if app.live_measurement == 1 || ...
(app.live_measurement == 2 && ...
~isfield(app.sl_data,'image_file_string'))
app.sl_data.image_file = [];
app.sl_data.image_file = app.live_image;
end
app.roi_box = imcrop(app.sl_data.image_file, ...
app.sl_data.box_handle(box_no).Position);
[app.live_fig_handle] = center_image_with_preserved_aspect_ratio( ...
app.roi_box, ...
app.inset_axis);
[m,n] = size(app.roi_box);
app.ROIHeightpxEditField.Value = m;
app.ROIWidthpxEditField.Value = n;
app.ROIRowSelectSpinner.Limits = [1,m];
p = app.sl_data.box_handle(box_no).Position;
set(app.sl_data.box_label(box_no),'String',sprintf('%.0f',box_no));
set(app.sl_data.box_label(box_no), ...
'Position',[p(1)+p(3)+20 ...
p(2)-30]);
app.profile = [];
app.sig = [];
sl_fft = [];
sl_acf = [];
app.P2 = [];
app.X_acf = [];
app.wavelength = [];
app.lags = [];
app.y_fit = [];
app.r_squared = [];
if (ndims(app.roi_box)==3)
app.roi_box = rgb2gray(app.roi_box);
end
for ct = 1 : size(app.roi_box,1)
app.profile(ct,:) = app.roi_box(ct,:);
app.sig(ct,:) = (diff(app.profile(ct,:)));
SLFFT(app,ct);
SLAutoCorr(app,ct);
end
app.mean_X_acf = mean(app.X_acf,1);
app.mean_P2 = mean(app.P2,1);
[lambda,fit_parameters,app.r_squared,app.y_fit] = fit_damped_sine_wave('y_data',app.mean_X_acf,...
'x_data',app.lags(1,:), 'min_x_index_spacing',5);
if app.sl_data.calibration.px_cal ~=0
app.sl_data.sarcomere_length_acf(box_no) = lambda * app.sl_data.calibration.px_cal;
app.wavelength = lambda;
else
app.sl_data.sarcomere_length_acf(box_no) = 0;
end
[peaks, locs] = findpeaks(app.mean_P2);
[max_mag,max_ix] = max(peaks);
F_p = 0:numel(app.mean_P2)-1;
if app.sl_data.calibration.px_cal ~=0
app.sl_data.sarcomere_length_fft(box_no) = ...
size(app.sig,2)*app.sl_data.calibration.px_cal/F_p(locs(max_ix));
else
app.sl_data.sarcomere_length_fft(box_no) = 0;
end
UpdateDisplay(app)
end
function SLAutoCorr(app,ct)
x = app.sig(ct,:);
[app.X_acf(ct,:),app.lags(ct,:)] = autocorr(x,NumLags=round(numel(x)/2));
if ct == inf
draw_graph = 1;
else
draw_graph = 0;
end
end
function SLFFT(app,ct)
x = app.sig(ct,:);
X = fft(x);
L = numel(X);
app.P2(ct,:) = abs(X/L);
end
function UpdateTable(app)
for i = 1:numel(app.sl_data.sarcomere_length_fft)
s.box_no{i,1} = sprintf('%i',i);
s.fft_sl(i,1) = app.sl_data.sarcomere_length_fft(i);
s.acf_sl(i,1) = app.sl_data.sarcomere_length_acf(i);
end
s.box_no{end+1,1} = 'Average';
s.fft_sl(end+1,1) = mean(app.sl_data.sarcomere_length_fft);
s.acf_sl(end+1,1) = mean(app.sl_data.sarcomere_length_acf);
t = struct2table(s);
app.UITable.Data = t;
end
function UpdateDisplay(app)
row_no = app.ROIRowSelectSpinner.Value;
hold(app.px_intensity,"on")
m = app.ROIRowSelectSpinner.Limits(2);
cm = lines(m);
for j = 1 : m
plot(app.px_intensity,app.roi_box(j,:), ...
'LineStyle',':',"LineWidth",0.25,'Color',cm(j,:));
end
plot(app.px_intensity,app.roi_box(row_no,:), ...
'LineStyle','-',"LineWidth",2,'Color',cm(row_no,:));
hold(app.calculation_axis,"on")
plot(app.calculation_axis, app.sig(row_no,:),'Color',cm(row_no,:),"LineWidth",2)
xlim(app.px_intensity, [1 numel(app.profile(row_no,:))])
xlim(app.calculation_axis, [1 numel(app.profile(row_no,:))])
hold(app.fft_axis,"on")
F_p = 0:numel(app.mean_P2)-1;
[peaks, locs] = findpeaks(app.mean_P2);
[max_mag,max_ix] = max(peaks);
fft_ix = (1:numel(app.mean_P2)-1)/(numel(app.mean_P2));
fft_ix = [0 fft_ix];
plot(app.fft_axis,fft_ix,app.mean_P2,'Color','b','LineWidth',2)
plot(app.fft_axis,fft_ix(locs(max_ix)),app.mean_P2(locs(max_ix)),'pentagram', ...
'MarkerSize',15,"Color",'k','MarkerFaceColor','k')
xlim(app.fft_axis,[0 fft_ix(end)])
ylim(app.fft_axis,[0 max(max(app.mean_P2))+1])
t = sprintf('Peak is at %.3f px^{-1}',fft_ix(locs(max_ix)));
text(app.fft_axis,fft_ix(locs(max_ix))+0.25,max(peaks),t)
plot(app.ac_axis,app.lags(1,:),app.mean_X_acf,'o','LineWidth',2,'color','r')
hold(app.ac_axis,"on")
plot(app.ac_axis,app.lags(1,:),app.y_fit,'color','k','LineWidth',1.5)
if ~isempty(app.wavelength)
t1 = sprintf('Wavelength = %.3f px',app.wavelength);
t2 = sprintf('R-squared = %.3f', app.r_squared);
text(app.ac_axis,round(0.7*numel(app.mean_X_acf))-10,0.6,t1)
text(app.ac_axis,round(0.7*numel(app.mean_X_acf))-10,0.8,t2)
end
% xlim(app.ac_axis,[0 round(numel(x)/2)])
ylim(app.ac_axis,[-1 1])
end
function LiveSarcomereLength(app,m)
app.sl_data.image_file = app.live_image;
for box_no = 1 : numel(app.sl_data.box_handle)
app.roi_box = [];
app.roi_box = imcrop(app.sl_data.image_file, ...
app.sl_data.box_handle(box_no).Position);
app.profile = [];
app.sig = [];
sl_fft = [];
sl_acf = [];
app.P2 = [];
app.X_acf = [];
app.wavelength = [];
app.lags = [];
app.y_fit = [];
app.r_squared = [];
if (ndims(app.roi_box)==3)
app.roi_box = rgb2gray(app.roi_box);
end
for ct = 1 : size(app.roi_box,1)
app.profile(ct,:) = app.roi_box(ct,:);
app.sig(ct,:) = (diff(app.profile(ct,:)));
SLFFT(app,ct);
% SLAutoCorr(app,ct);
end
app.mean_X_acf = mean(app.X_acf,1);
app.mean_P2 = mean(app.P2,1);
[lambda,fit_parameters,app.r_squared,app.y_fit] = fit_damped_sine_wave('y_data',app.mean_X_acf,...
'x_data',app.lags(1,:), 'min_x_index_spacing',5);
if app.sl_data.calibration.px_cal ~=0
app.sl_data.live.sarcomere_length_acf(m,box_no) = lambda * app.sl_data.calibration.px_cal;
app.wavelength = lambda;
else
app.sl_data.live.sarcomere_length_acf(m,box_no) = 0;
end
[peaks, locs] = findpeaks(app.mean_P2);
[max_mag,max_ix] = max(peaks);
F_p = 0:numel(app.mean_P2)-1;
if app.sl_data.calibration.px_cal ~=0
app.sl_data.live.sarcomere_length_fft(m,box_no) = ...
size(app.sig,2)*app.sl_data.calibration.px_cal/F_p(locs(max_ix));
else
app.sl_data.live.sarcomere_length_fft(m,box_no) = 0;
end
app.sl_data.live.sarcomere_length_fft(m,box_no) = 4 + rand(1,1);
app.sl_data.live.sarcomere_length_acf(m,box_no) = 2 + rand(1,1);
end
cm = parula(box_no);
hold(app.exp_axis,"on")
for i = 1:box_no
plot(app.exp_axis,1:m,app.sl_data.live.sarcomere_length_fft(1:m,i),'Color',cm(i,:),"Marker","pentagram",'LineWidth',2)
plot(app.exp_axis,1:m,app.sl_data.live.sarcomere_length_acf(1:m,i),'Color',cm(i,:),"Marker","o",'LineWidth',2)
end
end
end
% Callbacks that handle component events
methods (Access = private)
% Code that executes after component creation
function startupFcn(app)
colormap(app.SLMeasureUIFigure, 'gray');
addpath(genpath('utilities'))
movegui(app.SLMeasureUIFigure,'center')
addpath(genpath('camera'))
disableDefaultInteractivity(app.image_axis)
ipath = ['-I' fullfile(cd,'camera')];
f = fullfile(cd,'camera','mexToupcam.cpp');
lib = fullfile(cd,'camera','toupcam.lib');
% mex mexToupcam.cpp -ltoupcam
mex('-v','-R2017b',ipath,f,lib)
global bStop;
bStop = 0;
[devN, devList] = mexToupcam;
if devN == 0
app.DeviceList.Items = {'No Device'};
else
app.Devices = devList;
for i = 1 : numel(devList)
app.DeviceList.Items{i} = devList(i).name;
end
if numel(devList) == 1
app.DeviceList.Value = app.DeviceList.Items;
end
end
app.ResolutionDropDown.Value;
end
% Button pushed function: LoadImageButton
function LoadImageButtonPushed(app, event)
filterspec = {'*.tif;*.png','All Image Files'};
[file_string,path_string] = uigetfile2(filterspec);
if (path_string~=0)
ClearDisplay(app)
if isfield(app.sl_data,'calibration')
temp_cal = app.sl_data.calibration;
else
temp_cal = [];
end
app.sl_data = [];
if ~isempty(temp_cal)
app.sl_data.calibration = temp_cal;
end
app.UITable.Data = [];
app.sl_data.image_file_string = fullfile(path_string,file_string);
im = imread(app.sl_data.image_file_string);
if (ndims(im)==3)
im = rgb2gray(im);
end
[fig_handle] = center_image_with_preserved_aspect_ratio( ...
im, ...
app.image_axis);
app.sl_data.image_file = im;
end
end
% Button pushed function: NewBoxButton
function NewBoxButtonPushed(app, event)
if (~isfield(app.sl_data,'box_handle'))
n=1;
app.sl_data.box_handle(n) = drawrectangle(app.image_axis);
p = app.sl_data.box_handle(n).Position;
app.sl_data.old_width = p(3);
app.sl_data.old_height = p(4);
else
n = 1 + numel(app.sl_data.box_handle);
p = app.sl_data.box_handle(n-1).Position;
app.sl_data.box_handle(n) = images.roi.Rectangle(app.image_axis, ...
'Position',p + [150,0,0,0]);
for i=1:(n-1)
app.sl_data.box_handle(i).InteractionsAllowed = 'none';
end
end
app.sl_data.box_handle(n).Color = [0 1 0];
app.sl_data.box_handle(n).FaceAlpha = 0;
for i=1:(n-1)
app.sl_data.box_handle(i).Color = [1 0 0];
end
app.sl_data.box_label(n) = text(app.image_axis, ...
p(1)+p(3)+20,p(2)-30,sprintf('%.0f',n),'FontWeight',"bold","FontSize",18,"Color",'k');
app.roi_pos = get(app.roi_rec, 'Position');
% app.px_cal = app.MicroscopeCalibrationDropDown.Value;
for i=1:n
control_strings{i}=sprintf('%.0f',i);
end
app.BoxSelectionDropDown.Items = control_strings;
app.BoxSelectionDropDown.Value = control_strings{n};
SarcomereLength(app)
% app.boxes(n) = app.sl_data.box_handle(n);
UpdateTable(app);
addlistener(app.sl_data.box_handle(n),"MovingROI",@(src,evt) UpdateSL(evt));
function UpdateSL(evt)
box_no = str2num(app.BoxSelectionDropDown.Value);
app.ROIHeightpxEditField.Value = app.sl_data.box_handle(box_no).Position(3);
app.ROIWidthpxEditField.Value = app.sl_data.box_handle(box_no).Position(4);
cla(app.px_intensity)
cla(app.calculation_axis)
cla(app.fft_axis)
cla(app.ac_axis)
SarcomereLength(app)
UpdateTable(app);
end
end
% Value changed function: MicroscopeCalibrationDropDown
function MicroscopeCalibrationumpxEditFieldValueChanged(app, event)
app.sl_data.calibration.cal_ix = find(strcmp(app.MicroscopeCalibrationDropDown.Value,app.MicroscopeCalibrationDropDown.Items));
app.sl_data.calibration.px_cal = app.sl_data.calibration.table.Calibration(app.sl_data.calibration.cal_ix);
cur_box = app.BoxSelectionDropDown.Value;
if isfield(app.sl_data,'box_handle')
for i = 1 : numel(app.sl_data.box_handle)
app.BoxSelectionDropDown.Value = num2str(i);
SarcomereLength(app)
UpdateTable(app)
end
app.BoxSelectionDropDown.Value = cur_box;
app.BoxSelectionDropDownValueChanged;
end
end
% Value changed function: BoxSelectionDropDown
function BoxSelectionDropDownValueChanged(app, event)
selected_box = str2num(app.BoxSelectionDropDown.Value);
control_strings = app.BoxSelectionDropDown.Items;
n = numel(control_strings);
for i=1:n
if (i~=selected_box)
app.sl_data.box_handle(i).Color = [1 0 0];
app.sl_data.box_handle(i).InteractionsAllowed = 'none';
else
app.sl_data.box_handle(i).Color = [0 1 0];
app.sl_data.box_handle(i).InteractionsAllowed = 'all';
end
end
SarcomereLength(app)
UpdateTable(app)
end
% Value changed function: ROIRowSelectSpinner
function ROIRowSelectSpinnerValueChanged(app, event)
value = app.ROIRowSelectSpinner.Value;
cla(app.ac_axis)
cla(app.fft_axis)
cla(app.calculation_axis)
cla(app.px_intensity)
cla(app.ac_axis)
UpdateDisplay(app)
end
% Button pushed function: ExportSLTableButton
function ExportSLTableButtonPushed(app, event)
[file_string,path_string] = uiputfile2( ...
{'*.xlsx','Excel file'},'Select file name for SL Table');
output_file = fullfile(path_string,file_string);
try
delete(output_file);
end
writetable(app.UITable.Data,output_file,'Sheet','Summary')
end
% Menu selected function: SaveMeasurementMenu
function SaveMeasurementMenuSelected(app, event)
if ~isfield(app.sl_data,'box_handle')
warndlg('The measurement boxes are not available.')
return
end
[file_string,path_string] = uiputfile2( ...
{'*.sl','SL measurement file'},'Select file to save measurement');
for i=1:numel(app.sl_data.box_handle)
app.sl_data.box_position(i,:) = app.sl_data.box_handle(i).Position;
end
if isfield(app.sl_data,'image_file_string')
save_data.image_file_string = app.sl_data.image_file_string;
end
save_data.im_data = app.sl_data.image_file;
save_data.box_position = app.sl_data.box_position;
save_data.calibration = app.sl_data.calibration;
if isfield(app.sl_data,'live')
save_data.live.sarcomere_length_fft = app.sl_data.live.sarcomere_length_fft;
save_data.live.sarcomere_length_acf = app.sl_data.live.sarcomere_length_acf;
end
if (path_string~=0)
save(fullfile(path_string,file_string),'save_data');
end
end
% Menu selected function: LoadMeasurementMenu
function LoadMeasurementMenuSelected(app, event)
[file_string,path_string] = uigetfile2( ...
{'*.sl','SL measurement file'},'Select file to load measurement');
if (path_string~=0)
ClearDisplay(app)
if (isfield(app.sl_data,'box_handle'))
n = numel(app.sl_data.box_handle);
for i=1:n
delete(app.sl_data.box_handle(i));
end
end
temp = load(fullfile(path_string,file_string),'-mat','save_data');
save_data = temp.save_data;
app.sl_data = [];
if isfield(save_data,'image_file_string')
app.sl_data.image_file_string = save_data.image_file_string;
end
app.sl_data.image_file = save_data.im_data;
app.sl_data.calibration = save_data.calibration;
for i = 1 : size(app.sl_data.calibration.table,1)
name = sprintf('Obj. %s: %.4f um/px',app.sl_data.calibration.table.Objective{i}, app.sl_data.calibration.table.Calibration(i));
app.MicroscopeCalibrationDropDown.Items{i} = name;
end
app.CalibrationFilePathEditField.Value = app.sl_data.calibration.file_name;
app.MicroscopeCalibrationDropDown.Value = app.MicroscopeCalibrationDropDown.Items{app.sl_data.calibration.cal_ix};
[fig_handle] = center_image_with_preserved_aspect_ratio( ...
app.sl_data.image_file, ...
app.image_axis);
control_strings = [];
for i=1:size(save_data.box_position,1)
app.sl_data.box_handle(i) = images.roi.Rectangle(app.image_axis, ...
'Position',save_data.box_position(i,:));
control_strings{i} = sprintf('%.0f',i);
end
app.BoxSelectionDropDown.Items = control_strings;
app.BoxSelectionDropDown.Value = control_strings{1};
for i=1:size(save_data.box_position,1)
app.sl_data.box_handle(i).FaceAlpha = 0;
if (i~=1)
app.sl_data.box_handle(i).Color = [1 0 0];
app.sl_data.box_handle(i).InteractionsAllowed = 'none';
else
app.sl_data.box_handle(i).Color = [0 1 0];
app.sl_data.box_handle(i).InteractionsAllowed = 'all';
end
p = app.sl_data.box_handle(i).Position;
app.sl_data.box_label(i) = text(p(1)+p(3),p(2)-30,sprintf('%.0f',i), ...
'Parent',app.image_axis,'FontWeight',"bold","FontSize",18,"Color",'k');
app.sl_data.old_width = p(3);
app.sl_data.old_height = p(4);
i=i;
addlistener(app.sl_data.box_handle(i),"MovingROI",@(src,evt) UpdateSL2(evt));
end
for i = numel(app.sl_data.box_handle) :-1: 1
app.BoxSelectionDropDown.Value = num2str(i);
SarcomereLength(app)
UpdateDisplay(app)
UpdateTable(app);
end
if isfield(save_data,'live')
app.sl_data.live.sarcomere_length_fft = save_data.live.sarcomere_length_fft;
app.sl_data.live.sarcomere_length_acf = save_data.live.sarcomere_length_acf;
cm = parula(numel(app.sl_data.box_handle));
hold(app.exp_axis,"on")
for i = 1:numel(app.sl_data.box_handle)
m = numel(app.sl_data.live.sarcomere_length_fft(:,i));
plot(app.exp_axis,1:m,app.sl_data.live.sarcomere_length_fft(1:m,i),'Color',cm(i,:),"Marker","pentagram",'LineWidth',2)
plot(app.exp_axis,1:m,app.sl_data.live.sarcomere_length_acf(1:m,i),'Color',cm(i,:),"Marker","o",'LineWidth',2)
end
end
end
function UpdateSL2(evt)
box_no = str2num(app.BoxSelectionDropDown.Value);
app.ROIHeightpxEditField.Value = app.sl_data.box_handle(box_no).Position(3);
app.ROIWidthpxEditField.Value = app.sl_data.box_handle(box_no).Position(4);
cla(app.px_intensity)
cla(app.calculation_axis)
cla(app.fft_axis)
cla(app.ac_axis)
SarcomereLength(app)
UpdateTable(app);
end
end
% Button pushed function:
% LoadMicroscopeCalibrationFileButton
function LoadMicroscopeCalibrationFileButtonPushed(app, event)
filterspec = {'*.xlsx;*.xls','All Excel Files'};
[file_string,path_string] = uigetfile2(filterspec);
if (path_string~=0)
app.sl_data.calibration = [];
app.sl_data.calibration.file_name = [];
app.sl_data.calibration.file_name = fullfile(path_string,file_string);
app.CalibrationFilePathEditField.Value = app.sl_data.calibration.file_name;
app.sl_data.calibration.table = readtable(app.sl_data.calibration.file_name,'Sheet','Summary');
for i = 1 : size(app.sl_data.calibration.table,1)
name = sprintf('Obj. %s: %.4f um/px',app.sl_data.calibration.table.Objective{i}, app.sl_data.calibration.table.Calibration(i));
app.MicroscopeCalibrationDropDown.Items{i} = name;
end
app.sl_data.calibration.px_cal = app.sl_data.calibration.table.Calibration(1);
app.sl_data.calibration.cal_ix = 1;
end
end
% Callback function
function RSquaredThresholdEditFieldValueChanged(app, event)
value = app.RSquaredThresholdEditField.Value;
cla(app.r_squared_axes);
ct = app.ROIHeightpxEditField.Value;
plot(app.r_squared_axes,1:ct,ones(1,ct)*app.RSquaredThresholdEditField.Value,':','Color','r')
plot(app.r_squared_axes,1:ct,app.r_squared,'ks')
end
% Value changed function: LiveMeasurementModeButton
function LiveMeasurementModeButtonValueChanged(app, event)
global bStop
value = app.LiveMeasurementModeButton.Value;
if value
app.live_measurement = 1;
app.LoadImageButton.Enable = 'off';
selected_device = app.DeviceList.Value;
resolution = app.ResolutionDropDown.Value;
switch resolution
case '1024 x 822'
nResolutionIndex = 1;
width = 1024;
height = 822;
case '2048 x 1644'
nResolutionIndex = 1;
width = 2048;
height = 1644;
case '4096 x 3288'
nResolutionIndex = 1;
width = 4096;
height = 3288;
end
bStop = 0;
app.DeviceList.Enable = 'off';
items = app.DeviceList.Items;
index = strcmp(selected_device,items);
nSpeed = 1;
devList = app.Devices;
[im, ~, ~, app.ToupcamData] = mexToupcam(nResolutionIndex, nSpeed, devList(index).id, index);
app.ExposureSlider.Enable = 'off';
app.ExposureTimemsEditField.Enable = 'off';
app.ExposureTimemsEditFieldLabel.Enable = 'off';
app.ColorTempSlider.Enable = 'on';
app.ColorTempSlider.Value = app.ToupcamData.TOUPCAM_TEMP_DEF;
app.ColorTemperatureEditFieldLabel.Enable = 'on';
app.ColorTemperatureEditField.Enable = 'on';
app.ColorTemperatureEditField.Value = app.ToupcamData.TOUPCAM_TEMP_DEF;
app.TintSlider.Enable = 'on';
app.TintSlider.Value = app.ToupcamData.TOUPCAM_TINT_DEF;
app.TintEditFieldLabel.Enable = 'on';
app.TintEditField.Enable = 'on';
app.TintEditField.Value = app.ToupcamData.TOUPCAM_TINT_DEF;
app.WhiteBalanceButton.Enable = 'on';
app.WBDefaultsButton.Enable = 'on';
app.HueSlider.Enable = 'on';
app.HueSlider.Value = app.ToupcamData.TOUPCAM_HUE_DEF;
app.HueEditField.Enable = 'on';
app.HueEditFieldLabel.Enable = 'on';
app.HueEditField.Value = app.ToupcamData.TOUPCAM_HUE_DEF;
app.SaturationSlider.Enable = 'on';
app.SaturationSlider.Value = app.ToupcamData.TOUPCAM_SATURATION_DEF;
app.SaturationEditField.Enable = 'on';
app.SaturationEditFieldLabel.Enable = 'on';
app.SaturationEditField.Value = app.ToupcamData.TOUPCAM_SATURATION_DEF;
app.BrightnessSlider.Enable = 'on';
app.BrightnessSlider.Value = app.ToupcamData.TOUPCAM_BRIGHTNESS_DEF;
app.BrightnessEditField.Enable = 'on';
app.BrightnessEditFieldLabel.Enable = 'on';
app.BrightnessEditField.Value = app.ToupcamData.TOUPCAM_BRIGHTNESS_DEF;
app.ContrastSlider.Enable = 'on';
app.ContrastSlider.Value = app.ToupcamData.TOUPCAM_CONTRAST_DEF;
app.ContrastEditField.Enable = 'on';
app.ContrastEditFieldLabel.Enable = 'on';
app.ContrastEditField.Value = app.ToupcamData.TOUPCAM_CONTRAST_DEF;
app.GammaSlider.Enable = 'on';
app.ContrastSlider.Value = app.ToupcamData.TOUPCAM_GAMMA_DEF;
app.GammaEditField.Enable = 'on';
app.GammaEditFieldLabel.Enable = 'on';
app.GammaEditField.Value = app.ToupcamData.TOUPCAM_GAMMA_DEF;
app.DefaultsButton.Enable = 'on';
app.live_image = zeros(height,width, 3);
m = 1;
while ~isequal(bStop,2)
for i = 1 : height
for j = 1 :width
app.live_image(i,j,1) = im(3*(j-1)+3,i);
app.live_image(i,j,2) = im(3*(j-1)+2,i);
app.live_image(i,j,3) = im(3*(j-1)+1,i);
end
end
app.live_image = uint8(app.live_image);
if m == 1
[app.live_fig_handle] = center_image_with_preserved_aspect_ratio( ...
app.live_image, ...
app.image_axis);
else
app.live_fig_handle.CData = app.live_image;
end
if (isfield(app.sl_data,'box_handle'))
cla(app.px_intensity)
cla(app.calculation_axis)
cla(app.fft_axis)
cla(app.ac_axis)
SarcomereLength(app)
end
drawnow;
if bStop==1
uiwait;
end
m = m +1;
end
else
bStop = 2;
app.live_measurement = 2;
mexToupcam(0,0);
app.boxes = app.sl_data.box_handle;
clear im;
app.LoadImageButton.Enable = 'on';
app.DeviceList.Enable = 'on';
end
end
% Value changed function: LiveExperimentModeButton
function LiveExperimentModeButtonValueChanged(app, event)
value = app.LiveExperimentModeButton.Value;
global bStop
if value
app.live_experiment = 1;
app.LoadImageButton.Enable = 'off';
selected_device = app.DeviceList.Value;
resolution = app.ResolutionDropDown.Value;
switch resolution
case '1024 x 822'
nResolutionIndex = 1;
width = 1024;
height = 822;
case '2048 x 1644'
nResolutionIndex = 1;
width = 2048;
height = 1644;
case '4096 x 3288'
nResolutionIndex = 1;
width = 4096;
height = 3288;
end
bStop = 0;
app.DeviceList.Enable = 'off';
items = app.DeviceList.Items;
index = strcmp(selected_device,items);
nSpeed = 1;
devList = app.Devices;
[im, ~, ~, app.ToupcamData] = mexToupcam(nResolutionIndex, nSpeed, devList(index).id, index);
app.ExposureSlider.Enable = 'off';
app.ExposureTimemsEditField.Enable = 'off';
app.ExposureTimemsEditFieldLabel.Enable = 'off';
app.ColorTempSlider.Enable = 'on';
app.ColorTempSlider.Value = app.ToupcamData.TOUPCAM_TEMP_DEF;
app.ColorTemperatureEditFieldLabel.Enable = 'on';
app.ColorTemperatureEditField.Enable = 'on';
app.ColorTemperatureEditField.Value = app.ToupcamData.TOUPCAM_TEMP_DEF;
app.TintSlider.Enable = 'on';
app.TintSlider.Value = app.ToupcamData.TOUPCAM_TINT_DEF;
app.TintEditFieldLabel.Enable = 'on';
app.TintEditField.Enable = 'on';
app.TintEditField.Value = app.ToupcamData.TOUPCAM_TINT_DEF;
app.WhiteBalanceButton.Enable = 'on';
app.WBDefaultsButton.Enable = 'on';
app.HueSlider.Enable = 'on';
app.HueSlider.Value = app.ToupcamData.TOUPCAM_HUE_DEF;
app.HueEditField.Enable = 'on';
app.HueEditFieldLabel.Enable = 'on';
app.HueEditField.Value = app.ToupcamData.TOUPCAM_HUE_DEF;
app.SaturationSlider.Enable = 'on';
app.SaturationSlider.Value = app.ToupcamData.TOUPCAM_SATURATION_DEF;
app.SaturationEditField.Enable = 'on';
app.SaturationEditFieldLabel.Enable = 'on';
app.SaturationEditField.Value = app.ToupcamData.TOUPCAM_SATURATION_DEF;
app.BrightnessSlider.Enable = 'on';
app.BrightnessSlider.Value = app.ToupcamData.TOUPCAM_BRIGHTNESS_DEF;
app.BrightnessEditField.Enable = 'on';
app.BrightnessEditFieldLabel.Enable = 'on';
app.BrightnessEditField.Value = app.ToupcamData.TOUPCAM_BRIGHTNESS_DEF;
app.ContrastSlider.Enable = 'on';
app.ContrastSlider.Value = app.ToupcamData.TOUPCAM_CONTRAST_DEF;
app.ContrastEditField.Enable = 'on';
app.ContrastEditFieldLabel.Enable = 'on';
app.ContrastEditField.Value = app.ToupcamData.TOUPCAM_CONTRAST_DEF;
app.GammaSlider.Enable = 'on';
app.ContrastSlider.Value = app.ToupcamData.TOUPCAM_GAMMA_DEF;
app.GammaEditField.Enable = 'on';
app.GammaEditFieldLabel.Enable = 'on';
app.GammaEditField.Value = app.ToupcamData.TOUPCAM_GAMMA_DEF;
app.DefaultsButton.Enable = 'on';
app.live_image = zeros(height,width, 3);
m = 1;
while ~isequal(bStop,2)
for i = 1 : height
for j = 1 :width
app.live_image(i,j,1) = im(3*(j-1)+3,i);
app.live_image(i,j,2) = im(3*(j-1)+2,i);
app.live_image(i,j,3) = im(3*(j-1)+1,i);
end
end
app.time(m) = datetime;
app.live_image = uint8(app.live_image);
app.live_fig_handle.CData = app.live_image;
if (isfield(app.sl_data,'box_handle'))
LiveSarcomereLength(app,m)
end
drawnow;
if bStop==1
uiwait;
end
m = m +1;
end
else
bStop = 2;
app.live_measurement = 2;
mexToupcam(0,0);
clear im;
app.LoadImageButton.Enable = 'on';
app.DeviceList.Enable = 'on';
end
end
% Close request function: SLMeasureUIFigure
function SLMeasureUIFigureCloseRequest(app, event)
% mexToupcam(0,0);
% clear im;
global bStop;
bStop = 2;
delete(app)
end
% Button pushed function: SaveImageButton
function SaveImageButtonPushed(app, event)
[file_string,path_string] = uiputfile2( ...
{'*.png','PNG'},'Select file name for the image');
if (path_string~=0)
output_file = fullfile(path_string,file_string);
if isempty(app.image_file)
imwrite(app.live_image,output_file);
else
imwrite(app.image_file,output_file);
end
end
end
% Value changed function: AutoExposureCheckBox
function AutoExposureCheckBoxValueChanged(app, event)
value = app.AutoExposureCheckBox.Value;
global bStop;
bStop = 1;
[a,time] = mexToupcam(2,0);
time=roundn(time/1000,-2);