[2] | 1 | %'geometry_calib': performs geometric calibration from a set of reference points |
---|
| 2 | % |
---|
| 3 | % function varargout = geometry_calib(varargin) |
---|
| 4 | % |
---|
[109] | 5 | %A%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA |
---|
[2] | 6 | % Copyright Joel Sommeria, 2008, LEGI / CNRS-UJF-INPG, sommeria@coriolis-legi.org. |
---|
| 7 | %AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA |
---|
| 8 | % This file is part of the toolbox UVMAT. |
---|
| 9 | % |
---|
| 10 | % UVMAT is free software; you can redistribute it and/or modify |
---|
| 11 | % it under the terms of the GNU General Public License as published by |
---|
| 12 | % the Free Software Foundation; either version 2 of the License, or |
---|
| 13 | % (at your option) any later version. |
---|
| 14 | % |
---|
| 15 | % UVMAT is distributed in the hope that it will be useful, |
---|
| 16 | % but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
| 17 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
| 18 | % GNU General Public License (file UVMAT/COPYING.txt) for more details. |
---|
| 19 | %AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA |
---|
| 20 | |
---|
| 21 | function varargout = geometry_calib(varargin) |
---|
| 22 | % GEOMETRY_CALIB M-file for geometry_calib.fig |
---|
| 23 | % GEOMETRY_CALIB, by itself, creates a MenuCoord GEOMETRY_CALIB or raises the existing |
---|
| 24 | % singleton*. |
---|
| 25 | % |
---|
| 26 | % H = GEOMETRY_CALIB returns the handle to a MenuCoord GEOMETRY_CALIB or the handle to |
---|
| 27 | % the existing singleton*. |
---|
| 28 | % |
---|
| 29 | % GEOMETRY_CALIB('CALLBACK',hObject,eventData,handles,...) calls the local |
---|
| 30 | % function named CALLBACK in GEOMETRY_CALIB.M with the given input arguments. |
---|
| 31 | % |
---|
| 32 | % GEOMETRY_CALIB('Property','Value',...) creates a MenuCoord GEOMETRY_CALIB or raises the |
---|
| 33 | % existing singleton*. Starting from the left, property value pairs are |
---|
| 34 | % applied to the GUI before geometry_calib_OpeningFunction gets called. An |
---|
| 35 | % unrecognized property name or invalid value makes property application |
---|
| 36 | % stop. All inputs are passed to geometry_calib_OpeningFcn via varargin. |
---|
| 37 | % |
---|
| 38 | % *See GUI Options on GUIDE's Tools menu. Choose "GUI allows only one |
---|
| 39 | % instance to run (singleton)". |
---|
| 40 | % |
---|
| 41 | % See also: GUIDE, GUIDATA, GUIHANDLES |
---|
| 42 | |
---|
| 43 | % Edit the above text to modify the response to help geometry_calib |
---|
| 44 | |
---|
[114] | 45 | % Last Modified by GUIDE v2.5 05-Oct-2010 13:47:00 |
---|
[2] | 46 | |
---|
| 47 | % Begin initialization code - DO NOT edit |
---|
| 48 | gui_Singleton = 1; |
---|
| 49 | gui_State = struct('gui_Name', mfilename, ... |
---|
| 50 | 'gui_Singleton', gui_Singleton, ... |
---|
| 51 | 'gui_OpeningFcn', @geometry_calib_OpeningFcn, ... |
---|
| 52 | 'gui_OutputFcn', @geometry_calib_OutputFcn, ... |
---|
| 53 | 'gui_LayoutFcn', [] , ... |
---|
| 54 | 'gui_Callback', []); |
---|
| 55 | if nargin & isstr(varargin{1}) |
---|
| 56 | gui_State.gui_Callback = str2func(varargin{1}); |
---|
| 57 | end |
---|
| 58 | |
---|
| 59 | if nargout |
---|
| 60 | [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); |
---|
| 61 | else |
---|
| 62 | gui_mainfcn(gui_State, varargin{:}); |
---|
| 63 | end |
---|
| 64 | % End initialization code - DO NOT edit |
---|
| 65 | |
---|
| 66 | |
---|
| 67 | % --- Executes just before geometry_calib is made visible. |
---|
| 68 | %INPUT: |
---|
| 69 | %handles: handles of the geometry_calib interface elements |
---|
| 70 | % PlotHandles: set of handles of the elements contolling the plotting |
---|
| 71 | % parameters on the uvmat interface (obtained by 'get_plot_handle.m') |
---|
[60] | 72 | %------------------------------------------------------------------------ |
---|
[116] | 73 | function geometry_calib_OpeningFcn(hObject, eventdata, handles,inputfile,pos) |
---|
[60] | 74 | %------------------------------------------------------------------------ |
---|
[2] | 75 | % Choose default command line output for geometry_calib |
---|
| 76 | handles.output = hObject; |
---|
| 77 | |
---|
| 78 | % Update handles structure |
---|
| 79 | guidata(hObject, handles); |
---|
[71] | 80 | set(hObject,'DeleteFcn',{@closefcn})% |
---|
| 81 | |
---|
[2] | 82 | %set the position of the interface |
---|
[116] | 83 | if exist('pos','var')&& length(pos)>2 |
---|
[2] | 84 | pos_gui=get(hObject,'Position'); |
---|
| 85 | pos_gui(1)=pos(1); |
---|
| 86 | pos_gui(2)=pos(2); |
---|
| 87 | set(hObject,'Position',pos_gui); |
---|
| 88 | end |
---|
[109] | 89 | |
---|
| 90 | %set menu of calibration options |
---|
| 91 | %set(handles.calib_type,'String',{'rescale';'linear';'perspective';'normal';'tsai';'bouguet';'extrinsic'}) |
---|
| 92 | set(handles.calib_type,'String',{'rescale';'linear';'quadr';'3D_linear';'3D_quadr';'3D_extrinsic'}) |
---|
[2] | 93 | inputxml=''; |
---|
[116] | 94 | if exist('inputfile','var')&& ~isempty(inputfile) |
---|
[114] | 95 | struct.XmlInputFile=inputfile; |
---|
[109] | 96 | set(hObject,'UserData',struct) |
---|
| 97 | [Pathsub,RootFile,field_count,str2,str_a,str_b,ext,nom_type,subdir]=name2display(inputfile); |
---|
[116] | 98 | if ~strcmp(ext,'.xml') |
---|
| 99 | inputfile=[fullfile(Pathsub,RootFile) '.xml']%xml file corresponding to the input file |
---|
| 100 | end |
---|
[2] | 101 | end |
---|
[71] | 102 | set(handles.ListCoord,'String',{'......'}) |
---|
[116] | 103 | if exist(inputfile,'file') |
---|
| 104 | loadfile(handles,inputfile)% load the point coordiantes existing in the xml file |
---|
[2] | 105 | end |
---|
| 106 | set(handles.ListCoord,'KeyPressFcn',{@key_press_fcn,handles})%set keyboard action function |
---|
| 107 | |
---|
[71] | 108 | |
---|
[17] | 109 | %------------------------------------------------------------------------ |
---|
[2] | 110 | % --- Outputs from this function are returned to the command line. |
---|
| 111 | function varargout = geometry_calib_OutputFcn(hObject, eventdata, handles) |
---|
[17] | 112 | %------------------------------------------------------------------------ |
---|
[2] | 113 | % Get default command line output from handles structure |
---|
| 114 | varargout{1} = handles.output; |
---|
| 115 | varargout{2}=handles; |
---|
[67] | 116 | % |
---|
[60] | 117 | %------------------------------------------------------------------------ |
---|
[2] | 118 | % executed when closing: set the parent interface button to value 0 |
---|
[67] | 119 | function closefcn(gcbo,eventdata) |
---|
[60] | 120 | %------------------------------------------------------------------------ |
---|
[2] | 121 | huvmat=findobj(allchild(0),'Name','uvmat'); |
---|
[67] | 122 | if ~isempty(huvmat) |
---|
| 123 | handles=guidata(huvmat); |
---|
[71] | 124 | set(handles.MenuMask,'enable','on') |
---|
| 125 | set(handles.MenuGrid,'enable','on') |
---|
[67] | 126 | set(handles.MenuObject,'enable','on') |
---|
| 127 | set(handles.MenuEdit,'enable','on') |
---|
| 128 | set(handles.edit,'enable','on') |
---|
| 129 | hobject=findobj(handles.axes3,'tag','calib_points'); |
---|
| 130 | if ~isempty(hobject) |
---|
| 131 | delete(hobject) |
---|
| 132 | end |
---|
| 133 | hobject=findobj(handles.axes3,'tag','calib_marker'); |
---|
| 134 | if ~isempty(hobject) |
---|
| 135 | delete(hobject) |
---|
| 136 | end |
---|
[2] | 137 | end |
---|
| 138 | |
---|
[60] | 139 | %------------------------------------------------------------------------ |
---|
[2] | 140 | % --- Executes on button press in calibrate_lin. |
---|
| 141 | function APPLY_Callback(hObject, eventdata, handles) |
---|
[60] | 142 | %------------------------------------------------------------------------ |
---|
[109] | 143 | |
---|
| 144 | %read the current calibration points |
---|
[2] | 145 | Coord_cell=get(handles.ListCoord,'String'); |
---|
| 146 | Object=read_geometry_calib(Coord_cell); |
---|
[109] | 147 | Coord=Object.Coord; |
---|
[84] | 148 | |
---|
[109] | 149 | % apply the calibration, whose type is selected in handles.calib_type |
---|
| 150 | if ~isempty(Coord) |
---|
| 151 | calib_cell=get(handles.calib_type,'String'); |
---|
| 152 | val=get(handles.calib_type,'Value'); |
---|
| 153 | GeometryCalib=feval(['calib_' calib_cell{val}],Coord,handles); |
---|
| 154 | else |
---|
| 155 | msgbox_uvmat('ERROR','No calibration points, abort') |
---|
| 156 | return |
---|
| 157 | end |
---|
[114] | 158 | Z_plane=[]; |
---|
[109] | 159 | if ~isempty(Coord) |
---|
[114] | 160 | %check error |
---|
[109] | 161 | X=Coord(:,1); |
---|
| 162 | Y=Coord(:,2); |
---|
| 163 | Z=Coord(:,3); |
---|
| 164 | x_ima=Coord(:,4); |
---|
| 165 | y_ima=Coord(:,5); |
---|
[114] | 166 | [Xpoints,Ypoints]=px_XYZ(GeometryCalib,X,Y,Z); |
---|
[109] | 167 | GeometryCalib.ErrorRms(1)=sqrt(mean((Xpoints-x_ima).*(Xpoints-x_ima))); |
---|
| 168 | [GeometryCalib.ErrorMax(1),index(1)]=max(abs(Xpoints-x_ima)); |
---|
| 169 | GeometryCalib.ErrorRms(2)=sqrt(mean((Ypoints-y_ima).*(Ypoints-y_ima))); |
---|
| 170 | [GeometryCalib.ErrorMax(2),index(2)]=max(abs(Ypoints-y_ima)); |
---|
| 171 | [EM,ind_dim]=max(GeometryCalib.ErrorMax); |
---|
| 172 | index=index(ind_dim); |
---|
[114] | 173 | %set the Z position of the reference plane used for calibration |
---|
[109] | 174 | if isequal(max(Z),min(Z))%Z constant |
---|
[114] | 175 | Z_plane=Z(1); |
---|
[109] | 176 | GeometryCalib.NbSlice=1; |
---|
[114] | 177 | GeometryCalib.SliceCoord=[0 0 Z_plane]; |
---|
[109] | 178 | end |
---|
| 179 | end |
---|
[114] | 180 | %set the coordinate unit |
---|
[2] | 181 | unitlist=get(handles.CoordUnit,'String'); |
---|
| 182 | unit=unitlist{get(handles.CoordUnit,'value')}; |
---|
| 183 | GeometryCalib.CoordUnit=unit; |
---|
[114] | 184 | %record the points |
---|
[109] | 185 | GeometryCalib.SourceCalib.PointCoord=Coord; |
---|
[114] | 186 | display_intrinsic(GeometryCalib,handles)%display calibration intrinsic parameters |
---|
[109] | 187 | |
---|
[114] | 188 | % Display extrinsinc parameters (rotation and translation of camera with respect to the phys coordiantes) |
---|
[109] | 189 | set(handles.Tx,'String',num2str(GeometryCalib.Tx_Ty_Tz(1),4)) |
---|
| 190 | set(handles.Ty,'String',num2str(GeometryCalib.Tx_Ty_Tz(2),4)) |
---|
| 191 | set(handles.Tz,'String',num2str(GeometryCalib.Tx_Ty_Tz(3),4)) |
---|
[114] | 192 | set(handles.Phi,'String',num2str(GeometryCalib.omc(1),4)) |
---|
| 193 | set(handles.Theta,'String',num2str(GeometryCalib.omc(2),4)) |
---|
| 194 | set(handles.Psi,'String',num2str(GeometryCalib.omc(3),4)) |
---|
[109] | 195 | |
---|
[114] | 196 | % store the calibration data, by default in the xml file of the currently displayed image |
---|
| 197 | hhuvmat=guidata(findobj(allchild(0),'Name','uvmat'));%handles of elements in the GUI uvmat |
---|
[2] | 198 | RootPath=''; |
---|
| 199 | RootFile=''; |
---|
| 200 | if ~isempty(hhuvmat.RootPath)& ~isempty(hhuvmat.RootFile) |
---|
| 201 | testhandle=1; |
---|
| 202 | RootPath=get(hhuvmat.RootPath,'String'); |
---|
| 203 | RootFile=get(hhuvmat.RootFile,'String'); |
---|
| 204 | filebase=fullfile(RootPath,RootFile); |
---|
[114] | 205 | outputfile=[filebase '.xml'];%xml file associated with the currently displayed image |
---|
[2] | 206 | else |
---|
| 207 | question={'save the calibration data and point coordinates in'}; |
---|
| 208 | def={fullfile(RootPath,['ObjectCalib.xml'])}; |
---|
| 209 | options.Resize='on'; |
---|
| 210 | answer=inputdlg(question,'save average in a new file',1,def,options); |
---|
| 211 | outputfile=answer{1}; |
---|
| 212 | end |
---|
[84] | 213 | answer=msgbox_uvmat('INPUT_Y-N',{[outputfile ' updated with calibration data'];... |
---|
[2] | 214 | ['Error rms (along x,y)=' num2str(GeometryCalib.ErrorRms) ' pixels'];... |
---|
[84] | 215 | ['Error max (along x,y)=' num2str(GeometryCalib.ErrorMax) ' pixels']}); |
---|
| 216 | if isequal(answer,'Yes') |
---|
[114] | 217 | answer_1=msgbox_uvmat('INPUT_TXT',' Z= ',num2str(Z_plane)); |
---|
| 218 | Z_plane=str2num(answer_1); |
---|
| 219 | GeometryCalib.NbSlice=1; |
---|
| 220 | GeometryCalib.SliceCoord=[0 0 Z_plane]; |
---|
| 221 | errormsg=update_imadoc(GeometryCalib,outputfile);% introduce the calibration data in the xml file |
---|
| 222 | if ~strcmp(errormsg,'') |
---|
| 223 | msgbox_uvmat('ERROR',errormsg); |
---|
| 224 | end |
---|
[84] | 225 | %display image with new calibration in the currently opened uvmat interface |
---|
| 226 | hhh=findobj(hhuvmat.axes3,'Tag','calib_marker');% delete calib points and markers |
---|
| 227 | if ~isempty(hhh) |
---|
[109] | 228 | delete(hhh); |
---|
[84] | 229 | end |
---|
| 230 | hhh=findobj(hhuvmat.axes3,'Tag','calib_points'); |
---|
| 231 | if ~isempty(hhh) |
---|
| 232 | delete(hhh); |
---|
| 233 | end |
---|
| 234 | set(hhuvmat.FixedLimits,'Value',0)% put FixedLimits option to 'off' |
---|
| 235 | set(hhuvmat.FixedLimits,'BackgroundColor',[0.7 0.7 0.7]) |
---|
[116] | 236 | UserData=get(handles.geometry_calib,'UserData'); |
---|
| 237 | UserData.XmlInputFile=outputfile;%save the current xml file name |
---|
| 238 | set(handles.geometry_calib,'UserData',UserData) |
---|
[114] | 239 | uvmat('RootPath_Callback',hObject,eventdata,hhuvmat); %file input with xml reading in uvmat, show the image in phys coordinates |
---|
| 240 | MenuPlot_Callback(hObject, eventdata, handles) |
---|
| 241 | set(handles.ListCoord,'Value',index)% indicate in the list the point with max deviation (possible mistake) |
---|
| 242 | ListCoord_Callback(hObject, eventdata, handles) |
---|
[84] | 243 | figure(handles.geometry_calib) |
---|
[2] | 244 | end |
---|
[69] | 245 | |
---|
| 246 | %------------------------------------------------------------------ |
---|
[2] | 247 | % --- Executes on button press in calibrate_lin. |
---|
| 248 | function REPLICATE_Callback(hObject, eventdata, handles) |
---|
[109] | 249 | % %%%%%%Todo: correct on the model of APPLY_Callback%%%%%%%%%% |
---|
[60] | 250 | %------------------------------------------------------------------------ |
---|
[2] | 251 | calib_cell=get(handles.calib_type,'String'); |
---|
| 252 | val=get(handles.calib_type,'Value'); |
---|
| 253 | Coord_cell=get(handles.ListCoord,'String'); |
---|
| 254 | Object=read_geometry_calib(Coord_cell); |
---|
[114] | 255 | GeometryCalib=feval(['calib_' calib_cell{val}],Object.Coord,handles); |
---|
[2] | 256 | |
---|
[114] | 257 | %read the current calibration points |
---|
| 258 | Coord_cell=get(handles.ListCoord,'String'); |
---|
| 259 | Object=read_geometry_calib(Coord_cell); |
---|
| 260 | Coord=Object.Coord; |
---|
[2] | 261 | |
---|
[114] | 262 | % apply the calibration, whose type is selected in handles.calib_type |
---|
| 263 | if ~isempty(Coord) |
---|
| 264 | calib_cell=get(handles.calib_type,'String'); |
---|
| 265 | val=get(handles.calib_type,'Value'); |
---|
| 266 | GeometryCalib=feval(['calib_' calib_cell{val}],Coord,handles); |
---|
| 267 | else |
---|
| 268 | msgbox_uvmat('ERROR','No calibration points, abort') |
---|
| 269 | return |
---|
| 270 | end |
---|
| 271 | |
---|
| 272 | if ~isempty(Coord) |
---|
| 273 | %check error |
---|
| 274 | X=Coord(:,1); |
---|
| 275 | Y=Coord(:,2); |
---|
| 276 | Z=Coord(:,3); |
---|
| 277 | x_ima=Coord(:,4); |
---|
| 278 | y_ima=Coord(:,5); |
---|
| 279 | [Xpoints,Ypoints]=px_XYZ(GeometryCalib,X,Y,Z); |
---|
| 280 | GeometryCalib.ErrorRms(1)=sqrt(mean((Xpoints-x_ima).*(Xpoints-x_ima))); |
---|
| 281 | [GeometryCalib.ErrorMax(1),index(1)]=max(abs(Xpoints-x_ima)); |
---|
| 282 | GeometryCalib.ErrorRms(2)=sqrt(mean((Ypoints-y_ima).*(Ypoints-y_ima))); |
---|
| 283 | [GeometryCalib.ErrorMax(2),index(2)]=max(abs(Ypoints-y_ima)); |
---|
| 284 | [EM,ind_dim]=max(GeometryCalib.ErrorMax); |
---|
| 285 | index=index(ind_dim); |
---|
| 286 | %set the Z position of the reference plane used for calibration |
---|
| 287 | Z_plane=[]; |
---|
| 288 | if isequal(max(Z),min(Z)) |
---|
| 289 | Z_plane=Z(1); |
---|
| 290 | end |
---|
| 291 | answer_1=msgbox_uvmat('INPUT_TXT',' Z= ',num2str(Z_plane)); |
---|
| 292 | Z_plane=str2num(answer_1); |
---|
| 293 | GeometryCalib.NbSlice=1; |
---|
| 294 | GeometryCalib.SliceCoord=[0 0 Z_plane]; |
---|
| 295 | %set the coordinate unit |
---|
| 296 | unitlist=get(handles.CoordUnit,'String'); |
---|
| 297 | unit=unitlist{get(handles.CoordUnit,'value')}; |
---|
| 298 | GeometryCalib.CoordUnit=unit; |
---|
| 299 | %record the points |
---|
| 300 | GeometryCalib.SourceCalib.PointCoord=Coord; |
---|
[116] | 301 | % errormsg=update_imadoc(GeometryCalib,outputfile);% introduce the calibration data in the xml file |
---|
| 302 | % if ~strcmp(errormsg,'') |
---|
| 303 | % msgbox_uvmat('ERROR',errormsg); |
---|
| 304 | % end |
---|
[114] | 305 | end |
---|
| 306 | display_intrinsic(GeometryCalib,handles)%display calibration intrinsic parameters |
---|
| 307 | |
---|
| 308 | % Display extrinsinc parameters (rotation and translation of camera with respect to the phys coordiantes) |
---|
| 309 | set(handles.Tx,'String',num2str(GeometryCalib.Tx_Ty_Tz(1),4)) |
---|
| 310 | set(handles.Ty,'String',num2str(GeometryCalib.Tx_Ty_Tz(2),4)) |
---|
| 311 | set(handles.Tz,'String',num2str(GeometryCalib.Tx_Ty_Tz(3),4)) |
---|
| 312 | set(handles.Phi,'String',num2str(GeometryCalib.omc(1),4)) |
---|
| 313 | set(handles.Theta,'String',num2str(GeometryCalib.omc(2),4)) |
---|
| 314 | set(handles.Psi,'String',num2str(GeometryCalib.omc(3),4)) |
---|
| 315 | |
---|
| 316 | % indicate the plane of the calibration grid if defined |
---|
| 317 | % huvmat=findobj(allchild(0),'Name','uvmat'); |
---|
| 318 | % hhuvmat=guidata(huvmat);%handles of elements in the GUI uvmat |
---|
| 319 | % RootPath=''; |
---|
| 320 | % RootFile=''; |
---|
| 321 | % if ~isempty(hhuvmat.RootPath)& ~isempty(hhuvmat.RootFile) |
---|
| 322 | % testhandle=1; |
---|
| 323 | % RootPath=get(hhuvmat.RootPath,'String'); |
---|
| 324 | % RootFile=get(hhuvmat.RootFile,'String'); |
---|
| 325 | % filebase=fullfile(RootPath,RootFile); |
---|
| 326 | % outputfile=[filebase '.xml']; |
---|
| 327 | % else |
---|
| 328 | % question={'save the calibration data and point coordinates in'}; |
---|
| 329 | % def={fullfile(RootPath,['ObjectCalib.xml'])}; |
---|
| 330 | % options.Resize='on'; |
---|
| 331 | % answer=inputdlg(question,'save average in a new file',1,def,options); |
---|
| 332 | % outputfile=answer{1}; |
---|
| 333 | % end |
---|
| 334 | |
---|
[2] | 335 | %open and read the dataview GUI |
---|
| 336 | h_dataview=findobj(allchild(0),'name','dataview'); |
---|
[12] | 337 | if ~isempty(h_dataview) |
---|
| 338 | delete(h_dataview) |
---|
[2] | 339 | end |
---|
[61] | 340 | CalibData=get(handles.geometry_calib,'UserData');%read the calibration image source on the interface userdata |
---|
[2] | 341 | |
---|
[114] | 342 | if isfield(CalibData,'XmlInputFile') |
---|
| 343 | XmlInput=fileparts(CalibData.XmlInputFile); |
---|
[12] | 344 | [XmlInput,filename,ext]=fileparts(XmlInput); |
---|
[2] | 345 | end |
---|
[12] | 346 | SubCampaignTest='n'; %default |
---|
| 347 | testinput=0; |
---|
| 348 | if isfield(CalibData,'Heading') |
---|
| 349 | Heading=CalibData.Heading; |
---|
| 350 | if isfield(Heading,'Record') && isequal([filename ext],Heading.Record) |
---|
| 351 | [XmlInput,filename,ext]=fileparts(XmlInput); |
---|
[2] | 352 | end |
---|
[12] | 353 | if isfield(Heading,'Device') && isequal([filename ext],Heading.Device) |
---|
| 354 | [XmlInput,filename,ext]=fileparts(XmlInput); |
---|
| 355 | Device=Heading.Device; |
---|
| 356 | end |
---|
| 357 | if isfield(Heading,'Experiment') && isequal([filename ext],Heading.Experiment) |
---|
| 358 | [PP,filename,ext]=fileparts(XmlInput); |
---|
| 359 | end |
---|
| 360 | testinput=0; |
---|
| 361 | if isfield(Heading,'SubCampaign') && isequal([filename ext],Heading.SubCampaign) |
---|
| 362 | SubCampaignTest='y'; |
---|
| 363 | testinput=1; |
---|
| 364 | elseif isfield(Heading,'Campaign') && isequal([filename ext],Heading.Campaign) |
---|
| 365 | testinput=1; |
---|
| 366 | end |
---|
[2] | 367 | end |
---|
[12] | 368 | if ~testinput |
---|
| 369 | filename='PROJETS';%default |
---|
[114] | 370 | if isfield(CalibData,'XmlInputFile') |
---|
| 371 | [pp,filename]=fileparts(CalibData.XmlInputFile); |
---|
[2] | 372 | end |
---|
[12] | 373 | while ~isequal(filename,'PROJETS') && numel(filename)>1 |
---|
| 374 | filename_1=filename; |
---|
| 375 | pp_1=pp; |
---|
| 376 | [pp,filename]=fileparts(pp); |
---|
| 377 | end |
---|
| 378 | XmlInput=fullfile(pp_1,filename_1); |
---|
| 379 | testinput=1; |
---|
[2] | 380 | end |
---|
[12] | 381 | if testinput |
---|
[36] | 382 | outcome=dataview(XmlInput,SubCampaignTest,GeometryCalib); |
---|
[2] | 383 | end |
---|
| 384 | |
---|
[60] | 385 | %------------------------------------------------------------------------ |
---|
[2] | 386 | % determine the parameters for a calibration by an affine function (rescaling and offset, no rotation) |
---|
[109] | 387 | function GeometryCalib=calib_rescale(Coord,handles) |
---|
[60] | 388 | %------------------------------------------------------------------------ |
---|
[2] | 389 | X=Coord(:,1); |
---|
[109] | 390 | Y=Coord(:,2);% Z not used |
---|
[2] | 391 | x_ima=Coord(:,4); |
---|
| 392 | y_ima=Coord(:,5); |
---|
| 393 | [px,sx]=polyfit(X,x_ima,1); |
---|
| 394 | [py,sy]=polyfit(Y,y_ima,1); |
---|
| 395 | T_x=px(2); |
---|
| 396 | T_y=py(2); |
---|
| 397 | GeometryCalib.CalibrationType='rescale'; |
---|
[114] | 398 | GeometryCalib.fx_fy=[px(1) py(1)]; |
---|
[2] | 399 | GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function |
---|
[114] | 400 | GeometryCalib.Tx_Ty_Tz=[px(2)/px(1) py(2)/py(1) 1]; |
---|
| 401 | %GeometryCalib.R=[1,0,0;0,1,0;0,0,0]; |
---|
| 402 | GeometryCalib.omc=[0 0 0]; |
---|
[2] | 403 | |
---|
[60] | 404 | %------------------------------------------------------------------------ |
---|
[2] | 405 | % determine the parameters for a calibration by a linear transform matrix (rescale and rotation) |
---|
[114] | 406 | function GeometryCalib=calib_linear(Coord,handles) %TO UPDATE |
---|
[60] | 407 | %------------------------------------------------------------------------ |
---|
[2] | 408 | X=Coord(:,1); |
---|
[109] | 409 | Y=Coord(:,2);% Z not used |
---|
[2] | 410 | x_ima=Coord(:,4); |
---|
| 411 | y_ima=Coord(:,5); |
---|
| 412 | XY_mat=[ones(size(X)) X Y]; |
---|
| 413 | a_X1=XY_mat\x_ima; %transformation matrix for X |
---|
[109] | 414 | % x1=XY_mat*a_X1;%reconstruction |
---|
| 415 | % err_X1=max(abs(x1-x_ima));%error |
---|
[2] | 416 | a_Y1=XY_mat\y_ima;%transformation matrix for X |
---|
[109] | 417 | % y1=XY_mat*a_Y1; |
---|
| 418 | % err_Y1=max(abs(y1-y_ima));%error |
---|
[116] | 419 | R=[a_X1(2),a_X1(3),0;a_Y1(2),a_Y1(3),0;0,0,0]; |
---|
| 420 | norm=abs(det(R)); |
---|
[2] | 421 | GeometryCalib.CalibrationType='linear'; |
---|
[116] | 422 | GeometryCalib.fx_fy=[norm norm]; |
---|
[2] | 423 | GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function |
---|
[109] | 424 | R=[a_X1(2),a_X1(3),0;a_Y1(2),a_Y1(3),0;0,0,0]; |
---|
[116] | 425 | GeometryCalib.Tx_Ty_Tz=[a_X1(1) a_Y1(1) 1]; |
---|
[109] | 426 | GeometryCalib.R=R/norm; |
---|
[116] | 427 | GeometryCalib.omc=(180/pi)*[acos(R(1,1)) 0 0]; |
---|
[109] | 428 | %------------------------------------------------------------------------ |
---|
| 429 | % determine the tsai parameters for a view normal to the grid plane |
---|
[114] | 430 | % NOT USED |
---|
[109] | 431 | function GeometryCalib=calib_normal(Coord,handles) |
---|
| 432 | %------------------------------------------------------------------------ |
---|
| 433 | Calib.f1=str2num(get(handles.fx,'String')); |
---|
| 434 | Calib.f2=str2num(get(handles.fy,'String')); |
---|
[114] | 435 | Calib.k=str2num(get(handles.kc,'String')); |
---|
[109] | 436 | Calib.Cx=str2num(get(handles.Cx,'String')); |
---|
| 437 | Calib.Cy=str2num(get(handles.Cy,'String')); |
---|
| 438 | %default |
---|
| 439 | if isempty(Calib.f1) |
---|
| 440 | Calib.f1=25/0.012; |
---|
| 441 | end |
---|
| 442 | if isempty(Calib.f2) |
---|
| 443 | Calib.f2=25/0.012; |
---|
| 444 | end |
---|
| 445 | if isempty(Calib.k) |
---|
| 446 | Calib.k=0; |
---|
| 447 | end |
---|
| 448 | if isempty(Calib.Cx)||isempty(Calib.Cy) |
---|
| 449 | huvmat=findobj(allchild(0),'Tag','uvmat'); |
---|
| 450 | hhuvmat=guidata(huvmat); |
---|
| 451 | Calib.Cx=str2num(get(hhuvmat.npx,'String'))/2; |
---|
| 452 | Calib.Cx=str2num(get(hhuvmat.npy,'String'))/2; |
---|
| 453 | end |
---|
| 454 | %tsai parameters |
---|
| 455 | Calib.dpx=0.012;%arbitrary |
---|
| 456 | Calib.dpy=0.012; |
---|
| 457 | Calib.sx=Calib.f1*Calib.dpx/(Calib.f2*Calib.dpy); |
---|
| 458 | Calib.f=Calib.f2*Calib.dpy; |
---|
| 459 | Calib.kappa1=Calib.k/(Calib.f*Calib.f); |
---|
[2] | 460 | |
---|
[109] | 461 | %initial guess |
---|
| 462 | X=Coord(:,1); |
---|
| 463 | Y=Coord(:,2); |
---|
| 464 | Zmean=mean(Coord(:,3)); |
---|
| 465 | x_ima=Coord(:,4)-Calib.Cx; |
---|
| 466 | y_ima=Coord(:,5)-Calib.Cy; |
---|
| 467 | XY_mat=[ones(size(X)) X Y]; |
---|
| 468 | a_X1=XY_mat\x_ima; %transformation matrix for X |
---|
| 469 | a_Y1=XY_mat\y_ima;%transformation matrix for Y |
---|
| 470 | R=[a_X1(2),a_X1(3),0;a_Y1(2),a_Y1(3),0;0,0,-1];% rotation+ z axis reversal (upward) |
---|
| 471 | norm=sqrt(det(-R)); |
---|
| 472 | calib_param(1)=0;% quadratic distortion |
---|
| 473 | calib_param(2)=a_X1(1); |
---|
| 474 | calib_param(3)=a_Y1(1); |
---|
| 475 | calib_param(4)=Calib.f/(norm*Calib.dpx)-R(3,3)*Zmean; |
---|
| 476 | calib_param(5)=angle(a_X1(2)+i*a_X1(3)); |
---|
| 477 | display(['initial guess=' num2str(calib_param)]) |
---|
| 478 | |
---|
| 479 | %optimise the parameters: minimisation of error |
---|
| 480 | calib_param = fminsearch(@(calib_param) error_calib(calib_param,Calib,Coord),calib_param) |
---|
| 481 | |
---|
| 482 | GeometryCalib.CalibrationType='tsai_normal'; |
---|
| 483 | GeometryCalib.focal=Calib.f; |
---|
| 484 | GeometryCalib.dpx_dpy=[Calib.dpx Calib.dpy]; |
---|
| 485 | GeometryCalib.Cx_Cy=[Calib.Cx Calib.Cy]; |
---|
| 486 | GeometryCalib.sx=Calib.sx; |
---|
| 487 | GeometryCalib.kappa1=calib_param(1); |
---|
| 488 | GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function |
---|
| 489 | GeometryCalib.Tx_Ty_Tz=[calib_param(2) calib_param(3) calib_param(4)]; |
---|
| 490 | alpha=calib_param(5); |
---|
| 491 | GeometryCalib.R=[cos(alpha) sin(alpha) 0;-sin(alpha) cos(alpha) 0;0 0 -1]; |
---|
| 492 | |
---|
[60] | 493 | %------------------------------------------------------------------------ |
---|
[109] | 494 | function GeometryCalib=calib_3D_linear(Coord,handles) |
---|
[114] | 495 | %TO UPDATE |
---|
[69] | 496 | %------------------------------------------------------------------ |
---|
| 497 | path_uvmat=which('uvmat');% check the path detected for source file uvmat |
---|
| 498 | path_UVMAT=fileparts(path_uvmat); %path to UVMAT |
---|
[78] | 499 | huvmat=findobj(allchild(0),'Tag','uvmat'); |
---|
| 500 | hhuvmat=guidata(huvmat); |
---|
[109] | 501 | x_1=Coord(:,4:5)' |
---|
| 502 | X_1=Coord(:,1:3)' |
---|
[69] | 503 | n_ima=1; |
---|
| 504 | % check_cond=0; |
---|
[78] | 505 | |
---|
[109] | 506 | nx=str2num(get(hhuvmat.npx,'String')); |
---|
| 507 | ny=str2num(get(hhuvmat.npy,'String')); |
---|
[78] | 508 | |
---|
[109] | 509 | est_dist=[0;0;0;0;0]; |
---|
| 510 | est_aspect_ratio=0; |
---|
| 511 | est_fc=[1;1]; |
---|
| 512 | %fc=[25;25]/0.012; |
---|
| 513 | center_optim=0; |
---|
| 514 | run(fullfile(path_UVMAT,'toolbox_calib','go_calib_optim')); |
---|
| 515 | |
---|
| 516 | GeometryCalib.CalibrationType='3D_linear'; |
---|
| 517 | GeometryCalib.focal=fc(2); |
---|
| 518 | GeometryCalib.dpx_dpy=[1 1]; |
---|
| 519 | GeometryCalib.Cx_Cy=cc'; |
---|
| 520 | GeometryCalib.sx=fc(1)/fc(2); |
---|
| 521 | GeometryCalib.kappa1=-kc(1)/fc(2)^2; |
---|
| 522 | GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function |
---|
| 523 | GeometryCalib.Tx_Ty_Tz=Tc_1'; |
---|
| 524 | GeometryCalib.R=Rc_1; |
---|
| 525 | |
---|
| 526 | %------------------------------------------------------------------------ |
---|
| 527 | function GeometryCalib=calib_3D_quadr(Coord,handles) |
---|
| 528 | %------------------------------------------------------------------ |
---|
| 529 | |
---|
| 530 | path_uvmat=which('uvmat');% check the path detected for source file uvmat |
---|
| 531 | path_UVMAT=fileparts(path_uvmat); %path to UVMAT |
---|
| 532 | huvmat=findobj(allchild(0),'Tag','uvmat'); |
---|
| 533 | hhuvmat=guidata(huvmat); |
---|
| 534 | % check_cond=0; |
---|
| 535 | coord_files=get(handles.coord_files,'String'); |
---|
| 536 | if ischar(coord_files) |
---|
| 537 | coord_files={coord_files}; |
---|
| 538 | end |
---|
| 539 | if isempty(coord_files{1}) || isequal(coord_files,{''}) |
---|
| 540 | coord_files={}; |
---|
| 541 | end |
---|
| 542 | |
---|
| 543 | %retrieve the calibration points stored in the files listed in the popup list coord_files |
---|
[114] | 544 | x_1=Coord(:,4:5)';%px coordinates of the ref points |
---|
| 545 | nx=str2num(get(hhuvmat.npx,'String')); |
---|
| 546 | ny=str2num(get(hhuvmat.npy,'String')); |
---|
| 547 | x_1(2,:)=ny-x_1(2,:);%reverse the y image coordinates |
---|
| 548 | X_1=Coord(:,1:3)';%phys coordinates of the ref points |
---|
[109] | 549 | n_ima=numel(coord_files)+1; |
---|
| 550 | if ~isempty(coord_files) |
---|
| 551 | msgbox_uvmat('CONFIRMATION',['The xy coordinates of the calibration points in ' num2str(n_ima) ' planes will be used']) |
---|
| 552 | for ifile=1:numel(coord_files) |
---|
| 553 | t=xmltree(coord_files{ifile}); |
---|
| 554 | s=convert(t);%convert to matlab structure |
---|
| 555 | if isfield(s,'GeometryCalib') |
---|
| 556 | if isfield(s.GeometryCalib,'SourceCalib') |
---|
| 557 | if isfield(s.GeometryCalib.SourceCalib,'PointCoord') |
---|
| 558 | PointCoord=s.GeometryCalib.SourceCalib.PointCoord; |
---|
| 559 | Coord_file=[]; |
---|
| 560 | for i=1:length(PointCoord) |
---|
| 561 | line=str2num(PointCoord{i}); |
---|
| 562 | Coord_file(i,4:5)=line(4:5);%px x |
---|
| 563 | Coord_file(i,1:3)=line(1:3);%phys x |
---|
| 564 | end |
---|
| 565 | eval(['x_' num2str(ifile+1) '=Coord_file(:,4:5)'';']); |
---|
[114] | 566 | eval(['x_' num2str(ifile+1) '(2,:)=ny-x_' num2str(ifile+1) '(2,:);' ]); |
---|
[109] | 567 | eval(['X_' num2str(ifile+1) '=Coord_file(:,1:3)'';']); |
---|
| 568 | end |
---|
| 569 | end |
---|
| 570 | end |
---|
| 571 | end |
---|
| 572 | end |
---|
| 573 | |
---|
| 574 | n_ima=numel(coord_files)+1; |
---|
| 575 | whos |
---|
[78] | 576 | |
---|
[108] | 577 | est_dist=[1;0;0;0;0]; |
---|
[109] | 578 | est_aspect_ratio=1; |
---|
| 579 | %est_fc=[0;0]; |
---|
| 580 | %fc=[25;25]/0.012; |
---|
| 581 | center_optim=0; |
---|
[83] | 582 | run(fullfile(path_UVMAT,'toolbox_calib','go_calib_optim')); |
---|
[69] | 583 | |
---|
[109] | 584 | GeometryCalib.CalibrationType='3D_quadr'; |
---|
[114] | 585 | GeometryCalib.fx_fy=fc'; |
---|
| 586 | %GeometryCalib.focal=fc(2); |
---|
| 587 | %GeometryCalib.dpx_dpy=[1 1]; |
---|
[69] | 588 | GeometryCalib.Cx_Cy=cc'; |
---|
[114] | 589 | %GeometryCalib.sx=fc(1)/fc(2); |
---|
| 590 | GeometryCalib.kc=kc(1); |
---|
| 591 | %GeometryCalib.kappa1=-kc(1)/fc(2)^2; |
---|
[69] | 592 | GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function |
---|
| 593 | GeometryCalib.Tx_Ty_Tz=Tc_1'; |
---|
| 594 | GeometryCalib.R=Rc_1; |
---|
[114] | 595 | GeometryCalib.R(2,1:3)=-GeometryCalib.R(2,1:3);%inversion of the y image coordinate |
---|
| 596 | GeometryCalib.Tx_Ty_Tz(2)=-GeometryCalib.Tx_Ty_Tz(2);%inversion of the y image coordinate |
---|
| 597 | GeometryCalib.Cx_Cy(2)=ny-GeometryCalib.Cx_Cy(2);%inversion of the y image coordinate |
---|
| 598 | GeometryCalib.omc=(180/pi)*omc_1;%angles in degrees |
---|
[109] | 599 | GeometryCalib.ErrorRMS=[]; |
---|
| 600 | GeometryCalib.ErrorMax=[]; |
---|
[69] | 601 | |
---|
[109] | 602 | |
---|
[60] | 603 | %------------------------------------------------------------------------ |
---|
[109] | 604 | function GeometryCalib=calib_3D_extrinsic(Coord,handles) |
---|
| 605 | %------------------------------------------------------------------ |
---|
| 606 | path_uvmat=which('geometry_calib');% check the path detected for source file uvmat |
---|
| 607 | path_UVMAT=fileparts(path_uvmat); %path to UVMAT |
---|
[116] | 608 | x_1=double(Coord(:,4:5)');%image coordiantes |
---|
| 609 | X_1=double(Coord(:,1:3)');% phys coordinates |
---|
[114] | 610 | huvmat=findobj(allchild(0),'Tag','uvmat'); |
---|
| 611 | hhuvmat=guidata(huvmat); |
---|
[116] | 612 | ny=str2double(get(hhuvmat.npy,'String')); |
---|
[114] | 613 | x_1(2,:)=ny-x_1(2,:);%reverse the y image coordinates |
---|
[109] | 614 | n_ima=1; |
---|
[114] | 615 | GeometryCalib.CalibrationType='3D_extrinsic'; |
---|
| 616 | GeometryCalib.fx_fy(1)=str2num(get(handles.fx,'String')); |
---|
| 617 | GeometryCalib.fx_fy(2)=str2num(get(handles.fy,'String')); |
---|
| 618 | GeometryCalib.Cx_Cy(1)=str2num(get(handles.Cx,'String')); |
---|
| 619 | GeometryCalib.Cx_Cy(2)=str2num(get(handles.Cy,'String')); |
---|
| 620 | GeometryCalib.kc=str2num(get(handles.kc,'String')); |
---|
[109] | 621 | fct_path=fullfile(path_UVMAT,'toolbox_calib'); |
---|
| 622 | addpath(fct_path) |
---|
[116] | 623 | GeometryCalib.Cx_Cy(2)=ny-GeometryCalib.Cx_Cy(2);%reverse Cx_Cy(2) for calibration (inversion of px ordinate) |
---|
[109] | 624 | % [omc1,Tc1,Rc1,H,x,ex,JJ] = compute_extrinsic(x_1,X_1,... |
---|
| 625 | % [Calib.f Calib.f*Calib.sx]',... |
---|
| 626 | % [Calib.Cx Calib.Cy]',... |
---|
| 627 | % [-Calib.kappa1*Calib.f^2 0 0 0 0]); |
---|
[114] | 628 | [omc,Tc1,Rc1,H,x,ex,JJ] = compute_extrinsic(x_1,X_1,... |
---|
[116] | 629 | (GeometryCalib.fx_fy)',GeometryCalib.Cx_Cy',[GeometryCalib.kc 0 0 0 0]); |
---|
[109] | 630 | rmpath(fct_path); |
---|
| 631 | GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function |
---|
| 632 | GeometryCalib.Tx_Ty_Tz=Tc1'; |
---|
| 633 | %inversion of z axis |
---|
| 634 | GeometryCalib.R=Rc1; |
---|
[114] | 635 | GeometryCalib.R(2,1:3)=-GeometryCalib.R(2,1:3);%inversion of the y image coordinate |
---|
| 636 | GeometryCalib.Tx_Ty_Tz(2)=-GeometryCalib.Tx_Ty_Tz(2);%inversion of the y image coordinate |
---|
[116] | 637 | GeometryCalib.Cx_Cy(2)=ny-GeometryCalib.Cx_Cy(2);%inversion of the y image coordinate |
---|
[114] | 638 | GeometryCalib.omc=(180/pi)*omc'; |
---|
[109] | 639 | %GeometryCalib.R(3,1:3)=-GeometryCalib.R(3,1:3);%inversion for z upward |
---|
| 640 | |
---|
| 641 | |
---|
[116] | 642 | |
---|
[109] | 643 | %------------------------------------------------------------------------ |
---|
| 644 | %function GeometryCalib=calib_tsai_heikkila(Coord) |
---|
| 645 | % TEST: NOT IMPLEMENTED |
---|
| 646 | %------------------------------------------------------------------ |
---|
| 647 | % path_uvmat=which('uvmat');% check the path detected for source file uvmat |
---|
| 648 | % path_UVMAT=fileparts(path_uvmat); %path to UVMAT |
---|
| 649 | % path_calib=fullfile(path_UVMAT,'toolbox_calib_heikkila'); |
---|
| 650 | % addpath(path_calib) |
---|
| 651 | % npoints=size(Coord,1); |
---|
| 652 | % Coord(:,1:3)=10*Coord(:,1:3); |
---|
| 653 | % Coord=[Coord zeros(npoints,2) -ones(npoints,1)]; |
---|
| 654 | % [par,pos,iter,res,er,C]=cacal('dalsa',Coord); |
---|
| 655 | % GeometryCalib.CalibrationType='tsai'; |
---|
| 656 | % GeometryCalib.focal=par(2); |
---|
| 657 | |
---|
| 658 | |
---|
| 659 | %-------------------------------------------------------------------------- |
---|
| 660 | function GeometryCalib=calib_tsai(Coord,handles)% old version using gauthier's bianry ccal_fo |
---|
[114] | 661 | % NOT USED |
---|
[109] | 662 | %------------------------------------------------------------------------ |
---|
[2] | 663 | %TSAI |
---|
| 664 | path_uvmat=which('uvmat');% check the path detected for source file uvmat |
---|
| 665 | path_UVMAT=fileparts(path_uvmat); %path to UVMAT |
---|
[109] | 666 | xmlfile=fullfile(path_UVMAT,'PARAM.xml');%name of the file containing names of binary executables |
---|
[54] | 667 | if exist(xmlfile,'file') |
---|
[109] | 668 | t=xmltree(xmlfile);% read the (xml) file containing names of binary executables |
---|
| 669 | sparam=convert(t);% convert to matlab structure |
---|
[54] | 670 | end |
---|
[71] | 671 | if ~isfield(sparam,'GeometryCalibBin') |
---|
| 672 | msgbox_uvmat('ERROR',['calibration program <GeometryCalibBin> undefined in parameter file ' xmlfile]) |
---|
[2] | 673 | return |
---|
| 674 | end |
---|
[71] | 675 | Tsai_exe=sparam.GeometryCalibBin; |
---|
[54] | 676 | if ~exist(Tsai_exe,'file')%the binary is defined in /bin, default setting |
---|
| 677 | Tsai_exe=fullfile(path_UVMAT,Tsai_exe); |
---|
| 678 | end |
---|
[2] | 679 | if ~exist(Tsai_exe,'file') |
---|
[71] | 680 | msgbox_uvmat('ERROR',['calibration program ' sparam.GeometryCalibBin ' defined in PARAM.xml does not exist']) |
---|
[2] | 681 | return |
---|
| 682 | end |
---|
| 683 | |
---|
| 684 | textcoord=num2str(Coord,4); |
---|
| 685 | dlmwrite('t.txt',textcoord,''); |
---|
[109] | 686 | % ['!' Tsai_exe ' -fx 0 -fy t.txt'] |
---|
| 687 | eval(['!' Tsai_exe ' -f t.txt > tsaicalib.log']); |
---|
[2] | 688 | if ~exist('calib.dat','file') |
---|
[42] | 689 | msgbox_uvmat('ERROR','no output from calibration program Tsai_exe: possibly too few points') |
---|
[2] | 690 | end |
---|
| 691 | calibdat=dlmread('calib.dat'); |
---|
[60] | 692 | delete('calib.dat') |
---|
[109] | 693 | %delete('t.txt') |
---|
[2] | 694 | GeometryCalib.CalibrationType='tsai'; |
---|
| 695 | GeometryCalib.focal=calibdat(10); |
---|
| 696 | GeometryCalib.dpx_dpy=[calibdat(5) calibdat(6)]; |
---|
| 697 | GeometryCalib.Cx_Cy=[calibdat(7) calibdat(8)]; |
---|
| 698 | GeometryCalib.sx=calibdat(9); |
---|
| 699 | GeometryCalib.kappa1=calibdat(11); |
---|
| 700 | GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function |
---|
| 701 | GeometryCalib.Tx_Ty_Tz=[calibdat(12) calibdat(13) calibdat(14)]; |
---|
| 702 | Rx_Ry_Rz=calibdat([15:17]); |
---|
| 703 | sa = sin(Rx_Ry_Rz(1)) ; |
---|
| 704 | ca=cos(Rx_Ry_Rz(1)); |
---|
| 705 | sb=sin(Rx_Ry_Rz(2)); |
---|
| 706 | cb =cos(Rx_Ry_Rz(2)); |
---|
| 707 | sg =sin(Rx_Ry_Rz(3)); |
---|
| 708 | cg =cos(Rx_Ry_Rz(3)); |
---|
| 709 | r1 = cb * cg; |
---|
| 710 | r2 = cg * sa * sb - ca * sg; |
---|
| 711 | r3 = sa * sg + ca * cg * sb; |
---|
| 712 | r4 = cb * sg; |
---|
| 713 | r5 = sa * sb * sg + ca * cg; |
---|
| 714 | r6 = ca * sb * sg - cg * sa; |
---|
| 715 | r7 = -sb; |
---|
| 716 | r8 = cb * sa; |
---|
| 717 | r9 = ca * cb; |
---|
| 718 | %EN DEDUIRE MATRICE R ?? |
---|
| 719 | GeometryCalib.R=[r1,r2,r3;r4,r5,r6;r7,r8,r9]; |
---|
| 720 | |
---|
[60] | 721 | %------------------------------------------------------------------------ |
---|
[109] | 722 | % --- determine the rms of calibration error |
---|
| 723 | function ErrorRms=error_calib(calib_param,Calib,Coord) |
---|
| 724 | %calib_param: vector of free calibration parameters (to optimise) |
---|
| 725 | %Calib: structure of the given calibration parameters |
---|
| 726 | %Coord: list of phys coordinates (columns 1-3, and pixel coordinates (columns 4-5) |
---|
| 727 | Calib.f=25; |
---|
| 728 | Calib.dpx=0.012; |
---|
| 729 | Calib.dpy=0.012; |
---|
| 730 | Calib.sx=1; |
---|
| 731 | Calib.Cx=512; |
---|
| 732 | Calib.Cy=512; |
---|
| 733 | Calib.kappa1=calib_param(1); |
---|
| 734 | Calib.Tx=calib_param(2); |
---|
| 735 | Calib.Ty=calib_param(3); |
---|
| 736 | Calib.Tz=calib_param(4); |
---|
| 737 | alpha=calib_param(5); |
---|
| 738 | Calib.R=[cos(alpha) sin(alpha) 0;-sin(alpha) cos(alpha) 0;0 0 -1]; |
---|
[2] | 739 | |
---|
[109] | 740 | X=Coord(:,1); |
---|
| 741 | Y=Coord(:,2); |
---|
| 742 | Z=Coord(:,3); |
---|
| 743 | x_ima=Coord(:,4); |
---|
| 744 | y_ima=Coord(:,5); |
---|
| 745 | [Xpoints,Ypoints]=px_XYZ(Calib,X,Y,Z); |
---|
| 746 | ErrorRms(1)=sqrt(mean((Xpoints-x_ima).*(Xpoints-x_ima))); |
---|
| 747 | ErrorRms(2)=sqrt(mean((Ypoints-y_ima).*(Ypoints-y_ima))); |
---|
| 748 | ErrorRms=mean(ErrorRms); |
---|
| 749 | |
---|
[60] | 750 | %------------------------------------------------------------------------ |
---|
[2] | 751 | function XImage_Callback(hObject, eventdata, handles) |
---|
[60] | 752 | %------------------------------------------------------------------------ |
---|
[2] | 753 | update_list(hObject, eventdata,handles) |
---|
| 754 | |
---|
[60] | 755 | %------------------------------------------------------------------------ |
---|
[2] | 756 | function YImage_Callback(hObject, eventdata, handles) |
---|
[60] | 757 | %------------------------------------------------------------------------ |
---|
[2] | 758 | update_list(hObject, eventdata,handles) |
---|
| 759 | |
---|
[109] | 760 | %------------------------------------------------------------------------ |
---|
| 761 | % --- Executes on button press in STORE. |
---|
| 762 | function STORE_Callback(hObject, eventdata, handles) |
---|
| 763 | Coord_cell=get(handles.ListCoord,'String'); |
---|
| 764 | Object=read_geometry_calib(Coord_cell); |
---|
| 765 | unitlist=get(handles.CoordUnit,'String'); |
---|
| 766 | unit=unitlist{get(handles.CoordUnit,'value')}; |
---|
| 767 | GeometryCalib.CoordUnit=unit; |
---|
| 768 | GeometryCalib.SourceCalib.PointCoord=Object.Coord; |
---|
| 769 | huvmat=findobj(allchild(0),'Name','uvmat'); |
---|
| 770 | hhuvmat=guidata(huvmat);%handles of elements in the GUI uvmat |
---|
| 771 | RootPath=''; |
---|
| 772 | RootFile=''; |
---|
| 773 | if ~isempty(hhuvmat.RootPath)& ~isempty(hhuvmat.RootFile) |
---|
| 774 | testhandle=1; |
---|
| 775 | RootPath=get(hhuvmat.RootPath,'String'); |
---|
| 776 | RootFile=get(hhuvmat.RootFile,'String'); |
---|
| 777 | filebase=fullfile(RootPath,RootFile); |
---|
| 778 | while exist([filebase '.xml'],'file') |
---|
| 779 | filebase=[filebase '~']; |
---|
| 780 | end |
---|
| 781 | outputfile=[filebase '.xml']; |
---|
[114] | 782 | errormsg=update_imadoc(GeometryCalib,outputfile); |
---|
| 783 | if ~strcmp(errormsg,'') |
---|
| 784 | msgbox_uvmat('ERROR',errormsg); |
---|
| 785 | end |
---|
[109] | 786 | listfile=get(handles.coord_files,'string'); |
---|
| 787 | if isequal(listfile,{''}) |
---|
| 788 | listfile={outputfile}; |
---|
| 789 | else |
---|
| 790 | listfile=[listfile;{outputfile}]%update the list of coord files |
---|
| 791 | end |
---|
| 792 | set(handles.coord_files,'string',listfile); |
---|
| 793 | end |
---|
| 794 | set(handles.ListCoord,'Value',1)% refresh the display of coordinates |
---|
| 795 | set(handles.ListCoord,'String',{'......'}) |
---|
| 796 | |
---|
[114] | 797 | % -------------------------------------------------------------------- |
---|
| 798 | % --- Executes on button press in CLEAR_PTS: clear the list of calibration points |
---|
| 799 | function CLEAR_PTS_Callback(hObject, eventdata, handles) |
---|
| 800 | % -------------------------------------------------------------------- |
---|
| 801 | set(handles.ListCoord,'Value',1)% refresh the display of coordinates |
---|
| 802 | set(handles.ListCoord,'String',{'......'}) |
---|
| 803 | MenuPlot_Callback(hObject, eventdata, handles) |
---|
| 804 | |
---|
[109] | 805 | %------------------------------------------------------------------------ |
---|
| 806 | % --- Executes on button press in CLEAR. |
---|
| 807 | function CLEAR_Callback(hObject, eventdata, handles) |
---|
| 808 | %------------------------------------------------------------------------ |
---|
| 809 | set(handles.coord_files,'Value',1) |
---|
| 810 | set(handles.coord_files,'String',{''}) |
---|
| 811 | |
---|
| 812 | %------------------------------------------------------------------------ |
---|
[2] | 813 | function XObject_Callback(hObject, eventdata, handles) |
---|
[109] | 814 | %------------------------------------------------------------------------ |
---|
[2] | 815 | update_list(hObject, eventdata,handles) |
---|
| 816 | |
---|
[109] | 817 | %------------------------------------------------------------------------ |
---|
[2] | 818 | function YObject_Callback(hObject, eventdata, handles) |
---|
[109] | 819 | %------------------------------------------------------------------------ |
---|
[2] | 820 | update_list(hObject, eventdata,handles) |
---|
| 821 | |
---|
[109] | 822 | %------------------------------------------------------------------------ |
---|
[2] | 823 | function ZObject_Callback(hObject, eventdata, handles) |
---|
[109] | 824 | %------------------------------------------------------------------------ |
---|
[2] | 825 | update_list(hObject, eventdata,handles) |
---|
| 826 | |
---|
[60] | 827 | %------------------------------------------------------------------------ |
---|
[2] | 828 | function update_list(hObject, eventdata, handles) |
---|
[60] | 829 | %------------------------------------------------------------------------ |
---|
[2] | 830 | str4=get(handles.XImage,'String'); |
---|
| 831 | str5=get(handles.YImage,'String'); |
---|
| 832 | str1=get(handles.XObject,'String'); |
---|
| 833 | tt=double(str1); |
---|
| 834 | str2=get(handles.YObject,'String'); |
---|
| 835 | str3=get(handles.ZObject,'String'); |
---|
| 836 | if ~isempty(str1) & ~isequal(double(str1),32) & (isempty(str3)|isequal(double(str3),32)) |
---|
| 837 | str3='0';%put z to 0 by default |
---|
| 838 | end |
---|
| 839 | strline=[str1 ' | ' str2 ' | ' str3 ' | ' str4 ' | ' str5]; |
---|
| 840 | Coord=get(handles.ListCoord,'String'); |
---|
| 841 | val=get(handles.ListCoord,'Value'); |
---|
| 842 | Coord{val}=strline; |
---|
| 843 | set(handles.ListCoord,'String',Coord) |
---|
[60] | 844 | %update the plot |
---|
| 845 | ListCoord_Callback(hObject, eventdata, handles) |
---|
[67] | 846 | MenuPlot_Callback(hObject, eventdata, handles) |
---|
[71] | 847 | |
---|
[60] | 848 | %------------------------------------------------------------------------ |
---|
[2] | 849 | % --- Executes on selection change in ListCoord. |
---|
| 850 | function ListCoord_Callback(hObject, eventdata, handles) |
---|
[60] | 851 | %------------------------------------------------------------------------ |
---|
[71] | 852 | huvmat=findobj(allchild(0),'Name','uvmat');%find the current uvmat interface handle |
---|
| 853 | hplot=findobj(huvmat,'Tag','axes3');%main plotting axis of uvmat |
---|
| 854 | hhh=findobj(hplot,'Tag','calib_marker'); |
---|
[2] | 855 | Coord_cell=get(handles.ListCoord,'String'); |
---|
| 856 | val=get(handles.ListCoord,'Value'); |
---|
[78] | 857 | if numel(val)>1 |
---|
| 858 | return %no action if several lines have been selected |
---|
| 859 | end |
---|
[71] | 860 | coord_str=Coord_cell{val}; |
---|
| 861 | k=findstr('|',coord_str); |
---|
| 862 | if isempty(k)%last line '.....' selected |
---|
| 863 | if ~isempty(hhh) |
---|
| 864 | delete(hhh)%delete the circle marker |
---|
[2] | 865 | end |
---|
[71] | 866 | return |
---|
| 867 | end |
---|
| 868 | %fill the edit boxex |
---|
| 869 | set(handles.XObject,'String',coord_str(1:k(1)-5)) |
---|
| 870 | set(handles.YObject,'String',coord_str(k(1)+5:k(2)-5)) |
---|
| 871 | set(handles.ZObject,'String',coord_str(k(2)+5:k(3)-5)) |
---|
| 872 | set(handles.XImage,'String',coord_str(k(3)+5:k(4)-5)) |
---|
| 873 | set(handles.YImage,'String',coord_str(k(4)+5:end)) |
---|
| 874 | h_menu_coord=findobj(huvmat,'Tag','transform_fct'); |
---|
| 875 | menu=get(h_menu_coord,'String'); |
---|
| 876 | choice=get(h_menu_coord,'Value'); |
---|
| 877 | if iscell(menu) |
---|
| 878 | option=menu{choice}; |
---|
| 879 | else |
---|
| 880 | option='px'; %default |
---|
| 881 | end |
---|
| 882 | if isequal(option,'phys') |
---|
| 883 | XCoord=str2num(coord_str(1:k(1)-5)); |
---|
| 884 | YCoord=str2num(coord_str(k(1)+5:k(2)-5)); |
---|
| 885 | elseif isequal(option,'px')|| isequal(option,'') |
---|
| 886 | XCoord=str2num(coord_str(k(3)+5:k(4)-5)); |
---|
| 887 | YCoord=str2num(coord_str(k(4)+5:end)); |
---|
| 888 | else |
---|
| 889 | msgbox_uvmat('ERROR','the choice in menu_coord of uvmat must be px or phys ') |
---|
| 890 | end |
---|
| 891 | if isempty(XCoord)||isempty(YCoord) |
---|
| 892 | if ~isempty(hhh) |
---|
| 893 | delete(hhh)%delete the circle marker |
---|
[2] | 894 | end |
---|
[71] | 895 | return |
---|
[2] | 896 | end |
---|
[71] | 897 | xlim=get(hplot,'XLim'); |
---|
| 898 | ylim=get(hplot,'YLim'); |
---|
| 899 | ind_range=max(abs(xlim(2)-xlim(1)),abs(ylim(end)-ylim(1)))/20;%defines the size of the circle marker |
---|
| 900 | if isempty(hhh) |
---|
| 901 | axes(hplot) |
---|
| 902 | rectangle('Curvature',[1 1],... |
---|
| 903 | 'Position',[XCoord-ind_range/2 YCoord-ind_range/2 ind_range ind_range],'EdgeColor','m',... |
---|
| 904 | 'LineStyle','-','Tag','calib_marker'); |
---|
| 905 | else |
---|
| 906 | set(hhh,'Position',[XCoord-ind_range/2 YCoord-ind_range/2 ind_range ind_range]) |
---|
| 907 | end |
---|
[2] | 908 | |
---|
[60] | 909 | %------------------------------------------------------------------------ |
---|
[2] | 910 | % --- Executes on selection change in edit_append. |
---|
| 911 | function edit_append_Callback(hObject, eventdata, handles) |
---|
[60] | 912 | %------------------------------------------------------------------------ |
---|
[2] | 913 | choice=get(handles.edit_append,'Value'); |
---|
[78] | 914 | if choice |
---|
| 915 | set(handles.edit_append,'BackgroundColor',[1 1 0]) |
---|
| 916 | else |
---|
| 917 | set(handles.edit_append,'BackgroundColor',[0.7 0.7 0.7]) |
---|
[2] | 918 | end |
---|
| 919 | |
---|
| 920 | function NEW_Callback(hObject, eventdata, handles) |
---|
| 921 | %A METTRE SOUS UN BOUTON |
---|
| 922 | huvmat=findobj(allchild(0),'Name','uvmat'); |
---|
| 923 | hchild=get(huvmat,'children'); |
---|
| 924 | hcoord=findobj(hchild,'Tag','menu_coord'); |
---|
| 925 | coordtype=get(hcoord,'Value'); |
---|
| 926 | haxes=findobj(hchild,'Tag','axes3'); |
---|
| 927 | AxeData=get(haxes,'UserData'); |
---|
| 928 | if ~isequal(hcoord,2) |
---|
| 929 | set(hcoord,'Value',2) |
---|
| 930 | huvmat=uvmat(AxeData); |
---|
| 931 | 'relancer uvmat'; |
---|
| 932 | end |
---|
| 933 | if ~isfield(AxeData,'ZoomAxes') |
---|
[42] | 934 | msgbox_uvmat('ERROR','first draw a window around a grid marker') |
---|
[2] | 935 | return |
---|
| 936 | end |
---|
| 937 | XLim=get(AxeData.ZoomAxes,'XLim'); |
---|
| 938 | YLim=get(AxeData.ZoomAxes,'YLim'); |
---|
| 939 | np=size(AxeData.A); |
---|
| 940 | ind_sub_x=round(XLim); |
---|
| 941 | ind_sub_y=np(1)-round(YLim); |
---|
| 942 | Mfiltre=AxeData.A([ind_sub_y(2):ind_sub_y(1)] ,ind_sub_x,:); |
---|
| 943 | Mfiltre_norm=double(Mfiltre); |
---|
| 944 | Mfiltre_norm=Mfiltre_norm/sum(sum(Mfiltre_norm)); |
---|
| 945 | Mfiltre_norm=100*(Mfiltre_norm-mean(mean(Mfiltre_norm))); |
---|
| 946 | Atype=class(AxeData.A); |
---|
| 947 | Data.NbDim=2; |
---|
| 948 | Data.A=filter2(Mfiltre_norm,double(AxeData.A)); |
---|
| 949 | Data.A=feval(Atype,Data.A); |
---|
| 950 | Data.AName='image'; |
---|
| 951 | Data.AX=AxeData.AX; |
---|
| 952 | Data.AY=AxeData.AY; |
---|
| 953 | Data.CoordType='px'; |
---|
| 954 | plot_field(Data) |
---|
| 955 | |
---|
[60] | 956 | %------------------------------------------------------------------------ |
---|
[2] | 957 | function MenuPlot_Callback(hObject, eventdata, handles) |
---|
[60] | 958 | %------------------------------------------------------------------------ |
---|
[2] | 959 | huvmat=findobj(allchild(0),'Name','uvmat');%find the current uvmat interface handle |
---|
| 960 | UvData=get(huvmat,'UserData');%Data associated to the current uvmat interface |
---|
| 961 | hhuvmat=guidata(huvmat); %handles of GUI elements in uvmat |
---|
| 962 | hplot=findobj(huvmat,'Tag','axes3');%main plotting axis of uvmat |
---|
[60] | 963 | h_menu_coord=findobj(huvmat,'Tag','transform_fct'); |
---|
[2] | 964 | menu=get(h_menu_coord,'String'); |
---|
| 965 | choice=get(h_menu_coord,'Value'); |
---|
| 966 | if iscell(menu) |
---|
| 967 | option=menu{choice}; |
---|
| 968 | else |
---|
| 969 | option='px'; %default |
---|
| 970 | end |
---|
| 971 | Coord_cell=get(handles.ListCoord,'String'); |
---|
| 972 | ObjectData=read_geometry_calib(Coord_cell); |
---|
| 973 | %ObjectData=read_geometry_calib(handles);%read the interface input parameters defining the object |
---|
[71] | 974 | if ~isempty(ObjectData.Coord) |
---|
| 975 | if isequal(option,'phys') |
---|
| 976 | ObjectData.Coord=ObjectData.Coord(:,[1:3]); |
---|
| 977 | elseif isequal(option,'px')||isequal(option,'') |
---|
| 978 | ObjectData.Coord=ObjectData.Coord(:,[4:5]); |
---|
| 979 | else |
---|
| 980 | msgbox_uvmat('ERROR','the choice in menu_coord of uvmat must be '''', px or phys ') |
---|
| 981 | end |
---|
[2] | 982 | end |
---|
| 983 | axes(hhuvmat.axes3) |
---|
| 984 | hh=findobj('Tag','calib_points'); |
---|
[71] | 985 | if ~isempty(ObjectData.Coord) && isempty(hh) |
---|
[2] | 986 | hh=line(ObjectData.Coord(:,1),ObjectData.Coord(:,2),'Color','m','Tag','calib_points','LineStyle','.','Marker','+'); |
---|
[71] | 987 | elseif isempty(ObjectData.Coord)%empty list of points, suppress the plot |
---|
| 988 | delete(hh) |
---|
[2] | 989 | else |
---|
| 990 | set(hh,'XData',ObjectData.Coord(:,1)) |
---|
| 991 | set(hh,'YData',ObjectData.Coord(:,2)) |
---|
| 992 | end |
---|
[61] | 993 | pause(.1) |
---|
| 994 | figure(handles.geometry_calib) |
---|
[2] | 995 | |
---|
| 996 | % -------------------------------------------------------------------- |
---|
| 997 | function MenuHelp_Callback(hObject, eventdata, handles) |
---|
[116] | 998 | path_to_uvmat=which('uvmat');% check the path of uvmat |
---|
[2] | 999 | pathelp=fileparts(path_to_uvmat); |
---|
[116] | 1000 | helpfile=fullfile(pathelp,'uvmat_doc','uvmat_doc.html'); |
---|
[36] | 1001 | if isempty(dir(helpfile)), msgbox_uvmat('ERROR','Please put the help file uvmat_doc.html in the sub-directory /uvmat_doc of the UVMAT package') |
---|
[2] | 1002 | else |
---|
[36] | 1003 | addpath (fullfile(pathelp,'uvmat_doc')) |
---|
[2] | 1004 | web([helpfile '#geometry_calib']) |
---|
| 1005 | end |
---|
| 1006 | |
---|
[17] | 1007 | %------------------------------------------------------------------------ |
---|
[2] | 1008 | function MenuCreateGrid_Callback(hObject, eventdata, handles) |
---|
[17] | 1009 | %------------------------------------------------------------------------ |
---|
[36] | 1010 | %hcalib=get(handles.calib_type,'parent');%handles of the GUI geometry_calib |
---|
[61] | 1011 | CalibData=get(handles.geometry_calib,'UserData'); |
---|
[12] | 1012 | Tinput=[];%default |
---|
| 1013 | if isfield(CalibData,'grid') |
---|
| 1014 | Tinput=CalibData.grid; |
---|
| 1015 | end |
---|
[71] | 1016 | [T,CalibData.grid]=create_grid(Tinput);%display the GUI create_grid |
---|
[61] | 1017 | set(handles.geometry_calib,'UserData',CalibData) |
---|
[2] | 1018 | |
---|
[12] | 1019 | %grid in phys space |
---|
[71] | 1020 | Coord=get(handles.ListCoord,'String'); |
---|
| 1021 | val=get(handles.ListCoord,'Value'); |
---|
| 1022 | data=read_geometry_calib(Coord); |
---|
| 1023 | %nbpoints=size(data.Coord,1); %nbre of calibration points |
---|
| 1024 | data.Coord(val:val+size(T,1)-1,1:3)=T(end:-1:1,:);%update the existing list of phys coordinates from the GUI create_grid |
---|
| 1025 | % for i=1:nbpoints |
---|
| 1026 | % for j=1:5 |
---|
| 1027 | % Coord{i,j}=num2str(data.Coord(i,j),4);%display coordiantes with 4 digits |
---|
| 1028 | % end |
---|
| 1029 | % end |
---|
| 1030 | %update the phys coordinates starting from the selected point (down in the |
---|
| 1031 | Coord(end,:)=[]; %remove last string '.....' |
---|
| 1032 | for i=1:size(data.Coord,1) |
---|
| 1033 | for j=1:5 |
---|
[17] | 1034 | Coord{i,j}=num2str(data.Coord(i,j),4);%display coordiantes with 4 digits |
---|
| 1035 | end |
---|
| 1036 | end |
---|
| 1037 | |
---|
| 1038 | %size(data.Coord,1) |
---|
[12] | 1039 | Tabchar=cell2tab(Coord,' | '); |
---|
[71] | 1040 | Tabchar=[Tabchar ;{'......'}]; |
---|
[12] | 1041 | set(handles.ListCoord,'String',Tabchar) |
---|
[2] | 1042 | |
---|
[71] | 1043 | % ----------------------------------------------------------------------- |
---|
| 1044 | % --- automatic grid dectection from local maxima of the images |
---|
[60] | 1045 | function MenuDetectGrid_Callback(hObject, eventdata, handles) |
---|
[71] | 1046 | %------------------------------------------------------------------------ |
---|
[108] | 1047 | CalibData=get(handles.geometry_calib,'UserData');%get information stored on the GUI geometry_calib |
---|
[60] | 1048 | grid_input=[];%default |
---|
| 1049 | if isfield(CalibData,'grid') |
---|
| 1050 | grid_input=CalibData.grid;%retrieve the previously used grid |
---|
| 1051 | end |
---|
[109] | 1052 | [T,CalibData.grid]=create_grid(grid_input,'detect_grid');%display the GUI create_grid, read the set of phys coordinates T |
---|
| 1053 | |
---|
[108] | 1054 | set(handles.geometry_calib,'UserData',CalibData)%store the phys grid parameters for later use |
---|
[60] | 1055 | |
---|
[108] | 1056 | %read the four last point coordinates in pixels |
---|
| 1057 | Coord_cell=get(handles.ListCoord,'String');%read list of coordinates on geometry_calib |
---|
[60] | 1058 | data=read_geometry_calib(Coord_cell); |
---|
| 1059 | nbpoints=size(data.Coord,1); %nbre of calibration points |
---|
[62] | 1060 | if nbpoints~=4 |
---|
| 1061 | msgbox_uvmat('ERROR','four points must be selected by the mouse, beginning by the new x axis, to delimitate the phs grid area') |
---|
[71] | 1062 | return |
---|
[60] | 1063 | end |
---|
[71] | 1064 | corners_X=(data.Coord(end:-1:end-3,4)); %pixel absissa of the four corners |
---|
| 1065 | corners_Y=(data.Coord(end:-1:end-3,5)); |
---|
[60] | 1066 | |
---|
[71] | 1067 | %reorder the last two points (the two first in the list) if needed |
---|
[63] | 1068 | angles=angle((corners_X-corners_X(1))+i*(corners_Y-corners_Y(1))); |
---|
[62] | 1069 | if abs(angles(4)-angles(2))>abs(angles(3)-angles(2)) |
---|
| 1070 | X_end=corners_X(4); |
---|
| 1071 | Y_end=corners_Y(4); |
---|
| 1072 | corners_X(4)=corners_X(3); |
---|
| 1073 | corners_Y(4)=corners_Y(3); |
---|
| 1074 | corners_X(3)=X_end; |
---|
| 1075 | corners_Y(3)=Y_end; |
---|
| 1076 | end |
---|
| 1077 | |
---|
[108] | 1078 | %read the current image, displayed in the GUI uvmat |
---|
[60] | 1079 | huvmat=findobj(allchild(0),'Name','uvmat'); |
---|
| 1080 | UvData=get(huvmat,'UserData'); |
---|
| 1081 | A=UvData.Field.A; |
---|
| 1082 | npxy=size(A); |
---|
| 1083 | %linear transform on the current image |
---|
[108] | 1084 | X=[CalibData.grid.x_0 CalibData.grid.x_1 CalibData.grid.x_0 CalibData.grid.x_1]';%corner absissa in the phys coordinates |
---|
| 1085 | Y=[CalibData.grid.y_0 CalibData.grid.y_0 CalibData.grid.y_1 CalibData.grid.y_1]';%corner ordinates in the phys coordinates |
---|
[109] | 1086 | |
---|
[108] | 1087 | %calculate transform matrices: |
---|
| 1088 | % reference: http://alumni.media.mit.edu/~cwren/interpolator/ by Christopher R. Wren |
---|
| 1089 | B = [ X Y ones(size(X)) zeros(4,3) -X.*corners_X -Y.*corners_X ... |
---|
| 1090 | zeros(4,3) X Y ones(size(X)) -X.*corners_Y -Y.*corners_Y ]; |
---|
| 1091 | B = reshape (B', 8 , 8 )'; |
---|
| 1092 | D = [ corners_X , corners_Y ]; |
---|
| 1093 | D = reshape (D', 8 , 1 ); |
---|
| 1094 | l = inv(B' * B) * B' * D; |
---|
| 1095 | Amat = reshape([l(1:6)' 0 0 1 ],3,3)'; |
---|
| 1096 | C = [l(7:8)' 1]; |
---|
| 1097 | |
---|
[114] | 1098 | %GeometryCalib.CalibrationType='tsai'; |
---|
| 1099 | %GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function |
---|
| 1100 | % GeometryCalib.f=1; |
---|
| 1101 | % GeometryCalib.dpx=1; |
---|
| 1102 | % GeometryCalib.dpy=1; |
---|
| 1103 | % GeometryCalib.sx=1; |
---|
| 1104 | % GeometryCalib.Cx=0; |
---|
| 1105 | % GeometryCalib.Cy=0; |
---|
| 1106 | % GeometryCalib.kappa1=0; |
---|
| 1107 | % GeometryCalib.Tx=Amat(1,3); |
---|
| 1108 | % GeometryCalib.Ty=Amat(2,3); |
---|
| 1109 | % GeometryCalib.Tz=1; |
---|
| 1110 | % GeometryCalib.R=[Amat(1,1),Amat(1,2),0;Amat(2,1),Amat(2,2),0;C(1),C(2),0]; |
---|
| 1111 | % |
---|
| 1112 | % [Amod,Rangx,Rangy]=phys_Ima(A-min(min(A)),GeometryCalib,0); |
---|
| 1113 | |
---|
| 1114 | GeometryCalib.fx_fy=[1 1]; |
---|
| 1115 | GeometryCalib.Tx_Ty_Tz=[Amat(1,3) Amat(2,3) 1]; |
---|
[108] | 1116 | GeometryCalib.R=[Amat(1,1),Amat(1,2),0;Amat(2,1),Amat(2,2),0;C(1),C(2),0]; |
---|
[114] | 1117 | path_uvmat=which('uvmat');% check the path detected for source file uvmat |
---|
| 1118 | path_UVMAT=fileparts(path_uvmat); %path to UVMAT |
---|
| 1119 | addpath(fullfile(path_UVMAT,'transform_field')) |
---|
| 1120 | Data.ListVarName={'AY','AX','A'}; |
---|
| 1121 | Data.VarDimName={'AY','AX',{'AY','AX'}}; |
---|
| 1122 | if ndims(A)==3 |
---|
| 1123 | A=mean(A,3); |
---|
| 1124 | end |
---|
| 1125 | Data.A=A-min(min(A)); |
---|
| 1126 | Data.AY=[npxy(1)-0.5 0.5]; |
---|
| 1127 | Data.AX=[0.5 npxy(2)]; |
---|
| 1128 | Data.CoordType='px'; |
---|
| 1129 | Calib.GeometryCalib=GeometryCalib; |
---|
| 1130 | DataOut=phys(Data,Calib) |
---|
| 1131 | rmpath(fullfile(path_UVMAT,'transform_field')) |
---|
| 1132 | Amod=DataOut.A; |
---|
| 1133 | Rangx=DataOut.AX; |
---|
| 1134 | Rangy=DataOut.AY; |
---|
| 1135 | % GeometryCalib.dpx=1; |
---|
| 1136 | % GeometryCalib.dpy=1; |
---|
| 1137 | % GeometryCalib.sx=1; |
---|
| 1138 | % GeometryCalib.Cx=0; |
---|
| 1139 | % GeometryCalib.Cy=0; |
---|
| 1140 | % GeometryCalib.kappa1=0; |
---|
| 1141 | % GeometryCalib.Tx=Amat(1,3); |
---|
| 1142 | % GeometryCalib.Ty=Amat(2,3); |
---|
| 1143 | % GeometryCalib.Tz=1; |
---|
| 1144 | % GeometryCalib.R=[Amat(1,1),Amat(1,2),0;Amat(2,1),Amat(2,2),0;C(1),C(2),0]; |
---|
| 1145 | % |
---|
| 1146 | % [Amod,Rangx,Rangy]=phys_Ima(A-min(min(A)),GeometryCalib,0); |
---|
[108] | 1147 | |
---|
| 1148 | |
---|
[60] | 1149 | Amod=double(Amod); |
---|
[114] | 1150 | % figure(12) %display corrected image |
---|
[109] | 1151 | % Amax=max(max(Amod)); |
---|
| 1152 | % image(Rangx,Rangy,uint8(255*Amod/Amax)) |
---|
[114] | 1153 | |
---|
[88] | 1154 | Dx=(Rangx(2)-Rangx(1))/(npxy(2)-1); %x mesh in real space |
---|
| 1155 | Dy=(Rangy(2)-Rangy(1))/(npxy(1)-1); %y mesh in real space |
---|
[114] | 1156 | ind_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 |
---|
| 1157 | ind_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 |
---|
[60] | 1158 | nbpoints=size(T,1); |
---|
| 1159 | for ipoint=1:nbpoints |
---|
| 1160 | i0=1+round((T(ipoint,1)-Rangx(1))/Dx);%round(Xpx(ipoint)); |
---|
| 1161 | j0=1+round((T(ipoint,2)-Rangy(1))/Dy);%round(Xpx(ipoint)); |
---|
[109] | 1162 | j0min=max(j0-ind_range_y,1); |
---|
| 1163 | j0max=min(j0+ind_range_y,size(Amod,1)); |
---|
| 1164 | i0min=max(i0-ind_range_x,1); |
---|
| 1165 | i0max=min(i0+ind_range_x,size(Amod,2)); |
---|
| 1166 | Asub=Amod(j0min:j0max,i0min:i0max); |
---|
[60] | 1167 | x_profile=sum(Asub,1); |
---|
| 1168 | y_profile=sum(Asub,2); |
---|
| 1169 | [Amax,ind_x_max]=max(x_profile); |
---|
| 1170 | [Amax,ind_y_max]=max(y_profile); |
---|
[61] | 1171 | %sub-pixel improvement using moments |
---|
| 1172 | x_shift=0; |
---|
| 1173 | y_shift=0; |
---|
[109] | 1174 | %if ind_x_max+2<=2*ind_range_x+1 && ind_x_max-2>=1 |
---|
| 1175 | if ind_x_max+2<=numel(x_profile) && ind_x_max-2>=1 |
---|
[61] | 1176 | Atop=x_profile(ind_x_max-2:ind_x_max+2); |
---|
| 1177 | x_shift=sum(Atop.*[-2 -1 0 1 2])/sum(Atop); |
---|
| 1178 | end |
---|
[114] | 1179 | if ind_y_max+2<=numel(y_profile) && ind_y_max-2>=1 |
---|
[61] | 1180 | Atop=y_profile(ind_y_max-2:ind_y_max+2); |
---|
| 1181 | y_shift=sum(Atop.*[-2 -1 0 1 2]')/sum(Atop); |
---|
| 1182 | end |
---|
[109] | 1183 | Delta(ipoint,1)=(x_shift+ind_x_max+i0min-i0-1)*Dx;%shift from the initial guess |
---|
| 1184 | Delta(ipoint,2)=(y_shift+ind_y_max+j0min-j0-1)*Dy; |
---|
[60] | 1185 | end |
---|
| 1186 | Tmod=T(:,(1:2))+Delta; |
---|
| 1187 | [Xpx,Ypx]=px_XYZ(GeometryCalib,Tmod(:,1),Tmod(:,2)); |
---|
[63] | 1188 | for ipoint=1:nbpoints |
---|
| 1189 | Coord{ipoint,1}=num2str(T(ipoint,1),4);%display coordiantes with 4 digits |
---|
| 1190 | Coord{ipoint,2}=num2str(T(ipoint,2),4);%display coordiantes with 4 digits |
---|
[109] | 1191 | Coord{ipoint,3}=num2str(T(ipoint,3),4);%display coordiantes with 4 digits; |
---|
| 1192 | Coord{ipoint,4}=num2str(Xpx(ipoint),4);%display coordiantes with 4 digits |
---|
| 1193 | Coord{ipoint,5}=num2str(Ypx(ipoint),4);%display coordiantes with 4 digits |
---|
[60] | 1194 | end |
---|
[71] | 1195 | Tabchar=cell2tab(Coord(end:-1:1,:),' | '); |
---|
| 1196 | Tabchar=[Tabchar ;{'......'}]; |
---|
[60] | 1197 | set(handles.ListCoord,'Value',1) |
---|
| 1198 | set(handles.ListCoord,'String',Tabchar) |
---|
[67] | 1199 | MenuPlot_Callback(hObject, eventdata, handles) |
---|
[60] | 1200 | |
---|
[71] | 1201 | %----------------------------------------------------------------------- |
---|
| 1202 | function MenuTranslatePoints_Callback(hObject, eventdata, handles) |
---|
| 1203 | %----------------------------------------------------------------------- |
---|
| 1204 | %hcalib=get(handles.calib_type,'parent');%handles of the GUI geometry_calib |
---|
| 1205 | CalibData=get(handles.geometry_calib,'UserData'); |
---|
| 1206 | Tinput=[];%default |
---|
| 1207 | if isfield(CalibData,'translate') |
---|
| 1208 | Tinput=CalibData.translate; |
---|
| 1209 | end |
---|
| 1210 | T=translate_points(Tinput);%display translate_points GUI and get shift parameters |
---|
| 1211 | CalibData.translate=T; |
---|
| 1212 | set(handles.geometry_calib,'UserData',CalibData) |
---|
| 1213 | %translation |
---|
| 1214 | Coord_cell=get(handles.ListCoord,'String'); |
---|
| 1215 | data=read_geometry_calib(Coord_cell); |
---|
| 1216 | data.Coord(:,1)=T(1)+data.Coord(:,1); |
---|
| 1217 | data.Coord(:,2)=T(2)+data.Coord(:,2); |
---|
| 1218 | data.Coord(:,3)=T(3)+data.Coord(:,3); |
---|
| 1219 | data.Coord(:,[4 5])=data.Coord(:,[4 5]); |
---|
| 1220 | for i=1:size(data.Coord,1) |
---|
| 1221 | for j=1:5 |
---|
| 1222 | Coord{i,j}=num2str(data.Coord(i,j),4);%phys x,y,z |
---|
| 1223 | end |
---|
| 1224 | end |
---|
| 1225 | Tabchar=cell2tab(Coord,' | '); |
---|
[88] | 1226 | Tabchar=[Tabchar; {'.....'}]; |
---|
[71] | 1227 | %set(handles.ListCoord,'Value',1) |
---|
| 1228 | set(handles.ListCoord,'String',Tabchar) |
---|
| 1229 | |
---|
| 1230 | |
---|
| 1231 | % -------------------------------------------------------------------- |
---|
| 1232 | function MenuRotatePoints_Callback(hObject, eventdata, handles) |
---|
| 1233 | %hcalib=get(handles.calib_type,'parent');%handles of the GUI geometry_calib |
---|
| 1234 | CalibData=get(handles.geometry_calib,'UserData'); |
---|
| 1235 | Tinput=[];%default |
---|
| 1236 | if isfield(CalibData,'rotate') |
---|
| 1237 | Tinput=CalibData.rotate; |
---|
| 1238 | end |
---|
| 1239 | T=rotate_points(Tinput);%display translate_points GUI and get shift parameters |
---|
| 1240 | CalibData.rotate=T; |
---|
| 1241 | set(handles.geometry_calib,'UserData',CalibData) |
---|
| 1242 | %----------------------------------------------------- |
---|
| 1243 | %rotation |
---|
| 1244 | Phi=T(1); |
---|
| 1245 | O_x=0;%default |
---|
| 1246 | O_y=0;%default |
---|
| 1247 | if numel(T)>=2 |
---|
| 1248 | O_x=T(2);%default |
---|
| 1249 | end |
---|
| 1250 | if numel(T)>=3 |
---|
| 1251 | O_y=T(3);%default |
---|
| 1252 | end |
---|
| 1253 | Coord_cell=get(handles.ListCoord,'String'); |
---|
| 1254 | data=read_geometry_calib(Coord_cell); |
---|
| 1255 | r1=cos(pi*Phi/180); |
---|
| 1256 | r2=-sin(pi*Phi/180); |
---|
| 1257 | r3=sin(pi*Phi/180); |
---|
| 1258 | r4=cos(pi*Phi/180); |
---|
| 1259 | x=data.Coord(:,1)-O_x; |
---|
| 1260 | y=data.Coord(:,2)-O_y; |
---|
| 1261 | data.Coord(:,1)=r1*x+r2*y; |
---|
| 1262 | data.Coord(:,2)=r3*x+r4*y; |
---|
| 1263 | % data.Coord(:,[4 5])=data.Coord(:,[4 5]); |
---|
| 1264 | for i=1:size(data.Coord,1) |
---|
| 1265 | for j=1:5 |
---|
| 1266 | Coord{i,j}=num2str(data.Coord(i,j),4);%phys x,y,z |
---|
| 1267 | end |
---|
| 1268 | end |
---|
| 1269 | Tabchar=cell2tab(Coord,' | '); |
---|
| 1270 | Tabchar=[Tabchar;{'......'}]; |
---|
| 1271 | set(handles.ListCoord,'Value',1) |
---|
| 1272 | set(handles.ListCoord,'String',Tabchar) |
---|
| 1273 | |
---|
[109] | 1274 | |
---|
| 1275 | % %------------------------------------------------------------------------ |
---|
| 1276 | % % --- Executes on button press in rotation. |
---|
| 1277 | % function rotation_Callback(hObject, eventdata, handles) |
---|
| 1278 | % %------------------------------------------------------------------------ |
---|
| 1279 | % angle_rot=(pi/180)*str2num(get(handles.Phi,'String')); |
---|
| 1280 | % Coord_cell=get(handles.ListCoord,'String'); |
---|
| 1281 | % data=read_geometry_calib(Coord_cell); |
---|
| 1282 | % data.Coord(:,1)=cos(angle_rot)*data.Coord(:,1)+sin(angle_rot)*data.Coord(:,2); |
---|
| 1283 | % data.Coord(:,1)=-sin(angle_rot)*data.Coord(:,1)+cos(angle_rot)*data.Coord(:,2); |
---|
| 1284 | % set(handles.XObject,'String',num2str(data.Coord(:,1),4)); |
---|
| 1285 | % set(handles.YObject,'String',num2str(data.Coord(:,2),4)); |
---|
| 1286 | |
---|
| 1287 | |
---|
[108] | 1288 | %------------------------------------------------------------------------ |
---|
| 1289 | % image transform from px to phys |
---|
| 1290 | %INPUT: |
---|
| 1291 | %Zindex: index of plane |
---|
[114] | 1292 | % function [A_out,Rangx,Rangy]=phys_Ima(A,Calib,ZIndex) |
---|
| 1293 | % %------------------------------------------------------------------------ |
---|
| 1294 | % xcorner=[]; |
---|
| 1295 | % ycorner=[]; |
---|
| 1296 | % npx=[]; |
---|
| 1297 | % npy=[]; |
---|
| 1298 | % siz=size(A) |
---|
| 1299 | % npx=[npx siz(2)]; |
---|
| 1300 | % npy=[npy siz(1)] |
---|
| 1301 | % xima=[0.5 siz(2)-0.5 0.5 siz(2)-0.5];%image coordinates of corners |
---|
| 1302 | % yima=[0.5 0.5 siz(1)-0.5 siz(1)-0.5]; |
---|
| 1303 | % [xcorner,ycorner]=phys_XYZ(Calib,xima,yima,ZIndex);%corresponding physical coordinates |
---|
| 1304 | % Rangx(1)=min(xcorner); |
---|
| 1305 | % Rangx(2)=max(xcorner); |
---|
| 1306 | % Rangy(2)=min(ycorner); |
---|
| 1307 | % Rangy(1)=max(ycorner); |
---|
| 1308 | % test_multi=(max(npx)~=min(npx)) | (max(npy)~=min(npy)); |
---|
| 1309 | % npx=max(npx); |
---|
| 1310 | % npy=max(npy); |
---|
| 1311 | % x=linspace(Rangx(1),Rangx(2),npx); |
---|
| 1312 | % y=linspace(Rangy(1),Rangy(2),npy); |
---|
| 1313 | % [X,Y]=meshgrid(x,y);%grid in physical coordiantes |
---|
| 1314 | % vec_B=[]; |
---|
| 1315 | % |
---|
| 1316 | % zphys=0; %default |
---|
| 1317 | % if isfield(Calib,'SliceCoord') %.Z= index of plane |
---|
| 1318 | % SliceCoord=Calib.SliceCoord(ZIndex,:); |
---|
| 1319 | % zphys=SliceCoord(3); %to generalize for non-parallel planes |
---|
| 1320 | % end |
---|
| 1321 | % [XIMA,YIMA]=px_XYZ(Calib,X,Y,zphys);%corresponding image indices for each point in the real space grid |
---|
| 1322 | % XIMA=reshape(round(XIMA),1,npx*npy);%indices reorganized in 'line' |
---|
| 1323 | % YIMA=reshape(round(YIMA),1,npx*npy); |
---|
| 1324 | % flagin=XIMA>=1 & XIMA<=npx & YIMA >=1 & YIMA<=npy;%flagin=1 inside the original image |
---|
| 1325 | % testuint8=isa(A,'uint8'); |
---|
| 1326 | % testuint16=isa(A,'uint16'); |
---|
| 1327 | % if numel(siz)==2 %(B/W images) |
---|
| 1328 | % vec_A=reshape(A,1,npx*npy);%put the original image in line |
---|
| 1329 | % ind_in=find(flagin); |
---|
| 1330 | % ind_out=find(~flagin); |
---|
| 1331 | % ICOMB=((XIMA-1)*npy+(npy+1-YIMA)); |
---|
| 1332 | % ICOMB=ICOMB(flagin);%index corresponding to XIMA and YIMA in the aligned original image vec_A |
---|
| 1333 | % vec_B(ind_in)=vec_A(ICOMB); |
---|
| 1334 | % vec_B(ind_out)=zeros(size(ind_out)); |
---|
| 1335 | % A_out=reshape(vec_B,npy,npx);%new image in real coordinates |
---|
| 1336 | % elseif numel(siz)==3 |
---|
| 1337 | % for icolor=1:siz(3) |
---|
| 1338 | % vec_A=reshape(A{icell}(:,:,icolor),1,npx*npy);%put the original image in line |
---|
| 1339 | % ind_in=find(flagin); |
---|
| 1340 | % ind_out=find(~flagin); |
---|
| 1341 | % ICOMB=((XIMA-1)*npy+(npy+1-YIMA)); |
---|
| 1342 | % ICOMB=ICOMB(flagin);%index corresponding to XIMA and YIMA in the aligned original image vec_A |
---|
| 1343 | % vec_B(ind_in)=vec_A(ICOMB); |
---|
| 1344 | % vec_B(ind_out)=zeros(size(ind_out)); |
---|
| 1345 | % A_out(:,:,icolor)=reshape(vec_B,npy,npx);%new image in real coordinates |
---|
| 1346 | % end |
---|
| 1347 | % end |
---|
| 1348 | % if testuint8 |
---|
| 1349 | % A_out=uint8(A_out); |
---|
| 1350 | % end |
---|
| 1351 | % if testuint16 |
---|
| 1352 | % A_out=uint16(A_out); |
---|
| 1353 | % end |
---|
[60] | 1354 | |
---|
[108] | 1355 | %------------------------------------------------------------------------ |
---|
| 1356 | % pointwise transform from px to phys |
---|
[60] | 1357 | %INPUT: |
---|
| 1358 | %Z: index of plane |
---|
[114] | 1359 | % function [Xphys,Yphys,Zphys]=phys_XYZ(Calib,X,Y,Z) |
---|
| 1360 | % %------------------------------------------------------------------------ |
---|
| 1361 | % if exist('Z','var')& isequal(Z,round(Z))& Z>0 & isfield(Calib,'SliceCoord')&length(Calib.SliceCoord)>=Z |
---|
| 1362 | % Zindex=Z; |
---|
| 1363 | % Zphys=Calib.SliceCoord(Zindex,3);%GENERALISER AUX CAS AVEC ANGLE |
---|
| 1364 | % else |
---|
| 1365 | % Zphys=0; |
---|
| 1366 | % end |
---|
| 1367 | % if ~exist('X','var')||~exist('Y','var') |
---|
| 1368 | % Xphys=[]; |
---|
| 1369 | % Yphys=[];%default |
---|
| 1370 | % return |
---|
| 1371 | % end |
---|
| 1372 | % Xphys=X;%default |
---|
| 1373 | % Yphys=Y; |
---|
| 1374 | % %image transform |
---|
| 1375 | % if isfield(Calib,'R') |
---|
| 1376 | % R=(Calib.R)'; |
---|
| 1377 | % Dx=R(5)*R(7)-R(4)*R(8); |
---|
| 1378 | % Dy=R(1)*R(8)-R(2)*R(7); |
---|
| 1379 | % D0=Calib.f*(R(2)*R(4)-R(1)*R(5)); |
---|
| 1380 | % Z11=R(6)*R(8)-R(5)*R(9); |
---|
| 1381 | % Z12=R(2)*R(9)-R(3)*R(8); |
---|
| 1382 | % Z21=R(4)*R(9)-R(6)*R(7); |
---|
| 1383 | % Z22=R(3)*R(7)-R(1)*R(9); |
---|
| 1384 | % Zx0=R(3)*R(5)-R(2)*R(6); |
---|
| 1385 | % Zy0=R(1)*R(6)-R(3)*R(4); |
---|
| 1386 | % A11=R(8)*Calib.Ty-R(5)*Calib.Tz+Z11*Zphys; |
---|
| 1387 | % A12=R(2)*Calib.Tz-R(8)*Calib.Tx+Z12*Zphys; |
---|
| 1388 | % A21=-R(7)*Calib.Ty+R(4)*Calib.Tz+Z21*Zphys; |
---|
| 1389 | % A22=-R(1)*Calib.Tz+R(7)*Calib.Tx+Z11*Zphys; |
---|
| 1390 | % X0=Calib.f*(R(5)*Calib.Tx-R(2)*Calib.Ty+Zx0*Zphys); |
---|
| 1391 | % Y0=Calib.f*(-R(4)*Calib.Tx+R(1)*Calib.Ty+Zy0*Zphys); |
---|
| 1392 | % %px to camera: |
---|
| 1393 | % Xd=(Calib.dpx/Calib.sx)*(X-Calib.Cx); % sensor coordinates |
---|
| 1394 | % Yd=Calib.dpy*(Y-Calib.Cy); |
---|
| 1395 | % dist_fact=1+Calib.kappa1*(Xd.*Xd+Yd.*Yd); %distortion factor |
---|
| 1396 | % Xu=dist_fact.*Xd;%undistorted sensor coordinates |
---|
| 1397 | % Yu=dist_fact.*Yd; |
---|
| 1398 | % denom=Dx*Xu+Dy*Yu+D0; |
---|
| 1399 | % % denom2=denom.*denom; |
---|
| 1400 | % Xphys=(A11.*Xu+A12.*Yu+X0)./denom;%world coordinates |
---|
| 1401 | % Yphys=(A21.*Xu+A22.*Yu+Y0)./denom; |
---|
| 1402 | % end |
---|
[109] | 1403 | |
---|
| 1404 | |
---|
| 1405 | % -------------------------------------------------------------------- |
---|
| 1406 | function MenuImportPoints_Callback(hObject, eventdata, handles) |
---|
| 1407 | fileinput=browse_xml(hObject, eventdata, handles) |
---|
| 1408 | if isempty(fileinput) |
---|
| 1409 | return |
---|
| 1410 | end |
---|
| 1411 | [s,errormsg]=imadoc2struct(fileinput,'GeometryCalib'); |
---|
| 1412 | GeometryCalib=s.GeometryCalib; |
---|
[114] | 1413 | %GeometryCalib=load_calib(hObject, eventdata, handles) |
---|
[116] | 1414 | calib=reshape(GeometryCalib.PointCoord,[],1); |
---|
[109] | 1415 | for ilist=1:numel(calib) |
---|
| 1416 | CoordCell{ilist}=num2str(calib(ilist)); |
---|
| 1417 | end |
---|
| 1418 | CoordCell=reshape(CoordCell,[],5); |
---|
| 1419 | Tabchar=cell2tab(CoordCell,' | ');%transform cells into table ready for display |
---|
| 1420 | Tabchar=[Tabchar;{'......'}]; |
---|
| 1421 | set(handles.ListCoord,'Value',1) |
---|
| 1422 | set(handles.ListCoord,'String',Tabchar) |
---|
| 1423 | MenuPlot_Callback(handles.geometry_calib, [], handles) |
---|
| 1424 | |
---|
| 1425 | % ----------------------------------------------------------------------- |
---|
| 1426 | function MenuImportIntrinsic_Callback(hObject, eventdata, handles) |
---|
| 1427 | %------------------------------------------------------------------------ |
---|
| 1428 | fileinput=browse_xml(hObject, eventdata, handles); |
---|
| 1429 | if isempty(fileinput) |
---|
| 1430 | return |
---|
| 1431 | end |
---|
| 1432 | [s,errormsg]=imadoc2struct(fileinput,'GeometryCalib'); |
---|
| 1433 | GeometryCalib=s.GeometryCalib; |
---|
[114] | 1434 | display_intrinsic(GeometryCalib,handles) |
---|
[109] | 1435 | |
---|
| 1436 | % ----------------------------------------------------------------------- |
---|
| 1437 | function MenuImportAll_Callback(hObject, eventdata, handles) |
---|
| 1438 | %------------------------------------------------------------------------ |
---|
| 1439 | fileinput=browse_xml(hObject, eventdata, handles) |
---|
| 1440 | if ~isempty(fileinput) |
---|
| 1441 | loadfile(handles,fileinput) |
---|
| 1442 | end |
---|
| 1443 | |
---|
| 1444 | % ----------------------------------------------------------------------- |
---|
| 1445 | % --- Executes on menubar option Import/Grid file: introduce previous grid files |
---|
| 1446 | function MenuGridFile_Callback(hObject, eventdata, handles) |
---|
| 1447 | % ----------------------------------------------------------------------- |
---|
| 1448 | inputfile=browse_xml(hObject, eventdata, handles) |
---|
| 1449 | listfile=get(handles.coord_files,'string'); |
---|
| 1450 | if isequal(listfile,{''}) |
---|
| 1451 | listfile={inputfile}; |
---|
| 1452 | else |
---|
| 1453 | listfile=[listfile;{inputfile}]%update the list of coord files |
---|
| 1454 | end |
---|
| 1455 | set(handles.coord_files,'string',listfile); |
---|
| 1456 | |
---|
| 1457 | %------------------------------------------------------------------------ |
---|
[114] | 1458 | % --- 'key_press_fcn:' function activated when a key is pressed on the keyboard |
---|
| 1459 | function key_press_fcn(hObject,eventdata,handles) |
---|
| 1460 | %------------------------------------------------------------------------ |
---|
| 1461 | xx=double(get(handles.geometry_calib,'CurrentCharacter')); %get the keyboard character |
---|
| 1462 | if ismember(xx,[8 127])%backspace or delete |
---|
| 1463 | Coord_cell=get(handles.ListCoord,'String'); |
---|
| 1464 | val=get(handles.ListCoord,'Value'); |
---|
| 1465 | if max(val)<numel(Coord_cell) % the last element '...' has not been selected |
---|
| 1466 | Coord_cell(val)=[];%remove the selected line |
---|
| 1467 | set(handles.ListCoord,'Value',min(val)) |
---|
| 1468 | set(handles.ListCoord,'String',Coord_cell) |
---|
| 1469 | ListCoord_Callback(hObject, eventdata, handles) |
---|
| 1470 | MenuPlot_Callback(hObject,eventdata,handles) |
---|
| 1471 | end |
---|
| 1472 | end |
---|
| 1473 | |
---|
| 1474 | %------------------------------------------------------------------------ |
---|
[109] | 1475 | function fileinput=browse_xml(hObject, eventdata, handles) |
---|
| 1476 | %------------------------------------------------------------------------ |
---|
| 1477 | fileinput=[];%default |
---|
| 1478 | oldfile=''; %default |
---|
[116] | 1479 | UserData=get(handles.geometry_calib,'UserData'); |
---|
[114] | 1480 | if isfield(UserData,'XmlInputFile') |
---|
| 1481 | oldfile=UserData.XmlInputFile; |
---|
[109] | 1482 | end |
---|
| 1483 | [FileName, PathName, filterindex] = uigetfile( ... |
---|
| 1484 | {'*.xml;*.mat', ' (*.xml,*.mat)'; |
---|
| 1485 | '*.xml', '.xml files '; ... |
---|
| 1486 | '*.mat', '.mat matlab files '}, ... |
---|
| 1487 | 'Pick a file',oldfile); |
---|
| 1488 | fileinput=[PathName FileName];%complete file name |
---|
| 1489 | testblank=findstr(fileinput,' ');%look for blanks |
---|
| 1490 | if ~isempty(testblank) |
---|
| 1491 | msgbox_uvmat('ERROR','forbidden input file name or path: no blank character allowed') |
---|
| 1492 | return |
---|
| 1493 | end |
---|
| 1494 | sizf=size(fileinput); |
---|
| 1495 | if (~ischar(fileinput)||~isequal(sizf(1),1)),return;end |
---|
[114] | 1496 | UserData.XmlInputFile=fileinput; |
---|
[109] | 1497 | set(handles.geometry_calib,'UserData',UserData)%record current file foer further use of browser |
---|
| 1498 | |
---|
| 1499 | % ----------------------------------------------------------------------- |
---|
| 1500 | function loadfile(handles,fileinput) |
---|
| 1501 | %------------------------------------------------------------------------ |
---|
[116] | 1502 | fileinput |
---|
| 1503 | [s,errormsg]=imadoc2struct(fileinput,'GeometryCalib') |
---|
| 1504 | GeometryCalib=s.GeometryCalib; |
---|
[114] | 1505 | fx=1;fy=1;Cx=0;Cy=0;kc=0; %default |
---|
| 1506 | % Tabchar={}; |
---|
| 1507 | CoordCell={}; |
---|
| 1508 | % kc=0;%default |
---|
| 1509 | % f1=1000; |
---|
| 1510 | % f2=1000; |
---|
| 1511 | % hhuvmat=guidata(findobj(allchild(0),'Name','uvmat')); |
---|
| 1512 | % Cx=str2num(get(hhuvmat.npx,'String'))/2; |
---|
| 1513 | % Cy=str2num(get(hhuvmat.npy,'String'))/2; |
---|
| 1514 | Tabchar={};%default |
---|
| 1515 | val_cal=1;%default |
---|
| 1516 | if ~isempty(GeometryCalib) |
---|
| 1517 | % choose the calibration option |
---|
| 1518 | if isfield(GeometryCalib,'CalibrationType') |
---|
| 1519 | calib_list=get(handles.calib_type,'String'); |
---|
| 1520 | for ilist=1:numel(calib_list) |
---|
| 1521 | if strcmp(calib_list{ilist},GeometryCalib.CalibrationType) |
---|
| 1522 | val_cal=ilist; |
---|
| 1523 | break |
---|
| 1524 | end |
---|
| 1525 | end |
---|
| 1526 | end |
---|
| 1527 | display_intrinsic(GeometryCalib,handles)%intrinsic param |
---|
| 1528 | %extrinsic param |
---|
| 1529 | if isfield(GeometryCalib,'Tx_Ty_Tz') |
---|
| 1530 | Tx_Ty_Tz=GeometryCalib.Tx_Ty_Tz; |
---|
| 1531 | set(handles.Tx,'String',num2str(GeometryCalib.Tx_Ty_Tz(1),4)) |
---|
| 1532 | set(handles.Ty,'String',num2str(GeometryCalib.Tx_Ty_Tz(2),4)) |
---|
| 1533 | set(handles.Tz,'String',num2str(GeometryCalib.Tx_Ty_Tz(3),4)) |
---|
| 1534 | end |
---|
| 1535 | if isfield(GeometryCalib,'omc') |
---|
| 1536 | set(handles.Phi,'String',num2str(GeometryCalib.omc(1),4)) |
---|
| 1537 | set(handles.Theta,'String',num2str(GeometryCalib.omc(2),4)) |
---|
| 1538 | set(handles.Psi,'String',num2str(GeometryCalib.omc(3),4)) |
---|
| 1539 | end |
---|
[116] | 1540 | calib=reshape(GeometryCalib.PointCoord,[],1); |
---|
[109] | 1541 | for ilist=1:numel(calib) |
---|
| 1542 | CoordCell{ilist}=num2str(calib(ilist)); |
---|
| 1543 | end |
---|
| 1544 | CoordCell=reshape(CoordCell,[],5); |
---|
| 1545 | Tabchar=cell2tab(CoordCell,' | ');%transform cells into table ready for display |
---|
| 1546 | MenuPlot_Callback(handles.geometry_calib, [], handles) |
---|
| 1547 | end |
---|
[114] | 1548 | set(handles.calib_type,'Value',val_cal) |
---|
[109] | 1549 | Tabchar=[Tabchar;{'......'}]; |
---|
[114] | 1550 | set(handles.ListCoord,'Value',1) |
---|
| 1551 | set(handles.ListCoord,'String',Tabchar) |
---|
| 1552 | |
---|
[109] | 1553 | if isempty(CoordCell)% allow mouse action by default in the absence of input points |
---|
| 1554 | set(handles.edit_append,'Value',1) |
---|
| 1555 | set(handles.edit_append,'BackgroundColor',[1 1 0]) |
---|
| 1556 | else % does not allow mouse action by default in the presence of input points |
---|
| 1557 | set(handles.edit_append,'Value',0) |
---|
| 1558 | set(handles.edit_append,'BackgroundColor',[0.7 0.7 0.7]) |
---|
| 1559 | end |
---|
| 1560 | |
---|
[114] | 1561 | %------------------------------------------------------------------------ |
---|
| 1562 | %---display calibration intrinsic parameters |
---|
| 1563 | function display_intrinsic(GeometryCalib,handles) |
---|
| 1564 | %------------------------------------------------------------------------ |
---|
| 1565 | fx=[]; |
---|
| 1566 | fy=[]; |
---|
| 1567 | if isfield(GeometryCalib,'fx_fy') |
---|
| 1568 | fx=GeometryCalib.fx_fy(1); |
---|
| 1569 | fy=GeometryCalib.fx_fy(2); |
---|
| 1570 | end |
---|
| 1571 | Cx_Cy=[0 0];%default |
---|
| 1572 | if isfield(GeometryCalib,'Cx_Cy') |
---|
| 1573 | Cx_Cy=GeometryCalib.Cx_Cy; |
---|
| 1574 | end |
---|
| 1575 | kc=0; |
---|
| 1576 | if isfield(GeometryCalib,'kc') |
---|
| 1577 | kc=GeometryCalib.kc %* GeometryCalib.focal*GeometryCalib.focal; |
---|
| 1578 | end |
---|
| 1579 | set(handles.fx,'String',num2str(fx,5)) |
---|
| 1580 | set(handles.fy,'String',num2str(fy,5)) |
---|
| 1581 | set(handles.Cx,'String',num2str(Cx_Cy(1),'%1.1f')) |
---|
| 1582 | set(handles.Cy,'String',num2str(Cx_Cy(2),'%1.1f')) |
---|
| 1583 | set(handles.kc,'String',num2str(kc,'%1.4f')) |
---|
[109] | 1584 | |
---|
| 1585 | |
---|