1 | %'set_grid':produce grid for PIV with one or two images (stereo case) |
---|
2 | %------------------------------------------------------------------------ |
---|
3 | % function varargout = set_grid(varargin) |
---|
4 | % associated with the GUI set_grid.fig |
---|
5 | |
---|
6 | %======================================================================= |
---|
7 | % Copyright 2008-2021, LEGI UMR 5519 / CNRS UGA G-INP, Grenoble, France |
---|
8 | % http://www.legi.grenoble-inp.fr |
---|
9 | % Joel.Sommeria - Joel.Sommeria (A) legi.cnrs.fr |
---|
10 | % |
---|
11 | % This file is part of the toolbox UVMAT. |
---|
12 | % |
---|
13 | % UVMAT is free software; you can redistribute it and/or modify |
---|
14 | % it under the terms of the GNU General Public License as published |
---|
15 | % by the Free Software Foundation; either version 2 of the license, |
---|
16 | % or (at your option) any later version. |
---|
17 | % |
---|
18 | % UVMAT is distributed in the hope that it will be useful, |
---|
19 | % but WITHOUT ANY WARRANTY; without even the implied warranty of |
---|
20 | % MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
---|
21 | % GNU General Public License (see LICENSE.txt) for more details. |
---|
22 | %======================================================================= |
---|
23 | |
---|
24 | function varargout = set_grid(varargin) |
---|
25 | |
---|
26 | % Last Modified by GUIDE v2.5 26-Jun-2015 08:54:56 |
---|
27 | |
---|
28 | % Begin initialization code - DO NOT PLOT |
---|
29 | gui_Singleton = 1; |
---|
30 | gui_State = struct('gui_Name', mfilename, ... |
---|
31 | 'gui_Singleton', gui_Singleton, ... |
---|
32 | 'gui_OpeningFcn', @set_grid_OpeningFcn, ... |
---|
33 | 'gui_OutputFcn', @set_grid_OutputFcn, ... |
---|
34 | 'gui_LayoutFcn', [] , ... |
---|
35 | 'gui_Callback', []); |
---|
36 | |
---|
37 | if nargin && ischar(varargin{1}) |
---|
38 | gui_State.gui_Callback = str2func(varargin{1}); |
---|
39 | end |
---|
40 | if nargout |
---|
41 | [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:}); |
---|
42 | else |
---|
43 | gui_mainfcn(gui_State, varargin{:}); |
---|
44 | end |
---|
45 | |
---|
46 | %------------------------------------------------------------------- |
---|
47 | % --- Executes just before set_grid is made visible. |
---|
48 | %INPUT: |
---|
49 | % handles: handles of the set_grid interface elements |
---|
50 | %'IndexObj': index of the object (on the UvData list) that set_grid will modify |
---|
51 | % if =[] or absent: index still undefined (create mode in uvmat) |
---|
52 | % if=0; no associated object (used for series), the button 'PLOT' is then unvisible |
---|
53 | %'data': read from an existing object selected in the interface |
---|
54 | % .TITLE : class of object ('POINTS','LINE',....) |
---|
55 | % .num_DX,num_DY,DZ; meshes for regular grids |
---|
56 | % .Coord: object position coordinates |
---|
57 | % .ParentButton: handle of the uicontrol object calling the interface |
---|
58 | % PlotHandles: set of handles of the elements contolling the plotting of the projected field: |
---|
59 | % if =[] or absent, no plot (mask mode in uvmat) |
---|
60 | % parameters on the uvmat interface (obtained by 'get_plot_handle.m') |
---|
61 | function set_grid_OpeningFcn(hObject, eventdata, handles,InputFile,InputField) |
---|
62 | |
---|
63 | % Choose default command line output for set_grid |
---|
64 | handles.output = hObject; |
---|
65 | |
---|
66 | % Update handles structure |
---|
67 | guidata(hObject, handles); |
---|
68 | |
---|
69 | %default |
---|
70 | set(hObject,'DeleteFcn',@closefcn) |
---|
71 | set(hObject,'WindowButtonDownFcn',{'mouse_down'})%set mouse click action function |
---|
72 | set(handles.CoordType,'ListboxTop',1) |
---|
73 | set(handles.CoordType,'Value',1); |
---|
74 | set(handles.CoordType,'String',{'phys';'px'}); |
---|
75 | if exist('InputFile','var') |
---|
76 | set(handles.ImageA,'String',InputFile) |
---|
77 | end |
---|
78 | |
---|
79 | %% use InputField input from uvmat |
---|
80 | check_pixel=0; |
---|
81 | if exist('InputField','var') |
---|
82 | if strcmp(InputField.CoordUnit,'pixel') |
---|
83 | set(handles.CoordType,'Value',2) |
---|
84 | set(handles.TxtWarning,'Visible','on') |
---|
85 | Mesh=20;%default mesh in pixel |
---|
86 | check_pixel=1; |
---|
87 | else |
---|
88 | set(handles.CoordType,'Value',1) |
---|
89 | InputField.CoordMesh=20*InputField.CoordMesh; % about 20 pixels |
---|
90 | % adjust the mesh to a value 1, 2 , 5 *10^n |
---|
91 | ord=10^(floor(log10(InputField.CoordMesh)));%order of magnitude |
---|
92 | if InputField.CoordMesh/ord>=5 |
---|
93 | Mesh=5*ord; |
---|
94 | elseif InputField.CoordMesh/ord>=2 |
---|
95 | Mesh=2*ord; |
---|
96 | else |
---|
97 | Mesh=ord; |
---|
98 | end |
---|
99 | end |
---|
100 | Input.DX=Mesh; |
---|
101 | Input.DY=Mesh; |
---|
102 | Input.XMin=(Mesh/2)*ceil(InputField.XMin/(Mesh/2))-0.5*check_pixel; |
---|
103 | Input.XMax=Input.XMin+Mesh*floor((InputField.XMax-Input.XMin)/Mesh)-0.5*check_pixel; |
---|
104 | Input.YMin=(Mesh/2)*ceil(InputField.YMin/(Mesh/2))-0.5*check_pixel; |
---|
105 | Input.YMax=Input.YMin+Mesh*floor((InputField.YMax-Input.YMin)/Mesh)-0.5*check_pixel; |
---|
106 | errormsg=fill_GUI(Input,handles.set_grid); |
---|
107 | end |
---|
108 | |
---|
109 | % --- Outputs from this function are returned to the command line. |
---|
110 | function varargout = set_grid_OutputFcn(hObject, eventdata, handles) |
---|
111 | % Get default command line output from handles structure |
---|
112 | varargout{1} = handles.output; |
---|
113 | varargout{2}=handles; |
---|
114 | |
---|
115 | %---------------------------------------------------- |
---|
116 | % executed when closing: set the parent interface button to value 0 |
---|
117 | function closefcn(gcbo,eventdata) |
---|
118 | huvmat=findobj(allchild(0),'Name','uvmat');%find the current uvmat interface handle |
---|
119 | parent_button=findobj(huvmat,'Tag','grid'); |
---|
120 | if ~isempty(parent_button) |
---|
121 | set(parent_button,'Value',0)%put unactivated buttons to green |
---|
122 | tag=get(parent_button,'Tag'); |
---|
123 | if isequal(tag,'edit') |
---|
124 | set(parent_button,'BackgroundColor',[0.7 0.7 0.7]); |
---|
125 | else |
---|
126 | set(parent_button,'BackgroundColor',[0 1 0]); |
---|
127 | end |
---|
128 | end |
---|
129 | |
---|
130 | %----------------------------------------------------------------------- |
---|
131 | % --- Executes on button press in plot: PLOT the defined object and its projected field |
---|
132 | function plot_Callback(hObject, eventdata, handles) |
---|
133 | [grid_pix_A,grid_pix_B,grid_phys]=get_grid(read_GUI(handles.set_grid)); |
---|
134 | huvmat=findobj(allchild(0),'tag','uvmat'); |
---|
135 | hhuvmat=guidata(huvmat); |
---|
136 | axes(hhuvmat.PlotAxes); |
---|
137 | hold on |
---|
138 | UvData=get(huvmat,'UserData'); |
---|
139 | if isfield(UvData.Field, 'CoordUnit')&& strcmp(UvData.Field.CoordUnit,'pixel') |
---|
140 | plot(grid_pix_A(:,1),grid_pix_A(:,2),'.','Tag','proj_object') |
---|
141 | else |
---|
142 | plot(grid_phys(:,1),grid_phys(:,2),'.','Tag','proj_object') |
---|
143 | end |
---|
144 | |
---|
145 | %% display grid in second image defined |
---|
146 | if ~isempty(grid_pix_B) |
---|
147 | hviewfield=view_field(get(handles.imageB,'String')); |
---|
148 | hhviewfield=guidata(hviewfield); |
---|
149 | axes(hhviewfield.PlotAxes); |
---|
150 | hold on |
---|
151 | if isfield(UvData.Field, 'CoordUnit')&& strcmp(UvData.Field.CoordUnit,'pixel') |
---|
152 | plot(grid_pix_B(:,1),grid_pix_B(:,2),'.') |
---|
153 | else |
---|
154 | plot(grid_phys(:,1),grid_phys(:,2),'.') |
---|
155 | end |
---|
156 | end |
---|
157 | |
---|
158 | % --- Executes on button press in clear. |
---|
159 | function clear_Callback(hObject, eventdata, handles) |
---|
160 | huvmat=findobj(allchild(0),'tag','uvmat'); |
---|
161 | if ~isempty(huvmat) |
---|
162 | hhuvmat=guidata(huvmat); |
---|
163 | hpoints=findobj(hhuvmat.PlotAxes,'Tag','proj_object'); |
---|
164 | if ~isempty(hpoints) |
---|
165 | delete(hpoints) |
---|
166 | end |
---|
167 | end |
---|
168 | %------------------------------------------------------------------------ |
---|
169 | % --- Executes on button press in CoordType. |
---|
170 | function CoordType_Callback(hObject, eventdata, handles) |
---|
171 | %------------------------------------------------------------------------ |
---|
172 | set(handles.num_XMin,'String','') |
---|
173 | set(handles.num_XMax,'String','') |
---|
174 | set(handles.num_DX,'String','') |
---|
175 | set(handles.num_YMin,'String','') |
---|
176 | set(handles.num_YMax,'String','') |
---|
177 | set(handles.num_DY,'String','') |
---|
178 | set(handles.num_Z,'String','') |
---|
179 | if isequal(get(handles.CoordType,'Value'),2) |
---|
180 | set(handles.TxtWarning,'visible','on') |
---|
181 | else |
---|
182 | set(handles.TxtWarning,'visible','on') |
---|
183 | end |
---|
184 | |
---|
185 | % ------------------------------------------------------ |
---|
186 | function Save_Callback(hObject, eventdata, handles) |
---|
187 | % ------------------------------------------------------ |
---|
188 | [grid_pix_A,grid_pix_B]=get_grid(read_GUI(handles.set_grid)); |
---|
189 | |
---|
190 | |
---|
191 | %ECRIRE FICHIERS |
---|
192 | nbpointsA=size(grid_pix_A); |
---|
193 | XA=grid_pix_A(:,1);%index=position+0.5 rounded at the nearest integer value |
---|
194 | YA=grid_pix_A(:,2); |
---|
195 | unitcolumn=32*ones(size(XA)); |
---|
196 | Xchar=num2str(XA,'%1.1f');% write x coordinate in px, rounded at the first decimal |
---|
197 | blanc=char(unitcolumn); |
---|
198 | Ychar=num2str(YA,'%1.1f');% write y coordinate in px, rounded at the first decimal |
---|
199 | tete=['1 ' num2str(nbpointsA(1))]; |
---|
200 | txt=[Xchar blanc Ychar]; |
---|
201 | textgrid={tete;txt}; |
---|
202 | textout=char(textgrid); |
---|
203 | imageA=get(handles.ImageA,'String'); |
---|
204 | RootPath=fileparts_uvmat(imageA); |
---|
205 | Answer = msgbox_uvmat('INPUT_TXT','grid file name (*.grid)',fullfile(RootPath,'gridA.grid')); |
---|
206 | dlmwrite(Answer,textout,''); |
---|
207 | msgbox_uvmat('CONFIRMATION',[Answer ' written as ASCII text file']); |
---|
208 | if ~isempty(grid_pix_B) |
---|
209 | nbpointsB=size(grid_pix_B); |
---|
210 | XB=round(grid_pix_B(:,1)+0.5);%index=position+0.5 rounded at the nearest integer value |
---|
211 | YB=round(grid_pix_B(:,2)+0.5); |
---|
212 | unitcolumn=32*ones(size(XB)); |
---|
213 | Xchar=num2str(XB); |
---|
214 | blanc=char(unitcolumn); |
---|
215 | Ychar=num2str(YB); |
---|
216 | tete=['1 ' num2str(nbpointsB(1))]; |
---|
217 | txt=[Xchar blanc Ychar]; |
---|
218 | textgrid={tete;txt}; |
---|
219 | textout=char(textgrid); |
---|
220 | Answer = msgbox_uvmat('INPUT_TXT','grid file name (*.grid)',fullfile(RootPath,'gridB.grid')); |
---|
221 | dlmwrite(Answer,textout,''); |
---|
222 | msgbox_uvmat('CONFIRMATION',[Answer ' written as ASCII text file']); |
---|
223 | end |
---|
224 | |
---|
225 | |
---|
226 | %------------------------------------------------------------------------ |
---|
227 | function [grid_pix_A,grid_pix_B,grid_phys]=get_grid(GUI) |
---|
228 | %------------------------------------------------------------------------ |
---|
229 | grid_pix_B=[];%default |
---|
230 | array_x=GUI.XMin:GUI.DX:GUI.XMax;% array of x values |
---|
231 | array_y=GUI.YMin:GUI.DY:GUI.YMax;% array of y values |
---|
232 | [grid_x,grid_y]=meshgrid(array_x,array_y);% matrices of x and y values |
---|
233 | grid_x=reshape(grid_x,[],1); %matrix of x values reshaped in line |
---|
234 | grid_y=reshape(grid_y,[],1);%matrix of y values reshaped in line |
---|
235 | % grid_z=zeros(nx_patch*ny_patch,1);% plane coordinates (TODO: 3D grids) |
---|
236 | |
---|
237 | %% check the input image A |
---|
238 | if ~exist(GUI.ImageA,'file') |
---|
239 | msgbox_uvmat('ERROR',['input image file' imageA 'does not exist']) |
---|
240 | return |
---|
241 | end |
---|
242 | [FileInfo,VideoObject]=get_file_info(GUI.ImageA); |
---|
243 | if ~strcmp(FileInfo.FieldType,'image');% =1 for images |
---|
244 | msgbox_uvmat('ERROR',['error: ' GUI.ImageA ' is not an image type recognized by Matlab ']) |
---|
245 | return |
---|
246 | end |
---|
247 | |
---|
248 | [RootPath,SubDir,RootFile,tild,tild,tild,tild,FileExt]=fileparts_uvmat(GUI.ImageA); |
---|
249 | |
---|
250 | %% transform to pixels if the grid is defined in phys coordinates |
---|
251 | grid_x_imaA=grid_x;%default grid in image A coordinates |
---|
252 | grid_y_imaA=grid_y; |
---|
253 | % MenuCoord=get(handles.CoordType,'String');% type of coordinates for grid definition, phys or pixel |
---|
254 | if strcmp(GUI.CoordType,'phys') |
---|
255 | fileAxml=fullfile(RootPath,[SubDir '.xml']);% new convention for xml name |
---|
256 | if ~exist(fileAxml,'file') |
---|
257 | fileAxml=[fullfile(RootPath,RootFile) '.xml'];% old convention for xml name |
---|
258 | end |
---|
259 | tsaiA=[];%default |
---|
260 | if exist(fileAxml,'file') |
---|
261 | [XmlDataA,errormsg]=imadoc2struct(fileAxml); |
---|
262 | if ~isempty(errormsg) |
---|
263 | msgbox_uvmat('ERROR',['error in ' fileAxml ': ' errormsg]) |
---|
264 | return |
---|
265 | end |
---|
266 | if isfield(XmlDataA,'GeometryCalib') |
---|
267 | tsaiA=XmlDataA.GeometryCalib; |
---|
268 | end |
---|
269 | end |
---|
270 | if isempty(tsaiA) |
---|
271 | msgbox_uvmat('WARNING','no geometric calibration available for image A, phys =pixel') |
---|
272 | else |
---|
273 | [grid_x_imaA,grid_y_imaA]=px_XYZ(tsaiA,grid_x,grid_y,GUI.Z); |
---|
274 | end |
---|
275 | end |
---|
276 | |
---|
277 | %% detect the grid points which are inside image A |
---|
278 | A=read_image(GUI.ImageA,FileInfo.FileType,VideoObject,1); |
---|
279 | npxA=size(A,2); |
---|
280 | npyA=size(A,1); |
---|
281 | flag=grid_x_imaA>=1 & grid_x_imaA<=npxA & grid_y_imaA>=1 & grid_y_imaA<=npyA;% ='true' inside the image |
---|
282 | |
---|
283 | %% detect the grid points which are inside image B if relevant (use for stereo PIV) |
---|
284 | if isfield(GUI,'ImageB') |
---|
285 | if ~exist(imageB,'file') |
---|
286 | msgbox_uvmat('ERROR',['input image file' GUI.ImageB 'does not exist']) |
---|
287 | return |
---|
288 | end |
---|
289 | [RootPathB,SubDirB,RootFileB,tild,tild,tild,tild,FileExt]=fileparts_uvmat(GUI.ImageB); |
---|
290 | fileBxml=fullfile(RootPathB,[SubDirB '.xml']);% new convention for xml name |
---|
291 | if ~exist(fileBxml,'file') |
---|
292 | fileBxml=[fullfile(RootPathB,RootFileB) '.xml'];% old convention for xml name |
---|
293 | end |
---|
294 | tsaiB=[];%default |
---|
295 | if exist(fileBxml,'file') |
---|
296 | [XmlDataB,errormsg]=imadoc2struct(fileBxml); |
---|
297 | if ~isempty(errormsg) |
---|
298 | msgbox_uvmat('ERROR',['error in ' fileAxml ': ' errormsg]) |
---|
299 | return |
---|
300 | end |
---|
301 | if isfield(XmlDataB,'GeometryCalib') |
---|
302 | tsaiB=XmlDataB.GeometryCalib; |
---|
303 | end |
---|
304 | end |
---|
305 | if isempty(tsaiB) |
---|
306 | msgbox_uvmat('WARNING','no geometric calibration available for image B, phys =pixel') |
---|
307 | grid_x_imaB=grid_x; |
---|
308 | grid_y_imaB=grid_y; |
---|
309 | else |
---|
310 | [grid_x_imaB,grid_y_imaB]=px_XYZ(tsaiB,grid_x,grid_y,GUI.Z); |
---|
311 | end |
---|
312 | B=imread(GUI.ImageB); |
---|
313 | npxB=size(B,2); |
---|
314 | npyB=size(B,1); |
---|
315 | flagB=grid_x_imaB>=1 & grid_x_imaB<=npxB & grid_y_imaB>=1 & grid_y_imaB<=npyB; |
---|
316 | flag=flagA & flagB; |
---|
317 | grid_pix_B(:,1)=round(grid_x_imaB(flag)); |
---|
318 | grid_pix_B(:,2)=round(grid_y_imaB(flag)); |
---|
319 | end |
---|
320 | |
---|
321 | grid_x_imaA=grid_x_imaA(flag); |
---|
322 | grid_y_imaA=grid_y_imaA(flag); |
---|
323 | grid_pix_A=[grid_x_imaA grid_y_imaA]; |
---|
324 | grid_x=grid_x(flag); |
---|
325 | grid_y=grid_y(flag); |
---|
326 | grid_phys=[grid_x grid_y]; |
---|
327 | |
---|
328 | |
---|
329 | function GetImageB_Callback(hObject, eventdata, handles) |
---|
330 | if isequal(get(handles.GetImageB,'Value'),1) |
---|
331 | set(handles.ImageB,'Visible','on') |
---|
332 | [FileName, PathName, filterindex] = uigetfile( ... |
---|
333 | {'*.*', 'All Files (*.*)'}, ... |
---|
334 | 'Pick the second image file',fileparts(fileparts(get(handles.ImageA,'String')))); |
---|
335 | ImageB=fullfile(PathName,FileName); |
---|
336 | [FileInfo,tild,VideoObject]=get_file_info(ImageB); |
---|
337 | if ~strcmp(FileInfo.FieldType,'image');% =1 for images |
---|
338 | msgbox_uvmat('ERROR',['error: ' imageB ' is not an image type recognised by Matlab ']) |
---|
339 | return |
---|
340 | end |
---|
341 | else |
---|
342 | set(handles.ImageB,'Visible','off') |
---|
343 | end |
---|
344 | |
---|
345 | |
---|
346 | %------------------------------------------------------------------------ |
---|
347 | % --- Executes on button press in HELP. |
---|
348 | function HELP_Callback(hObject, eventdata, handles) |
---|
349 | %------------------------------------------------------------------------ |
---|
350 | path_to_uvmat=which ('uvmat');% check the path of uvmat |
---|
351 | pathelp=fileparts(path_to_uvmat); |
---|
352 | helpfile=fullfile(pathelp,'uvmat_doc','uvmat_doc.html'); |
---|
353 | if isempty(dir(helpfile)), errordlg('Please put the help file uvmat_doc.html in the directory UVMAT/UVMAT_DOC') |
---|
354 | else |
---|
355 | web([helpfile '#set_grid']) |
---|
356 | end |
---|
357 | |
---|
358 | |
---|
359 | |
---|
360 | function ImageA_Callback(hObject, eventdata, handles) |
---|
361 | % hObject handle to ImageA (see GCBO) |
---|
362 | % eventdata reserved - to be defined in a future version of MATLAB |
---|
363 | % handles structure with handles and user data (see GUIDATA) |
---|
364 | |
---|
365 | % Hints: get(hObject,'String') returns contents of ImageA as text |
---|
366 | % str2double(get(hObject,'String')) returns contents of ImageA as a double |
---|
367 | |
---|
368 | |
---|
369 | |
---|
370 | |
---|