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