source: trunk/src/geometry_calib.m @ 977

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