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