source: trunk/src/geometry_calib.m @ 258

Last change on this file since 258 was 252, checked in by sommeria, 14 years ago

various bugs corrected.

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