-
Notifications
You must be signed in to change notification settings - Fork 7
/
DLTcal5.m
1690 lines (1421 loc) · 58.6 KB
/
DLTcal5.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 [] = DLTcal5(varargin)
% function [] = DLTcal5()
%
% DLTcal5 is a graphic interface for creating DLT calibration coefficients
% from an arbitrary frame specification and image series.
%
% See the associated RTF document for complete usage information
%
% Version 1 - Ty Hedrick 8/1/04
% Version 2 - Ty Hedrick 8/4/05
% Version 3 - Ty Hedrick 3/27/08
% Version 5 - Ty Hedrick 7/12/10
if nargin==0 % no inputs, run the gui initialization routine
% check Matlab version and don't start if not >= 7. Many of the figure
% and gui controls changed in the 6.5 --> 7.0 jump and it is no longer
% possible to support the older versions.
v=version;
if str2double(v(1:3))<7
beep
disp('DLTcal5 requires MATLAB version 7 or later.')
return
end
% check to make sure that mmreader is available
if exist('mmreader')~=2 && exist('VideoReader')~=2
beep
disp('DLTcal5 requires mmreader or VideoReader to read AVIs or other')
disp('standard video files. Those functions are not available to you')
disp('so you will only be able to read uncompressed Phantom cine and')
disp('IDT mrf high speed camera files.')
disp('Check the Mathworks online documentation to learn about mmreader')
disp('and VideoReader. Use DLTcal3 if you have an older MATLAB version')
disp('that has the aviread function.')
end
call=99; % go creat the GUI (see the Switch statement below)
elseif nargin==1 % assume a switchyard call but no data
call=varargin{1};
ud=get(gcbo,'Userdata'); % get simple userdata
h=ud.handles; % get the handles
uda=get(h(1),'Userdata'); % get complete userdata
h=uda.handles; % get complete handles
sp=get(h(32),'Value'); % get the selected point
elseif nargin==2 % assume a switchyard call and data
call=varargin{1};
uda=varargin{2};
h=uda.handles; % get complete handles
sp=get(h(32),'Value'); % get the selected point
elseif nargin==3 % switchyard call, data & subfunction specific data
call=varargin{1};
uda=varargin{2};
h=uda.handles; % get complete handles
sp=get(h(32),'Value'); % get the selected point
oData=varargin{3}; % store the misc. other variable in oData
else
disp('DLTcal5: incorrect number of input arguments')
return
end
% switchyard to handle updating & processing tasks for the figure
switch call
%% case 99 - GUI figure creation
case {99} % Initialize the GUI
disp(sprintf('\n'))
disp('DLTcal5 (updated March 13, 2015)')
disp(sprintf('\n'))
disp('Visit http://www.unc.edu/~thedrick/ for more information,')
disp('tutorials, sample data & updates to this program.')
disp(sprintf('\n'))
top=20.2; % top of the controls layout
h(1) = figure('Units','characters',... % control figure
'Color',[0.831 0.815 0.784]','Doublebuffer','on', ...
'IntegerHandle','off','MenuBar','none',...
'Name','DLTcal5 controls','NumberTitle','off',...
'Position',[10 10 58 36.73],'Resize','off',...
'HandleVisibility','callback','Tag','figure1',...
'UserData',[],'Visible','on','deletefcn','DLTcal5(13)');
h(29) = uicontrol('Parent',h(1),'Units','characters',... % Video frame
'Position',[3 top+2.5 51.5 8],'Style','frame','BackgroundColor',...
[0.568 0.784 1],'string','Points and calibration');
% Points frame part 1
h(30) = uicontrol('Parent',h(1),'Units','characters',...
'Position',[3 top-5.7 51.5 7.3],'Style','frame', ...
'BackgroundColor',[0.788 1 0.576],'string','Points and calibration');
uicontrol('Parent',h(1),'Units','characters',... % Points frame part 2
'Position',[3 top-19.2 30.5 13.6],'Style','frame',...
'BackgroundColor',[0.788 1 0.576],'string','Points and calibration');
uicontrol('Parent',h(1),'Units','characters',... % blank string
'Position',[3.1 top-8.6 30.3 4.6],'Style','text',...
'BackgroundColor',[0.788 1 0.576],'string','');
uicontrol('Parent',h(1),'Units','characters',... % Points frame part 3
'Position',[10 top-19.2 44.5 7.0],'Style','frame',...
'BackgroundColor',[0.788 1 0.576],'string','Points and auto-tracking');
uicontrol('Parent',h(1),'Units','characters',... % blank string
'Position',[3.1 top-19.1 30.3 14.1],'Style','text',...
'BackgroundColor',[0.788 1 0.576],'string','');
h(5) = uicontrol('Parent',h(1),'Units','characters',... % frame slider
'Position',[7.2 top+3.4 44.2 1.2],'String',{ 'frame' },...
'Style','slider','Tag','slider2','Callback',...
'DLTcal5(1)','Enable','off','Value',1);
% initialize button
h(6) = uicontrol('Parent',h(1),'Units','characters',...
'Position',[2.2 top+12.1 22 3],'String','Initialize',...
'Callback','DLTcal5(0)','ToolTipString', ...
'Load the frame specification and calibration image files',...
'Tag','pushbutton1');
% Save data button
h(7) = uicontrol('Parent',h(1),'Units','characters',...
'Position',[27 top+13.8 14.4 1.8],'String','Save Data','Tag',...
'pushbutton3','Callback','DLTcal5(5)','Enable','off');
h(12)=uicontrol('Parent',h(1),... % Color display checkbox
'Units','characters','BackgroundColor',[0.568 0.784 1],...
'Position',[43 top+7.3 4 1],'Value',0,'Style','checkbox','Tag',...
'colorVideos','enable','off','Callback','DLTcal5(9)');
% video gamma slider
h(13) = uicontrol('Parent',h(1),'Units','characters',...
'Position',[7.2 top+7.1 28 1.3],'String',{ 'gamma' },...
'Style','slider','Tag','slider1','Min',.1','Max',2,'Value',1,...
'Callback','DLTcal5(1)', ...
'Enable','off', 'ToolTipString','Video Gamma slider');
h(14) = uicontrol('Parent',h(1),'Units','characters',... % quit button
'Position',[43.8 top+12.1 11.2 3],'String','Quit',...
'Tag','pushbutton4','Callback','DLTcal5(11)','enable','on');
% gamma control label
h(18) = uicontrol('Parent',h(1),'Units','characters',...
'BackgroundColor',[0.568 0.784 1],'HorizontalAlignment','left',...
'Position',[7 top+8.5 20.4 1.3],'String','Video gamma','Style',...
'text','Tag','text1');
uicontrol('Parent',h(1),'Units','characters',... % color video label
'BackgroundColor',[0.568 0.784 1],'HorizontalAlignment','left',...
'Position',[37 top+8.5 15.4 1.3],'String','Display in color',...
'Style','text','Tag','text1');
% frame slider label
h(21) = uicontrol('Parent',h(1),'Units','characters',...
'BackgroundColor',[0.568 0.784 1],'HorizontalAlignment','left',...
'Position',[7 top+5.0 24.6 1.1],'String','Frame number: NaN/NaN',...
'Style','text','Tag','text6');
% load previous data button
h(22) = uicontrol('Parent',h(1),'Units','characters',...
'Position',[27 top+11.3 14.4 1.7],'String','Load Data',...
'Tag','pushbutton5','enable','off','Callback','DLTcal5(6)');
h(23) = uicontrol('Parent',h(1),... % Calibration type label
'Units','characters','BackgroundColor',[0.788 1 0.576],...
'HorizontalAlignment','left','Position',[7 top-10.5 20.6 1.3],...
'String','DLT calibration type:','Style','text',...
'Tag','text15');
menuString='11 parameter | modified 11 parameter';
h(25) = uicontrol('Parent',h(1),... % Calibration type menu
'Units','characters',...
'HorizontalAlignment','left','Position',[7 top-12.2 24.6 1.3],...
'String',menuString,'Style','popupmenu',...
'Value',1,'Tag','text16');
h(27) = uicontrol('Parent',h(1),... % DLT average residuals
'Units','characters','BackgroundColor',[0.788 1 0.576],...
'HorizontalAlignment','left','Position',[34.5 top-18.0 19 2.6],...
'String',sprintf('Calibration \nresidual: --'), ...
'Style','text','Tag','text17');
h(31) = uicontrol('Parent',h(1),... % Current point label
'Units','characters','BackgroundColor',[0.788 1 0.576],...
'HorizontalAlignment','left','Position',[7 top-0.5 14.2 1.3],...
'String','Current point','Style','text','Tag','text13');
h(32) = uicontrol(... % Current point pull-down menu
'Parent',h(1),'Units','characters','Position',[7 top-2.3 12 1.6],...
'String',' 1','Style','popupmenu','Value',1,'Tag','popupmenu1', ...
'Enable','off','Callback','DLTcal5(1)');
% compute coefficients button
h(33) = uicontrol('Parent',h(1),'Units','characters',...
'Position',[7 top-15.8 24.6 2.2],'String','Compute coefficients',...
'Callback','DLTcal5(7)','ToolTipString', ...
'Compute the DLT coefficients','Tag','pushbutton1', ...
'enable','off','HorizontalAlignment','center');
h(34) = uicontrol('Parent',h(1), ... % Advance on click label
'Units','characters','Position',[28 top-3 24.2 3],...
'BackgroundColor',[0.788 1 0.576],'HorizontalAlignment','left',...
'Style','text','String',sprintf('Auto-advance \non click:'));
% Advance on click checkbox
h(35) = uicontrol('Parent',h(1),'Units','characters',...
'BackgroundColor',[0.788 1 0.576],'Position',[47 top-1.6 3 1.3],...
'String','','Style','checkbox','Value',0,'Tag','AoC checkbox',...
'enable','off');
% DLT error analysis button
h(36) = uicontrol('Parent',h(1),'Units','characters',...
'Position',[7 top-18.5 24.6 2.2],'String','Semi','Style', ...
'pushbutton','Tag','radiobutton2','enable','off','Callback', ...
'DLTcal5(8)','String',sprintf('Error analysis'));
% h(40) movie axes in video figure
h(50) = uicontrol('Parent',h(1),... % Magnified image label
'Units','characters','BackgroundColor',[0.788 1 0.576],...
'HorizontalAlignment','left','Position',[34 top-14.0 18.6 1.3],...
'String','Magnified image:','Style','text','Tag','text18');
% zoomed image axis
h(51)=axes('Position',[.61 .225 .30 .165],'XTickLabel','', ...
'YTickLabel','','Visible','on','Parent',h(1),'box','off');
checkerBoard=chex(1,21,21).*255;
c=(repmat((0:1/254:1)',1,3)); % grayscale color map
% display the default image
image('Parent',h(51),'Cdata',checkerBoard,'CDataMapping','direct');
colormap(h(51),c)
xlim(h(51),[1 21]);
ylim(h(51),[1 21]);
h(54)=uicontrol('Parent',h(1), ... % Centroid finding label
'Units','characters','Position',[7 top-6.5 24.2 3],...
'BackgroundColor',[0.788 1 0.576],'HorizontalAlignment','left',...
'Style','text','String',sprintf('Find marker centroid:'));
% Centroid finding checkbox
h(55)=uicontrol('Parent',h(1),'Units','characters',...
'BackgroundColor',[0.788 1 0.576],'Position',[32 top-4.5 4 1],...
'Value',0,'Style','checkbox','Tag','findCentroidBox',...
'enable','off','Callback','DLTcal5(14)');
h(56) = uicontrol('Parent',h(1), ... % Centroid color label
'Units','characters','Position',[8 top-6.4 24.2 1.3],...
'BackgroundColor',[0.788 1 0.576],'HorizontalAlignment','left',...
'Style','text','String','Color');
h(57) = uicontrol(... % Centroid color menu
'Parent',h(1),'Units','characters','Position',[17 top-6.4 15 1.6],...
'String',{'black','white'},'Style','popupmenu','Value',1, ...
'Tag','popupmenu3','Enable','off');
h(58) = uicontrol('Parent',h(1),... % Autotrack search width label
'Units','characters','BackgroundColor',[0.788 1 0.576],...
'HorizontalAlignment','left','Position',[7 top-8.5 24.2 1.3],...
'String','Search width','Style','text',...
'Tag','text15');
h(59) = uicontrol('Parent',h(1),... % Autotrack search area size
'Units','characters','BackgroundColor',[1 1 1],'Position',...
[22 top-8.5 7 1.6],'String','9','Style','edit','Tag',...
'edit7','Callback','DLTcal5(1)','enable','off');
ud.handles=h; % simple userdata object
% for each handle set all handle info in userdata
for i=1:numel(h)
try
set(h(i),'Userdata',ud);
catch
% do nothing
end
end
return
%% case 0 - Initialize button
case {0} % Initialize button press from the GUI
% get the calibration frame specification file if necessary
if isfield(uda,'specdata')==0
[specfile,specpath]=uigetfile({'*.csv','comma separated values'}, ...
'Please select your calibration object specification file');
specdata=dlmread([specpath,specfile],',',1,0); % read the cal. file
if size(specdata,2)==3 % got a good spec file
uda.specdata=specdata; % put it in userdata
uda.specpath=specpath; % keep the path too
% tell the user
msg=sprintf(...
'Loaded a %d point calibration object specification file.',...
size(specdata,1));
uiwait(msgbox(msg,'Success'));
% write back any modifications to the main figure userdata
set(h(1),'Userdata',uda);
cd(specpath); % change to spec path directory
else
msg=['The calibration object specification file must have 3 ', ...
'data columns. Aborting.'];
msgbox(msg,'Error','error');
return
end
uda.cNum=1; % initial camera # is 1 (first camera)
else % adding a camera to an ongoing calibration run
set(h(2),'deletefcn','');
close(h(2)) % close the old video window
uda.cNum=uda.cNum+1; % increment camera number
uda.cp=1; % set current point back to 1
end
% get the image files
[calfnames,uda.calpname]=uigetfile( {'*.bmp;*.tif;*.jpg;*.avi;*.cin;*.mp4;*.mov', ...
'Image and movie files (*.bmp, *.tif, *.jpg, *.avi, *.cin, *.mp4, *.mov)'}, ...
['Select the calibration image files. (Ctrl-click to pick',...
' several)'],'MultiSelect','on');
if iscell(calfnames)==0 % convert strings to cells
uda.calfnames={calfnames};
else
for i=2:numel(calfnames)
uda.calfnames{i-1}=calfnames{i};
uda.calfnames{end+1}=calfnames{1};
end
end
% sort the image file names - Matlab's multi-select does an uncertain
% job of returning the selection order, so we just sort to give a
% consistent output every time
uda.calfnames=sort(uda.calfnames);
% Create a new figure for movie display
h(2) = figure('Units','characters',... % movie figure
'Color',[0.831 0.815 0.784]','Doublebuffer','on', ...
'KeyPressFcn','DLTcal5(2)', ...
'IntegerHandle','off','MenuBar','none',...
'Name','DLTcal5 images','NumberTitle','off',...
'HandleVisibility','callback','Tag','figure1',...
'UserData',uda,'Visible','on','Units','Pixels', ...
'deletefcn','DLTcal5(13)','pointer','cross', ...
'Tag','VideoFigure');
h(40)=axes('Position',[.01 .01 .99 .95],'XTickLabel','', ...
'YTickLabel','','ButtonDownFcn','DLTcal5(3)', ...
'Visible','off'); % create movie1 axis
% Initialize the data arrays
%
% set the slider size
if length(uda.calfnames)>1
set(h(5),'Max',length(uda.calfnames),'Min',1,'Value',1,...
'SliderStep',[1/(length(uda.calfnames)-1) ...
1/(length(uda.calfnames)-1)]);
set(h(5),'Enable','on'); % turn slider on
else
set(h(5),'Enable','off') % turn off the slider if only 1 image
end
uda.numpts=size(uda.specdata,1); % number of pts to digitize per camera
uda.xypts(1:uda.numpts,2*uda.cNum-1:2*uda.cNum)=NaN; % digitized point
uda.sp=1; % selected point
uda.recentlysaved=true; % start the "saved" flag at true
figure(h(2)) % make the video figure active
% update the number of points settings
ptstring=char(ones(1,uda.numpts*2+uda.numpts-1));
for i=1:uda.numpts-1
ptstring(1,i*4-3:i*4)=sprintf('%3d|',i);
end
ptstring(1,uda.numpts*4-3:uda.numpts*4-1)=sprintf('%3d',uda.numpts);
set(h(32),'String',ptstring);
% read & plot the first image
im=calimread([uda.calpname,uda.calfnames{1}],true);
% detect color
if size(im,3)>1
set(h(12),'Value',true);
end
set(h(40),'Visible','on');
set(h(2),'CurrentAxes',h(40));
redlakeplot(im);
uda.movsizes(1,1)=size(im,1);
uda.movsizes(1,2)=size(im,2);
set(h(40),'XTickLabel','');
set(h(40),'YTickLabel','');
set(get(h(40),'Children'),'ButtonDownFcn','DLTcal5(3)');
set(get(h(40),'Children'),'UserData',uda);
set(h(40),'Userdata',uda);
title(h(40),uda.calfnames{1},'Interpreter','none');
% turn on the other controls
set(h(7),'enable','on'); % save button
set(h(13),'enable','on'); % video gamma control
set(h(22),'enable','on'); % load previous data button
set(h(32),'enable','on'); % point # pulldown menu
set(h(35),'enable','on'); % advance on click checkbox
set(h(59),'enable','on'); % search area size
set(h(12),'enable','on'); % color display
% turn on Centroid finding & set value to "On" if the image analysis
% toolbox functions that it depends on are available
if exist('im2bw','file')>1 && exist('regionprops','file')>1 ...
&& exist('bwlabel','file')>1
set(h(55),'enable','on','Value',0);
disp('Detected Image Analysis toolbox, centroid localization is')
disp('available, enable it via the checkbox in the Controls window.')
else
disp('The Image Analysis toolbox is not available, centroid ')
disp('localization has been disabled.')
end
% turn off the Initialize button
set(h(6),'enable','off');
% write the userdata back
uda.handles=h; % copy in new handles
set(h(1),'Userdata',uda);
% call self to update the string fields
DLTcal5(4,uda);
%% case 1 - video frame refresh
case {1} % refresh the video frames
cfh=gcf; % handle to current figure
colorVal=get(h(12),'Value'); % get color mode info
fr=round(get(h(5),'Value')); % get the frame # from the slider
figure(h(2)); % activate the video figure
set(h(2),'CurrentAxes',h(40)); % activate the video axis
xl=xlim; % current x & y axis limits
yl=ylim;
% read the image
im=calimread([uda.calpname,uda.calfnames{fr}],colorVal);
% plot the new image data
hold off
if colorVal==0 %gray
redlakeplot(im(:,:,1));
% set the figure gamma
c=colormap(gray);
cnew=c.^get(h(13),'Value');
colormap(cnew);
elseif colorVal==1 && size(im,3)==1 % color w/ gray video
redlakeplot(repmat(im(:,:,1)+(1-get(h(13),'Value'))*128,[1,1,3]));
else
redlakeplot(im+(1-get(h(13),'Value'))*128);
end
% fix other figure parameters
set(h(40),'XTickLabel','');
set(h(40),'YTickLabel','');
set(h(40),'Userdata',uda);
set(get(h(40),'Children'),'ButtonDownFcn','DLTcal5(3)');
set(get(h(40),'Children'),'UserData',uda);
xlim(xl); ylim(yl); % restore axis zoom
hold on
title(uda.calfnames{fr},'Interpreter','none');
% plot any existing digitized points (in a loop, ick!)
idx=find(isnan(uda.xypts(:,uda.cNum*2))==0);
idx(idx==sp)=[];
for i=1:length(idx)
% red dot on the point
plot(uda.xypts(idx(i),uda.cNum*2-1), ...
uda.xypts(idx(i),uda.cNum*2),'r.','HitTest','off');
% text label of point # slightly up & left
text(uda.xypts(idx(i),uda.cNum*2-1)+2, ...
uda.xypts(idx(i),uda.cNum*2)+2,num2str(idx(i)), ...
'Color','r','HitTest','off');
end
% plot the selected point
plot(uda.xypts(sp,uda.cNum*2-1), ...
uda.xypts(sp,uda.cNum*2),'ro','MarkerFaceColor','r', ...
'HitTest','off');
text(uda.xypts(sp,uda.cNum*2-1)+2,uda.xypts(sp,uda.cNum*2)+2, ...
num2str(sp),'Color','r','FontSize',14,'HitTest','off');
% write back any modifications to the main figure userdata
set(h(1),'Userdata',uda);
% restore the pre-existing top figure
figure(cfh);
% call self to update the text fields
DLTcal5(4,uda);
% update the small plot
updateSmallPlot(h,1,uda.xypts(sp,uda.cNum*2-1:uda.cNum*2));
%% case 2 - keypress callback
case {2} % handle keypresses in the figure window - zoom & unzoom axes
% disp('case 2: handle keypresses')
cc=get(h(2),'CurrentCharacter'); % the key pressed
pl=get(0,'PointerLocation'); % pointer location on the screen
pos=get(h(2),'Position'); % get the figure position
% calculate pointer location in normalized units
plocal=[(pl(1)-pos(1,1)+1)/pos(1,3), (pl(2)-pos(1,2)+1)/pos(1,4)];
% if the keypress is empty set it to some value
if isempty(cc)
cc='X'; % any value that we don't look for later would do
axh=0; % set handle to active axis to zero
else
if plocal(1)<=0.99 && plocal(2)<=0.99, axh=h(40); vnum=1;
else
disp('The mouse pointer is not over an image.')
return
end
end
% process the key press
if (cc=='=' || cc=='-' || cc=='r') && axh~=0; % check for zoom keys
% zoom in or out as indicated
if axh~=0
set(h(2),'CurrentAxes',axh); % set the current axis
axpos=get(axh,'Position'); % axis position in figure
xl=xlim; yl=ylim; % x & y limits on axis
% calculate the normalized position within the axis
plocal2=[(plocal(1)-axpos(1,1))/axpos(1,3) (plocal(2) ...
-axpos(1,2))/axpos(1,4)];
% calculate the actual pixel postion of the pointer
pixpos=round([(xl(2)-xl(1))*plocal2(1)+xl(1) ...
(yl(2)-yl(1))*plocal2(2)+yl(1)]);
% axis location in pixels (idealized)
axpix(3)=pos(3)*axpos(3);
axpix(4)=pos(4)*axpos(4);
% adjust pixels for distortion due to normalized axes
xRatio=(axpix(3)/axpix(4))/(diff(xl)/diff(yl));
yRatio=(axpix(4)/axpix(3))/(diff(yl)/diff(xl));
if xRatio > 1
xmp=xl(1)+(xl(2)-xl(1))/2;
xmpd=pixpos(1)-xmp;
pixpos(1)=pixpos(1)+xmpd*(xRatio-1);
elseif yRatio > 1
ymp=yl(1)+(yl(2)-yl(1))/2;
ympd=pixpos(2)-ymp;
pixpos(2)=pixpos(2)+ympd*(yRatio-1);
end
% set the figure xlimit and ylimit
if cc=='=' % zoom in
xlim([pixpos(1)-(xl(2)-xl(1))/3 pixpos(1)+(xl(2)-xl(1))/3]);
ylim([pixpos(2)-(yl(2)-yl(1))/3 pixpos(2)+(yl(2)-yl(1))/3]);
elseif cc=='-' % zoom out
xlim([pixpos(1)-(xl(2)-xl(1))/1.5 pixpos(1)+(xl(2)-xl(1))/1.5]);
ylim([pixpos(2)-(yl(2)-yl(1))/1.5 pixpos(2)+(yl(2)-yl(1))/1.5]);
else % restore zoom
xlim([0 uda.movsizes(vnum,2)]);
ylim([0 uda.movsizes(vnum,1)]);
end
% update the zoomed plot
cp=uda.xypts(sp,uda.cNum*2-1:uda.cNum*2);
updateSmallPlot(h,1,cp);
end
elseif cc=='f' || cc=='b' && axh~=0 % check for valid movement keys
fr=round(get(h(5),'Value')); % get current slider value
smax=get(h(5),'Max'); % max slider value
smin=get(h(5),'Min'); % min slider value
if smin==0, return, end % avoid edge case
axn=find(h==axh)-39; % axis number
if isnan(axn),
disp('Error: The mouse pointer is not in an axis.')
return
end
if cc=='f' && fr+1 <= smax
set(h(5),'Value',fr+1); % current frame + 1
elseif cc=='b' && fr-1 >= smin
set(h(5),'Value',fr-1); % current frame - 1
end
% full redraw of the screen
DLTcal5(1,uda);
% update the control / zoom window
% 1st retrieve the cp from the data file in case the autotracker
% changed it
cp=uda.xypts(sp,uda.cNum*2-1:uda.cNum*2);
updateSmallPlot(h,axn,cp);
elseif cc=='.' || cc==',' % change point
ptnum=numel(get(h(32),'String'))/2; % # of points defined
ptval=get(h(32),'Value'); % selected point
if cc==',' && ptval>1 % decrease point value if possible
set(h(32),'Value',ptval-1);
ptval=ptval-1;
elseif cc=='.' && ptval<ptnum % increase point value if possible
set(h(32),'Value',ptval+1);
ptval=ptval+1;
end
pt=uda.xypts(ptval,uda.cNum*2-1:uda.cNum*2);
% update the magnified point view
updateSmallPlot(h,vnum,pt);
% do a quick screen redraw
quickRedraw(uda,h,ptval);
elseif cc=='i' || cc=='j' || cc=='k' || cc=='m' || cc=='4' || ...
cc=='8' || cc=='6' || cc=='2' % nudge point
% check and see if there is a point to nudge, get it's value if
% possible
if isnan(uda.xypts(sp,uda.cNum*2-1))
return
else
pt=uda.xypts(sp,uda.cNum*2-1:uda.cNum*2);
end
% modify pt based on the 'nudge' value
nudge=0.5; % 1/2 pixel nudge
if cc=='i' || cc=='8'
pt(1,2)=pt(1,2)+nudge; % up
elseif cc=='j' || cc=='4'
pt(1,1)=pt(1,1)-nudge; % left
elseif cc=='k' || cc=='6'
pt(1,1)=pt(1,1)+nudge; % right
else
pt(1,2)=pt(1,2)-nudge; % down
end
% set the modified point
uda.xypts(sp,uda.cNum*2-1:uda.cNum*2)=pt;
% update the magnified point view
updateSmallPlot(h,vnum,pt);
% do a quick screen redraw
quickRedraw(uda,h,sp);
elseif cc==' ' % space bar (digitize a point)
set(h(2),'CurrentAxes',axh); % set the current axis
axpos=get(axh,'Position'); % axis position in figure
xl=xlim; yl=ylim; % x & y limits on axis
% calculate the normalized position within the axis
plocal2=[(plocal(1)-axpos(1,1))/axpos(1,3) (plocal(2) ...
-axpos(1,2))/axpos(1,4)];
% calculate the actual pixel postion of the pointer
pixpos=([(xl(2)-xl(1)+0)*plocal2(1)+xl(1) ...
(yl(2)-yl(1)+0)*plocal2(2)+yl(1)]);
% axis location in pixels (idealized)
axpix(3)=pos(3)*axpos(3);
axpix(4)=pos(4)*axpos(4);
% adjust pixels for distortion due to normalized axes
xRatio=(axpix(3)/axpix(4))/(diff(xl)/diff(yl));
yRatio=(axpix(4)/axpix(3))/(diff(yl)/diff(xl));
if xRatio > 1
xmp=xl(1)+(xl(2)-xl(1))/2;
xmpd=pixpos(1)-xmp;
pixpos(1)=pixpos(1)+xmpd*(xRatio-1);
elseif yRatio > 1
ymp=yl(1)+(yl(2)-yl(1))/2;
ympd=pixpos(2)-ymp;
pixpos(2)=pixpos(2)+ympd*(yRatio-1);
end
% setup oData for the digitization routine
oData.seltype='standard';
oData.cp=pixpos;
DLTcal5(3,uda,oData); % digitize a point
return
elseif cc=='z' % delete the current point
oData.seltype='alt';
oData.cp=[NaN,NaN];
DLTcal5(3,uda,oData); % digitize a point
return
end
% write back any modifications to the main figure userdata
set(h(1),'Userdata',uda);
%% case 3 - axis mouse click callback
case {3} % handle button clicks in axes
if strcmp(get(gcbo,'Tag'),'VideoFigure')
% entered the function via space bar, not mouse click
seltype=oData.seltype;
cp=oData.cp;
else
% entered via mouse click
set(h(2),'CurrentAxes',get(gcbo,'Parent')); % set the current axis
cp=get(get(gcbo,'Parent'),'CurrentPoint'); % get the xy coordinates
seltype=cellstr(get(h(2),'SelectionType')); % selection type
end
% if detect right button click, erase the point
if strcmp(seltype,'normal')==false
cp(:,:)=NaN;
end
% Search for a marker centroid (if desired & possible)
findCent=get(h(55),'Value'); % check the UI for permission
if isnan(cp(1))==0 && findCent==1
[cp]=click2centroid(h,cp);
end
% set the points for the current frame
uda.xypts(sp,uda.cNum*2-1)=cp(1,1); % set x point
uda.xypts(sp,uda.cNum*2)=cp(1,2); % set y point
% new data available, change the recently saved parameter to false
uda.recentlysaved=0;
% zoomed window update
updateSmallPlot(h,1,cp);
% auto-advance on click
if(get(h(35),'Value')==1)
ptnum=numel(get(h(32),'String'))/2; % get # of points in list
ptval=get(h(32),'Value'); % selected point
if ptval<uda.numpts % increase point value if possible
set(h(32),'Value',ptval+1);
ptval=ptval+1;
end
else
ptval=sp;
end
% enable computation buttons if appropriate
goodpts=find(isnan(uda.xypts(:,uda.cNum*2))==0);
if length(goodpts)>=6
set(h(33),'Enable','on')
set(h(36),'Enable','on')
else
set(h(33),'Enable','off')
set(h(36),'Enable','off')
end
set(h(1),'Userdata',uda); % pass back complete user data
% quick screen refresh to show the new point & possibly DLT info
quickRedraw(uda,h,ptval);
%% case 4 - update GUI text fields
case {4} % update the text fields
% set the frame # string
fr=round(get(h(5),'Value')); % get current & max frame from slider
frmax=get(h(5),'Max');
set(h(21),'String',['Frame number: ' num2str(fr) '/' num2str(frmax)]);
%% case 5 - save data
case {5} % save data
% get a place to save it
pname=uigetdir(pwd,'Pick a directory to contain the output files');
pause(0.1); % make sure that the uigetdir ran (MATLAB bug workaround)
% get a prefix
pfix=inputdlg({'Enter a prefix for the data files'},'Data prefix',...
1,{'cal01_'});
if numel(pfix)==0
return
else
pfix=pfix{1};
end
% test for existing files
if exist([pname,filesep,pfix,'xypts.csv'],'file')~=0
overwrite=questdlg('Overwrite existing data?', ...
'Overwrite?','Yes','No','No');
else
overwrite='Yes';
end
% create headers (xypts)
xyh=cell(uda.cNum*2,1);
for i=1:uda.cNum
xyh{i*2-1}=sprintf('cam%s_X',num2str(i));
xyh{i*2}=sprintf('cam%s_Y',num2str(i));
end
if strcmp(overwrite,'Yes')==1
% xypts
f1=fopen([pname,filesep,pfix,'xypts.csv'],'w');
% header
for i=1:numel(xyh)-1
fprintf(f1,'%s,',xyh{i});
end
fprintf(f1,'%s\n',xyh{end});
% data
for i=1:size(uda.xypts,1);
tempData=squeeze(uda.xypts(i,:,:));
for j=1:numel(tempData)-1
fprintf(f1,'%.6f,',tempData(j));
end
fprintf(f1,'%.6f\n',tempData(end));
end
fclose(f1);
% dltcoefs
if isfield(uda,'coefs')
dlmwrite([pname,filesep,pfix,'DLTcoefs.csv'],uda.coefs,',');
end
uda.recentlysaved=1;
set(h(1),'Userdata',uda); % pass back complete user data
msgbox('Data saved.');
end
%% case 6 - load saved data
case {6} % load previously saved points
[fname1,pname1]=uigetfile('*xypts*.csv',...
'Select the [prefix]xypts.csv file');
pause(0.1); % make sure that the uigetfile ran (MATLAB bug workaround)
pfix=[pname1,fname1];
% check for cancel button
if numel(fname1)==1 && fname1==0
disp('File load canceled.')
return
end
% load the exported xy points
tempData=dlmread(pfix,',',1,0);
% check for similarity with video data
if size(tempData,1)~=size(uda.xypts,1)
msgbox(['WARNING - the digitized point file size does not match',...
'the frame, aborting.'],'Warning','warn','modal')
return
else
if size(tempData,2)/2>=uda.cNum;
tempData=tempData(:,uda.cNum*2-1:uda.cNum*2);
else
tempData=tempData(:,1:2);
end
uda.xypts(:,uda.cNum*2-1:uda.cNum*2)=tempData;
% enable computation buttons if appropriate
goodpts=find(isnan(tempData(:,1))==0);
if length(goodpts)>=9
set(h(33),'Enable','on')
set(h(36),'Enable','on')
else
set(h(33),'Enable','off')
set(h(36),'Enable','off')
end
end
% call self to update the video fields
DLTcal5(1,uda);
%% case 7 - compute DLT coefficients
case {7} % compute DLT coefficients
if uda.cNum==1
uda.coefs=[];
end
set(gcf,'Pointer','watch')
data=uda.xypts(:,uda.cNum*2-1:uda.cNum*2);
goodpts=find(isnan(data(:,1))==0);
if length(goodpts)<6
msgbox(['You should use at least 6 digitized points for 11',...
'parameter DLT'])
return
end
if get(h(25),'Value')==1 % 11 parameter DLT
[uda.coefs(:,uda.cNum),uda.avgres(uda.cNum)]= ...
dlt_computeCoefficients(uda.specdata,data);
else % modified DLT
[uda.coefs(:,uda.cNum),uda.avgres(uda.cNum)]= ...
mdlt2(uda.specdata,data);
end
set(gcf,'Pointer','arrow')
% update the user interface
set(h(27),'String',sprintf('Calibration \nresidual: %0.3f',...
uda.avgres(uda.cNum)));
% write back any modifications to the main figure userdata
set(h(1),'Userdata',uda);
set(h(6),'Enable','on'); % enable the "calibrate another camera" option
set(h(6),'String','Add a camera')
%% case 8 - DLT error analysis
case {8} % DLT error analysis
msg{1}='This function computes the DLT coefficients and residuals';
msg{2}='with one of the calibration points removed. Calibration';
msg{3}='point # is shown on the X axis and DLT residual without that';
msg{4}='point is on the Y axis. A large drop in the residual';
msg{5}='indicates that the calibration point in question may be';
msg{6}='badly digitized or incorrectly specified in the calibration';
msg{7}='object file. This function does not modify any of the';
msg{8}='existing calibration points, it only shows the outcome of';
msg{9}='potential modifications.';
uiwait(msgbox(msg,'Information','modal'));
% start processing
wh=waitbar(0,'Processing error values');
rescheck(1:size(uda.specdata,1),1:2)=NaN;
for i=1:size(uda.specdata,1)
waitbar(i/size(uda.xypts,1)); % update waitbar size
ptstemp=uda.xypts(:,uda.cNum*2-1:uda.cNum*2);
rescheck(i,1)=i; % build X axis column
if isnan(ptstemp(i,1))==1 % no value anyway
rescheck(i,2)=NaN; % no need to process
else
ptstemp(i,1:2)=NaN; % set to NaN and recalculate
if get(h(25),'Value')==1 % standard DLT
[coefs,rescheck(i,2)]=dlt_computeCoefficients(uda.specdata,...
ptstemp);
else % modified DLT
[coefs,rescheck(i,2)]=mdlt2(uda.specdata,ptstemp);
end
end
end
close(wh)
figure
plot(rescheck(:,1),rescheck(:,2),'rd','MarkerFaceColor','r')
ylabel('DLT residual with 1 calibration point removed')
xlabel('Calibration point')
%% case 9 - Color video checkbox
case {9} % Click / unclick color video checkbox
if get(h(12),'Value')==0
set(h(55),'enable','on')
set(h(57),'enable','on')
else
set(h(55),'value',false)
set(h(55),'enable','off')
set(h(57),'enable','off')
end
DLTcal5(1,uda); % redraw screen
case {10} % unused
%% case 11 - Quit button callback
case {11} % Quit button
reallyquit=questdlg('Are you sure you want to quit?','Quit?',...
'yes','no','no');
pause(0.1); % make sure that the questdlg ran (MATLAB bug workaround)
if strcmp(reallyquit,'yes')==1
try
close(h(2));
catch
end
try
close(h(1));
catch
end
end
case {12} % unused
%% case 13 - Window close via non-Matlab event
case {13} % Window close via non-Matlab method
% if initialization completed and there is data to save
if h(2)~=0 && uda.recentlysaved==0;
savefirst=questdlg('Would you like to save your data now?', ...
'Save?','yes','no','yes');
pause(0.1); % make sure that the questdlg ran (MATLAB bug workaround)
if strcmp(savefirst,'yes')
DLTcal5(5,uda); % call self to save data
else
% consider the data saved anyway to avoid asking again
uda.recentlysaved=1;
set(h(1),'Userdata',uda);
end
end
try
delete(h(2));