source: trunk/src/geometry_calib.m @ 809

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