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