source: trunk/src/geometry_calib.m @ 116

Last change on this file since 116 was 116, checked in by sommeria, 10 years ago

geometry_calib is now updated when a new image is viewed by uvmat
imadoc2struct corrected for time reading

File size: 60.0 KB
Line 
1%'geometry_calib': performs geometric calibration from a set of reference points
2%
3% function varargout = geometry_calib(varargin)
4%
5%A%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
6%  Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org.
7%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
8%     This file is part of the toolbox UVMAT.
9%
10%     UVMAT is free software; you can redistribute it and/or modify
11%     it under the terms of the GNU General Public License as published by
12%     the Free Software Foundation; either version 2 of the License, or
13%     (at your option) any later version.
14%
15%     UVMAT is distributed in the hope that it will be useful,
16%     but WITHOUT ANY WARRANTY; without even the implied warranty of
17%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18%     GNU General Public License (file UVMAT/COPYING.txt) for more details.
19%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA
20
21function varargout = geometry_calib(varargin)
22% GEOMETRY_CALIB M-file for geometry_calib.fig
23%      GEOMETRY_CALIB, by itself, creates a MenuCoord GEOMETRY_CALIB or raises the existing
24%      singleton*.
25%
26%      H = GEOMETRY_CALIB returns the handle to a MenuCoord GEOMETRY_CALIB or the handle to
27%      the existing singleton*.
28%
29%      GEOMETRY_CALIB('CALLBACK',hObject,eventData,handles,...) calls the local
30%      function named CALLBACK in GEOMETRY_CALIB.M with the given input arguments.
31%
32%      GEOMETRY_CALIB('Property','Value',...) creates a MenuCoord GEOMETRY_CALIB or raises the
33%      existing singleton*.  Starting from the left, property value pairs are
34%      applied to the GUI before geometry_calib_OpeningFunction gets called.  An
35%      unrecognized property name or invalid value makes property application
36%      stop.  All inputs are passed to geometry_calib_OpeningFcn via varargin.
37%
38%      *See GUI Options on GUIDE's Tools menu.  Choose "GUI allows only one
39%      instance to run (singleton)".
40%
41% See also: GUIDE, GUIDATA, GUIHANDLES
42
43% Edit the above text to modify the response to help geometry_calib
44
45% Last Modified by GUIDE v2.5 05-Oct-2010 13:47:00
46
47% Begin initialization code - DO NOT edit
48gui_Singleton = 1;
49gui_State = struct('gui_Name',       mfilename, ...
50                   'gui_Singleton',  gui_Singleton, ...
51                   'gui_OpeningFcn', @geometry_calib_OpeningFcn, ...
52                   'gui_OutputFcn',  @geometry_calib_OutputFcn, ...
53                   'gui_LayoutFcn',  [] , ...
54                   'gui_Callback',   []);
55if nargin & isstr(varargin{1})
56    gui_State.gui_Callback = str2func(varargin{1});
57end
58
59if nargout
60    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
61else
62    gui_mainfcn(gui_State, varargin{:});
63end
64% End initialization code - DO NOT edit
65
66
67% --- Executes just before geometry_calib is made visible.
68%INPUT:
69%handles: handles of the geometry_calib interface elements
70% PlotHandles: set of handles of the elements contolling the plotting
71% parameters on the uvmat interface (obtained by 'get_plot_handle.m')
72%------------------------------------------------------------------------
73function geometry_calib_OpeningFcn(hObject, eventdata, handles,inputfile,pos)
74%------------------------------------------------------------------------
75% Choose default command line output for geometry_calib
76handles.output = hObject;
77
78% Update handles structure
79guidata(hObject, handles);
80set(hObject,'DeleteFcn',{@closefcn})%
81
82%set the position of the interface
83if exist('pos','var')&& length(pos)>2
84    pos_gui=get(hObject,'Position');
85    pos_gui(1)=pos(1);
86    pos_gui(2)=pos(2);
87    set(hObject,'Position',pos_gui);
88end
89
90%set menu of calibration options
91%set(handles.calib_type,'String',{'rescale';'linear';'perspective';'normal';'tsai';'bouguet';'extrinsic'})
92set(handles.calib_type,'String',{'rescale';'linear';'quadr';'3D_linear';'3D_quadr';'3D_extrinsic'})
93inputxml='';
94if exist('inputfile','var')&& ~isempty(inputfile)
95    struct.XmlInputFile=inputfile;
96    set(hObject,'UserData',struct)
97    [Pathsub,RootFile,field_count,str2,str_a,str_b,ext,nom_type,subdir]=name2display(inputfile);
98    if ~strcmp(ext,'.xml')
99        inputfile=[fullfile(Pathsub,RootFile) '.xml']%xml file corresponding to the input file
100    end
101end
102set(handles.ListCoord,'String',{'......'})
103if exist(inputfile,'file')
104    loadfile(handles,inputfile)% load the point coordiantes existing in the xml file
105end
106set(handles.ListCoord,'KeyPressFcn',{@key_press_fcn,handles})%set keyboard action function
107
108
109%------------------------------------------------------------------------
110% --- Outputs from this function are returned to the command line.
111function varargout = geometry_calib_OutputFcn(hObject, eventdata, handles)
112%------------------------------------------------------------------------
113% Get default command line output from handles structure
114varargout{1} = handles.output;
115varargout{2}=handles;
116%
117%------------------------------------------------------------------------
118% executed when closing: set the parent interface button to value 0
119function closefcn(gcbo,eventdata)
120%------------------------------------------------------------------------
121huvmat=findobj(allchild(0),'Name','uvmat');
122if ~isempty(huvmat)
123    handles=guidata(huvmat);
124    set(handles.MenuMask,'enable','on')
125    set(handles.MenuGrid,'enable','on')
126    set(handles.MenuObject,'enable','on')
127    set(handles.MenuEdit,'enable','on')
128    set(handles.edit,'enable','on')
129    hobject=findobj(handles.axes3,'tag','calib_points');
130    if ~isempty(hobject)
131        delete(hobject)
132    end
133    hobject=findobj(handles.axes3,'tag','calib_marker');
134    if ~isempty(hobject)
135        delete(hobject)
136    end   
137end
138
139%------------------------------------------------------------------------
140% --- Executes on button press in calibrate_lin.
141function APPLY_Callback(hObject, eventdata, handles)
142%------------------------------------------------------------------------
143
144%read the current calibration points
145Coord_cell=get(handles.ListCoord,'String');
146Object=read_geometry_calib(Coord_cell);
147Coord=Object.Coord;
148
149% apply the calibration, whose type is selected in  handles.calib_type
150if ~isempty(Coord)
151    calib_cell=get(handles.calib_type,'String');
152    val=get(handles.calib_type,'Value');
153    GeometryCalib=feval(['calib_' calib_cell{val}],Coord,handles);
154else
155    msgbox_uvmat('ERROR','No calibration points, abort')
156    return
157end
158Z_plane=[];
159if ~isempty(Coord)
160    %check error
161    X=Coord(:,1);
162    Y=Coord(:,2);
163    Z=Coord(:,3);
164    x_ima=Coord(:,4);
165    y_ima=Coord(:,5);
166    [Xpoints,Ypoints]=px_XYZ(GeometryCalib,X,Y,Z);
167    GeometryCalib.ErrorRms(1)=sqrt(mean((Xpoints-x_ima).*(Xpoints-x_ima)));
168    [GeometryCalib.ErrorMax(1),index(1)]=max(abs(Xpoints-x_ima));
169    GeometryCalib.ErrorRms(2)=sqrt(mean((Ypoints-y_ima).*(Ypoints-y_ima)));
170    [GeometryCalib.ErrorMax(2),index(2)]=max(abs(Ypoints-y_ima));
171    [EM,ind_dim]=max(GeometryCalib.ErrorMax);
172    index=index(ind_dim);
173    %set the Z position of the reference plane used for calibration
174    if isequal(max(Z),min(Z))%Z constant
175        Z_plane=Z(1);
176        GeometryCalib.NbSlice=1;
177        GeometryCalib.SliceCoord=[0 0 Z_plane];
178    end
179end
180%set the coordinate unit
181unitlist=get(handles.CoordUnit,'String');
182unit=unitlist{get(handles.CoordUnit,'value')};
183GeometryCalib.CoordUnit=unit;
184%record the points
185GeometryCalib.SourceCalib.PointCoord=Coord;
186display_intrinsic(GeometryCalib,handles)%display calibration intrinsic parameters
187
188% Display extrinsinc parameters (rotation and translation of camera with  respect to the phys coordiantes)
189set(handles.Tx,'String',num2str(GeometryCalib.Tx_Ty_Tz(1),4))
190set(handles.Ty,'String',num2str(GeometryCalib.Tx_Ty_Tz(2),4))
191set(handles.Tz,'String',num2str(GeometryCalib.Tx_Ty_Tz(3),4))
192set(handles.Phi,'String',num2str(GeometryCalib.omc(1),4))
193set(handles.Theta,'String',num2str(GeometryCalib.omc(2),4))
194set(handles.Psi,'String',num2str(GeometryCalib.omc(3),4))
195
196% store the calibration data, by default in the xml file of the currently displayed image
197hhuvmat=guidata(findobj(allchild(0),'Name','uvmat'));%handles of elements in the GUI uvmat
198RootPath='';
199RootFile='';
200if ~isempty(hhuvmat.RootPath)& ~isempty(hhuvmat.RootFile)
201    testhandle=1;
202    RootPath=get(hhuvmat.RootPath,'String');
203    RootFile=get(hhuvmat.RootFile,'String');
204    filebase=fullfile(RootPath,RootFile);
205    outputfile=[filebase '.xml'];%xml file associated with the currently displayed image
206else
207    question={'save the calibration data and point coordinates in'};
208    def={fullfile(RootPath,['ObjectCalib.xml'])};
209    options.Resize='on';
210    answer=inputdlg(question,'save average in a new file',1,def,options);
211    outputfile=answer{1};
212end
213answer=msgbox_uvmat('INPUT_Y-N',{[outputfile ' updated with calibration data'];...
214    ['Error rms (along x,y)=' num2str(GeometryCalib.ErrorRms) ' pixels'];...
215    ['Error max (along x,y)=' num2str(GeometryCalib.ErrorMax) ' pixels']});
216if isequal(answer,'Yes')
217    answer_1=msgbox_uvmat('INPUT_TXT',' Z= ',num2str(Z_plane));
218    Z_plane=str2num(answer_1);
219    GeometryCalib.NbSlice=1;
220    GeometryCalib.SliceCoord=[0 0 Z_plane];
221    errormsg=update_imadoc(GeometryCalib,outputfile);% introduce the calibration data in the xml file
222    if ~strcmp(errormsg,'')
223        msgbox_uvmat('ERROR',errormsg);
224    end
225    %display image with new calibration in the currently opened uvmat interface
226    hhh=findobj(hhuvmat.axes3,'Tag','calib_marker');% delete calib points and markers
227    if ~isempty(hhh)
228        delete(hhh);     
229    end
230    hhh=findobj(hhuvmat.axes3,'Tag','calib_points');
231    if ~isempty(hhh)
232        delete(hhh);
233    end
234    set(hhuvmat.FixedLimits,'Value',0)% put FixedLimits option to 'off'
235    set(hhuvmat.FixedLimits,'BackgroundColor',[0.7 0.7 0.7])
236    UserData=get(handles.geometry_calib,'UserData');
237    UserData.XmlInputFile=outputfile;%save the current xml file name
238    set(handles.geometry_calib,'UserData',UserData)
239    uvmat('RootPath_Callback',hObject,eventdata,hhuvmat); %file input with xml reading  in uvmat, show the image in phys coordinates
240    MenuPlot_Callback(hObject, eventdata, handles)
241    set(handles.ListCoord,'Value',index)% indicate in the list the point with max deviation (possible mistake)
242    ListCoord_Callback(hObject, eventdata, handles)
243    figure(handles.geometry_calib)
244end
245
246%------------------------------------------------------------------
247% --- Executes on button press in calibrate_lin.
248function REPLICATE_Callback(hObject, eventdata, handles)
249% %%%%%%Todo: correct on the model of APPLY_Callback%%%%%%%%%%
250%------------------------------------------------------------------------
251calib_cell=get(handles.calib_type,'String');
252val=get(handles.calib_type,'Value');
253Coord_cell=get(handles.ListCoord,'String');
254Object=read_geometry_calib(Coord_cell);
255GeometryCalib=feval(['calib_' calib_cell{val}],Object.Coord,handles);
256
257%read the current calibration points
258Coord_cell=get(handles.ListCoord,'String');
259Object=read_geometry_calib(Coord_cell);
260Coord=Object.Coord;
261
262% apply the calibration, whose type is selected in  handles.calib_type
263if ~isempty(Coord)
264    calib_cell=get(handles.calib_type,'String');
265    val=get(handles.calib_type,'Value');
266    GeometryCalib=feval(['calib_' calib_cell{val}],Coord,handles);
267else
268    msgbox_uvmat('ERROR','No calibration points, abort')
269    return
270end
271
272if ~isempty(Coord)
273    %check error
274    X=Coord(:,1);
275    Y=Coord(:,2);
276    Z=Coord(:,3);
277    x_ima=Coord(:,4);
278    y_ima=Coord(:,5);
279    [Xpoints,Ypoints]=px_XYZ(GeometryCalib,X,Y,Z);
280    GeometryCalib.ErrorRms(1)=sqrt(mean((Xpoints-x_ima).*(Xpoints-x_ima)));
281    [GeometryCalib.ErrorMax(1),index(1)]=max(abs(Xpoints-x_ima));
282    GeometryCalib.ErrorRms(2)=sqrt(mean((Ypoints-y_ima).*(Ypoints-y_ima)));
283    [GeometryCalib.ErrorMax(2),index(2)]=max(abs(Ypoints-y_ima));
284    [EM,ind_dim]=max(GeometryCalib.ErrorMax);
285    index=index(ind_dim);
286    %set the Z position of the reference plane used for calibration
287    Z_plane=[];
288    if isequal(max(Z),min(Z))
289        Z_plane=Z(1);
290    end
291    answer_1=msgbox_uvmat('INPUT_TXT',' Z= ',num2str(Z_plane));
292    Z_plane=str2num(answer_1);
293    GeometryCalib.NbSlice=1;
294    GeometryCalib.SliceCoord=[0 0 Z_plane];
295    %set the coordinate unit
296    unitlist=get(handles.CoordUnit,'String');
297    unit=unitlist{get(handles.CoordUnit,'value')};
298    GeometryCalib.CoordUnit=unit;
299    %record the points
300    GeometryCalib.SourceCalib.PointCoord=Coord;
301%     errormsg=update_imadoc(GeometryCalib,outputfile);% introduce the calibration data in the xml file
302%     if ~strcmp(errormsg,'')
303%         msgbox_uvmat('ERROR',errormsg);
304%     end
305end
306display_intrinsic(GeometryCalib,handles)%display calibration intrinsic parameters
307
308% Display extrinsinc parameters (rotation and translation of camera with  respect to the phys coordiantes)
309set(handles.Tx,'String',num2str(GeometryCalib.Tx_Ty_Tz(1),4))
310set(handles.Ty,'String',num2str(GeometryCalib.Tx_Ty_Tz(2),4))
311set(handles.Tz,'String',num2str(GeometryCalib.Tx_Ty_Tz(3),4))
312set(handles.Phi,'String',num2str(GeometryCalib.omc(1),4))
313set(handles.Theta,'String',num2str(GeometryCalib.omc(2),4))
314set(handles.Psi,'String',num2str(GeometryCalib.omc(3),4))
315
316% indicate the plane of the calibration grid if defined
317% huvmat=findobj(allchild(0),'Name','uvmat');
318% hhuvmat=guidata(huvmat);%handles of elements in the GUI uvmat
319% RootPath='';
320% RootFile='';
321% if ~isempty(hhuvmat.RootPath)& ~isempty(hhuvmat.RootFile)
322%     testhandle=1;
323%     RootPath=get(hhuvmat.RootPath,'String');
324%     RootFile=get(hhuvmat.RootFile,'String');
325%     filebase=fullfile(RootPath,RootFile);
326%     outputfile=[filebase '.xml'];
327% else
328%     question={'save the calibration data and point coordinates in'};
329%     def={fullfile(RootPath,['ObjectCalib.xml'])};
330%     options.Resize='on';
331%     answer=inputdlg(question,'save average in a new file',1,def,options);
332%     outputfile=answer{1};
333% end
334
335%open and read the dataview GUI
336h_dataview=findobj(allchild(0),'name','dataview');
337if ~isempty(h_dataview)
338    delete(h_dataview)
339end
340CalibData=get(handles.geometry_calib,'UserData');%read the calibration image source on the interface userdata
341
342if isfield(CalibData,'XmlInputFile')
343    XmlInput=fileparts(CalibData.XmlInputFile);
344    [XmlInput,filename,ext]=fileparts(XmlInput);
345end
346SubCampaignTest='n'; %default
347testinput=0;
348if isfield(CalibData,'Heading')
349    Heading=CalibData.Heading;
350    if isfield(Heading,'Record') && isequal([filename ext],Heading.Record)
351        [XmlInput,filename,ext]=fileparts(XmlInput);
352    end
353    if isfield(Heading,'Device') && isequal([filename ext],Heading.Device)
354        [XmlInput,filename,ext]=fileparts(XmlInput);
355        Device=Heading.Device;
356    end
357    if isfield(Heading,'Experiment') && isequal([filename ext],Heading.Experiment)
358        [PP,filename,ext]=fileparts(XmlInput);
359    end
360    testinput=0;
361    if isfield(Heading,'SubCampaign') && isequal([filename ext],Heading.SubCampaign)
362        SubCampaignTest='y';
363        testinput=1;
364    elseif isfield(Heading,'Campaign') && isequal([filename ext],Heading.Campaign)
365        testinput=1;
366    end
367end
368if ~testinput
369    filename='PROJETS';%default
370    if isfield(CalibData,'XmlInputFile')
371         [pp,filename]=fileparts(CalibData.XmlInputFile);
372    end
373    while ~isequal(filename,'PROJETS') && numel(filename)>1
374        filename_1=filename;
375        pp_1=pp;
376        [pp,filename]=fileparts(pp);
377    end
378    XmlInput=fullfile(pp_1,filename_1);
379    testinput=1;
380end
381if testinput
382    outcome=dataview(XmlInput,SubCampaignTest,GeometryCalib);
383end
384
385%------------------------------------------------------------------------
386% determine the parameters for a calibration by an affine function (rescaling and offset, no rotation)
387function GeometryCalib=calib_rescale(Coord,handles)
388%------------------------------------------------------------------------
389X=Coord(:,1);
390Y=Coord(:,2);% Z not used
391x_ima=Coord(:,4);
392y_ima=Coord(:,5);
393[px,sx]=polyfit(X,x_ima,1);
394[py,sy]=polyfit(Y,y_ima,1);
395T_x=px(2);
396T_y=py(2);
397GeometryCalib.CalibrationType='rescale';
398GeometryCalib.fx_fy=[px(1) py(1)];
399GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function
400GeometryCalib.Tx_Ty_Tz=[px(2)/px(1) py(2)/py(1) 1];
401%GeometryCalib.R=[1,0,0;0,1,0;0,0,0];
402GeometryCalib.omc=[0 0 0];
403
404%------------------------------------------------------------------------
405% determine the parameters for a calibration by a linear transform matrix (rescale and rotation)
406function GeometryCalib=calib_linear(Coord,handles) %TO UPDATE
407%------------------------------------------------------------------------
408X=Coord(:,1);
409Y=Coord(:,2);% Z not used
410x_ima=Coord(:,4);
411y_ima=Coord(:,5);
412XY_mat=[ones(size(X)) X Y];
413a_X1=XY_mat\x_ima; %transformation matrix for X
414% x1=XY_mat*a_X1;%reconstruction
415% err_X1=max(abs(x1-x_ima));%error
416a_Y1=XY_mat\y_ima;%transformation matrix for X
417% y1=XY_mat*a_Y1;
418% err_Y1=max(abs(y1-y_ima));%error
419R=[a_X1(2),a_X1(3),0;a_Y1(2),a_Y1(3),0;0,0,0];
420norm=abs(det(R));
421GeometryCalib.CalibrationType='linear';
422GeometryCalib.fx_fy=[norm norm];
423GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function
424R=[a_X1(2),a_X1(3),0;a_Y1(2),a_Y1(3),0;0,0,0];
425GeometryCalib.Tx_Ty_Tz=[a_X1(1) a_Y1(1) 1];
426GeometryCalib.R=R/norm;
427GeometryCalib.omc=(180/pi)*[acos(R(1,1)) 0 0];
428%------------------------------------------------------------------------
429% determine the tsai parameters for a view normal to the grid plane
430% NOT USED
431function GeometryCalib=calib_normal(Coord,handles)
432%------------------------------------------------------------------------
433Calib.f1=str2num(get(handles.fx,'String'));
434Calib.f2=str2num(get(handles.fy,'String'));
435Calib.k=str2num(get(handles.kc,'String'));
436Calib.Cx=str2num(get(handles.Cx,'String'));
437Calib.Cy=str2num(get(handles.Cy,'String'));
438%default
439if isempty(Calib.f1)
440    Calib.f1=25/0.012;
441end
442if isempty(Calib.f2)
443    Calib.f2=25/0.012;
444end
445if isempty(Calib.k)
446    Calib.k=0;
447end
448if isempty(Calib.Cx)||isempty(Calib.Cy)
449    huvmat=findobj(allchild(0),'Tag','uvmat');
450    hhuvmat=guidata(huvmat);
451    Calib.Cx=str2num(get(hhuvmat.npx,'String'))/2;
452    Calib.Cx=str2num(get(hhuvmat.npy,'String'))/2;
453end   
454%tsai parameters
455Calib.dpx=0.012;%arbitrary
456Calib.dpy=0.012;
457Calib.sx=Calib.f1*Calib.dpx/(Calib.f2*Calib.dpy);
458Calib.f=Calib.f2*Calib.dpy;
459Calib.kappa1=Calib.k/(Calib.f*Calib.f);
460
461%initial guess
462X=Coord(:,1);
463Y=Coord(:,2);
464Zmean=mean(Coord(:,3));
465x_ima=Coord(:,4)-Calib.Cx;
466y_ima=Coord(:,5)-Calib.Cy;
467XY_mat=[ones(size(X)) X Y];
468a_X1=XY_mat\x_ima; %transformation matrix for X
469a_Y1=XY_mat\y_ima;%transformation matrix for Y
470R=[a_X1(2),a_X1(3),0;a_Y1(2),a_Y1(3),0;0,0,-1];% rotation+ z axis reversal (upward)
471norm=sqrt(det(-R));
472calib_param(1)=0;% quadratic distortion
473calib_param(2)=a_X1(1);
474calib_param(3)=a_Y1(1);
475calib_param(4)=Calib.f/(norm*Calib.dpx)-R(3,3)*Zmean;
476calib_param(5)=angle(a_X1(2)+i*a_X1(3));
477display(['initial guess=' num2str(calib_param)])
478
479%optimise the parameters: minimisation of error
480calib_param = fminsearch(@(calib_param) error_calib(calib_param,Calib,Coord),calib_param)
481
482GeometryCalib.CalibrationType='tsai_normal';
483GeometryCalib.focal=Calib.f;
484GeometryCalib.dpx_dpy=[Calib.dpx Calib.dpy];
485GeometryCalib.Cx_Cy=[Calib.Cx Calib.Cy];
486GeometryCalib.sx=Calib.sx;
487GeometryCalib.kappa1=calib_param(1);
488GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function
489GeometryCalib.Tx_Ty_Tz=[calib_param(2) calib_param(3) calib_param(4)];
490alpha=calib_param(5);
491GeometryCalib.R=[cos(alpha) sin(alpha) 0;-sin(alpha) cos(alpha) 0;0 0 -1];
492
493%------------------------------------------------------------------------
494function GeometryCalib=calib_3D_linear(Coord,handles)
495%TO UPDATE
496%------------------------------------------------------------------
497path_uvmat=which('uvmat');% check the path detected for source file uvmat
498path_UVMAT=fileparts(path_uvmat); %path to UVMAT
499huvmat=findobj(allchild(0),'Tag','uvmat');
500hhuvmat=guidata(huvmat);
501x_1=Coord(:,4:5)'
502X_1=Coord(:,1:3)'
503n_ima=1;
504% check_cond=0;
505
506nx=str2num(get(hhuvmat.npx,'String'));
507ny=str2num(get(hhuvmat.npy,'String'));
508
509est_dist=[0;0;0;0;0];
510est_aspect_ratio=0;
511est_fc=[1;1];
512%fc=[25;25]/0.012;
513center_optim=0;
514run(fullfile(path_UVMAT,'toolbox_calib','go_calib_optim'));
515
516GeometryCalib.CalibrationType='3D_linear';
517GeometryCalib.focal=fc(2);
518GeometryCalib.dpx_dpy=[1 1];
519GeometryCalib.Cx_Cy=cc';
520GeometryCalib.sx=fc(1)/fc(2);
521GeometryCalib.kappa1=-kc(1)/fc(2)^2;
522GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function
523GeometryCalib.Tx_Ty_Tz=Tc_1';
524GeometryCalib.R=Rc_1;
525
526%------------------------------------------------------------------------
527function GeometryCalib=calib_3D_quadr(Coord,handles)
528%------------------------------------------------------------------
529
530path_uvmat=which('uvmat');% check the path detected for source file uvmat
531path_UVMAT=fileparts(path_uvmat); %path to UVMAT
532huvmat=findobj(allchild(0),'Tag','uvmat');
533hhuvmat=guidata(huvmat);
534% check_cond=0;
535coord_files=get(handles.coord_files,'String');
536if ischar(coord_files)
537    coord_files={coord_files};
538end
539if isempty(coord_files{1}) || isequal(coord_files,{''})
540    coord_files={};
541end
542
543%retrieve the calibration points stored in the files listed in the popup list coord_files
544x_1=Coord(:,4:5)';%px coordinates of the ref points
545nx=str2num(get(hhuvmat.npx,'String'));
546ny=str2num(get(hhuvmat.npy,'String'));
547x_1(2,:)=ny-x_1(2,:);%reverse the y image coordinates
548X_1=Coord(:,1:3)';%phys coordinates of the ref points
549n_ima=numel(coord_files)+1;
550if ~isempty(coord_files)
551    msgbox_uvmat('CONFIRMATION',['The xy coordinates of the calibration points in ' num2str(n_ima) ' planes will be used'])
552    for ifile=1:numel(coord_files)
553    t=xmltree(coord_files{ifile});
554    s=convert(t);%convert to matlab structure
555        if isfield(s,'GeometryCalib')
556            if isfield(s.GeometryCalib,'SourceCalib')
557                if isfield(s.GeometryCalib.SourceCalib,'PointCoord')
558                PointCoord=s.GeometryCalib.SourceCalib.PointCoord;
559                Coord_file=[];
560                for i=1:length(PointCoord)
561                    line=str2num(PointCoord{i});
562                    Coord_file(i,4:5)=line(4:5);%px x
563                    Coord_file(i,1:3)=line(1:3);%phys x
564                end
565                eval(['x_' num2str(ifile+1) '=Coord_file(:,4:5)'';']);
566                eval(['x_' num2str(ifile+1) '(2,:)=ny-x_' num2str(ifile+1) '(2,:);' ]);
567                eval(['X_' num2str(ifile+1) '=Coord_file(:,1:3)'';']);
568                end
569            end
570        end
571    end
572end
573
574n_ima=numel(coord_files)+1;
575whos
576
577est_dist=[1;0;0;0;0];
578est_aspect_ratio=1;
579%est_fc=[0;0];
580%fc=[25;25]/0.012;
581center_optim=0;
582run(fullfile(path_UVMAT,'toolbox_calib','go_calib_optim'));
583
584GeometryCalib.CalibrationType='3D_quadr';
585GeometryCalib.fx_fy=fc';
586%GeometryCalib.focal=fc(2);
587%GeometryCalib.dpx_dpy=[1 1];
588GeometryCalib.Cx_Cy=cc';
589%GeometryCalib.sx=fc(1)/fc(2);
590GeometryCalib.kc=kc(1);
591%GeometryCalib.kappa1=-kc(1)/fc(2)^2;
592GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function
593GeometryCalib.Tx_Ty_Tz=Tc_1';
594GeometryCalib.R=Rc_1;
595GeometryCalib.R(2,1:3)=-GeometryCalib.R(2,1:3);%inversion of the y image coordinate
596GeometryCalib.Tx_Ty_Tz(2)=-GeometryCalib.Tx_Ty_Tz(2);%inversion of the y image coordinate
597GeometryCalib.Cx_Cy(2)=ny-GeometryCalib.Cx_Cy(2);%inversion of the y image coordinate
598GeometryCalib.omc=(180/pi)*omc_1;%angles in degrees
599GeometryCalib.ErrorRMS=[];
600GeometryCalib.ErrorMax=[];
601
602
603%------------------------------------------------------------------------
604function GeometryCalib=calib_3D_extrinsic(Coord,handles)
605%------------------------------------------------------------------
606path_uvmat=which('geometry_calib');% check the path detected for source file uvmat
607path_UVMAT=fileparts(path_uvmat); %path to UVMAT
608x_1=double(Coord(:,4:5)');%image coordiantes
609X_1=double(Coord(:,1:3)');% phys coordinates
610huvmat=findobj(allchild(0),'Tag','uvmat');
611hhuvmat=guidata(huvmat);
612ny=str2double(get(hhuvmat.npy,'String'));
613x_1(2,:)=ny-x_1(2,:);%reverse the y image coordinates
614n_ima=1;
615GeometryCalib.CalibrationType='3D_extrinsic';
616GeometryCalib.fx_fy(1)=str2num(get(handles.fx,'String'));
617GeometryCalib.fx_fy(2)=str2num(get(handles.fy,'String'));
618GeometryCalib.Cx_Cy(1)=str2num(get(handles.Cx,'String'));
619GeometryCalib.Cx_Cy(2)=str2num(get(handles.Cy,'String'));
620GeometryCalib.kc=str2num(get(handles.kc,'String'));
621fct_path=fullfile(path_UVMAT,'toolbox_calib');
622addpath(fct_path)
623GeometryCalib.Cx_Cy(2)=ny-GeometryCalib.Cx_Cy(2);%reverse Cx_Cy(2) for calibration (inversion of px ordinate)
624% [omc1,Tc1,Rc1,H,x,ex,JJ] = compute_extrinsic(x_1,X_1,...
625%     [Calib.f Calib.f*Calib.sx]',...
626%     [Calib.Cx Calib.Cy]',...
627%     [-Calib.kappa1*Calib.f^2 0 0 0 0]);
628[omc,Tc1,Rc1,H,x,ex,JJ] = compute_extrinsic(x_1,X_1,...
629    (GeometryCalib.fx_fy)',GeometryCalib.Cx_Cy',[GeometryCalib.kc 0 0 0 0]);
630rmpath(fct_path);
631GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function
632GeometryCalib.Tx_Ty_Tz=Tc1';
633%inversion of z axis
634GeometryCalib.R=Rc1;
635GeometryCalib.R(2,1:3)=-GeometryCalib.R(2,1:3);%inversion of the y image coordinate
636GeometryCalib.Tx_Ty_Tz(2)=-GeometryCalib.Tx_Ty_Tz(2);%inversion of the y image coordinate
637GeometryCalib.Cx_Cy(2)=ny-GeometryCalib.Cx_Cy(2);%inversion of the y image coordinate
638GeometryCalib.omc=(180/pi)*omc';
639%GeometryCalib.R(3,1:3)=-GeometryCalib.R(3,1:3);%inversion for z upward
640
641
642
643%------------------------------------------------------------------------
644%function GeometryCalib=calib_tsai_heikkila(Coord)
645% TEST: NOT IMPLEMENTED
646%------------------------------------------------------------------
647% path_uvmat=which('uvmat');% check the path detected for source file uvmat
648% path_UVMAT=fileparts(path_uvmat); %path to UVMAT
649% path_calib=fullfile(path_UVMAT,'toolbox_calib_heikkila');
650% addpath(path_calib)
651% npoints=size(Coord,1);
652% Coord(:,1:3)=10*Coord(:,1:3);
653% Coord=[Coord zeros(npoints,2) -ones(npoints,1)];
654% [par,pos,iter,res,er,C]=cacal('dalsa',Coord);
655% GeometryCalib.CalibrationType='tsai';
656% GeometryCalib.focal=par(2);
657
658
659%--------------------------------------------------------------------------
660function GeometryCalib=calib_tsai(Coord,handles)% old version using gauthier's bianry ccal_fo
661% NOT USED
662%------------------------------------------------------------------------
663%TSAI
664path_uvmat=which('uvmat');% check the path detected for source file uvmat
665path_UVMAT=fileparts(path_uvmat); %path to UVMAT
666xmlfile=fullfile(path_UVMAT,'PARAM.xml');%name of the file containing names of binary executables
667if exist(xmlfile,'file')
668    t=xmltree(xmlfile);% read the (xml) file containing names of binary executables
669    sparam=convert(t);% convert to matlab structure
670end
671if ~isfield(sparam,'GeometryCalibBin')
672    msgbox_uvmat('ERROR',['calibration program <GeometryCalibBin> undefined in parameter file ' xmlfile])
673    return
674end
675Tsai_exe=sparam.GeometryCalibBin;
676if ~exist(Tsai_exe,'file')%the binary is defined in /bin, default setting
677     Tsai_exe=fullfile(path_UVMAT,Tsai_exe);
678end
679if ~exist(Tsai_exe,'file')
680    msgbox_uvmat('ERROR',['calibration program ' sparam.GeometryCalibBin ' defined in PARAM.xml does not exist'])
681    return
682end
683
684textcoord=num2str(Coord,4);
685dlmwrite('t.txt',textcoord,''); 
686% ['!' Tsai_exe ' -fx 0 -fy t.txt']
687eval(['!' Tsai_exe ' -f t.txt > tsaicalib.log']);
688if ~exist('calib.dat','file')
689    msgbox_uvmat('ERROR','no output from calibration program Tsai_exe: possibly too few points')
690end
691calibdat=dlmread('calib.dat');
692delete('calib.dat')
693%delete('t.txt')
694GeometryCalib.CalibrationType='tsai';
695GeometryCalib.focal=calibdat(10);
696GeometryCalib.dpx_dpy=[calibdat(5) calibdat(6)];
697GeometryCalib.Cx_Cy=[calibdat(7) calibdat(8)];
698GeometryCalib.sx=calibdat(9);
699GeometryCalib.kappa1=calibdat(11);
700GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function
701GeometryCalib.Tx_Ty_Tz=[calibdat(12) calibdat(13) calibdat(14)];
702Rx_Ry_Rz=calibdat([15:17]);
703sa = sin(Rx_Ry_Rz(1)) ;
704ca=cos(Rx_Ry_Rz(1));
705sb=sin(Rx_Ry_Rz(2));
706cb =cos(Rx_Ry_Rz(2));
707sg =sin(Rx_Ry_Rz(3));
708cg =cos(Rx_Ry_Rz(3));
709r1 = cb * cg;
710r2 = cg * sa * sb - ca * sg;
711r3 = sa * sg + ca * cg * sb;
712r4 = cb * sg;
713r5 = sa * sb * sg + ca * cg;
714r6 = ca * sb * sg - cg * sa;
715r7 = -sb;
716r8 = cb * sa;
717r9 = ca * cb;
718%EN DEDUIRE MATRICE R ??
719GeometryCalib.R=[r1,r2,r3;r4,r5,r6;r7,r8,r9];
720
721%------------------------------------------------------------------------
722% --- determine the rms of calibration error
723function ErrorRms=error_calib(calib_param,Calib,Coord)
724%calib_param: vector of free calibration parameters (to optimise)
725%Calib: structure of the given calibration parameters
726%Coord: list of phys coordinates (columns 1-3, and pixel coordinates (columns 4-5)
727Calib.f=25;
728Calib.dpx=0.012;
729Calib.dpy=0.012;
730Calib.sx=1;
731Calib.Cx=512;
732Calib.Cy=512;
733Calib.kappa1=calib_param(1);
734Calib.Tx=calib_param(2);
735Calib.Ty=calib_param(3);
736Calib.Tz=calib_param(4);
737alpha=calib_param(5);
738Calib.R=[cos(alpha) sin(alpha) 0;-sin(alpha) cos(alpha) 0;0 0 -1];
739
740X=Coord(:,1);
741Y=Coord(:,2);
742Z=Coord(:,3);
743x_ima=Coord(:,4);
744y_ima=Coord(:,5);
745[Xpoints,Ypoints]=px_XYZ(Calib,X,Y,Z);
746ErrorRms(1)=sqrt(mean((Xpoints-x_ima).*(Xpoints-x_ima)));
747ErrorRms(2)=sqrt(mean((Ypoints-y_ima).*(Ypoints-y_ima)));
748ErrorRms=mean(ErrorRms);
749
750%------------------------------------------------------------------------
751function XImage_Callback(hObject, eventdata, handles)
752%------------------------------------------------------------------------
753update_list(hObject, eventdata,handles)
754
755%------------------------------------------------------------------------
756function YImage_Callback(hObject, eventdata, handles)
757%------------------------------------------------------------------------
758update_list(hObject, eventdata,handles)
759
760%------------------------------------------------------------------------
761% --- Executes on button press in STORE.
762function STORE_Callback(hObject, eventdata, handles)
763Coord_cell=get(handles.ListCoord,'String');
764Object=read_geometry_calib(Coord_cell);
765unitlist=get(handles.CoordUnit,'String');
766unit=unitlist{get(handles.CoordUnit,'value')};
767GeometryCalib.CoordUnit=unit;
768GeometryCalib.SourceCalib.PointCoord=Object.Coord;
769huvmat=findobj(allchild(0),'Name','uvmat');
770hhuvmat=guidata(huvmat);%handles of elements in the GUI uvmat
771RootPath='';
772RootFile='';
773if ~isempty(hhuvmat.RootPath)& ~isempty(hhuvmat.RootFile)
774    testhandle=1;
775    RootPath=get(hhuvmat.RootPath,'String');
776    RootFile=get(hhuvmat.RootFile,'String');
777    filebase=fullfile(RootPath,RootFile);
778    while exist([filebase '.xml'],'file')
779        filebase=[filebase '~'];
780    end
781    outputfile=[filebase '.xml'];
782    errormsg=update_imadoc(GeometryCalib,outputfile);
783    if ~strcmp(errormsg,'')
784        msgbox_uvmat('ERROR',errormsg);
785    end
786    listfile=get(handles.coord_files,'string');
787    if isequal(listfile,{''})
788        listfile={outputfile};
789    else
790        listfile=[listfile;{outputfile}]%update the list of coord files
791    end
792    set(handles.coord_files,'string',listfile);
793end
794set(handles.ListCoord,'Value',1)% refresh the display of coordinates
795set(handles.ListCoord,'String',{'......'})
796
797% --------------------------------------------------------------------
798% --- Executes on button press in CLEAR_PTS: clear the list of calibration points
799function CLEAR_PTS_Callback(hObject, eventdata, handles)
800% --------------------------------------------------------------------
801set(handles.ListCoord,'Value',1)% refresh the display of coordinates
802set(handles.ListCoord,'String',{'......'})
803MenuPlot_Callback(hObject, eventdata, handles)
804
805%------------------------------------------------------------------------
806% --- Executes on button press in CLEAR.
807function CLEAR_Callback(hObject, eventdata, handles)
808%------------------------------------------------------------------------
809set(handles.coord_files,'Value',1)
810set(handles.coord_files,'String',{''})
811
812%------------------------------------------------------------------------
813function XObject_Callback(hObject, eventdata, handles)
814%------------------------------------------------------------------------
815update_list(hObject, eventdata,handles)
816
817%------------------------------------------------------------------------
818function YObject_Callback(hObject, eventdata, handles)
819%------------------------------------------------------------------------
820update_list(hObject, eventdata,handles)
821
822%------------------------------------------------------------------------
823function ZObject_Callback(hObject, eventdata, handles)
824%------------------------------------------------------------------------
825update_list(hObject, eventdata,handles)
826
827%------------------------------------------------------------------------
828function update_list(hObject, eventdata, handles)
829%------------------------------------------------------------------------
830str4=get(handles.XImage,'String');
831str5=get(handles.YImage,'String');
832str1=get(handles.XObject,'String');
833tt=double(str1);
834str2=get(handles.YObject,'String');
835str3=get(handles.ZObject,'String');
836if ~isempty(str1) & ~isequal(double(str1),32) & (isempty(str3)|isequal(double(str3),32))
837    str3='0';%put z to 0 by default
838end
839strline=[str1 '    |    ' str2 '    |    ' str3 '    |    ' str4 '    |    ' str5];
840Coord=get(handles.ListCoord,'String');
841val=get(handles.ListCoord,'Value');
842Coord{val}=strline;
843set(handles.ListCoord,'String',Coord)
844%update the plot
845ListCoord_Callback(hObject, eventdata, handles)
846MenuPlot_Callback(hObject, eventdata, handles)
847
848%------------------------------------------------------------------------
849% --- Executes on selection change in ListCoord.
850function ListCoord_Callback(hObject, eventdata, handles)
851%------------------------------------------------------------------------
852huvmat=findobj(allchild(0),'Name','uvmat');%find the current uvmat interface handle
853hplot=findobj(huvmat,'Tag','axes3');%main plotting axis of uvmat
854hhh=findobj(hplot,'Tag','calib_marker');
855Coord_cell=get(handles.ListCoord,'String');
856val=get(handles.ListCoord,'Value');
857if numel(val)>1
858    return %no action if several lines have been selected
859end
860coord_str=Coord_cell{val};
861k=findstr('|',coord_str);
862if isempty(k)%last line '.....' selected
863    if ~isempty(hhh)
864        delete(hhh)%delete the circle marker
865    end
866    return
867end
868%fill the edit boxex
869set(handles.XObject,'String',coord_str(1:k(1)-5))
870set(handles.YObject,'String',coord_str(k(1)+5:k(2)-5))
871set(handles.ZObject,'String',coord_str(k(2)+5:k(3)-5))
872set(handles.XImage,'String',coord_str(k(3)+5:k(4)-5))
873set(handles.YImage,'String',coord_str(k(4)+5:end))
874h_menu_coord=findobj(huvmat,'Tag','transform_fct');
875menu=get(h_menu_coord,'String');
876choice=get(h_menu_coord,'Value');
877if iscell(menu)
878    option=menu{choice};
879else
880    option='px'; %default
881end
882if isequal(option,'phys')
883    XCoord=str2num(coord_str(1:k(1)-5));
884    YCoord=str2num(coord_str(k(1)+5:k(2)-5));
885elseif isequal(option,'px')|| isequal(option,'')
886    XCoord=str2num(coord_str(k(3)+5:k(4)-5));
887    YCoord=str2num(coord_str(k(4)+5:end));
888else
889    msgbox_uvmat('ERROR','the choice in menu_coord of uvmat must be px or phys ')
890end
891if isempty(XCoord)||isempty(YCoord)
892     if ~isempty(hhh)
893        delete(hhh)%delete the circle marker
894    end
895    return
896end
897xlim=get(hplot,'XLim');
898ylim=get(hplot,'YLim');
899ind_range=max(abs(xlim(2)-xlim(1)),abs(ylim(end)-ylim(1)))/20;%defines the size of the circle marker
900if isempty(hhh)
901    axes(hplot)
902    rectangle('Curvature',[1 1],...
903              'Position',[XCoord-ind_range/2 YCoord-ind_range/2 ind_range ind_range],'EdgeColor','m',...
904              'LineStyle','-','Tag','calib_marker');
905else
906    set(hhh,'Position',[XCoord-ind_range/2 YCoord-ind_range/2 ind_range ind_range])
907end
908
909%------------------------------------------------------------------------
910% --- Executes on selection change in edit_append.
911function edit_append_Callback(hObject, eventdata, handles)
912%------------------------------------------------------------------------
913choice=get(handles.edit_append,'Value');
914if choice
915    set(handles.edit_append,'BackgroundColor',[1 1 0])
916else
917    set(handles.edit_append,'BackgroundColor',[0.7 0.7 0.7])
918end
919   
920function NEW_Callback(hObject, eventdata, handles)
921%A METTRE SOUS UN BOUTON
922huvmat=findobj(allchild(0),'Name','uvmat');
923hchild=get(huvmat,'children');
924hcoord=findobj(hchild,'Tag','menu_coord');
925coordtype=get(hcoord,'Value');
926haxes=findobj(hchild,'Tag','axes3');
927AxeData=get(haxes,'UserData');
928if ~isequal(hcoord,2)
929    set(hcoord,'Value',2)
930    huvmat=uvmat(AxeData);
931    'relancer uvmat';
932end
933if ~isfield(AxeData,'ZoomAxes')
934    msgbox_uvmat('ERROR','first draw a window around a grid marker')
935    return
936end
937XLim=get(AxeData.ZoomAxes,'XLim');
938YLim=get(AxeData.ZoomAxes,'YLim');
939np=size(AxeData.A);
940ind_sub_x=round(XLim);
941ind_sub_y=np(1)-round(YLim);
942Mfiltre=AxeData.A([ind_sub_y(2):ind_sub_y(1)] ,ind_sub_x,:);
943Mfiltre_norm=double(Mfiltre);
944Mfiltre_norm=Mfiltre_norm/sum(sum(Mfiltre_norm));
945Mfiltre_norm=100*(Mfiltre_norm-mean(mean(Mfiltre_norm)));
946Atype=class(AxeData.A);
947Data.NbDim=2;
948Data.A=filter2(Mfiltre_norm,double(AxeData.A));
949Data.A=feval(Atype,Data.A);
950Data.AName='image';
951Data.AX=AxeData.AX;
952Data.AY=AxeData.AY;
953Data.CoordType='px';
954plot_field(Data)
955
956%------------------------------------------------------------------------
957function MenuPlot_Callback(hObject, eventdata, handles)
958%------------------------------------------------------------------------
959huvmat=findobj(allchild(0),'Name','uvmat');%find the current uvmat interface handle
960UvData=get(huvmat,'UserData');%Data associated to the current uvmat interface
961hhuvmat=guidata(huvmat); %handles of GUI elements in uvmat
962hplot=findobj(huvmat,'Tag','axes3');%main plotting axis of uvmat
963h_menu_coord=findobj(huvmat,'Tag','transform_fct');
964menu=get(h_menu_coord,'String');
965choice=get(h_menu_coord,'Value');
966if iscell(menu)
967    option=menu{choice};
968else
969    option='px'; %default
970end
971Coord_cell=get(handles.ListCoord,'String');
972ObjectData=read_geometry_calib(Coord_cell);
973%ObjectData=read_geometry_calib(handles);%read the interface input parameters defining the object
974if ~isempty(ObjectData.Coord)
975    if isequal(option,'phys')
976        ObjectData.Coord=ObjectData.Coord(:,[1:3]);
977    elseif isequal(option,'px')||isequal(option,'')
978        ObjectData.Coord=ObjectData.Coord(:,[4:5]);
979    else
980        msgbox_uvmat('ERROR','the choice in menu_coord of uvmat must be '''', px or phys ')
981    end
982end
983axes(hhuvmat.axes3)
984hh=findobj('Tag','calib_points');
985if  ~isempty(ObjectData.Coord) && isempty(hh)
986    hh=line(ObjectData.Coord(:,1),ObjectData.Coord(:,2),'Color','m','Tag','calib_points','LineStyle','.','Marker','+');
987elseif isempty(ObjectData.Coord)%empty list of points, suppress the plot
988    delete(hh)
989else
990    set(hh,'XData',ObjectData.Coord(:,1))
991    set(hh,'YData',ObjectData.Coord(:,2))
992end
993pause(.1)
994figure(handles.geometry_calib)
995
996% --------------------------------------------------------------------
997function MenuHelp_Callback(hObject, eventdata, handles)
998path_to_uvmat=which('uvmat');% check the path of uvmat
999pathelp=fileparts(path_to_uvmat);
1000helpfile=fullfile(pathelp,'uvmat_doc','uvmat_doc.html');
1001if isempty(dir(helpfile)), msgbox_uvmat('ERROR','Please put the help file uvmat_doc.html in the sub-directory /uvmat_doc of the UVMAT package')
1002else
1003   addpath (fullfile(pathelp,'uvmat_doc'))
1004   web([helpfile '#geometry_calib'])
1005end
1006
1007%------------------------------------------------------------------------
1008function MenuCreateGrid_Callback(hObject, eventdata, handles)
1009%------------------------------------------------------------------------
1010%hcalib=get(handles.calib_type,'parent');%handles of the GUI geometry_calib
1011CalibData=get(handles.geometry_calib,'UserData');
1012Tinput=[];%default
1013if isfield(CalibData,'grid')
1014    Tinput=CalibData.grid;
1015end
1016[T,CalibData.grid]=create_grid(Tinput);%display the GUI create_grid
1017set(handles.geometry_calib,'UserData',CalibData)
1018
1019%grid in phys space
1020Coord=get(handles.ListCoord,'String');
1021val=get(handles.ListCoord,'Value');
1022data=read_geometry_calib(Coord);
1023%nbpoints=size(data.Coord,1); %nbre of calibration points
1024data.Coord(val:val+size(T,1)-1,1:3)=T(end:-1:1,:);%update the existing list of phys coordinates from the GUI create_grid
1025% for i=1:nbpoints
1026%    for j=1:5
1027%           Coord{i,j}=num2str(data.Coord(i,j),4);%display coordiantes with 4 digits
1028%    end
1029% end
1030%update the phys coordinates starting from the selected point (down in the
1031Coord(end,:)=[]; %remove last string '.....'
1032for i=1:size(data.Coord,1)
1033    for j=1:5
1034          Coord{i,j}=num2str(data.Coord(i,j),4);%display coordiantes with 4 digits
1035    end
1036end
1037
1038%size(data.Coord,1)
1039Tabchar=cell2tab(Coord,'    |    ');
1040Tabchar=[Tabchar ;{'......'}];
1041set(handles.ListCoord,'String',Tabchar)
1042
1043% -----------------------------------------------------------------------
1044% --- automatic grid dectection from local maxima of the images
1045function MenuDetectGrid_Callback(hObject, eventdata, handles)
1046%------------------------------------------------------------------------
1047CalibData=get(handles.geometry_calib,'UserData');%get information stored on the GUI geometry_calib
1048grid_input=[];%default
1049if isfield(CalibData,'grid')
1050    grid_input=CalibData.grid;%retrieve the previously used grid
1051end
1052[T,CalibData.grid]=create_grid(grid_input,'detect_grid');%display the GUI create_grid, read the set of phys coordinates T
1053
1054set(handles.geometry_calib,'UserData',CalibData)%store the phys grid parameters for later use
1055
1056%read the four last point coordinates in pixels
1057Coord_cell=get(handles.ListCoord,'String');%read list of coordinates on geometry_calib
1058data=read_geometry_calib(Coord_cell);
1059nbpoints=size(data.Coord,1); %nbre of calibration points
1060if nbpoints~=4
1061    msgbox_uvmat('ERROR','four points must be selected by the mouse, beginning by the new x axis, to delimitate the phs grid area')
1062    return
1063end
1064corners_X=(data.Coord(end:-1:end-3,4)); %pixel absissa of the four corners
1065corners_Y=(data.Coord(end:-1:end-3,5));
1066
1067%reorder the last two points (the two first in the list) if needed
1068angles=angle((corners_X-corners_X(1))+i*(corners_Y-corners_Y(1)));
1069if abs(angles(4)-angles(2))>abs(angles(3)-angles(2))
1070      X_end=corners_X(4);
1071      Y_end=corners_Y(4);
1072      corners_X(4)=corners_X(3);
1073      corners_Y(4)=corners_Y(3);
1074      corners_X(3)=X_end;
1075      corners_Y(3)=Y_end;
1076end
1077
1078%read the current image, displayed in the GUI uvmat
1079huvmat=findobj(allchild(0),'Name','uvmat');
1080UvData=get(huvmat,'UserData');
1081A=UvData.Field.A;
1082npxy=size(A);
1083%linear transform on the current image
1084X=[CalibData.grid.x_0 CalibData.grid.x_1 CalibData.grid.x_0 CalibData.grid.x_1]';%corner absissa in the phys coordinates
1085Y=[CalibData.grid.y_0 CalibData.grid.y_0 CalibData.grid.y_1 CalibData.grid.y_1]';%corner ordinates in the phys coordinates
1086
1087%calculate transform matrices:
1088% reference: http://alumni.media.mit.edu/~cwren/interpolator/ by Christopher R. Wren
1089B = [ X Y ones(size(X)) zeros(4,3)        -X.*corners_X -Y.*corners_X ...
1090      zeros(4,3)        X Y ones(size(X)) -X.*corners_Y -Y.*corners_Y ];
1091B = reshape (B', 8 , 8 )';
1092D = [ corners_X , corners_Y ];
1093D = reshape (D', 8 , 1 );
1094l = inv(B' * B) * B' * D;
1095Amat = reshape([l(1:6)' 0 0 1 ],3,3)';
1096C = [l(7:8)' 1];
1097
1098%GeometryCalib.CalibrationType='tsai';
1099%GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function
1100% GeometryCalib.f=1;
1101% GeometryCalib.dpx=1;
1102% GeometryCalib.dpy=1;
1103% GeometryCalib.sx=1;
1104% GeometryCalib.Cx=0;
1105% GeometryCalib.Cy=0;
1106% GeometryCalib.kappa1=0;
1107% GeometryCalib.Tx=Amat(1,3);
1108% GeometryCalib.Ty=Amat(2,3);
1109% GeometryCalib.Tz=1;
1110% GeometryCalib.R=[Amat(1,1),Amat(1,2),0;Amat(2,1),Amat(2,2),0;C(1),C(2),0];
1111%
1112% [Amod,Rangx,Rangy]=phys_Ima(A-min(min(A)),GeometryCalib,0);
1113
1114GeometryCalib.fx_fy=[1 1];
1115GeometryCalib.Tx_Ty_Tz=[Amat(1,3) Amat(2,3) 1];
1116GeometryCalib.R=[Amat(1,1),Amat(1,2),0;Amat(2,1),Amat(2,2),0;C(1),C(2),0];
1117path_uvmat=which('uvmat');% check the path detected for source file uvmat
1118path_UVMAT=fileparts(path_uvmat); %path to UVMAT
1119addpath(fullfile(path_UVMAT,'transform_field'))
1120Data.ListVarName={'AY','AX','A'};
1121Data.VarDimName={'AY','AX',{'AY','AX'}};
1122if ndims(A)==3
1123    A=mean(A,3);
1124end
1125Data.A=A-min(min(A));
1126Data.AY=[npxy(1)-0.5 0.5];
1127Data.AX=[0.5 npxy(2)];
1128Data.CoordType='px';
1129Calib.GeometryCalib=GeometryCalib;
1130DataOut=phys(Data,Calib)
1131rmpath(fullfile(path_UVMAT,'transform_field'))
1132Amod=DataOut.A;
1133Rangx=DataOut.AX;
1134Rangy=DataOut.AY;
1135% GeometryCalib.dpx=1;
1136% GeometryCalib.dpy=1;
1137% GeometryCalib.sx=1;
1138% GeometryCalib.Cx=0;
1139% GeometryCalib.Cy=0;
1140% GeometryCalib.kappa1=0;
1141% GeometryCalib.Tx=Amat(1,3);
1142% GeometryCalib.Ty=Amat(2,3);
1143% GeometryCalib.Tz=1;
1144% GeometryCalib.R=[Amat(1,1),Amat(1,2),0;Amat(2,1),Amat(2,2),0;C(1),C(2),0];
1145%
1146% [Amod,Rangx,Rangy]=phys_Ima(A-min(min(A)),GeometryCalib,0);
1147
1148
1149Amod=double(Amod);
1150% figure(12) %display corrected image
1151% Amax=max(max(Amod));
1152% image(Rangx,Rangy,uint8(255*Amod/Amax))
1153
1154Dx=(Rangx(2)-Rangx(1))/(npxy(2)-1); %x mesh in real space
1155Dy=(Rangy(2)-Rangy(1))/(npxy(1)-1); %y mesh in real space
1156ind_range_x=ceil(abs(GeometryCalib.R(1,1)*CalibData.grid.Dx/3))% range of search of image ma around each point obtained by linear interpolation from the marked points
1157ind_range_y=ceil(abs(GeometryCalib.R(2,2)*CalibData.grid.Dy/3))% range of search of image ma around each point obtained by linear interpolation from the marked points
1158nbpoints=size(T,1);
1159for ipoint=1:nbpoints
1160    i0=1+round((T(ipoint,1)-Rangx(1))/Dx);%round(Xpx(ipoint));
1161    j0=1+round((T(ipoint,2)-Rangy(1))/Dy);%round(Xpx(ipoint));
1162    j0min=max(j0-ind_range_y,1);
1163    j0max=min(j0+ind_range_y,size(Amod,1));
1164    i0min=max(i0-ind_range_x,1);
1165    i0max=min(i0+ind_range_x,size(Amod,2));
1166    Asub=Amod(j0min:j0max,i0min:i0max);
1167    x_profile=sum(Asub,1);
1168    y_profile=sum(Asub,2);
1169    [Amax,ind_x_max]=max(x_profile);
1170    [Amax,ind_y_max]=max(y_profile);
1171    %sub-pixel improvement using moments
1172    x_shift=0;
1173    y_shift=0;
1174    %if ind_x_max+2<=2*ind_range_x+1 && ind_x_max-2>=1
1175    if ind_x_max+2<=numel(x_profile) && ind_x_max-2>=1
1176        Atop=x_profile(ind_x_max-2:ind_x_max+2);
1177        x_shift=sum(Atop.*[-2 -1 0 1 2])/sum(Atop);
1178    end
1179    if ind_y_max+2<=numel(y_profile) && ind_y_max-2>=1
1180        Atop=y_profile(ind_y_max-2:ind_y_max+2);
1181        y_shift=sum(Atop.*[-2 -1 0 1 2]')/sum(Atop);
1182    end
1183    Delta(ipoint,1)=(x_shift+ind_x_max+i0min-i0-1)*Dx;%shift from the initial guess
1184    Delta(ipoint,2)=(y_shift+ind_y_max+j0min-j0-1)*Dy;
1185end
1186Tmod=T(:,(1:2))+Delta;
1187[Xpx,Ypx]=px_XYZ(GeometryCalib,Tmod(:,1),Tmod(:,2));
1188for ipoint=1:nbpoints
1189     Coord{ipoint,1}=num2str(T(ipoint,1),4);%display coordiantes with 4 digits
1190     Coord{ipoint,2}=num2str(T(ipoint,2),4);%display coordiantes with 4 digits
1191     Coord{ipoint,3}=num2str(T(ipoint,3),4);%display coordiantes with 4 digits;
1192     Coord{ipoint,4}=num2str(Xpx(ipoint),4);%display coordiantes with 4 digits
1193     Coord{ipoint,5}=num2str(Ypx(ipoint),4);%display coordiantes with 4 digits
1194end
1195Tabchar=cell2tab(Coord(end:-1:1,:),'    |    ');
1196Tabchar=[Tabchar ;{'......'}];
1197set(handles.ListCoord,'Value',1)
1198set(handles.ListCoord,'String',Tabchar)
1199MenuPlot_Callback(hObject, eventdata, handles)
1200
1201%-----------------------------------------------------------------------
1202function MenuTranslatePoints_Callback(hObject, eventdata, handles)
1203%-----------------------------------------------------------------------
1204%hcalib=get(handles.calib_type,'parent');%handles of the GUI geometry_calib
1205CalibData=get(handles.geometry_calib,'UserData');
1206Tinput=[];%default
1207if isfield(CalibData,'translate')
1208    Tinput=CalibData.translate;
1209end
1210T=translate_points(Tinput);%display translate_points GUI and get shift parameters
1211CalibData.translate=T;
1212set(handles.geometry_calib,'UserData',CalibData)
1213%translation
1214Coord_cell=get(handles.ListCoord,'String');
1215data=read_geometry_calib(Coord_cell);
1216data.Coord(:,1)=T(1)+data.Coord(:,1);
1217data.Coord(:,2)=T(2)+data.Coord(:,2);
1218data.Coord(:,3)=T(3)+data.Coord(:,3);
1219data.Coord(:,[4 5])=data.Coord(:,[4 5]);
1220for i=1:size(data.Coord,1)
1221    for j=1:5
1222          Coord{i,j}=num2str(data.Coord(i,j),4);%phys x,y,z
1223   end
1224end
1225Tabchar=cell2tab(Coord,'    |    ');
1226Tabchar=[Tabchar; {'.....'}];
1227%set(handles.ListCoord,'Value',1)
1228set(handles.ListCoord,'String',Tabchar)
1229
1230
1231% --------------------------------------------------------------------
1232function MenuRotatePoints_Callback(hObject, eventdata, handles)
1233%hcalib=get(handles.calib_type,'parent');%handles of the GUI geometry_calib
1234CalibData=get(handles.geometry_calib,'UserData');
1235Tinput=[];%default
1236if isfield(CalibData,'rotate')
1237    Tinput=CalibData.rotate;
1238end
1239T=rotate_points(Tinput);%display translate_points GUI and get shift parameters
1240CalibData.rotate=T;
1241set(handles.geometry_calib,'UserData',CalibData)
1242%-----------------------------------------------------
1243%rotation
1244Phi=T(1);
1245O_x=0;%default
1246O_y=0;%default
1247if numel(T)>=2
1248    O_x=T(2);%default
1249end
1250if numel(T)>=3
1251    O_y=T(3);%default
1252end
1253Coord_cell=get(handles.ListCoord,'String');
1254data=read_geometry_calib(Coord_cell);
1255r1=cos(pi*Phi/180);
1256r2=-sin(pi*Phi/180);
1257r3=sin(pi*Phi/180);
1258r4=cos(pi*Phi/180);
1259x=data.Coord(:,1)-O_x;
1260y=data.Coord(:,2)-O_y;
1261data.Coord(:,1)=r1*x+r2*y;
1262data.Coord(:,2)=r3*x+r4*y;
1263% data.Coord(:,[4 5])=data.Coord(:,[4 5]);
1264for i=1:size(data.Coord,1)
1265    for j=1:5
1266          Coord{i,j}=num2str(data.Coord(i,j),4);%phys x,y,z
1267   end
1268end
1269Tabchar=cell2tab(Coord,'    |    ');
1270Tabchar=[Tabchar;{'......'}];
1271set(handles.ListCoord,'Value',1)
1272set(handles.ListCoord,'String',Tabchar)
1273
1274
1275% %------------------------------------------------------------------------
1276% % --- Executes on button press in rotation.
1277% function rotation_Callback(hObject, eventdata, handles)
1278% %------------------------------------------------------------------------
1279% angle_rot=(pi/180)*str2num(get(handles.Phi,'String'));
1280% Coord_cell=get(handles.ListCoord,'String');
1281% data=read_geometry_calib(Coord_cell);
1282% data.Coord(:,1)=cos(angle_rot)*data.Coord(:,1)+sin(angle_rot)*data.Coord(:,2);
1283% data.Coord(:,1)=-sin(angle_rot)*data.Coord(:,1)+cos(angle_rot)*data.Coord(:,2);
1284% set(handles.XObject,'String',num2str(data.Coord(:,1),4));
1285% set(handles.YObject,'String',num2str(data.Coord(:,2),4));
1286
1287
1288%------------------------------------------------------------------------
1289% image transform from px to phys
1290%INPUT:
1291%Zindex: index of plane
1292% function [A_out,Rangx,Rangy]=phys_Ima(A,Calib,ZIndex)
1293% %------------------------------------------------------------------------
1294% xcorner=[];
1295% ycorner=[];
1296% npx=[];
1297% npy=[];
1298% siz=size(A)
1299% npx=[npx siz(2)];
1300% npy=[npy siz(1)]
1301% xima=[0.5 siz(2)-0.5 0.5 siz(2)-0.5];%image coordinates of corners
1302% yima=[0.5 0.5 siz(1)-0.5 siz(1)-0.5];
1303% [xcorner,ycorner]=phys_XYZ(Calib,xima,yima,ZIndex);%corresponding physical coordinates
1304% Rangx(1)=min(xcorner);
1305% Rangx(2)=max(xcorner);
1306% Rangy(2)=min(ycorner);
1307% Rangy(1)=max(ycorner);
1308% test_multi=(max(npx)~=min(npx)) | (max(npy)~=min(npy));
1309% npx=max(npx);
1310% npy=max(npy);
1311% x=linspace(Rangx(1),Rangx(2),npx);
1312% y=linspace(Rangy(1),Rangy(2),npy);
1313% [X,Y]=meshgrid(x,y);%grid in physical coordiantes
1314% vec_B=[];
1315%
1316% zphys=0; %default
1317% if isfield(Calib,'SliceCoord') %.Z= index of plane
1318%    SliceCoord=Calib.SliceCoord(ZIndex,:);
1319%    zphys=SliceCoord(3); %to generalize for non-parallel planes
1320% end
1321% [XIMA,YIMA]=px_XYZ(Calib,X,Y,zphys);%corresponding image indices for each point in the real space grid
1322% XIMA=reshape(round(XIMA),1,npx*npy);%indices reorganized in 'line'
1323% YIMA=reshape(round(YIMA),1,npx*npy);
1324% flagin=XIMA>=1 & XIMA<=npx & YIMA >=1 & YIMA<=npy;%flagin=1 inside the original image
1325% testuint8=isa(A,'uint8');
1326% testuint16=isa(A,'uint16');
1327% if numel(siz)==2 %(B/W images)
1328%     vec_A=reshape(A,1,npx*npy);%put the original image in line
1329%     ind_in=find(flagin);
1330%     ind_out=find(~flagin);
1331%     ICOMB=((XIMA-1)*npy+(npy+1-YIMA));
1332%     ICOMB=ICOMB(flagin);%index corresponding to XIMA and YIMA in the aligned original image vec_A
1333%     vec_B(ind_in)=vec_A(ICOMB);
1334%     vec_B(ind_out)=zeros(size(ind_out));
1335%     A_out=reshape(vec_B,npy,npx);%new image in real coordinates
1336% elseif numel(siz)==3     
1337%     for icolor=1:siz(3)
1338%         vec_A=reshape(A{icell}(:,:,icolor),1,npx*npy);%put the original image in line
1339%         ind_in=find(flagin);
1340%         ind_out=find(~flagin);
1341%         ICOMB=((XIMA-1)*npy+(npy+1-YIMA));
1342%         ICOMB=ICOMB(flagin);%index corresponding to XIMA and YIMA in the aligned original image vec_A
1343%         vec_B(ind_in)=vec_A(ICOMB);
1344%         vec_B(ind_out)=zeros(size(ind_out));
1345%         A_out(:,:,icolor)=reshape(vec_B,npy,npx);%new image in real coordinates
1346%     end
1347% end
1348% if testuint8
1349%     A_out=uint8(A_out);
1350% end
1351% if testuint16
1352%     A_out=uint16(A_out);
1353% end
1354
1355%------------------------------------------------------------------------
1356% pointwise transform from px to phys
1357%INPUT:
1358%Z: index of plane
1359% function [Xphys,Yphys,Zphys]=phys_XYZ(Calib,X,Y,Z)
1360% %------------------------------------------------------------------------
1361% if exist('Z','var')& isequal(Z,round(Z))& Z>0 & isfield(Calib,'SliceCoord')&length(Calib.SliceCoord)>=Z
1362%     Zindex=Z;
1363%     Zphys=Calib.SliceCoord(Zindex,3);%GENERALISER AUX CAS AVEC ANGLE
1364% else
1365%     Zphys=0;
1366% end
1367% if ~exist('X','var')||~exist('Y','var')
1368%     Xphys=[];
1369%     Yphys=[];%default
1370%     return
1371% end
1372% Xphys=X;%default
1373% Yphys=Y;
1374% %image transform
1375% if isfield(Calib,'R')
1376%     R=(Calib.R)';
1377%     Dx=R(5)*R(7)-R(4)*R(8);
1378%     Dy=R(1)*R(8)-R(2)*R(7);
1379%     D0=Calib.f*(R(2)*R(4)-R(1)*R(5));
1380%     Z11=R(6)*R(8)-R(5)*R(9);
1381%     Z12=R(2)*R(9)-R(3)*R(8); 
1382%     Z21=R(4)*R(9)-R(6)*R(7);
1383%     Z22=R(3)*R(7)-R(1)*R(9);
1384%     Zx0=R(3)*R(5)-R(2)*R(6);
1385%     Zy0=R(1)*R(6)-R(3)*R(4);
1386%     A11=R(8)*Calib.Ty-R(5)*Calib.Tz+Z11*Zphys;
1387%     A12=R(2)*Calib.Tz-R(8)*Calib.Tx+Z12*Zphys;
1388%     A21=-R(7)*Calib.Ty+R(4)*Calib.Tz+Z21*Zphys;
1389%     A22=-R(1)*Calib.Tz+R(7)*Calib.Tx+Z11*Zphys;
1390%     X0=Calib.f*(R(5)*Calib.Tx-R(2)*Calib.Ty+Zx0*Zphys);
1391%     Y0=Calib.f*(-R(4)*Calib.Tx+R(1)*Calib.Ty+Zy0*Zphys);
1392%         %px to camera:
1393%     Xd=(Calib.dpx/Calib.sx)*(X-Calib.Cx); % sensor coordinates
1394%     Yd=Calib.dpy*(Y-Calib.Cy);
1395%     dist_fact=1+Calib.kappa1*(Xd.*Xd+Yd.*Yd); %distortion factor
1396%     Xu=dist_fact.*Xd;%undistorted sensor coordinates
1397%     Yu=dist_fact.*Yd;
1398%     denom=Dx*Xu+Dy*Yu+D0;
1399%     % denom2=denom.*denom;
1400%     Xphys=(A11.*Xu+A12.*Yu+X0)./denom;%world coordinates
1401%     Yphys=(A21.*Xu+A22.*Yu+Y0)./denom;
1402% end
1403
1404
1405% --------------------------------------------------------------------
1406function MenuImportPoints_Callback(hObject, eventdata, handles)
1407fileinput=browse_xml(hObject, eventdata, handles)
1408if isempty(fileinput)
1409    return
1410end
1411[s,errormsg]=imadoc2struct(fileinput,'GeometryCalib');
1412GeometryCalib=s.GeometryCalib;
1413%GeometryCalib=load_calib(hObject, eventdata, handles)
1414calib=reshape(GeometryCalib.PointCoord,[],1);
1415for ilist=1:numel(calib)
1416    CoordCell{ilist}=num2str(calib(ilist));
1417end
1418CoordCell=reshape(CoordCell,[],5);
1419Tabchar=cell2tab(CoordCell,'    |    ');%transform cells into table ready for display
1420Tabchar=[Tabchar;{'......'}];
1421set(handles.ListCoord,'Value',1)
1422set(handles.ListCoord,'String',Tabchar)
1423MenuPlot_Callback(handles.geometry_calib, [], handles)
1424
1425% -----------------------------------------------------------------------
1426function MenuImportIntrinsic_Callback(hObject, eventdata, handles)
1427%------------------------------------------------------------------------
1428fileinput=browse_xml(hObject, eventdata, handles);
1429if isempty(fileinput)
1430    return
1431end
1432[s,errormsg]=imadoc2struct(fileinput,'GeometryCalib');
1433GeometryCalib=s.GeometryCalib;
1434display_intrinsic(GeometryCalib,handles)
1435
1436% -----------------------------------------------------------------------
1437function MenuImportAll_Callback(hObject, eventdata, handles)
1438%------------------------------------------------------------------------
1439fileinput=browse_xml(hObject, eventdata, handles)
1440if ~isempty(fileinput)
1441    loadfile(handles,fileinput)
1442end
1443
1444% -----------------------------------------------------------------------
1445% --- Executes on menubar option Import/Grid file: introduce previous grid files
1446function MenuGridFile_Callback(hObject, eventdata, handles)
1447% -----------------------------------------------------------------------
1448inputfile=browse_xml(hObject, eventdata, handles)
1449listfile=get(handles.coord_files,'string');
1450if isequal(listfile,{''})
1451    listfile={inputfile};
1452else
1453    listfile=[listfile;{inputfile}]%update the list of coord files
1454end
1455set(handles.coord_files,'string',listfile);
1456
1457%------------------------------------------------------------------------
1458% --- 'key_press_fcn:' function activated when a key is pressed on the keyboard
1459function key_press_fcn(hObject,eventdata,handles)
1460%------------------------------------------------------------------------
1461xx=double(get(handles.geometry_calib,'CurrentCharacter')); %get the keyboard character
1462if ismember(xx,[8 127])%backspace or delete
1463    Coord_cell=get(handles.ListCoord,'String');
1464    val=get(handles.ListCoord,'Value');
1465     if max(val)<numel(Coord_cell) % the last element '...' has not been selected
1466        Coord_cell(val)=[];%remove the selected line
1467        set(handles.ListCoord,'Value',min(val))
1468        set(handles.ListCoord,'String',Coord_cell)         
1469        ListCoord_Callback(hObject, eventdata, handles)
1470        MenuPlot_Callback(hObject,eventdata,handles)
1471     end
1472end
1473
1474%------------------------------------------------------------------------
1475function fileinput=browse_xml(hObject, eventdata, handles)
1476%------------------------------------------------------------------------
1477fileinput=[];%default
1478oldfile=''; %default
1479UserData=get(handles.geometry_calib,'UserData');
1480if isfield(UserData,'XmlInputFile')
1481    oldfile=UserData.XmlInputFile;
1482end
1483[FileName, PathName, filterindex] = uigetfile( ...
1484       {'*.xml;*.mat', ' (*.xml,*.mat)';
1485       '*.xml',  '.xml files '; ...
1486        '*.mat',  '.mat matlab files '}, ...
1487        'Pick a file',oldfile);
1488fileinput=[PathName FileName];%complete file name
1489testblank=findstr(fileinput,' ');%look for blanks
1490if ~isempty(testblank)
1491    msgbox_uvmat('ERROR','forbidden input file name or path: no blank character allowed')
1492    return
1493end
1494sizf=size(fileinput);
1495if (~ischar(fileinput)||~isequal(sizf(1),1)),return;end
1496UserData.XmlInputFile=fileinput;
1497set(handles.geometry_calib,'UserData',UserData)%record current file foer further use of browser
1498
1499% -----------------------------------------------------------------------
1500function loadfile(handles,fileinput)
1501%------------------------------------------------------------------------
1502fileinput
1503[s,errormsg]=imadoc2struct(fileinput,'GeometryCalib')
1504GeometryCalib=s.GeometryCalib;
1505fx=1;fy=1;Cx=0;Cy=0;kc=0; %default
1506%     Tabchar={};
1507CoordCell={};
1508%     kc=0;%default
1509%     f1=1000;
1510%     f2=1000;
1511%     hhuvmat=guidata(findobj(allchild(0),'Name','uvmat'));
1512%     Cx=str2num(get(hhuvmat.npx,'String'))/2;
1513%     Cy=str2num(get(hhuvmat.npy,'String'))/2;
1514Tabchar={};%default
1515val_cal=1;%default
1516if ~isempty(GeometryCalib)
1517    % choose the calibration option
1518    if isfield(GeometryCalib,'CalibrationType')
1519       calib_list=get(handles.calib_type,'String');
1520       for ilist=1:numel(calib_list)
1521           if strcmp(calib_list{ilist},GeometryCalib.CalibrationType)
1522               val_cal=ilist;
1523               break
1524           end
1525       end
1526    end
1527    display_intrinsic(GeometryCalib,handles)%intrinsic param
1528    %extrinsic param
1529    if isfield(GeometryCalib,'Tx_Ty_Tz')
1530        Tx_Ty_Tz=GeometryCalib.Tx_Ty_Tz;
1531        set(handles.Tx,'String',num2str(GeometryCalib.Tx_Ty_Tz(1),4))
1532        set(handles.Ty,'String',num2str(GeometryCalib.Tx_Ty_Tz(2),4))
1533        set(handles.Tz,'String',num2str(GeometryCalib.Tx_Ty_Tz(3),4))
1534    end
1535    if isfield(GeometryCalib,'omc')
1536        set(handles.Phi,'String',num2str(GeometryCalib.omc(1),4))
1537        set(handles.Theta,'String',num2str(GeometryCalib.omc(2),4))
1538        set(handles.Psi,'String',num2str(GeometryCalib.omc(3),4))
1539    end
1540    calib=reshape(GeometryCalib.PointCoord,[],1);
1541    for ilist=1:numel(calib)
1542        CoordCell{ilist}=num2str(calib(ilist));
1543    end
1544    CoordCell=reshape(CoordCell,[],5);
1545    Tabchar=cell2tab(CoordCell,'    |    ');%transform cells into table ready for display
1546    MenuPlot_Callback(handles.geometry_calib, [], handles)
1547end
1548set(handles.calib_type,'Value',val_cal)
1549Tabchar=[Tabchar;{'......'}];
1550set(handles.ListCoord,'Value',1)
1551set(handles.ListCoord,'String',Tabchar)
1552
1553if isempty(CoordCell)% allow mouse action by default in the absence of input points
1554    set(handles.edit_append,'Value',1)
1555    set(handles.edit_append,'BackgroundColor',[1 1 0])
1556else % does not allow mouse action by default in the presence of input points
1557    set(handles.edit_append,'Value',0)
1558    set(handles.edit_append,'BackgroundColor',[0.7 0.7 0.7])
1559end
1560
1561%------------------------------------------------------------------------
1562%---display calibration intrinsic parameters
1563function display_intrinsic(GeometryCalib,handles)
1564%------------------------------------------------------------------------
1565fx=[];
1566fy=[];
1567if isfield(GeometryCalib,'fx_fy')
1568    fx=GeometryCalib.fx_fy(1);
1569    fy=GeometryCalib.fx_fy(2);
1570end
1571Cx_Cy=[0 0];%default
1572if isfield(GeometryCalib,'Cx_Cy')
1573    Cx_Cy=GeometryCalib.Cx_Cy;
1574end
1575kc=0;
1576if isfield(GeometryCalib,'kc')
1577    kc=GeometryCalib.kc %* GeometryCalib.focal*GeometryCalib.focal;
1578end
1579set(handles.fx,'String',num2str(fx,5))
1580set(handles.fy,'String',num2str(fy,5))
1581set(handles.Cx,'String',num2str(Cx_Cy(1),'%1.1f'))
1582set(handles.Cy,'String',num2str(Cx_Cy(2),'%1.1f'))
1583set(handles.kc,'String',num2str(kc,'%1.4f'))
1584
1585
Note: See TracBrowser for help on using the repository browser.