source: trunk/src/geometry_calib.m @ 507

Last change on this file since 507 was 507, checked in by sommeria, 9 years ago

various improvments and corrections
dtatatree_browser tested

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