[2] | 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 |
---|
[809] | 5 | |
---|
| 6 | %======================================================================= |
---|
| 7 | % Copyright 2008-2014, LEGI UMR 5519 / CNRS UJF G-INP, Grenoble, France |
---|
| 8 | % http://www.legi.grenoble-inp.fr |
---|
| 9 | % Joel.Sommeria - Joel.Sommeria (A) legi.cnrs.fr |
---|
[2] | 10 | % |
---|
| 11 | % This file is part of the toolbox UVMAT. |
---|
[809] | 12 | % |
---|
[2] | 13 | % UVMAT is free software; you can redistribute it and/or modify |
---|
[809] | 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 | % |
---|
[2] | 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 |
---|
[809] | 21 | % GNU General Public License (see LICENSE.txt) for more details. |
---|
| 22 | %======================================================================= |
---|
[2] | 23 | |
---|
| 24 | function varargout = set_grid(varargin) |
---|
| 25 | |
---|
[576] | 26 | % Last Modified by GUIDE v2.5 01-Mar-2013 22:41:43 |
---|
[2] | 27 | |
---|
[84] | 28 | % Begin initialization code - DO NOT PLOT |
---|
[2] | 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', []); |
---|
[576] | 36 | |
---|
[315] | 37 | if nargin && ischar(varargin{1}) |
---|
[2] | 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',....) |
---|
[576] | 55 | % .num_DX,num_DY,DZ; meshes for regular grids |
---|
[2] | 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') |
---|
[576] | 61 | function set_grid_OpeningFcn(hObject, eventdata, handles,InputFile,InputField) |
---|
[2] | 62 | |
---|
| 63 | % Choose default command line output for set_grid |
---|
| 64 | handles.output = hObject; |
---|
| 65 | |
---|
| 66 | % Update handles structure |
---|
| 67 | guidata(hObject, handles); |
---|
| 68 | |
---|
[576] | 69 | %default |
---|
[2] | 70 | set(hObject,'DeleteFcn',@closefcn) |
---|
[576] | 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) |
---|
[2] | 77 | end |
---|
[576] | 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 |
---|
[84] | 99 | end |
---|
[576] | 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; |
---|
[591] | 106 | errormsg=fill_GUI(Input,handles.set_grid); |
---|
[84] | 107 | end |
---|
[2] | 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]); |
---|
[576] | 125 | else |
---|
[2] | 126 | set(parent_button,'BackgroundColor',[0 1 0]); |
---|
| 127 | end |
---|
| 128 | end |
---|
| 129 | |
---|
| 130 | %----------------------------------------------------------------------- |
---|
[84] | 131 | % --- Executes on button press in plot: PLOT the defined object and its projected field |
---|
| 132 | function plot_Callback(hObject, eventdata, handles) |
---|
[576] | 133 | [grid_pix_A,grid_pix_B,grid_phys]=get_grid(read_GUI(handles.set_grid)); |
---|
| 134 | huvmat=findobj(allchild(0),'tag','uvmat'); |
---|
[84] | 135 | hhuvmat=guidata(huvmat); |
---|
[576] | 136 | axes(hhuvmat.PlotAxes); |
---|
[84] | 137 | hold on |
---|
[576] | 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),'.') |
---|
| 141 | else |
---|
| 142 | plot(grid_phys(:,1),grid_phys(:,2),'.') |
---|
| 143 | end |
---|
[2] | 144 | |
---|
[576] | 145 | %% display grid in second image defiend |
---|
| 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 |
---|
[2] | 157 | |
---|
[576] | 158 | %------------------------------------------------------------------------ |
---|
| 159 | % --- Executes on button press in CoordType. |
---|
| 160 | function CoordType_Callback(hObject, eventdata, handles) |
---|
| 161 | %------------------------------------------------------------------------ |
---|
| 162 | set(handles.num_XMin,'String','') |
---|
| 163 | set(handles.num_XMax,'String','') |
---|
| 164 | set(handles.num_DX,'String','') |
---|
| 165 | set(handles.num_YMin,'String','') |
---|
| 166 | set(handles.num_YMax,'String','') |
---|
| 167 | set(handles.num_DY,'String','') |
---|
| 168 | set(handles.num_Z,'String','') |
---|
| 169 | if isequal(get(handles.CoordType,'Value'),2) |
---|
| 170 | set(handles.TxtWarning,'visible','on') |
---|
| 171 | else |
---|
| 172 | set(handles.TxtWarning,'visible','on') |
---|
| 173 | end |
---|
[84] | 174 | |
---|
[2] | 175 | % ------------------------------------------------------ |
---|
[576] | 176 | function Save_Callback(hObject, eventdata, handles) |
---|
[2] | 177 | % ------------------------------------------------------ |
---|
[576] | 178 | [grid_pix_A,grid_pix_B]=get_grid(read_GUI(handles.set_grid)); |
---|
[84] | 179 | |
---|
[576] | 180 | |
---|
[84] | 181 | %ECRIRE FICHIERS |
---|
| 182 | nbpointsA=size(grid_pix_A); |
---|
[576] | 183 | XA=grid_pix_A(:,1);%index=position+0.5 rounded at the nearest integer value |
---|
[84] | 184 | YA=grid_pix_A(:,2); |
---|
| 185 | unitcolumn=32*ones(size(XA)); |
---|
| 186 | Xchar=num2str(XA); |
---|
| 187 | blanc=char(unitcolumn); |
---|
| 188 | Ychar=num2str(YA); |
---|
| 189 | tete=['1 ' num2str(nbpointsA(1))]; |
---|
| 190 | txt=[Xchar blanc Ychar]; |
---|
| 191 | textgrid={tete;txt}; |
---|
| 192 | textout=char(textgrid); |
---|
[576] | 193 | imageA=get(handles.ImageA,'String'); |
---|
[342] | 194 | RootPath=fileparts_uvmat(imageA); |
---|
| 195 | Answer = msgbox_uvmat('INPUT_TXT','grid file name (*.grid)',fullfile(RootPath,'gridA.grid')); |
---|
[84] | 196 | dlmwrite(Answer,textout,''); |
---|
| 197 | msgbox_uvmat('CONFIRMATION',[Answer ' written as ASCII text file']); |
---|
| 198 | if ~isempty(grid_pix_B) |
---|
| 199 | nbpointsB=size(grid_pix_B); |
---|
[576] | 200 | XB=round(grid_pix_B(:,1)+0.5);%index=position+0.5 rounded at the nearest integer value |
---|
| 201 | YB=round(grid_pix_B(:,2)+0.5); |
---|
[84] | 202 | unitcolumn=32*ones(size(XB)); |
---|
| 203 | Xchar=num2str(XB); |
---|
| 204 | blanc=char(unitcolumn); |
---|
| 205 | Ychar=num2str(YB); |
---|
| 206 | tete=['1 ' num2str(nbpointsB(1))]; |
---|
| 207 | txt=[Xchar blanc Ychar]; |
---|
| 208 | textgrid={tete;txt}; |
---|
| 209 | textout=char(textgrid); |
---|
[342] | 210 | Answer = msgbox_uvmat('INPUT_TXT','grid file name (*.grid)',fullfile(RootPath,'gridB.grid')); |
---|
[84] | 211 | dlmwrite(Answer,textout,''); |
---|
| 212 | msgbox_uvmat('CONFIRMATION',[Answer ' written as ASCII text file']); |
---|
| 213 | end |
---|
| 214 | |
---|
[576] | 215 | |
---|
| 216 | %------------------------------------------------------------------------ |
---|
| 217 | function [grid_pix_A,grid_pix_B,grid_phys]=get_grid(GUI) |
---|
| 218 | %------------------------------------------------------------------------ |
---|
[84] | 219 | grid_pix_B=[];%default |
---|
[576] | 220 | array_x=GUI.XMin:GUI.DX:GUI.XMax;% array of x values |
---|
| 221 | array_y=GUI.YMin:GUI.DY:GUI.YMax;% array of y values |
---|
| 222 | [grid_x,grid_y]=meshgrid(array_x,array_y);% matrices of x and y values |
---|
| 223 | grid_x=reshape(grid_x,[],1); %matrix of x values reshaped in line |
---|
| 224 | grid_y=reshape(grid_y,[],1);%matrix of y values reshaped in line |
---|
| 225 | % grid_z=zeros(nx_patch*ny_patch,1);% plane coordinates (TODO: 3D grids) |
---|
[2] | 226 | |
---|
[576] | 227 | %% check the input image A |
---|
| 228 | if ~exist(GUI.ImageA,'file') |
---|
[2] | 229 | msgbox_uvmat('ERROR',['input image file' imageA 'does not exist']) |
---|
| 230 | return |
---|
| 231 | end |
---|
[784] | 232 | [FileInfo,VideoObject]=get_file_info(GUI.ImageA); |
---|
[783] | 233 | switch FileInfo.FileType |
---|
[576] | 234 | case {'image','multimage','video','mmreader'}% case of input image or movie OK |
---|
| 235 | otherwise |
---|
| 236 | msgbox_uvmat('ERROR',['error: ' GUI.ImageA ' is not an image type recognized by Matlab ']) |
---|
| 237 | return |
---|
[2] | 238 | end |
---|
[576] | 239 | [RootPath,SubDir,RootFile,tild,tild,tild,tild,FileExt]=fileparts_uvmat(GUI.ImageA); |
---|
| 240 | |
---|
| 241 | %% transform to pixels if the grid is defined in phys coordinates |
---|
| 242 | grid_x_imaA=grid_x;%default grid in image A coordinates |
---|
| 243 | grid_y_imaA=grid_y; |
---|
| 244 | % MenuCoord=get(handles.CoordType,'String');% type of coordinates for grid definition, phys or pixel |
---|
| 245 | if strcmp(GUI.CoordType,'phys') |
---|
| 246 | fileAxml=fullfile(RootPath,[SubDir '.xml']);% new convention for xml name |
---|
| 247 | if ~exist(fileAxml,'file') |
---|
| 248 | fileAxml=[fullfile(RootPath,RootFile) '.xml'];% old convention for xml name |
---|
| 249 | end |
---|
| 250 | tsaiA=[];%default |
---|
| 251 | if exist(fileAxml,'file') |
---|
| 252 | [XmlDataA,errormsg]=imadoc2struct(fileAxml); |
---|
| 253 | if ~isempty(errormsg) |
---|
| 254 | msgbox_uvmat('ERROR',['error in ' fileAxml ': ' errormsg]) |
---|
| 255 | return |
---|
| 256 | end |
---|
| 257 | if isfield(XmlDataA,'GeometryCalib') |
---|
| 258 | tsaiA=XmlDataA.GeometryCalib; |
---|
| 259 | end |
---|
| 260 | end |
---|
| 261 | if isempty(tsaiA) |
---|
| 262 | msgbox_uvmat('WARNING','no geometric calibration available for image A, phys =pixel') |
---|
| 263 | else |
---|
| 264 | [grid_x_imaA,grid_y_imaA]=px_XYZ(tsaiA,grid_x,grid_y,GUI.Z); |
---|
| 265 | end |
---|
[2] | 266 | end |
---|
| 267 | |
---|
[576] | 268 | %% detect the grid points which are inside image A |
---|
[783] | 269 | A=read_image(GUI.ImageA,FileInfo.FileType,VideoObject,1); |
---|
[576] | 270 | npxA=size(A,2); |
---|
| 271 | npyA=size(A,1); |
---|
| 272 | flag=grid_x_imaA>=1 & grid_x_imaA<=npxA & grid_y_imaA>=1 & grid_y_imaA<=npyA;% ='true' inside the image |
---|
[2] | 273 | |
---|
[576] | 274 | %% detect the grid points which are inside image B if relevant (use for stereo PIV) |
---|
| 275 | if isfield(GUI,'ImageB') |
---|
| 276 | if ~exist(imageB,'file') |
---|
| 277 | msgbox_uvmat('ERROR',['input image file' GUI.ImageB 'does not exist']) |
---|
[2] | 278 | return |
---|
| 279 | end |
---|
[576] | 280 | [RootPathB,SubDirB,RootFileB,tild,tild,tild,tild,FileExt]=fileparts_uvmat(GUI.ImageB); |
---|
| 281 | fileBxml=fullfile(RootPathB,[SubDirB '.xml']);% new convention for xml name |
---|
| 282 | if ~exist(fileBxml,'file') |
---|
| 283 | fileBxml=[fullfile(RootPathB,RootFileB) '.xml'];% old convention for xml name |
---|
[2] | 284 | end |
---|
[576] | 285 | tsaiB=[];%default |
---|
| 286 | if exist(fileBxml,'file') |
---|
| 287 | [XmlDataB,errormsg]=imadoc2struct(fileBxml); |
---|
| 288 | if ~isempty(errormsg) |
---|
| 289 | msgbox_uvmat('ERROR',['error in ' fileAxml ': ' errormsg]) |
---|
| 290 | return |
---|
| 291 | end |
---|
| 292 | if isfield(XmlDataB,'GeometryCalib') |
---|
| 293 | tsaiB=XmlDataB.GeometryCalib; |
---|
| 294 | end |
---|
| 295 | end |
---|
| 296 | if isempty(tsaiB) |
---|
| 297 | msgbox_uvmat('WARNING','no geometric calibration available for image B, phys =pixel') |
---|
| 298 | grid_x_imaB=grid_x; |
---|
| 299 | grid_y_imaB=grid_y; |
---|
[2] | 300 | else |
---|
[576] | 301 | [grid_x_imaB,grid_y_imaB]=px_XYZ(tsaiB,grid_x,grid_y,GUI.Z); |
---|
[342] | 302 | end |
---|
[576] | 303 | B=imread(GUI.ImageB); |
---|
| 304 | npxB=size(B,2); |
---|
| 305 | npyB=size(B,1); |
---|
| 306 | flagB=grid_x_imaB>=1 & grid_x_imaB<=npxB & grid_y_imaB>=1 & grid_y_imaB<=npyB; |
---|
| 307 | flag=flagA & flagB; |
---|
| 308 | grid_pix_B(:,1)=round(grid_x_imaB(flag)); |
---|
| 309 | grid_pix_B(:,2)=round(grid_y_imaB(flag)); |
---|
[2] | 310 | end |
---|
| 311 | |
---|
[576] | 312 | grid_x_imaA=grid_x_imaA(flag); |
---|
| 313 | grid_y_imaA=grid_y_imaA(flag); |
---|
| 314 | grid_pix_A=[grid_x_imaA grid_y_imaA]; |
---|
| 315 | grid_x=grid_x(flag); |
---|
| 316 | grid_y=grid_y(flag); |
---|
| 317 | grid_phys=[grid_x grid_y]; |
---|
| 318 | |
---|
| 319 | |
---|
| 320 | function GetImageB_Callback(hObject, eventdata, handles) |
---|
| 321 | if isequal(get(handles.GetImageB,'Value'),1) |
---|
| 322 | set(handles.ImageB,'Visible','on') |
---|
| 323 | [FileName, PathName, filterindex] = uigetfile( ... |
---|
| 324 | {'*.*', 'All Files (*.*)'}, ... |
---|
| 325 | 'Pick the second image file',fileparts(fileparts(get(handles.ImageA,'String')))); |
---|
| 326 | ImageB=fullfile(PathName,FileName); |
---|
[784] | 327 | [FileInfo,tild,VideoObject]=get_file_info(ImageB); |
---|
[783] | 328 | switch FileInfo.FileType |
---|
[576] | 329 | case {'image','multimage','video','mmreader'}% case of input image or movie OK |
---|
| 330 | set(handles.ImageB,'String',ImageB) |
---|
| 331 | otherwise |
---|
| 332 | msgbox_uvmat('ERROR',['error: ' imageB ' is not an image type recognized by Matlab ']) |
---|
| 333 | return |
---|
| 334 | end |
---|
[73] | 335 | else |
---|
[576] | 336 | set(handles.ImageB,'Visible','off') |
---|
[73] | 337 | end |
---|
[2] | 338 | |
---|
| 339 | |
---|
[84] | 340 | %------------------------------------------------------------------------ |
---|
[2] | 341 | % --- Executes on button press in HELP. |
---|
| 342 | function HELP_Callback(hObject, eventdata, handles) |
---|
[84] | 343 | %------------------------------------------------------------------------ |
---|
[2] | 344 | path_to_uvmat=which ('uvmat');% check the path of uvmat |
---|
| 345 | pathelp=fileparts(path_to_uvmat); |
---|
[84] | 346 | helpfile=fullfile(pathelp,'uvmat_doc','uvmat_doc.html'); |
---|
[2] | 347 | if isempty(dir(helpfile)), errordlg('Please put the help file uvmat_doc.html in the directory UVMAT/UVMAT_DOC') |
---|
| 348 | else |
---|
| 349 | web([helpfile '#set_grid']) |
---|
| 350 | end |
---|
| 351 | |
---|
[84] | 352 | |
---|
| 353 | |
---|
[576] | 354 | function ImageA_Callback(hObject, eventdata, handles) |
---|
| 355 | % hObject handle to ImageA (see GCBO) |
---|
| 356 | % eventdata reserved - to be defined in a future version of MATLAB |
---|
| 357 | % handles structure with handles and user data (see GUIDATA) |
---|
| 358 | |
---|
| 359 | % Hints: get(hObject,'String') returns contents of ImageA as text |
---|
| 360 | % str2double(get(hObject,'String')) returns contents of ImageA as a double |
---|