source: trunk/src/geometry_calib.m @ 682

Last change on this file since 682 was 682, checked in by sommeria, 11 years ago

various bugs repaired

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