source: trunk/src/geometry_calib.m @ 412

Last change on this file since 412 was 379, checked in by sommeria, 13 years ago

several bugs corrected
set_object.fig rationalized so that read_set_object is replaced by the rgeneral fct read_GUI.

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