source: trunk/src/geometry_calib.m @ 168

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

comments added at the head of functions

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