[2] | 1 | %'geometry_calib': performs geometric calibration from a set of reference points |
---|
| 2 | % |
---|
| 3 | % function varargout = geometry_calib(varargin) |
---|
| 4 | % |
---|
| 5 | %A%AAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAAA |
---|
| 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 | |
---|
[69] | 45 | % Last Modified by GUIDE v2.5 25-Mar-2010 19:10:05 |
---|
[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 | %------------------------------------------------------------------------ |
---|
[2] | 73 | function geometry_calib_OpeningFcn(hObject, eventdata, handles, handles_uvmat,pos,inputfile) |
---|
[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 |
---|
| 83 | if exist('pos','var')& length(pos)>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 |
---|
| 89 | inputxml=''; |
---|
| 90 | if exist('inputfile','var')& ~isempty(inputfile) |
---|
| 91 | [Path,Name,ext]=fileparts(inputfile); |
---|
| 92 | form=imformats(ext([2:end])); |
---|
| 93 | if ~isempty(form)% if the input file is an image |
---|
| 94 | struct.XmlInputfile=inputfile; |
---|
| 95 | set(hObject,'UserData',struct) |
---|
| 96 | [Pathsub,RootFile,field_count,str2,str_a,str_b,ext,nom_type,subdir]=name2display(inputfile); |
---|
| 97 | inputxml=[fullfile(Pathsub,RootFile) '.xml']; |
---|
| 98 | end |
---|
| 99 | end |
---|
[71] | 100 | set(handles.ListCoord,'String',{'......'}) |
---|
[2] | 101 | if exist(inputxml,'file') |
---|
| 102 | loadfile(handles,inputxml)% load the point coordiantes existing in the xml file |
---|
| 103 | end |
---|
| 104 | |
---|
| 105 | set(handles.ListCoord,'KeyPressFcn',{@key_press_fcn,handles})%set keyboard action function |
---|
| 106 | |
---|
[71] | 107 | |
---|
[17] | 108 | %------------------------------------------------------------------------ |
---|
[2] | 109 | % --- Outputs from this function are returned to the command line. |
---|
| 110 | function varargout = geometry_calib_OutputFcn(hObject, eventdata, handles) |
---|
[17] | 111 | %------------------------------------------------------------------------ |
---|
[2] | 112 | % Get default command line output from handles structure |
---|
| 113 | varargout{1} = handles.output; |
---|
| 114 | varargout{2}=handles; |
---|
| 115 | |
---|
| 116 | %------------ |
---|
| 117 | function Phi_Callback(hObject, eventdata, handles) |
---|
| 118 | |
---|
[17] | 119 | %------------------------------------------------------------------------ |
---|
[2] | 120 | %read input xml file and update the edit boxes |
---|
| 121 | function loadfile(handles,fileinput) |
---|
[17] | 122 | %------------------------------------------------------------------------ |
---|
[2] | 123 | %read the input xml file |
---|
| 124 | t=xmltree(fileinput); |
---|
| 125 | s=convert(t);%convert to matlab structure |
---|
| 126 | %read data currently displayed on the interface |
---|
| 127 | PointCoord=[]; |
---|
| 128 | Coord_cell=get(handles.ListCoord,'String'); |
---|
| 129 | data=read_geometry_calib(Coord_cell); |
---|
| 130 | %data=read_geometry_calib(handles); |
---|
| 131 | Coord=[]; %default |
---|
| 132 | if isfield(data,'Coord') |
---|
| 133 | Coord=data.Coord; |
---|
| 134 | end |
---|
| 135 | TabChar_0=get(handles.ListCoord,'String'); |
---|
| 136 | nbcoord_0=size(TabChar_0,1); |
---|
| 137 | if isequal(get(handles.edit_append,'Value'),2) %edit mode A REVOIR |
---|
| 138 | val=get(handles.ListCoord,'Value')-1; |
---|
| 139 | else |
---|
| 140 | val=length(TabChar_0); |
---|
| 141 | end |
---|
| 142 | nbcoord=0; |
---|
| 143 | |
---|
| 144 | %case of calibration (ImaDoc) input file |
---|
[36] | 145 | % hcalib=get(handles.calib_type,'parent'); |
---|
[61] | 146 | CalibData=get(handles.geometry_calib,'UserData'); |
---|
[2] | 147 | CalibData.XmlInput=fileinput; |
---|
| 148 | if isfield(s,'Heading') |
---|
| 149 | CalibData.Heading=s.Heading; |
---|
| 150 | end |
---|
| 151 | |
---|
[61] | 152 | set(handles.geometry_calib,'UserData',CalibData);%store the heading in the interface 'UserData' |
---|
[2] | 153 | if isfield(s,'GeometryCalib') |
---|
| 154 | Calib=s.GeometryCalib; |
---|
| 155 | if isfield(Calib,'CalibrationType') |
---|
| 156 | CalibrationType=Calib.CalibrationType; |
---|
| 157 | switch CalibrationType |
---|
| 158 | case 'linear' |
---|
| 159 | set(handles.calib_type,'Value',2) |
---|
| 160 | case 'tsai' |
---|
| 161 | set(handles.calib_type,'Value',3) |
---|
| 162 | end |
---|
| 163 | end |
---|
| 164 | if isfield(Calib,'SourceCalib') |
---|
| 165 | if isfield(Calib.SourceCalib,'PointCoord') |
---|
| 166 | PointCoord=Calib.SourceCalib.PointCoord; |
---|
| 167 | end |
---|
| 168 | end |
---|
| 169 | nbcoord=length(PointCoord); |
---|
| 170 | if ~isfield(Calib,'ErrorRms')&~isfield(Calib,'ErrorMax') %old convention of Gauthier (cord in mm) |
---|
| 171 | for i=1:length(PointCoord) |
---|
| 172 | line=str2num(PointCoord{i}); |
---|
| 173 | Coord(i+val,4:5)=line(4:5);%px x |
---|
| 174 | Coord(i+val,1:3)=line(1:3)/10;%phys x |
---|
| 175 | end |
---|
| 176 | else |
---|
| 177 | for i=1:length(PointCoord) |
---|
| 178 | line=str2num(PointCoord{i}); |
---|
| 179 | Coord(i,4:5)=line(4:5);%px x |
---|
| 180 | Coord(i,1:3)=line(1:3);%phys x |
---|
| 181 | end |
---|
| 182 | end |
---|
| 183 | end |
---|
| 184 | %case of xml files of points |
---|
| 185 | if isfield(s,'Coord') |
---|
| 186 | PointCoord=s.Coord; |
---|
| 187 | nbcoord=length(PointCoord); |
---|
| 188 | %case of image coordinates |
---|
| 189 | if isfield(s,'CoordType')& isequal(s.CoordType,'px') |
---|
| 190 | for i=1:nbcoord |
---|
| 191 | line=str2num(PointCoord{i}); |
---|
| 192 | Coord(i+val,4:5)=line(1:2); |
---|
| 193 | end |
---|
| 194 | %case of physical coordinates |
---|
| 195 | else |
---|
| 196 | for i=1:nbcoord |
---|
| 197 | line=str2num(PointCoord{i}); |
---|
| 198 | Coord(i+val,1:3)=line(1:3); |
---|
| 199 | nbcolumn=size(Coord,2); |
---|
| 200 | if nbcolumn<5 |
---|
| 201 | Coord(i+val,nbcolumn+1:5)=zeros(1,5-nbcolumn); |
---|
| 202 | end |
---|
| 203 | end |
---|
| 204 | end |
---|
| 205 | end |
---|
| 206 | CoordCell={}; |
---|
| 207 | for iline=1:size(Coord,1) |
---|
| 208 | for j=1:5 |
---|
| 209 | CoordCell{iline,j}=num2str(Coord(iline,j),4); |
---|
| 210 | end |
---|
[67] | 211 | end |
---|
[71] | 212 | % CoordCell=[CoordCell;{' ',' ',' ',' ',' '}]; |
---|
[2] | 213 | Tabchar=cell2tab(CoordCell,' | ');%transform cells into table ready for display |
---|
[71] | 214 | Tabchar=[Tabchar;{'......'}]; |
---|
[2] | 215 | set(handles.ListCoord,'Value',1) |
---|
| 216 | set(handles.ListCoord,'String',Tabchar) |
---|
[71] | 217 | MenuPlot_Callback(handles.geometry_calib, [], handles) |
---|
[2] | 218 | |
---|
[67] | 219 | % |
---|
[60] | 220 | %------------------------------------------------------------------------ |
---|
[2] | 221 | % executed when closing: set the parent interface button to value 0 |
---|
[67] | 222 | function closefcn(gcbo,eventdata) |
---|
[60] | 223 | %------------------------------------------------------------------------ |
---|
[2] | 224 | huvmat=findobj(allchild(0),'Name','uvmat'); |
---|
[67] | 225 | if ~isempty(huvmat) |
---|
| 226 | handles=guidata(huvmat); |
---|
[71] | 227 | set(handles.MenuMask,'enable','on') |
---|
| 228 | set(handles.MenuGrid,'enable','on') |
---|
[67] | 229 | set(handles.MenuObject,'enable','on') |
---|
| 230 | set(handles.MenuEdit,'enable','on') |
---|
| 231 | set(handles.edit,'enable','on') |
---|
| 232 | hobject=findobj(handles.axes3,'tag','calib_points'); |
---|
| 233 | if ~isempty(hobject) |
---|
| 234 | delete(hobject) |
---|
| 235 | end |
---|
| 236 | hobject=findobj(handles.axes3,'tag','calib_marker'); |
---|
| 237 | if ~isempty(hobject) |
---|
| 238 | delete(hobject) |
---|
| 239 | end |
---|
[2] | 240 | end |
---|
| 241 | |
---|
[60] | 242 | %------------------------------------------------------------------------ |
---|
[2] | 243 | % --- Executes on button press in calibrate_lin. |
---|
| 244 | function APPLY_Callback(hObject, eventdata, handles) |
---|
[60] | 245 | %------------------------------------------------------------------------ |
---|
[2] | 246 | calib_cell=get(handles.calib_type,'String'); |
---|
| 247 | val=get(handles.calib_type,'Value'); |
---|
| 248 | calib_type=calib_cell{val}; |
---|
| 249 | Coord_cell=get(handles.ListCoord,'String'); |
---|
| 250 | Object=read_geometry_calib(Coord_cell); |
---|
| 251 | |
---|
| 252 | if isequal(calib_type,'rescale') |
---|
| 253 | GeometryCalib=calib_rescale(Object.Coord); |
---|
| 254 | elseif isequal(calib_type,'linear') |
---|
| 255 | GeometryCalib=calib_linear(Object.Coord); |
---|
[69] | 256 | elseif isequal(calib_type,'tsai_cpp') |
---|
[2] | 257 | GeometryCalib=calib_tsai(Object.Coord); |
---|
[69] | 258 | elseif isequal(calib_type,'tsai_matlab') |
---|
| 259 | GeometryCalib=calib_tsai2(Object.Coord); |
---|
[2] | 260 | end |
---|
| 261 | unitlist=get(handles.CoordUnit,'String'); |
---|
| 262 | unit=unitlist{get(handles.CoordUnit,'value')}; |
---|
| 263 | GeometryCalib.CoordUnit=unit; |
---|
[17] | 264 | GeometryCalib.SourceCalib.PointCoord=Object.Coord; |
---|
[2] | 265 | huvmat=findobj(allchild(0),'Name','uvmat'); |
---|
| 266 | hhuvmat=guidata(huvmat);%handles of elements in the GUI uvmat |
---|
| 267 | RootPath=''; |
---|
| 268 | RootFile=''; |
---|
| 269 | if ~isempty(hhuvmat.RootPath)& ~isempty(hhuvmat.RootFile) |
---|
| 270 | testhandle=1; |
---|
| 271 | RootPath=get(hhuvmat.RootPath,'String'); |
---|
| 272 | RootFile=get(hhuvmat.RootFile,'String'); |
---|
| 273 | filebase=fullfile(RootPath,RootFile); |
---|
| 274 | outputfile=[filebase '.xml']; |
---|
| 275 | else |
---|
| 276 | question={'save the calibration data and point coordinates in'}; |
---|
| 277 | def={fullfile(RootPath,['ObjectCalib.xml'])}; |
---|
| 278 | options.Resize='on'; |
---|
| 279 | answer=inputdlg(question,'save average in a new file',1,def,options); |
---|
| 280 | outputfile=answer{1}; |
---|
| 281 | end |
---|
[17] | 282 | update_imadoc(GeometryCalib,outputfile) |
---|
[2] | 283 | msgbox_uvmat('CONFIRMATION',{[outputfile ' updated with calibration data'];... |
---|
| 284 | ['Error rms (along x,y)=' num2str(GeometryCalib.ErrorRms) ' pixels'];... |
---|
| 285 | ['Error max (along x,y)=' num2str(GeometryCalib.ErrorMax) ' pixels']}) |
---|
| 286 | |
---|
| 287 | %display image with new calibration in the currently opened uvmat interface |
---|
| 288 | hhh=findobj(hhuvmat.axes3,'Tag','calib_marker');% delete calib points and markers |
---|
| 289 | if ~isempty(hhh) |
---|
| 290 | delete(hhh); |
---|
| 291 | end |
---|
| 292 | hhh=findobj(hhuvmat.axes3,'Tag','calib_points'); |
---|
| 293 | if ~isempty(hhh) |
---|
| 294 | delete(hhh); |
---|
| 295 | end |
---|
| 296 | set(hhuvmat.FixedLimits,'Value',0)% put FixedLimits option to 'off' |
---|
| 297 | set(hhuvmat.FixedLimits,'BackgroundColor',[0.7 0.7 0.7]) |
---|
| 298 | uvmat('RootPath_Callback',hObject,eventdata,hhuvmat); %file input with xml reading in uvmat |
---|
[71] | 299 | MenuPlot_Callback(hObject, eventdata, handles) |
---|
[69] | 300 | figure(handles.geometry_calib) |
---|
| 301 | |
---|
| 302 | %------------------------------------------------------------------ |
---|
[2] | 303 | % --- Executes on button press in calibrate_lin. |
---|
| 304 | function REPLICATE_Callback(hObject, eventdata, handles) |
---|
[60] | 305 | %------------------------------------------------------------------------ |
---|
[2] | 306 | calib_cell=get(handles.calib_type,'String'); |
---|
| 307 | val=get(handles.calib_type,'Value'); |
---|
| 308 | calib_type=calib_cell{val}; |
---|
| 309 | Coord_cell=get(handles.ListCoord,'String'); |
---|
| 310 | Object=read_geometry_calib(Coord_cell); |
---|
| 311 | |
---|
| 312 | if isequal(calib_type,'rescale') |
---|
| 313 | GeometryCalib=calib_rescale(Object.Coord); |
---|
| 314 | elseif isequal(calib_type,'linear') |
---|
| 315 | GeometryCalib=calib_linear(Object.Coord); |
---|
| 316 | elseif isequal(calib_type,'tsai') |
---|
| 317 | GeometryCalib=calib_tsai(Object.Coord); |
---|
| 318 | end |
---|
| 319 | % %record image source |
---|
| 320 | GeometryCalib.SourceCalib.PointCoord=Object.Coord; |
---|
| 321 | |
---|
| 322 | %open and read the dataview GUI |
---|
| 323 | h_dataview=findobj(allchild(0),'name','dataview'); |
---|
[12] | 324 | if ~isempty(h_dataview) |
---|
| 325 | delete(h_dataview) |
---|
[2] | 326 | end |
---|
[61] | 327 | CalibData=get(handles.geometry_calib,'UserData');%read the calibration image source on the interface userdata |
---|
[2] | 328 | |
---|
[12] | 329 | if isfield(CalibData,'XmlInput') |
---|
| 330 | XmlInput=fileparts(CalibData.XmlInput); |
---|
| 331 | [XmlInput,filename,ext]=fileparts(XmlInput); |
---|
[2] | 332 | end |
---|
[12] | 333 | SubCampaignTest='n'; %default |
---|
| 334 | testinput=0; |
---|
| 335 | if isfield(CalibData,'Heading') |
---|
| 336 | Heading=CalibData.Heading; |
---|
| 337 | if isfield(Heading,'Record') && isequal([filename ext],Heading.Record) |
---|
| 338 | [XmlInput,filename,ext]=fileparts(XmlInput); |
---|
[2] | 339 | end |
---|
[12] | 340 | if isfield(Heading,'Device') && isequal([filename ext],Heading.Device) |
---|
| 341 | [XmlInput,filename,ext]=fileparts(XmlInput); |
---|
| 342 | Device=Heading.Device; |
---|
| 343 | end |
---|
| 344 | if isfield(Heading,'Experiment') && isequal([filename ext],Heading.Experiment) |
---|
| 345 | [PP,filename,ext]=fileparts(XmlInput); |
---|
| 346 | end |
---|
| 347 | testinput=0; |
---|
| 348 | if isfield(Heading,'SubCampaign') && isequal([filename ext],Heading.SubCampaign) |
---|
| 349 | SubCampaignTest='y'; |
---|
| 350 | testinput=1; |
---|
| 351 | elseif isfield(Heading,'Campaign') && isequal([filename ext],Heading.Campaign) |
---|
| 352 | testinput=1; |
---|
| 353 | end |
---|
[2] | 354 | end |
---|
[12] | 355 | if ~testinput |
---|
| 356 | filename='PROJETS';%default |
---|
| 357 | if isfield(CalibData,'XmlInput') |
---|
| 358 | [pp,filename]=fileparts(CalibData.XmlInput); |
---|
[2] | 359 | end |
---|
[12] | 360 | while ~isequal(filename,'PROJETS') && numel(filename)>1 |
---|
| 361 | filename_1=filename; |
---|
| 362 | pp_1=pp; |
---|
| 363 | [pp,filename]=fileparts(pp); |
---|
| 364 | end |
---|
| 365 | XmlInput=fullfile(pp_1,filename_1); |
---|
| 366 | testinput=1; |
---|
[2] | 367 | end |
---|
[12] | 368 | if testinput |
---|
[36] | 369 | outcome=dataview(XmlInput,SubCampaignTest,GeometryCalib); |
---|
[2] | 370 | end |
---|
| 371 | |
---|
[60] | 372 | %------------------------------------------------------------------------ |
---|
[2] | 373 | % determine the parameters for a calibration by an affine function (rescaling and offset, no rotation) |
---|
| 374 | function GeometryCalib=calib_rescale(Coord) |
---|
[60] | 375 | %------------------------------------------------------------------------ |
---|
[2] | 376 | |
---|
| 377 | X=Coord(:,1); |
---|
| 378 | Y=Coord(:,2); |
---|
| 379 | x_ima=Coord(:,4); |
---|
| 380 | y_ima=Coord(:,5); |
---|
| 381 | [px,sx]=polyfit(X,x_ima,1); |
---|
| 382 | [py,sy]=polyfit(Y,y_ima,1); |
---|
| 383 | T_x=px(2); |
---|
| 384 | T_y=py(2); |
---|
| 385 | GeometryCalib.CalibrationType='rescale'; |
---|
| 386 | GeometryCalib.focal=1; |
---|
| 387 | GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function |
---|
| 388 | GeometryCalib.Tx_Ty_Tz=[T_x T_y 1]; |
---|
| 389 | GeometryCalib.R=[px(1),0,0;0,py(1),0;0,0,1]; |
---|
| 390 | |
---|
| 391 | %check error |
---|
| 392 | Calib.dpx=1; |
---|
| 393 | Calib.dpy=1; |
---|
| 394 | Calib.sx=1; |
---|
| 395 | Calib.Cx=0; |
---|
| 396 | Calib.Cy=0; |
---|
| 397 | Calib.Tz=1; |
---|
| 398 | Calib.kappa1=0; |
---|
| 399 | Calib.f=GeometryCalib.focal; |
---|
| 400 | Calib.Tx=T_x; |
---|
| 401 | Calib.Ty=T_y; |
---|
| 402 | Calib.R=GeometryCalib.R; |
---|
| 403 | [Xpoints,Ypoints]=px_XYZ(Calib,X,Y,0); |
---|
| 404 | GeometryCalib.ErrorRms(1)=sqrt(mean((Xpoints-x_ima).*(Xpoints-x_ima))); |
---|
| 405 | GeometryCalib.ErrorMax(1)=max(abs(Xpoints-x_ima)); |
---|
| 406 | GeometryCalib.ErrorRms(2)=sqrt(mean((Ypoints-y_ima).*(Ypoints-y_ima))); |
---|
| 407 | GeometryCalib.ErrorMax(2)=max(abs(Ypoints-y_ima)); |
---|
| 408 | |
---|
[60] | 409 | %------------------------------------------------------------------------ |
---|
[2] | 410 | % determine the parameters for a calibration by a linear transform matrix (rescale and rotation) |
---|
| 411 | function GeometryCalib=calib_linear(Coord) |
---|
[60] | 412 | %------------------------------------------------------------------------ |
---|
[2] | 413 | X=Coord(:,1); |
---|
| 414 | Y=Coord(:,2); |
---|
| 415 | x_ima=Coord(:,4); |
---|
| 416 | y_ima=Coord(:,5); |
---|
| 417 | XY_mat=[ones(size(X)) X Y]; |
---|
| 418 | a_X1=XY_mat\x_ima; %transformation matrix for X |
---|
| 419 | x1=XY_mat*a_X1;%reconstruction |
---|
| 420 | err_X1=max(abs(x1-x_ima));%error |
---|
| 421 | a_Y1=XY_mat\y_ima;%transformation matrix for X |
---|
| 422 | y1=XY_mat*a_Y1; |
---|
| 423 | err_Y1=max(abs(y1-y_ima));%error |
---|
| 424 | T_x=a_X1(1); |
---|
| 425 | T_y=a_Y1(1); |
---|
| 426 | GeometryCalib.CalibrationType='linear'; |
---|
| 427 | GeometryCalib.focal=1; |
---|
| 428 | GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function |
---|
| 429 | GeometryCalib.Tx_Ty_Tz=[T_x T_y 1]; |
---|
| 430 | GeometryCalib.R=[a_X1(2),a_X1(3),0;a_Y1(2),a_Y1(3),0;0,0,1]; |
---|
| 431 | |
---|
| 432 | %check error |
---|
| 433 | GeometryCalib.ErrorRms(1)=sqrt(mean((x1-x_ima).*(x1-x_ima))); |
---|
| 434 | GeometryCalib.ErrorMax(1)=max(abs(x1-x_ima)); |
---|
| 435 | GeometryCalib.ErrorRms(2)=sqrt(mean((y1-y_ima).*(y1-y_ima))); |
---|
| 436 | GeometryCalib.ErrorMax(2)=max(abs(y1-y_ima)); |
---|
| 437 | |
---|
[60] | 438 | %------------------------------------------------------------------------ |
---|
[69] | 439 | function GeometryCalib=calib_tsai2(Coord) |
---|
| 440 | %------------------------------------------------------------------ |
---|
| 441 | path_uvmat=which('uvmat');% check the path detected for source file uvmat |
---|
| 442 | path_UVMAT=fileparts(path_uvmat); %path to UVMAT |
---|
| 443 | |
---|
| 444 | x_1=Coord(:,4:5)'; |
---|
| 445 | X_1=Coord(:,1:3)'; |
---|
| 446 | n_ima=1; |
---|
| 447 | % check_cond=0; |
---|
| 448 | nx=1024;ny=1024; |
---|
| 449 | % est_kc=[1;0;0;0;0]; |
---|
| 450 | est_dist=[1;0;0;0;0]; |
---|
[71] | 451 | run(fullfile(path_UVMAT,'TOOLBOX_calib','go_calib_optim')); |
---|
[69] | 452 | |
---|
| 453 | GeometryCalib.CalibrationType='tsai'; |
---|
| 454 | GeometryCalib.focal=f(2); |
---|
| 455 | GeometryCalib.dpx_dpy=[1 1]; |
---|
| 456 | GeometryCalib.Cx_Cy=cc'; |
---|
| 457 | GeometryCalib.sx=fc(1)/fc(2); |
---|
| 458 | GeometryCalib.kappa1=-k(1)/f(2)^2; |
---|
| 459 | GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function |
---|
| 460 | GeometryCalib.Tx_Ty_Tz=Tc_1'; |
---|
| 461 | GeometryCalib.R=Rc_1; |
---|
| 462 | Calib.dpx=GeometryCalib.dpx_dpy(1); |
---|
| 463 | Calib.dpy=GeometryCalib.dpx_dpy(2); |
---|
| 464 | Calib.sx=GeometryCalib.sx; |
---|
| 465 | Calib.Cx=GeometryCalib.Cx_Cy(1); |
---|
| 466 | Calib.Cy=GeometryCalib.Cx_Cy(2); |
---|
| 467 | Calib.kappa1=GeometryCalib.kappa1; |
---|
| 468 | Calib.f=GeometryCalib.focal; |
---|
| 469 | Calib.Tx=GeometryCalib.Tx_Ty_Tz(1); |
---|
| 470 | Calib.Ty=GeometryCalib.Tx_Ty_Tz(2); |
---|
| 471 | Calib.Tz=GeometryCalib.Tx_Ty_Tz(3); |
---|
| 472 | Calib.R=GeometryCalib.R; |
---|
| 473 | X=Coord(:,1); |
---|
| 474 | Y=Coord(:,2); |
---|
| 475 | Z=Coord(:,3); |
---|
| 476 | x_ima=Coord(:,4); |
---|
| 477 | y_ima=Coord(:,5); |
---|
| 478 | [Xpoints,Ypoints]=px_XYZ(Calib,X,Y,Z); |
---|
| 479 | |
---|
| 480 | GeometryCalib.ErrorRms(1)=sqrt(mean((Xpoints-x_ima).*(Xpoints-x_ima))); |
---|
| 481 | GeometryCalib.ErrorMax(1)=max(abs(Xpoints-x_ima)); |
---|
| 482 | GeometryCalib.ErrorRms(2)=sqrt(mean((Ypoints-y_ima).*(Ypoints-y_ima))); |
---|
| 483 | GeometryCalib.ErrorMax(2)=max(abs(Ypoints-y_ima)); |
---|
| 484 | |
---|
[2] | 485 | function GeometryCalib=calib_tsai(Coord) |
---|
[60] | 486 | %------------------------------------------------------------------------ |
---|
[2] | 487 | %TSAI |
---|
| 488 | % 'calibration_lin' provides a linear transform on coordinates, |
---|
| 489 | path_uvmat=which('uvmat');% check the path detected for source file uvmat |
---|
| 490 | path_UVMAT=fileparts(path_uvmat); %path to UVMAT |
---|
[54] | 491 | % if isunix |
---|
[2] | 492 | %fid = fopen(fullfile(path_UVMAT,'PARAM_LINUX.txt'),'r');%open the file with civ binary names |
---|
[54] | 493 | xmlfile=fullfile(path_UVMAT,'PARAM.xml'); |
---|
| 494 | if exist(xmlfile,'file') |
---|
| 495 | t=xmltree(xmlfile); |
---|
| 496 | sparam=convert(t); |
---|
| 497 | end |
---|
[71] | 498 | if ~isfield(sparam,'GeometryCalibBin') |
---|
| 499 | msgbox_uvmat('ERROR',['calibration program <GeometryCalibBin> undefined in parameter file ' xmlfile]) |
---|
[2] | 500 | return |
---|
| 501 | end |
---|
[71] | 502 | Tsai_exe=sparam.GeometryCalibBin; |
---|
[54] | 503 | if ~exist(Tsai_exe,'file')%the binary is defined in /bin, default setting |
---|
| 504 | Tsai_exe=fullfile(path_UVMAT,Tsai_exe); |
---|
| 505 | end |
---|
[2] | 506 | if ~exist(Tsai_exe,'file') |
---|
[71] | 507 | msgbox_uvmat('ERROR',['calibration program ' sparam.GeometryCalibBin ' defined in PARAM.xml does not exist']) |
---|
[2] | 508 | return |
---|
| 509 | end |
---|
| 510 | |
---|
| 511 | textcoord=num2str(Coord,4); |
---|
| 512 | dlmwrite('t.txt',textcoord,''); |
---|
| 513 | % ['!' Tsai_exe ' -f1 0 -f2 t.txt'] |
---|
| 514 | eval(['!' Tsai_exe ' -f t.txt > tsaicalib.log']); |
---|
| 515 | if ~exist('calib.dat','file') |
---|
[42] | 516 | msgbox_uvmat('ERROR','no output from calibration program Tsai_exe: possibly too few points') |
---|
[2] | 517 | end |
---|
| 518 | calibdat=dlmread('calib.dat'); |
---|
[60] | 519 | delete('calib.dat') |
---|
| 520 | delete('t.txt') |
---|
[2] | 521 | GeometryCalib.CalibrationType='tsai'; |
---|
| 522 | GeometryCalib.focal=calibdat(10); |
---|
| 523 | GeometryCalib.dpx_dpy=[calibdat(5) calibdat(6)]; |
---|
| 524 | GeometryCalib.Cx_Cy=[calibdat(7) calibdat(8)]; |
---|
| 525 | GeometryCalib.sx=calibdat(9); |
---|
| 526 | GeometryCalib.kappa1=calibdat(11); |
---|
| 527 | GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function |
---|
| 528 | GeometryCalib.Tx_Ty_Tz=[calibdat(12) calibdat(13) calibdat(14)]; |
---|
| 529 | Rx_Ry_Rz=calibdat([15:17]); |
---|
| 530 | sa = sin(Rx_Ry_Rz(1)) ; |
---|
| 531 | ca=cos(Rx_Ry_Rz(1)); |
---|
| 532 | sb=sin(Rx_Ry_Rz(2)); |
---|
| 533 | cb =cos(Rx_Ry_Rz(2)); |
---|
| 534 | sg =sin(Rx_Ry_Rz(3)); |
---|
| 535 | cg =cos(Rx_Ry_Rz(3)); |
---|
| 536 | r1 = cb * cg; |
---|
| 537 | r2 = cg * sa * sb - ca * sg; |
---|
| 538 | r3 = sa * sg + ca * cg * sb; |
---|
| 539 | r4 = cb * sg; |
---|
| 540 | r5 = sa * sb * sg + ca * cg; |
---|
| 541 | r6 = ca * sb * sg - cg * sa; |
---|
| 542 | r7 = -sb; |
---|
| 543 | r8 = cb * sa; |
---|
| 544 | r9 = ca * cb; |
---|
| 545 | %EN DEDUIRE MATRICE R ?? |
---|
| 546 | GeometryCalib.R=[r1,r2,r3;r4,r5,r6;r7,r8,r9]; |
---|
| 547 | %erreur a caracteriser? |
---|
| 548 | %check error |
---|
| 549 | Calib.dpx=GeometryCalib.dpx_dpy(1); |
---|
| 550 | Calib.dpy=GeometryCalib.dpx_dpy(2); |
---|
| 551 | Calib.sx=GeometryCalib.sx; |
---|
| 552 | Calib.Cx=GeometryCalib.Cx_Cy(1); |
---|
| 553 | Calib.Cy=GeometryCalib.Cx_Cy(2); |
---|
| 554 | Calib.kappa1=GeometryCalib.kappa1; |
---|
| 555 | Calib.f=GeometryCalib.focal; |
---|
| 556 | Calib.Tx=GeometryCalib.Tx_Ty_Tz(1); |
---|
| 557 | Calib.Ty=GeometryCalib.Tx_Ty_Tz(2); |
---|
| 558 | Calib.Tz=GeometryCalib.Tx_Ty_Tz(3); |
---|
| 559 | Calib.R=GeometryCalib.R; |
---|
| 560 | X=Coord(:,1); |
---|
| 561 | Y=Coord(:,2); |
---|
| 562 | Z=Coord(:,3); |
---|
| 563 | x_ima=Coord(:,4); |
---|
| 564 | y_ima=Coord(:,5); |
---|
| 565 | [Xpoints,Ypoints]=px_XYZ(Calib,X,Y,Z); |
---|
| 566 | |
---|
| 567 | GeometryCalib.ErrorRms(1)=sqrt(mean((Xpoints-x_ima).*(Xpoints-x_ima))); |
---|
| 568 | GeometryCalib.ErrorMax(1)=max(abs(Xpoints-x_ima)); |
---|
| 569 | GeometryCalib.ErrorRms(2)=sqrt(mean((Ypoints-y_ima).*(Ypoints-y_ima))); |
---|
| 570 | GeometryCalib.ErrorMax(2)=max(abs(Ypoints-y_ima)); |
---|
| 571 | % Nfx |
---|
| 572 | % dx |
---|
| 573 | % dy |
---|
| 574 | % 5 dpx |
---|
| 575 | % 6 dpy |
---|
| 576 | % cx |
---|
| 577 | % cy |
---|
| 578 | % sx |
---|
| 579 | % f |
---|
| 580 | % kappa1 |
---|
| 581 | % tx |
---|
| 582 | % ty |
---|
| 583 | % tz |
---|
| 584 | % rx |
---|
| 585 | % ry |
---|
| 586 | % rz |
---|
| 587 | % p1 |
---|
| 588 | % p2 |
---|
| 589 | |
---|
| 590 | %calibcoeff=str2num(calibdat) |
---|
| 591 | |
---|
| 592 | |
---|
[60] | 593 | %------------------------------------------------------------------------ |
---|
[2] | 594 | % --- Executes on button press in rotation. |
---|
| 595 | function rotation_Callback(hObject, eventdata, handles) |
---|
[60] | 596 | %------------------------------------------------------------------------ |
---|
[2] | 597 | angle_rot=(pi/180)*str2num(get(handles.Phi,'String')); |
---|
| 598 | Coord_cell=get(handles.ListCoord,'String'); |
---|
| 599 | data=read_geometry_calib(Coord_cell); |
---|
| 600 | data.Coord(:,1)=cos(angle_rot)*data.Coord(:,1)+sin(angle_rot)*data.Coord(:,2); |
---|
| 601 | data.Coord(:,1)=-sin(angle_rot)*data.Coord(:,1)+cos(angle_rot)*data.Coord(:,2); |
---|
| 602 | set(handles.XObject,'String',num2str(data.Coord(:,1),4)); |
---|
| 603 | set(handles.YObject,'String',num2str(data.Coord(:,2),4)); |
---|
| 604 | |
---|
[60] | 605 | %------------------------------------------------------------------------ |
---|
[2] | 606 | function XImage_Callback(hObject, eventdata, handles) |
---|
[60] | 607 | %------------------------------------------------------------------------ |
---|
[2] | 608 | update_list(hObject, eventdata,handles) |
---|
| 609 | |
---|
[60] | 610 | %------------------------------------------------------------------------ |
---|
[2] | 611 | function YImage_Callback(hObject, eventdata, handles) |
---|
[60] | 612 | %------------------------------------------------------------------------ |
---|
[2] | 613 | update_list(hObject, eventdata,handles) |
---|
| 614 | |
---|
| 615 | function XObject_Callback(hObject, eventdata, handles) |
---|
| 616 | update_list(hObject, eventdata,handles) |
---|
| 617 | |
---|
| 618 | function YObject_Callback(hObject, eventdata, handles) |
---|
| 619 | update_list(hObject, eventdata,handles) |
---|
| 620 | |
---|
| 621 | function ZObject_Callback(hObject, eventdata, handles) |
---|
| 622 | update_list(hObject, eventdata,handles) |
---|
| 623 | |
---|
[60] | 624 | %------------------------------------------------------------------------ |
---|
[2] | 625 | function update_list(hObject, eventdata, handles) |
---|
[60] | 626 | %------------------------------------------------------------------------ |
---|
[2] | 627 | str4=get(handles.XImage,'String'); |
---|
| 628 | str5=get(handles.YImage,'String'); |
---|
| 629 | str1=get(handles.XObject,'String'); |
---|
| 630 | tt=double(str1); |
---|
| 631 | str2=get(handles.YObject,'String'); |
---|
| 632 | str3=get(handles.ZObject,'String'); |
---|
| 633 | if ~isempty(str1) & ~isequal(double(str1),32) & (isempty(str3)|isequal(double(str3),32)) |
---|
| 634 | str3='0';%put z to 0 by default |
---|
| 635 | end |
---|
| 636 | strline=[str1 ' | ' str2 ' | ' str3 ' | ' str4 ' | ' str5]; |
---|
| 637 | Coord=get(handles.ListCoord,'String'); |
---|
| 638 | val=get(handles.ListCoord,'Value'); |
---|
| 639 | Coord{val}=strline; |
---|
| 640 | set(handles.ListCoord,'String',Coord) |
---|
[60] | 641 | %update the plot |
---|
| 642 | ListCoord_Callback(hObject, eventdata, handles) |
---|
[67] | 643 | MenuPlot_Callback(hObject, eventdata, handles) |
---|
[71] | 644 | |
---|
[60] | 645 | %------------------------------------------------------------------------ |
---|
[2] | 646 | % --- Executes on selection change in ListCoord. |
---|
| 647 | function ListCoord_Callback(hObject, eventdata, handles) |
---|
[60] | 648 | %------------------------------------------------------------------------ |
---|
[71] | 649 | huvmat=findobj(allchild(0),'Name','uvmat');%find the current uvmat interface handle |
---|
| 650 | hplot=findobj(huvmat,'Tag','axes3');%main plotting axis of uvmat |
---|
| 651 | hhh=findobj(hplot,'Tag','calib_marker'); |
---|
[2] | 652 | Coord_cell=get(handles.ListCoord,'String'); |
---|
| 653 | val=get(handles.ListCoord,'Value'); |
---|
[71] | 654 | coord_str=Coord_cell{val}; |
---|
| 655 | k=findstr('|',coord_str); |
---|
| 656 | if isempty(k)%last line '.....' selected |
---|
| 657 | if ~isempty(hhh) |
---|
| 658 | delete(hhh)%delete the circle marker |
---|
[2] | 659 | end |
---|
[71] | 660 | return |
---|
| 661 | end |
---|
| 662 | %fill the edit boxex |
---|
| 663 | set(handles.XObject,'String',coord_str(1:k(1)-5)) |
---|
| 664 | set(handles.YObject,'String',coord_str(k(1)+5:k(2)-5)) |
---|
| 665 | set(handles.ZObject,'String',coord_str(k(2)+5:k(3)-5)) |
---|
| 666 | set(handles.XImage,'String',coord_str(k(3)+5:k(4)-5)) |
---|
| 667 | set(handles.YImage,'String',coord_str(k(4)+5:end)) |
---|
| 668 | h_menu_coord=findobj(huvmat,'Tag','transform_fct'); |
---|
| 669 | menu=get(h_menu_coord,'String'); |
---|
| 670 | choice=get(h_menu_coord,'Value'); |
---|
| 671 | if iscell(menu) |
---|
| 672 | option=menu{choice}; |
---|
| 673 | else |
---|
| 674 | option='px'; %default |
---|
| 675 | end |
---|
| 676 | if isequal(option,'phys') |
---|
| 677 | XCoord=str2num(coord_str(1:k(1)-5)); |
---|
| 678 | YCoord=str2num(coord_str(k(1)+5:k(2)-5)); |
---|
| 679 | elseif isequal(option,'px')|| isequal(option,'') |
---|
| 680 | XCoord=str2num(coord_str(k(3)+5:k(4)-5)); |
---|
| 681 | YCoord=str2num(coord_str(k(4)+5:end)); |
---|
| 682 | else |
---|
| 683 | msgbox_uvmat('ERROR','the choice in menu_coord of uvmat must be px or phys ') |
---|
| 684 | end |
---|
| 685 | if isempty(XCoord)||isempty(YCoord) |
---|
| 686 | if ~isempty(hhh) |
---|
| 687 | delete(hhh)%delete the circle marker |
---|
[2] | 688 | end |
---|
[71] | 689 | return |
---|
[2] | 690 | end |
---|
[71] | 691 | xlim=get(hplot,'XLim'); |
---|
| 692 | ylim=get(hplot,'YLim'); |
---|
| 693 | ind_range=max(abs(xlim(2)-xlim(1)),abs(ylim(end)-ylim(1)))/20;%defines the size of the circle marker |
---|
| 694 | if isempty(hhh) |
---|
| 695 | axes(hplot) |
---|
| 696 | rectangle('Curvature',[1 1],... |
---|
| 697 | 'Position',[XCoord-ind_range/2 YCoord-ind_range/2 ind_range ind_range],'EdgeColor','m',... |
---|
| 698 | 'LineStyle','-','Tag','calib_marker'); |
---|
| 699 | else |
---|
| 700 | set(hhh,'Position',[XCoord-ind_range/2 YCoord-ind_range/2 ind_range ind_range]) |
---|
| 701 | end |
---|
[2] | 702 | |
---|
[60] | 703 | %------------------------------------------------------------------------ |
---|
[2] | 704 | % --- Executes on selection change in edit_append. |
---|
| 705 | function edit_append_Callback(hObject, eventdata, handles) |
---|
[60] | 706 | %------------------------------------------------------------------------ |
---|
[2] | 707 | choice=get(handles.edit_append,'Value'); |
---|
| 708 | if choice==1 |
---|
| 709 | Coord=get(handles.ListCoord,'String'); |
---|
| 710 | val=length(Coord); |
---|
| 711 | if val>=1 & isequal(Coord{val},'') |
---|
| 712 | val=val-1; %do not take into account blank |
---|
| 713 | end |
---|
| 714 | Coord{val+1}=''; |
---|
| 715 | set(handles.ListCoord,'String',Coord) |
---|
| 716 | set(handles.ListCoord,'Value',val+1) |
---|
| 717 | end |
---|
| 718 | |
---|
| 719 | |
---|
| 720 | |
---|
| 721 | function NEW_Callback(hObject, eventdata, handles) |
---|
| 722 | %A METTRE SOUS UN BOUTON |
---|
| 723 | huvmat=findobj(allchild(0),'Name','uvmat'); |
---|
| 724 | hchild=get(huvmat,'children'); |
---|
| 725 | hcoord=findobj(hchild,'Tag','menu_coord'); |
---|
| 726 | coordtype=get(hcoord,'Value'); |
---|
| 727 | haxes=findobj(hchild,'Tag','axes3'); |
---|
| 728 | AxeData=get(haxes,'UserData'); |
---|
| 729 | if ~isequal(hcoord,2) |
---|
| 730 | set(hcoord,'Value',2) |
---|
| 731 | huvmat=uvmat(AxeData); |
---|
| 732 | 'relancer uvmat'; |
---|
| 733 | end |
---|
| 734 | if ~isfield(AxeData,'ZoomAxes') |
---|
[42] | 735 | msgbox_uvmat('ERROR','first draw a window around a grid marker') |
---|
[2] | 736 | return |
---|
| 737 | end |
---|
| 738 | XLim=get(AxeData.ZoomAxes,'XLim'); |
---|
| 739 | YLim=get(AxeData.ZoomAxes,'YLim'); |
---|
| 740 | np=size(AxeData.A); |
---|
| 741 | ind_sub_x=round(XLim); |
---|
| 742 | ind_sub_y=np(1)-round(YLim); |
---|
| 743 | Mfiltre=AxeData.A([ind_sub_y(2):ind_sub_y(1)] ,ind_sub_x,:); |
---|
| 744 | Mfiltre_norm=double(Mfiltre); |
---|
| 745 | Mfiltre_norm=Mfiltre_norm/sum(sum(Mfiltre_norm)); |
---|
| 746 | Mfiltre_norm=100*(Mfiltre_norm-mean(mean(Mfiltre_norm))); |
---|
| 747 | Atype=class(AxeData.A); |
---|
| 748 | Data.NbDim=2; |
---|
| 749 | Data.A=filter2(Mfiltre_norm,double(AxeData.A)); |
---|
| 750 | Data.A=feval(Atype,Data.A); |
---|
| 751 | Data.AName='image'; |
---|
| 752 | Data.AX=AxeData.AX; |
---|
| 753 | Data.AY=AxeData.AY; |
---|
| 754 | Data.CoordType='px'; |
---|
| 755 | plot_field(Data) |
---|
| 756 | |
---|
[60] | 757 | %------------------------------------------------------------------------ |
---|
| 758 | % --- 'key_press_fcn:' function activated when a key is pressed on the keyboard |
---|
[2] | 759 | function key_press_fcn(hObject,eventdata,handles) |
---|
[60] | 760 | %------------------------------------------------------------------------ |
---|
[71] | 761 | xx=double(get(handles.geometry_calib,'CurrentCharacter')); %get the keyboard character |
---|
[2] | 762 | if ismember(xx,[8 127])%backspace or delete |
---|
| 763 | Coord_cell=get(handles.ListCoord,'String'); |
---|
| 764 | val=get(handles.ListCoord,'Value'); |
---|
[71] | 765 | if val<numel(Coord_cell) % the last element '...' has not been selected |
---|
| 766 | Coord_cell(val)=[];%remove the selected line |
---|
| 767 | set(handles.ListCoord,'String',Coord_cell) |
---|
| 768 | ListCoord_Callback(hObject, eventdata, handles) |
---|
| 769 | MenuPlot_Callback(hObject,eventdata,handles) |
---|
[2] | 770 | end |
---|
| 771 | end |
---|
| 772 | |
---|
[71] | 773 | % %------------------------------------------------------------------------ |
---|
| 774 | % % --- Executes on button press in append_point. |
---|
| 775 | % function append_point_Callback(hObject, eventdata, handles) |
---|
| 776 | % %------------------------------------------------------------------------ |
---|
| 777 | % Coord=get(handles.ListCoord,'String'); |
---|
| 778 | % val=length(Coord); |
---|
| 779 | % if val>=1 & isequal(Coord{val},'') |
---|
| 780 | % val=val-1; %do not take into account blank |
---|
| 781 | % end |
---|
| 782 | % Coord{val+1}=''; |
---|
| 783 | % set(handles.ListCoord,'String',Coord) |
---|
| 784 | % set(handles.ListCoord,'Value',val+1) |
---|
[2] | 785 | |
---|
[60] | 786 | %------------------------------------------------------------------------ |
---|
[2] | 787 | function MenuOpen_Callback(hObject, eventdata, handles) |
---|
[60] | 788 | %------------------------------------------------------------------------ |
---|
[2] | 789 | %get the object file |
---|
| 790 | huvmat=findobj(allchild(0),'Name','uvmat'); |
---|
| 791 | UvData=get(huvmat,'UserData'); |
---|
| 792 | hchild=get(huvmat,'Children'); |
---|
| 793 | hrootpath=findobj(hchild,'Tag','RootPath'); |
---|
| 794 | oldfile=get(hrootpath,'String'); |
---|
| 795 | if isempty(oldfile) |
---|
| 796 | oldfile=''; |
---|
| 797 | end |
---|
| 798 | %[FileName,PathName] = uigetfile('*.civ','Select a .civ file',oldfile) |
---|
| 799 | [FileName, PathName, filterindex] = uigetfile( ... |
---|
| 800 | {'*.xml;*.mat', ' (*.xml,*.mat)'; |
---|
| 801 | '*.xml', '.xml files '; ... |
---|
| 802 | '*.mat', '.mat matlab files '}, ... |
---|
| 803 | 'Pick a file',oldfile); |
---|
| 804 | fileinput=[PathName FileName];%complete file name |
---|
| 805 | testblank=findstr(fileinput,' ');%look for blanks |
---|
| 806 | if ~isempty(testblank) |
---|
| 807 | msgbox_uvmat('ERROR','forbidden input file name or path: no blank character allowed') |
---|
| 808 | return |
---|
| 809 | end |
---|
| 810 | sizf=size(fileinput); |
---|
| 811 | if (~ischar(fileinput)|~isequal(sizf(1),1)),return;end |
---|
| 812 | loadfile(handles,fileinput) |
---|
| 813 | |
---|
| 814 | |
---|
[60] | 815 | %------------------------------------------------------------------------ |
---|
[2] | 816 | function MenuPlot_Callback(hObject, eventdata, handles) |
---|
[60] | 817 | %------------------------------------------------------------------------ |
---|
[2] | 818 | huvmat=findobj(allchild(0),'Name','uvmat');%find the current uvmat interface handle |
---|
| 819 | UvData=get(huvmat,'UserData');%Data associated to the current uvmat interface |
---|
| 820 | hhuvmat=guidata(huvmat); %handles of GUI elements in uvmat |
---|
| 821 | hplot=findobj(huvmat,'Tag','axes3');%main plotting axis of uvmat |
---|
[60] | 822 | h_menu_coord=findobj(huvmat,'Tag','transform_fct'); |
---|
[2] | 823 | menu=get(h_menu_coord,'String'); |
---|
| 824 | choice=get(h_menu_coord,'Value'); |
---|
| 825 | if iscell(menu) |
---|
| 826 | option=menu{choice}; |
---|
| 827 | else |
---|
| 828 | option='px'; %default |
---|
| 829 | end |
---|
| 830 | Coord_cell=get(handles.ListCoord,'String'); |
---|
| 831 | ObjectData=read_geometry_calib(Coord_cell); |
---|
| 832 | %ObjectData=read_geometry_calib(handles);%read the interface input parameters defining the object |
---|
[71] | 833 | if ~isempty(ObjectData.Coord) |
---|
| 834 | if isequal(option,'phys') |
---|
| 835 | ObjectData.Coord=ObjectData.Coord(:,[1:3]); |
---|
| 836 | elseif isequal(option,'px')||isequal(option,'') |
---|
| 837 | ObjectData.Coord=ObjectData.Coord(:,[4:5]); |
---|
| 838 | else |
---|
| 839 | msgbox_uvmat('ERROR','the choice in menu_coord of uvmat must be '''', px or phys ') |
---|
| 840 | end |
---|
[2] | 841 | end |
---|
| 842 | axes(hhuvmat.axes3) |
---|
| 843 | hh=findobj('Tag','calib_points'); |
---|
[71] | 844 | if ~isempty(ObjectData.Coord) && isempty(hh) |
---|
[2] | 845 | hh=line(ObjectData.Coord(:,1),ObjectData.Coord(:,2),'Color','m','Tag','calib_points','LineStyle','.','Marker','+'); |
---|
[71] | 846 | elseif isempty(ObjectData.Coord)%empty list of points, suppress the plot |
---|
| 847 | delete(hh) |
---|
[2] | 848 | else |
---|
| 849 | set(hh,'XData',ObjectData.Coord(:,1)) |
---|
| 850 | set(hh,'YData',ObjectData.Coord(:,2)) |
---|
| 851 | end |
---|
[61] | 852 | pause(.1) |
---|
| 853 | figure(handles.geometry_calib) |
---|
[2] | 854 | |
---|
| 855 | % -------------------------------------------------------------------- |
---|
| 856 | function MenuHelp_Callback(hObject, eventdata, handles) |
---|
| 857 | path_to_uvmat=which ('uvmat');% check the path of uvmat |
---|
| 858 | pathelp=fileparts(path_to_uvmat); |
---|
[36] | 859 | helpfile=fullfile(pathelp,'uvmat_doc','uvmat_doc.html'); |
---|
| 860 | 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] | 861 | else |
---|
[36] | 862 | addpath (fullfile(pathelp,'uvmat_doc')) |
---|
[2] | 863 | web([helpfile '#geometry_calib']) |
---|
| 864 | end |
---|
| 865 | |
---|
[17] | 866 | %------------------------------------------------------------------------ |
---|
[2] | 867 | function MenuCreateGrid_Callback(hObject, eventdata, handles) |
---|
[17] | 868 | %------------------------------------------------------------------------ |
---|
[36] | 869 | %hcalib=get(handles.calib_type,'parent');%handles of the GUI geometry_calib |
---|
[61] | 870 | CalibData=get(handles.geometry_calib,'UserData'); |
---|
[12] | 871 | Tinput=[];%default |
---|
| 872 | if isfield(CalibData,'grid') |
---|
| 873 | Tinput=CalibData.grid; |
---|
| 874 | end |
---|
[71] | 875 | [T,CalibData.grid]=create_grid(Tinput);%display the GUI create_grid |
---|
[61] | 876 | set(handles.geometry_calib,'UserData',CalibData) |
---|
[2] | 877 | |
---|
[12] | 878 | %grid in phys space |
---|
[71] | 879 | Coord=get(handles.ListCoord,'String'); |
---|
| 880 | val=get(handles.ListCoord,'Value'); |
---|
| 881 | data=read_geometry_calib(Coord); |
---|
| 882 | %nbpoints=size(data.Coord,1); %nbre of calibration points |
---|
| 883 | 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 |
---|
| 884 | % for i=1:nbpoints |
---|
| 885 | % for j=1:5 |
---|
| 886 | % Coord{i,j}=num2str(data.Coord(i,j),4);%display coordiantes with 4 digits |
---|
| 887 | % end |
---|
| 888 | % end |
---|
| 889 | %update the phys coordinates starting from the selected point (down in the |
---|
| 890 | Coord(end,:)=[]; %remove last string '.....' |
---|
| 891 | for i=1:size(data.Coord,1) |
---|
| 892 | for j=1:5 |
---|
[17] | 893 | Coord{i,j}=num2str(data.Coord(i,j),4);%display coordiantes with 4 digits |
---|
| 894 | end |
---|
| 895 | end |
---|
| 896 | |
---|
| 897 | %size(data.Coord,1) |
---|
[12] | 898 | Tabchar=cell2tab(Coord,' | '); |
---|
[71] | 899 | Tabchar=[Tabchar ;{'......'}]; |
---|
[12] | 900 | set(handles.ListCoord,'String',Tabchar) |
---|
[2] | 901 | |
---|
[71] | 902 | % ----------------------------------------------------------------------- |
---|
| 903 | % --- automatic grid dectection from local maxima of the images |
---|
[60] | 904 | function MenuDetectGrid_Callback(hObject, eventdata, handles) |
---|
[71] | 905 | %------------------------------------------------------------------------ |
---|
[61] | 906 | CalibData=get(handles.geometry_calib,'UserData'); |
---|
[60] | 907 | grid_input=[];%default |
---|
| 908 | if isfield(CalibData,'grid') |
---|
| 909 | grid_input=CalibData.grid;%retrieve the previously used grid |
---|
| 910 | end |
---|
| 911 | [T,CalibData.grid]=create_grid(grid_input);%display the GUI create_grid |
---|
[61] | 912 | set(handles.geometry_calib,'UserData',CalibData)%store the phys grid for later use |
---|
[60] | 913 | |
---|
| 914 | %read the four last point coordiantes in pixels |
---|
| 915 | Coord_cell=get(handles.ListCoord,'String');%read list of coordiantes on geometry_calib |
---|
| 916 | data=read_geometry_calib(Coord_cell); |
---|
| 917 | nbpoints=size(data.Coord,1); %nbre of calibration points |
---|
[62] | 918 | if nbpoints~=4 |
---|
| 919 | msgbox_uvmat('ERROR','four points must be selected by the mouse, beginning by the new x axis, to delimitate the phs grid area') |
---|
[71] | 920 | return |
---|
[60] | 921 | end |
---|
[71] | 922 | corners_X=(data.Coord(end:-1:end-3,4)); %pixel absissa of the four corners |
---|
| 923 | corners_Y=(data.Coord(end:-1:end-3,5)); |
---|
[60] | 924 | |
---|
[71] | 925 | %reorder the last two points (the two first in the list) if needed |
---|
[63] | 926 | angles=angle((corners_X-corners_X(1))+i*(corners_Y-corners_Y(1))); |
---|
[62] | 927 | if abs(angles(4)-angles(2))>abs(angles(3)-angles(2)) |
---|
| 928 | X_end=corners_X(4); |
---|
| 929 | Y_end=corners_Y(4); |
---|
| 930 | corners_X(4)=corners_X(3); |
---|
| 931 | corners_Y(4)=corners_Y(3); |
---|
| 932 | corners_X(3)=X_end; |
---|
| 933 | corners_Y(3)=Y_end; |
---|
| 934 | end |
---|
| 935 | |
---|
[60] | 936 | %read the current image |
---|
| 937 | huvmat=findobj(allchild(0),'Name','uvmat'); |
---|
| 938 | UvData=get(huvmat,'UserData'); |
---|
| 939 | A=UvData.Field.A; |
---|
| 940 | npxy=size(A); |
---|
| 941 | %linear transform on the current image |
---|
| 942 | X=[CalibData.grid.x_0 CalibData.grid.x_1 CalibData.grid.x_0 CalibData.grid.x_1]';%corner absissa in the rectified image |
---|
| 943 | Y=[CalibData.grid.y_0 CalibData.grid.y_0 CalibData.grid.y_1 CalibData.grid.y_1]';%corner absissa in the rectified image |
---|
| 944 | XY_mat=[ones(size(X)) X Y]; |
---|
| 945 | a_X1=XY_mat\corners_X; %transformation matrix for X |
---|
| 946 | x1=XY_mat*a_X1;%reconstruction |
---|
[71] | 947 | err_X1=max(abs(x1-corners_X));%error |
---|
[60] | 948 | a_Y1=XY_mat\corners_Y;%transformation matrix for X |
---|
| 949 | y1=XY_mat*a_Y1; |
---|
[71] | 950 | err_Y1=max(abs(y1-corners_Y));%error |
---|
[60] | 951 | GeometryCalib.CalibrationType='linear'; |
---|
| 952 | GeometryCalib.CoordUnit=[];% default value, to be updated by the calling function |
---|
| 953 | GeometryCalib.f=1; |
---|
| 954 | GeometryCalib.dpx=1; |
---|
| 955 | GeometryCalib.dpy=1; |
---|
| 956 | GeometryCalib.sx=1; |
---|
| 957 | GeometryCalib.Cx=0; |
---|
| 958 | GeometryCalib.Cy=0; |
---|
| 959 | GeometryCalib.kappa1=0; |
---|
| 960 | GeometryCalib.Tx=a_X1(1); |
---|
| 961 | GeometryCalib.Ty=a_Y1(1); |
---|
| 962 | GeometryCalib.Tz=1; |
---|
| 963 | GeometryCalib.R=[a_X1(2),a_X1(3),0;a_Y1(2),a_Y1(3),0;0,0,1]; |
---|
| 964 | [Amod,Rangx,Rangy]=phys_Ima(A-min(min(A)),GeometryCalib,0); |
---|
| 965 | Amod=double(Amod); |
---|
| 966 | %figure(12) |
---|
| 967 | %Amax=max(max(Amod)) |
---|
| 968 | %image(Rangx,Rangy,uint8(255*Amod/Amax)) |
---|
| 969 | ind_range=10;% range of search of image ma around each point obtained by linear interpolation from the marked points |
---|
| 970 | nbpoints=size(T,1); |
---|
| 971 | for ipoint=1:nbpoints |
---|
| 972 | Dx=(Rangx(2)-Rangx(1))/(npxy(2)-1); %x mesh in real space |
---|
| 973 | Dy=(Rangy(2)-Rangy(1))/(npxy(1)-1); %y mesh in real space |
---|
| 974 | i0=1+round((T(ipoint,1)-Rangx(1))/Dx);%round(Xpx(ipoint)); |
---|
| 975 | j0=1+round((T(ipoint,2)-Rangy(1))/Dy);%round(Xpx(ipoint)); |
---|
| 976 | Asub=Amod(j0-ind_range:j0+ind_range,i0-ind_range:i0+ind_range); |
---|
| 977 | x_profile=sum(Asub,1); |
---|
| 978 | y_profile=sum(Asub,2); |
---|
| 979 | [Amax,ind_x_max]=max(x_profile); |
---|
| 980 | [Amax,ind_y_max]=max(y_profile); |
---|
[61] | 981 | %sub-pixel improvement using moments |
---|
| 982 | x_shift=0; |
---|
| 983 | y_shift=0; |
---|
| 984 | if ind_x_max+2<=2*ind_range+1 && ind_x_max-2>=1 |
---|
| 985 | Atop=x_profile(ind_x_max-2:ind_x_max+2); |
---|
| 986 | x_shift=sum(Atop.*[-2 -1 0 1 2])/sum(Atop); |
---|
| 987 | end |
---|
| 988 | if ind_y_max+2<=2*ind_range+1 && ind_y_max-2>=1 |
---|
| 989 | Atop=y_profile(ind_y_max-2:ind_y_max+2); |
---|
| 990 | y_shift=sum(Atop.*[-2 -1 0 1 2]')/sum(Atop); |
---|
| 991 | end |
---|
| 992 | Delta(ipoint,1)=(x_shift+ind_x_max-ind_range-1)*Dx;%shift from the initial guess |
---|
| 993 | Delta(ipoint,2)=(y_shift+ind_y_max-ind_range-1)*Dy; |
---|
[60] | 994 | end |
---|
| 995 | Tmod=T(:,(1:2))+Delta; |
---|
| 996 | [Xpx,Ypx]=px_XYZ(GeometryCalib,Tmod(:,1),Tmod(:,2)); |
---|
[63] | 997 | for ipoint=1:nbpoints |
---|
| 998 | Coord{ipoint,1}=num2str(T(ipoint,1),4);%display coordiantes with 4 digits |
---|
| 999 | Coord{ipoint,2}=num2str(T(ipoint,2),4);%display coordiantes with 4 digits |
---|
| 1000 | Coord{ipoint,3}='0'; |
---|
| 1001 | Coord{ipoint,4}=num2str(Xpx(ipoint),4);%display coordiantes with 4 digi |
---|
| 1002 | Coord{ipoint,5}=num2str(Ypx(ipoint),4);%display coordiantes with 4 digi |
---|
[60] | 1003 | end |
---|
[71] | 1004 | Tabchar=cell2tab(Coord(end:-1:1,:),' | '); |
---|
| 1005 | Tabchar=[Tabchar ;{'......'}]; |
---|
[60] | 1006 | set(handles.ListCoord,'Value',1) |
---|
| 1007 | set(handles.ListCoord,'String',Tabchar) |
---|
[67] | 1008 | MenuPlot_Callback(hObject, eventdata, handles) |
---|
[60] | 1009 | |
---|
[71] | 1010 | %----------------------------------------------------------------------- |
---|
| 1011 | function MenuTranslatePoints_Callback(hObject, eventdata, handles) |
---|
| 1012 | %----------------------------------------------------------------------- |
---|
| 1013 | %hcalib=get(handles.calib_type,'parent');%handles of the GUI geometry_calib |
---|
| 1014 | CalibData=get(handles.geometry_calib,'UserData'); |
---|
| 1015 | Tinput=[];%default |
---|
| 1016 | if isfield(CalibData,'translate') |
---|
| 1017 | Tinput=CalibData.translate; |
---|
| 1018 | end |
---|
| 1019 | T=translate_points(Tinput);%display translate_points GUI and get shift parameters |
---|
| 1020 | CalibData.translate=T; |
---|
| 1021 | set(handles.geometry_calib,'UserData',CalibData) |
---|
| 1022 | %translation |
---|
| 1023 | Coord_cell=get(handles.ListCoord,'String'); |
---|
| 1024 | data=read_geometry_calib(Coord_cell); |
---|
| 1025 | data.Coord(:,1)=T(1)+data.Coord(:,1); |
---|
| 1026 | data.Coord(:,2)=T(2)+data.Coord(:,2); |
---|
| 1027 | data.Coord(:,3)=T(3)+data.Coord(:,3); |
---|
| 1028 | data.Coord(:,[4 5])=data.Coord(:,[4 5]); |
---|
| 1029 | for i=1:size(data.Coord,1) |
---|
| 1030 | for j=1:5 |
---|
| 1031 | Coord{i,j}=num2str(data.Coord(i,j),4);%phys x,y,z |
---|
| 1032 | end |
---|
| 1033 | end |
---|
| 1034 | Tabchar=cell2tab(Coord,' | '); |
---|
| 1035 | Tabchar=[Tabchar {'.....'}]; |
---|
| 1036 | %set(handles.ListCoord,'Value',1) |
---|
| 1037 | set(handles.ListCoord,'String',Tabchar) |
---|
| 1038 | |
---|
| 1039 | |
---|
| 1040 | % -------------------------------------------------------------------- |
---|
| 1041 | function MenuRotatePoints_Callback(hObject, eventdata, handles) |
---|
| 1042 | %hcalib=get(handles.calib_type,'parent');%handles of the GUI geometry_calib |
---|
| 1043 | CalibData=get(handles.geometry_calib,'UserData'); |
---|
| 1044 | Tinput=[];%default |
---|
| 1045 | if isfield(CalibData,'rotate') |
---|
| 1046 | Tinput=CalibData.rotate; |
---|
| 1047 | end |
---|
| 1048 | T=rotate_points(Tinput);%display translate_points GUI and get shift parameters |
---|
| 1049 | CalibData.rotate=T; |
---|
| 1050 | set(handles.geometry_calib,'UserData',CalibData) |
---|
| 1051 | %----------------------------------------------------- |
---|
| 1052 | %rotation |
---|
| 1053 | Phi=T(1); |
---|
| 1054 | O_x=0;%default |
---|
| 1055 | O_y=0;%default |
---|
| 1056 | if numel(T)>=2 |
---|
| 1057 | O_x=T(2);%default |
---|
| 1058 | end |
---|
| 1059 | if numel(T)>=3 |
---|
| 1060 | O_y=T(3);%default |
---|
| 1061 | end |
---|
| 1062 | Coord_cell=get(handles.ListCoord,'String'); |
---|
| 1063 | data=read_geometry_calib(Coord_cell); |
---|
| 1064 | r1=cos(pi*Phi/180); |
---|
| 1065 | r2=-sin(pi*Phi/180); |
---|
| 1066 | r3=sin(pi*Phi/180); |
---|
| 1067 | r4=cos(pi*Phi/180); |
---|
| 1068 | x=data.Coord(:,1)-O_x; |
---|
| 1069 | y=data.Coord(:,2)-O_y; |
---|
| 1070 | data.Coord(:,1)=r1*x+r2*y; |
---|
| 1071 | data.Coord(:,2)=r3*x+r4*y; |
---|
| 1072 | % data.Coord(:,[4 5])=data.Coord(:,[4 5]); |
---|
| 1073 | for i=1:size(data.Coord,1) |
---|
| 1074 | for j=1:5 |
---|
| 1075 | Coord{i,j}=num2str(data.Coord(i,j),4);%phys x,y,z |
---|
| 1076 | end |
---|
| 1077 | end |
---|
| 1078 | Tabchar=cell2tab(Coord,' | '); |
---|
| 1079 | Tabchar=[Tabchar;{'......'}]; |
---|
| 1080 | set(handles.ListCoord,'Value',1) |
---|
| 1081 | set(handles.ListCoord,'String',Tabchar) |
---|
| 1082 | |
---|
| 1083 | |
---|
[60] | 1084 | %%%%%%%%%%%%%%%%%%%% |
---|
| 1085 | function [A_out,Rangx,Rangy]=phys_Ima(A,Calib,ZIndex) |
---|
| 1086 | xcorner=[]; |
---|
| 1087 | ycorner=[]; |
---|
| 1088 | npx=[]; |
---|
| 1089 | npy=[]; |
---|
| 1090 | siz=size(A); |
---|
| 1091 | npx=[npx siz(2)]; |
---|
| 1092 | npy=[npy siz(1)]; |
---|
| 1093 | xima=[0.5 siz(2)-0.5 0.5 siz(2)-0.5];%image coordiantes of corners |
---|
| 1094 | yima=[0.5 0.5 siz(1)-0.5 siz(1)-0.5]; |
---|
| 1095 | [xcorner,ycorner]=phys_XYZ(Calib,xima,yima,ZIndex);%corresponding physical coordinates |
---|
| 1096 | Rangx(1)=min(xcorner); |
---|
| 1097 | Rangx(2)=max(xcorner); |
---|
| 1098 | Rangy(2)=min(ycorner); |
---|
| 1099 | Rangy(1)=max(ycorner); |
---|
| 1100 | test_multi=(max(npx)~=min(npx)) | (max(npy)~=min(npy)); |
---|
| 1101 | npx=max(npx); |
---|
| 1102 | npy=max(npy); |
---|
| 1103 | x=linspace(Rangx(1),Rangx(2),npx); |
---|
| 1104 | y=linspace(Rangy(1),Rangy(2),npy); |
---|
| 1105 | [X,Y]=meshgrid(x,y);%grid in physical coordiantes |
---|
| 1106 | vec_B=[]; |
---|
| 1107 | |
---|
| 1108 | zphys=0; %default |
---|
| 1109 | if isfield(Calib,'SliceCoord') %.Z= index of plane |
---|
| 1110 | SliceCoord=Calib.SliceCoord(ZIndex,:); |
---|
| 1111 | zphys=SliceCoord(3); %to generalize for non-parallel planes |
---|
| 1112 | end |
---|
| 1113 | [XIMA,YIMA]=px_XYZ(Calib,X,Y,zphys);%corresponding image indices for each point in the real space grid |
---|
| 1114 | XIMA=reshape(round(XIMA),1,npx*npy);%indices reorganized in 'line' |
---|
| 1115 | YIMA=reshape(round(YIMA),1,npx*npy); |
---|
| 1116 | flagin=XIMA>=1 & XIMA<=npx & YIMA >=1 & YIMA<=npy;%flagin=1 inside the original image |
---|
| 1117 | testuint8=isa(A,'uint8'); |
---|
| 1118 | testuint16=isa(A,'uint16'); |
---|
| 1119 | if numel(siz)==2 %(B/W images) |
---|
| 1120 | vec_A=reshape(A,1,npx*npy);%put the original image in line |
---|
| 1121 | ind_in=find(flagin); |
---|
| 1122 | ind_out=find(~flagin); |
---|
| 1123 | ICOMB=((XIMA-1)*npy+(npy+1-YIMA)); |
---|
| 1124 | ICOMB=ICOMB(flagin);%index corresponding to XIMA and YIMA in the aligned original image vec_A |
---|
| 1125 | vec_B(ind_in)=vec_A(ICOMB); |
---|
| 1126 | vec_B(ind_out)=zeros(size(ind_out)); |
---|
| 1127 | A_out=reshape(vec_B,npy,npx);%new image in real coordinates |
---|
| 1128 | elseif numel(siz)==3 |
---|
| 1129 | for icolor=1:siz(3) |
---|
| 1130 | vec_A=reshape(A{icell}(:,:,icolor),1,npx*npy);%put the original image in line |
---|
| 1131 | ind_in=find(flagin); |
---|
| 1132 | ind_out=find(~flagin); |
---|
| 1133 | ICOMB=((XIMA-1)*npy+(npy+1-YIMA)); |
---|
| 1134 | ICOMB=ICOMB(flagin);%index corresponding to XIMA and YIMA in the aligned original image vec_A |
---|
| 1135 | vec_B(ind_in)=vec_A(ICOMB); |
---|
| 1136 | vec_B(ind_out)=zeros(size(ind_out)); |
---|
| 1137 | A_out(:,:,icolor)=reshape(vec_B,npy,npx);%new image in real coordinates |
---|
| 1138 | end |
---|
| 1139 | end |
---|
| 1140 | if testuint8 |
---|
| 1141 | A_out=uint8(A_out); |
---|
| 1142 | end |
---|
| 1143 | if testuint16 |
---|
| 1144 | A_out=uint16(A_out); |
---|
| 1145 | end |
---|
| 1146 | |
---|
| 1147 | %INPUT: |
---|
| 1148 | %Z: index of plane |
---|
| 1149 | function [Xphys,Yphys,Zphys]=phys_XYZ(Calib,X,Y,Z) |
---|
| 1150 | if exist('Z','var')& isequal(Z,round(Z))& Z>0 & isfield(Calib,'SliceCoord')&length(Calib.SliceCoord)>=Z |
---|
| 1151 | Zindex=Z; |
---|
| 1152 | Zphys=Calib.SliceCoord(Zindex,3);%GENERALISER AUX CAS AVEC ANGLE |
---|
| 1153 | else |
---|
| 1154 | % if exist('Z','var') |
---|
| 1155 | % Zphys=Z; |
---|
| 1156 | % else |
---|
| 1157 | Zphys=0; |
---|
| 1158 | % end |
---|
| 1159 | end |
---|
| 1160 | if ~exist('X','var')||~exist('Y','var') |
---|
| 1161 | Xphys=[]; |
---|
| 1162 | Yphys=[];%default |
---|
| 1163 | return |
---|
| 1164 | end |
---|
| 1165 | Xphys=X;%default |
---|
| 1166 | Yphys=Y; |
---|
| 1167 | %image transform |
---|
| 1168 | if isfield(Calib,'R') |
---|
| 1169 | R=(Calib.R)'; |
---|
| 1170 | Dx=R(5)*R(7)-R(4)*R(8); |
---|
| 1171 | Dy=R(1)*R(8)-R(2)*R(7); |
---|
| 1172 | D0=Calib.f*(R(2)*R(4)-R(1)*R(5)); |
---|
| 1173 | Z11=R(6)*R(8)-R(5)*R(9); |
---|
| 1174 | Z12=R(2)*R(9)-R(3)*R(8); |
---|
| 1175 | Z21=R(4)*R(9)-R(6)*R(7); |
---|
| 1176 | Z22=R(3)*R(7)-R(1)*R(9); |
---|
| 1177 | Zx0=R(3)*R(5)-R(2)*R(6); |
---|
| 1178 | Zy0=R(1)*R(6)-R(3)*R(4); |
---|
| 1179 | A11=R(8)*Calib.Ty-R(5)*Calib.Tz+Z11*Zphys; |
---|
| 1180 | A12=R(2)*Calib.Tz-R(8)*Calib.Tx+Z12*Zphys; |
---|
| 1181 | A21=-R(7)*Calib.Ty+R(4)*Calib.Tz+Z21*Zphys; |
---|
| 1182 | A22=-R(1)*Calib.Tz+R(7)*Calib.Tx+Z11*Zphys; |
---|
| 1183 | X0=Calib.f*(R(5)*Calib.Tx-R(2)*Calib.Ty+Zx0*Zphys); |
---|
| 1184 | Y0=Calib.f*(-R(4)*Calib.Tx+R(1)*Calib.Ty+Zy0*Zphys); |
---|
| 1185 | %px to camera: |
---|
| 1186 | Xd=(Calib.dpx/Calib.sx)*(X-Calib.Cx); % sensor coordinates |
---|
| 1187 | Yd=Calib.dpy*(Y-Calib.Cy); |
---|
| 1188 | dist_fact=1+Calib.kappa1*(Xd.*Xd+Yd.*Yd); %distortion factor |
---|
| 1189 | Xu=dist_fact.*Xd;%undistorted sensor coordinates |
---|
| 1190 | Yu=dist_fact.*Yd; |
---|
| 1191 | denom=Dx*Xu+Dy*Yu+D0; |
---|
| 1192 | % denom2=denom.*denom; |
---|
| 1193 | Xphys=(A11.*Xu+A12.*Yu+X0)./denom;%world coordinates |
---|
| 1194 | Yphys=(A21.*Xu+A22.*Yu+Y0)./denom; |
---|
[69] | 1195 | end |
---|