source: trunk/src/geometry_calib.m @ 109

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

merge_proj.m, proj_field.m: bugs repaired merging of images
plot_field.m: minor cleaning
imadoc2struct: introduce the possibility of readying the calibration point coordinates
sub_field.m:bugs repaired
geometry_calib, create_grid: introduction of new calibration methods, improvement of detect_grid

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