source: trunk/src/set_grid.m @ 418

Last change on this file since 418 was 342, checked in by sommeria, 13 years ago

various bugs corrected

File size: 14.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 23-Apr-2010 15:44:47
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% if nargin & isstr(varargin{1})
35%     gui_State.gui_Callback = str2func(varargin{1});
36% end
37if nargin && ischar(varargin{1})
38    gui_State.gui_Callback = str2func(varargin{1});
39end
40if nargout
41    [varargout{1:nargout}] = gui_mainfcn(gui_State, varargin{:});
42else
43    gui_mainfcn(gui_State, varargin{:});
44end
45% End initialization code - DO NOT PLOT
46
47%-------------------------------------------------------------------
48% --- Executes just before set_grid is made visible.
49%INPUT:
50% handles: handles of the set_grid interface elements
51%'IndexObj': index of the object (on the UvData list) that set_grid will modify
52%        if =[] or absent: index still undefined (create mode in uvmat)
53%        if=0; no associated object (used for series), the button 'PLOT' is  then unvisible
54%'data': read from an existing object selected in the interface
55%      .TITLE : class of object ('POINTS','LINE',....)
56%      .DX,DY,DZ; meshes for regular grids
57%      .Coord: object position coordinates
58%      .ParentButton: handle of the uicontrol object calling the interface
59% PlotHandles: set of handles of the elements contolling the plotting of the projected field:
60%  if =[] or absent, no plot (mask mode in uvmat)
61% parameters on the uvmat interface (obtained by 'get_plot_handle.m')
62function set_grid_OpeningFcn(hObject, eventdata, handles,inputfile,CoordType)
63
64% Choose default command line output for set_grid
65handles.output = hObject;
66
67% Update handles structure
68guidata(hObject, handles);
69
70%default
71% set(hObject,'Unit','Normalized')% set the unit normalized to the screen size
72% set(hObject,'Position',[0.7 0.1 0.25 0.5])%set the position of the set_grid interface
73set(hObject,'DeleteFcn',@closefcn)
74% set(handles.TITLE,'Value',1)
75%set(handles.ObjectStyle,'Value',1)
76%set(handles.ProjMode,'Value',1)
77set(handles.MenuCoord,'ListboxTop',1)
78set(handles.MenuCoord,'Value',1);
79set(handles.MenuCoord,'String',{'phys';'px'});
80if exist('inputfile','var')& ~isempty(inputfile)
81   set(handles.image_1,'String',inputfile)
82   set(handles.image_2,'String',inputfile)
83end
84if exist('CoordType','var')
85    if strcmp(CoordType,'px')
86        set(handles.MenuCoord,'Value',2)
87    end
88end
89
90% --- Outputs from this function are returned to the command line.
91function varargout = set_grid_OutputFcn(hObject, eventdata, handles)
92% Get default command line output from handles structure
93varargout{1} = handles.output;
94varargout{2}=handles;
95
96
97%-----------------------------------------------------
98% --- Executes on button press in import.
99function import_Callback(hObject, eventdata, handles)
100%get the object file
101oldfile='';
102huvmat=findobj('Tag','uvmat');
103if isempty(huvmat)
104    huvmat=findobj(allchild(0),'Name','series');
105end
106hchild=get(huvmat,'Children');
107hrootpath=findobj(hchild,'Tag','RootPath');
108oldfile=get(hrootpath,'String');
109if iscell(oldfile)
110    oldfile=oldfile{1};
111end
112%[FileName,PathName] = uigetfile('*.civ','Select a .civ file',oldfile)
113[FileName, PathName, filterindex] = uigetfile( ...
114       {'*.xml;*.mat', ' (*.xml,*.mat)';
115       '*.xml',  '.xml files '; ...
116        '*.mat',  '.mat matlab files '}, ...
117        'Pick a file',oldfile);
118fileinput=[PathName FileName];%complete file name
119testblank=findstr(fileinput,' ');%look for blanks
120if ~isempty(testblank)
121    errordlg('forbidden input file name: contain blanks')
122    return
123end
124sizf=size(fileinput);
125if (~ischar(fileinput)|~isequal(sizf(1),1)),return;end
126
127%read the file
128 t=xmltree(fileinput);
129 s=convert(t);
130testmode=0;
131if isfield(s,'ProjMode')
132        menu=get(handles.ProjMode,'String');
133        for iline=1:length(menu)
134            if isequal(menu{iline},s.ProjMode)
135                set(handles.ProjMode,'Value',iline)
136                testmode=1;
137                break
138            end
139        end
140end
141
142ProjMode_Callback(hObject, eventdata, handles);%visualize the appropriate edit boxes
143if isfield(s,'CoordType')
144    if isequal(s.CoordType,'phys')
145        set(handles.MenuCoord,'Value',1)
146    elseif isequal(s.CoordType,'px')
147        set(handles.MenuCoord,'Value',2)
148    else
149        warndlg('unknown CoordType (px or phys) in set_grid.m')
150    end
151end
152if isfield(s,'XMax')
153    set(handles.XMax,'String',s.XMax)
154end
155if isfield(s,'XMin')
156    set(handles.XMin,'String',s.XMin)
157end
158if isfield(s,'YMax')
159    set(handles.YMax,'String',s.YMax)
160end
161if isfield(s,'YMin')
162    set(handles.YMin,'String',s.YMin)
163end
164if isfield(s,'DX')
165    set(handles.DX,'String',s.DX)
166end
167if isfield(s,'DY')
168    set(handles.DY,'String',s.DY)
169end
170if ~isfield(s,'Coord')
171    XObject='0';%default
172    YObject='0';
173elseif ischar(s.Coord)
174    line=str2num(s.Coord);
175    XObject=num2str(line(1));
176    YObject=num2str(line(2));
177else
178    for i=1:length(s.Coord)
179        line=str2num(s.Coord{i});
180        XObject{i}=num2str(line(1));
181        YObject{i}=num2str(line(2));
182    end
183end
184set(handles.XObject,'String',XObject)
185set(handles.YObject,'String',YObject)
186%METTRA A JOUR ASPECT DE L'INTERFACE (COMME set_grid_Opening
187
188%----------------------------------------------------
189% executed when closing: set the parent interface button to value 0
190function closefcn(gcbo,eventdata)
191huvmat=findobj(allchild(0),'Name','uvmat');%find the current uvmat interface handle
192parent_button=findobj(huvmat,'Tag','grid');
193if ~isempty(parent_button)
194    set(parent_button,'Value',0)%put unactivated buttons to green
195    tag=get(parent_button,'Tag');
196    if isequal(tag,'edit')
197        set(parent_button,'BackgroundColor',[0.7 0.7 0.7]);
198    else
199        set(parent_button,'BackgroundColor',[0 1 0]);
200    end
201end
202
203%-----------------------------------------------------------------------
204% --- Executes on button press in plot: PLOT the defined object and its projected field
205function plot_Callback(hObject, eventdata, handles)
206grid_pix_A=get_grid(handles);
207huvmat=uvmat(get(handles.image_1,'String'));
208hhuvmat=guidata(huvmat);
209set(hhuvmat.transform_fct,'Value',1)
210uvmat('run0_Callback',hObject,eventdata,hhuvmat); %file input with xml reading  in uvmat
211axes(hhuvmat.axes3);
212hold on
213plot(grid_pix_A(:,1),grid_pix_A(:,2),'.')
214
215% --- Executes on button press in plot_2.
216function plot_2_Callback(hObject, eventdata, handles)
217[grid_pix_A,grid_pix_B]=get_grid(handles);
218huvmat=uvmat(get(handles.image_2,'String'));
219hhuvmat=guidata(huvmat);
220set(hhuvmat.transform_fct,'Value',1)
221uvmat('run0_Callback',hObject,eventdata,hhuvmat); %file input with xml reading  in uvmat
222axes(hhuvmat.axes3);
223hold on
224plot(grid_pix_B(:,1),grid_pix_B(:,2),'.')
225
226
227
228% --- Executes on button press in MenuCoord.
229function MenuCoord_Callback(hObject, eventdata, handles)
230
231
232%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
233% --- Executes on button press in delete.
234function delete_Callback(hObject, eventdata, handles)
235
236%SetData=get(gcbf,'UserData');%get the interface data
237%IndexObj=SetData.IndexObj;
238huvmat=findobj('Name','uvmat');%find the current uvmat interface handle
239UvData=get(huvmat,'UserData');%Data associated to the current uvmat interface
240if isfield(UvData,'CurrentObjectIndex')
241    IndexObj=UvData.CurrentObjectIndex;
242else
243    IndexObj=[];
244end
245delete_object(IndexObj);
246
247
248% ------------------------------------------------------
249function save_Callback(hObject, eventdata, handles)
250% ------------------------------------------------------
251[grid_pix_A,grid_pix_B]=get_grid(handles);
252
253 %ECRIRE FICHIERS
254nbpointsA=size(grid_pix_A);
255XA=grid_pix_A(:,1);
256YA=grid_pix_A(:,2);
257unitcolumn=32*ones(size(XA));
258Xchar=num2str(XA);
259blanc=char(unitcolumn);
260Ychar=num2str(YA);
261tete=['1 ' num2str(nbpointsA(1))];
262txt=[Xchar blanc Ychar];
263textgrid={tete;txt};
264textout=char(textgrid);
265imageA=get(handles.image_1,'String');
266RootPath=fileparts_uvmat(imageA);
267%[Pathsub]=name2display(imageA);
268Answer = msgbox_uvmat('INPUT_TXT','grid file name (*.grid)',fullfile(RootPath,'gridA.grid'));
269% Answer = inputdlg('grid file name (*.grid)',' ',1,{fullfile(Pathsub,'gridA.grid')},'on');
270dlmwrite(Answer,textout,'');
271msgbox_uvmat('CONFIRMATION',[Answer ' written as ASCII text file']);
272if ~isempty(grid_pix_B)
273    nbpointsB=size(grid_pix_B);
274    XB=grid_pix_B(:,1);
275    YB=grid_pix_B(:,2);
276    unitcolumn=32*ones(size(XB));
277    Xchar=num2str(XB);
278    blanc=char(unitcolumn);
279    Ychar=num2str(YB);
280    tete=['1 ' num2str(nbpointsB(1))];
281    txt=[Xchar blanc Ychar];
282    textgrid={tete;txt};
283    textout=char(textgrid);
284    Answer = msgbox_uvmat('INPUT_TXT','grid file name (*.grid)',fullfile(RootPath,'gridB.grid'));
285    dlmwrite(Answer,textout,'');
286    msgbox_uvmat('CONFIRMATION',[Answer ' written as ASCII text file']);
287end
288
289%-------------------------
290function [grid_pix_A,grid_pix_B]=get_grid(handles)
291%Object=read_set_object(handles);%read the set_grid interface;
292grid_pix_B=[];%default
293DX=str2num(get(handles.DX,'String'));
294DY=str2num(get(handles.DY,'String'));
295XMin=str2num(get(handles.XMin,'String'));
296XMax=str2num(get(handles.XMax,'String'));
297YMin=str2num(get(handles.YMin,'String'));
298YMax=str2num(get(handles.YMax,'String'));
299array_realx=XMin:DX:XMax;
300array_realy=YMin:DY:YMax;
301nx_patch=length(array_realx);
302ny_patch=length(array_realy);
303[grid_realx,grid_realy]=meshgrid(array_realx,array_realy);
304grid_real(:,1)=reshape(grid_realx,nx_patch*ny_patch,1);
305grid_real(:,2)=reshape(grid_realy,nx_patch*ny_patch,1);
306grid_real(:,3)=zeros(nx_patch*ny_patch,1);
307 
308imageA=get(handles.image_1,'String');
309imageB=get(handles.image_2,'String');
310testB=1;
311if isempty(imageA) || isequal(imageA,'')
312    if isempty(imageB) || isequal(imageB,'')
313        msgbox_uvmat('ERROR','at least one image file name must be introduced')
314    else
315        imageA=imageB;
316        testB=0;
317    end
318end
319if isempty(imageB) || isequal(imageB,'') || isequal(imageA,imageB)
320    testB=0;
321end
322
323testexist=exist(imageA,'file');
324if isequal(testexist,0)
325    msgbox_uvmat('ERROR',['input image file' imageA 'does not exist'])
326    return
327end
328%[Pathsub,RootFile,field_count,str2,str_a,str_b,FileExt,NomType,SubDir]=name2display(imageA);
329[RootPath,~,RootFile,~,~,~,~,FileExt]=fileparts_uvmat(imageA);
330form=imformats(FileExt(2:end));
331if isempty(form)% if the extension corresponds to an image format recognized by Matlab
332     msgbox_uvmat('ERROR',['error: ' imageA ' is not an image name recognized by Matlab '])
333     return
334end
335fileAxml=[fullfile(RootPath,RootFile) '.xml'];
336[XmlDataA,error]=imadoc2struct(fileAxml);
337if isfield(XmlDataA,'GeometryCalib')
338     tsaiA=XmlDataA.GeometryCalib;
339 else
340     msgbox_uvmat('WARNING','no geometric calibration available for image A')
341     tsaiA=[];
342end
343MenuCoord=get(handles.MenuCoord,'String');
344val=get(handles.MenuCoord,'Value');
345if isempty(tsaiA)||strcmp(MenuCoord{val},'px')
346    grid_imaA(:,1)=grid_real(:,1);
347    grid_imaA(:,2)=grid_real(:,2);
348else
349    [grid_imaA(:,1),grid_imaA(:,2)]=px_XYZ(tsaiA,grid_real(:,1),grid_real(:,2),0);
350end
351    A=imread(imageA);
352   siz=size(A);
353   npxA=siz(2);
354   npyA=siz(1);
355
356flagA=grid_imaA(:,1)>0 & grid_imaA(:,1)<npxA & grid_imaA(:,2)>0 & grid_imaA(:,2)<npyA;
357
358if testB
359    testexist=exist(imageB,'file');
360    if isequal(testexist,0)
361        msgbox_uvmat('ERROR',['input image file' imageB 'does not exist'])
362        return
363    end
364    %[RootPath,RootFile,field_count,str2,str_a,str_b,FileExt,NomType,SubDir]=name2display(imageB);
365    [RootPath,~,RootFile,~,~,~,~,FileExt]=fileparts_uvmat(imageB);
366    form=imformats(FileExt(2:end));
367    if isempty(form)% if the extension corresponds to an image format recognized by Matlab
368        msgbox_uvmat('ERROR',['error: ' imageB ' is not an image name recognized by Matlab '])
369        return
370    end
371    fileBxml=[fullfile(RootPath,RootFile) '.xml'];
372    [XmlDataB,error]=imadoc2struct(fileBxml);
373    if isfield(XmlDataB,'GeometryCalib')
374        tsaiB=XmlDataB.GeometryCalib;
375    else
376        msgbox_uvmat('WARNING','no geometric calibration available for image B')
377        tsaiB=[];
378    end
379    %[error,Heading,NomType_read,ext_ima_read,time,TimeUnit,mode,NbSlice,...
380    %     npxB,npyB,tsaiB]=read_imadoc(fileBxml,0);
381    [grid_imaB(:,1),grid_imaB(:,2)]=px_XYZ(tsaiB,grid_real(:,1),grid_real(:,2),0);
382    %     if isempty(npxB)|isempty(npyB)
383    B=imread(imageB);
384    siz=size(B);
385    npxB=siz(2);
386    npyB=siz(1);
387    %     end
388    flagB=grid_imaB(:,1)>0 & grid_imaB(:,1)<npxB & grid_imaB(:,2)>0 & grid_imaB(:,2)<npyB;
389end
390if testB
391    ind_good=find(flagA==1&flagB==1);
392    XimaB=grid_imaB(ind_good,1);
393    YimaB=grid_imaB(ind_good,2);
394else
395    ind_good=find(flagA==1);
396end
397XimaA=grid_imaA(ind_good,1);
398YimaA=grid_imaA(ind_good,2);
399
400grid_real_x=grid_real(ind_good,1);
401grid_real_y=grid_real(ind_good,2);
402nx_patch_new=length(grid_real_x);
403grid_real2(:,1)=grid_real_x;
404grid_real2(:,2)=grid_real_y;
405grid_real2(:,3)=zeros(nx_patch_new,1);
406if isempty(tsaiA)||strcmp(MenuCoord{val},'px')
407    grid_pix_A(:,1)=grid_real2(:,1);
408   grid_pix_A(:,2)= grid_real2(:,2);
409else
410    [grid_pix_A(:,1),grid_pix_A(:,2)]=px_XYZ(tsaiA,grid_real2(:,1),grid_real2(:,2));
411end
412if testB
413    [grid_pix_B(:,1),grid_pix_B(:,2)]=px_XYZ(tsaiB,grid_real2(:,1),grid_real2(:,2));
414end
415
416
417
418%------------------------------------------------------------------------
419% --- Executes on button press in HELP.
420function HELP_Callback(hObject, eventdata, handles)
421%------------------------------------------------------------------------
422path_to_uvmat=which ('uvmat');% check the path of uvmat
423pathelp=fileparts(path_to_uvmat);
424helpfile=fullfile(pathelp,'uvmat_doc','uvmat_doc.html');
425if isempty(dir(helpfile)), errordlg('Please put the help file uvmat_doc.html in the directory UVMAT/UVMAT_DOC')
426else
427web([helpfile '#set_grid'])   
428end
429
430
431
Note: See TracBrowser for help on using the repository browser.