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