source: trunk/src/geometry_calib.m @ 743

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

various bug resolutions and improvements in particular with geometry_calib. Introduction of a tool set_slices in uvmat.

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