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