source: trunk/src/set_grid.m @ 785

Last change on this file since 785 was 784, checked in by sommeria, 10 years ago

adpat uvmat to reading data from rdvision

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