source: trunk/src/geometry_calib.m @ 671

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

geometry_calib corrected for translation and rotation + cleaning

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