source: trunk/src/series/relabel_i_j.m @ 216

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

correct bug on xml reading

File size: 18.2 KB
Line 
1% relabel_i_j: relabel an image series with two indices, and correct errors from the RDvision transfer program
2%----------------------------------------------------------------------
3function GUI_input=relabel_i_j(num_i1,num_i2,num_j1,num_j2,Series)
4%requests for the visibility of input windows in the GUI series  (activated directly by the selection in the menu ACTION)
5
6GUI_input={};
7
8%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%enable waitbar
9hGUI=findobj(allchild(0),'name','series');
10hseries=guidata(hGUI);%handles of the GUI series
11WaitbarPos=get(hseries.waitbar_frame,'Position');
12%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
13
14%% PARAMETERS (for RDvision system)
15display('RDvision system')
16first_label=0 %image numbers start from 0
17%errorfactor=1 %correct a factor of 2 in NbDk+1
18
19%% read imadoc
20RootPath=get(hseries.RootPath,'String');
21RootFile=get(hseries.RootFile,'String');
22if ~iscell(RootFile)
23    msgbox_uvmat('ERROR','please enter an input image series from RDVision system')%error message for xml file reading
24    return
25end
26basename=fullfile(RootPath{1},RootFile{1});
27[XmlData,warntext]=imadoc2struct([basename '.xml'])% read the xml file appended to the present function (containing bug corrections)
28if ~isempty(warntext)
29    msgbox_uvmat('ERROR',warntext)%error message for xml file reading
30end
31nbfield1=size(XmlData.Time,1);
32nbfield2=size(XmlData.Time,2);
33
34set(hseries.first_i,'String',num2str(first_label))% display the first image in the process
35set(hseries.last_i,'String',num2str(nbfield1*nbfield2-1+first_label))% display the last image in the process
36set(hseries.nb_field,'String',{num2str(nbfield1*nbfield2-1+first_label)})% display the total nbre of images
37SeriesData=get(hGUI,'UserData');
38if ~strcmp(SeriesData.NomType,'_000001')
39    msgbox_uvmat('WARNING','the input is not a file from RDvision: this function relabel_i_j has no action')%error message for directory creation
40    return
41else
42    msgbox_uvmat('CONFIRMATION','this function will relabel the file series from RDvision and correct the xml file')%error message for directory creation
43end
44
45%% stop program ther when it is selected in the menu (no run action)
46if ~exist('num_i1','var')
47    return
48end
49
50answer=msgbox_uvmat('CONFIRMATION',[num2str(nbfield1) ' bursts containing ' num2str(nbfield2) ' images each'])%error message for directory creation
51
52%% apply the image rescaling function 'level' (avoid bright particles)
53% answer=msgbox_uvmat('INPUT_Y-N','apply image rescaling function levels.m');
54% test_level=isequal(answer,'Yes');
55
56%% copy and adapt the xml file
57if exist([basename '.xml'],'file')
58    try
59    copyfile([basename '.xml'],[basename '.xml~']);% backup the xml file
60    catch
61    errormsg=lasterr
62            msgbox_uvmat('ERROR',errormsg);
63            return
64    end
65    t=xmltree([basename '.xml']);
66   
67    %update information on the first image name in the series
68    uid_Heading=find(t,'ImaDoc/Heading');
69    if isempty(uid_Heading)
70        [t,uid_Heading]=add(t,1,'element','Heading');
71    end   
72    uid_ImageName=find(t,'ImaDoc/Heading/ImageName');
73    ImageName=name_generator(basename,1,1,'.png','_i_j');
74    [pth,ImageName]=fileparts(ImageName);
75    ImageName=[ImageName '.png']
76    if isempty(uid_ImageName)
77       [t,uid_ImageName]=add(t,uid_Heading,'element','ImageName');
78    end
79    uid_value=children(t,uid_ImageName);
80    if isempty(uid_value)
81        t=add(t,uid_ImageName,'chardata',ImageName);%indicate  name of the first image, with ;png extension
82    else
83        t=set(t,uid_value(1),'value',ImageName);%indicate  name of the first image, with ;png extension
84    end
85   
86    %%%% correction RDvision %%%%
87    uid_NbDtj=find(t,'ImaDoc/Camera/BurstTiming/NbDtj');
88    t=set(t,uid_NbDtj,'value',num2str(XmlData.NbDtj));
89    uid_NbDtk=find(t,'ImaDoc/Camera/BurstTiming/NbDtk');
90    t=set(t,uid_NbDtk,'value',num2str(XmlData.NbDtk));
91    %%%
92   
93    save(t,[basename '.xml'])
94end
95
96%% main loop
97for ifile=1:nbfield1*nbfield2
98    update_waitbar(hseries.waitbar,WaitbarPos,ifile/(nbfield1*nbfield2))
99    filename=name_generator(basename,ifile-1,1,Series.FileExt,Series.NomType);
100    num_j=mod(ifile-1+first_label,nbfield2)+1;
101    num_i=floor((ifile-1+first_label)/nbfield2)+1;
102    filename_new=name_generator(basename,num_i,num_j,'.png','_i_j');
103    try
104        movefile(filename,filename_new);
105        [s,errormsg] = fileattrib(filename_new,'-w','a'); %set images to read only '-w' for all users ('a')
106        if ~s
107            msgbox_uvmat('ERROR',errormsg);
108            return
109        end
110    catch
111        errormsg=lasterr
112        msgbox_uvmat('ERROR',errormsg);
113        return
114    end
115    %     if test_level
116    %         A=imread(filename);
117    %         C=levels(A);
118    %         imwrite(C,filename_new)
119    %     else
120    %         try
121    %             copyfile(filename,filename_new);
122    %         catch
123    %             errormsg=lasterr
124    %             msgbox_uvmat('ERROR',errormsg);
125    %             return
126    %         end
127    %     end
128end
129
130
131function C=levels(A)
132%whos A;
133B=double(A(:,:,1));
134windowsize=round(min(size(B,1),size(B,2))/20);
135windowsize=floor(windowsize/2)*2+1;
136ix=[1/2-windowsize/2:-1/2+windowsize/2];%
137%del=np/3;
138%fct=exp(-(ix/del).^2);
139fct2=cos(ix/(windowsize-1)/2*pi/2);
140%Mfiltre=(ones(5,5)/5^2);
141%Mfiltre=fct2';
142Mfiltre=fct2'*fct2;
143Mfiltre=Mfiltre/(sum(sum(Mfiltre)));
144
145C=filter2(Mfiltre,B);
146C(:,1:windowsize)=C(:,windowsize)*ones(1,windowsize);
147C(:,end-windowsize+1:end)=C(:,end-windowsize+1)*ones(1,windowsize);
148C(1:windowsize,:)=ones(windowsize,1)*C(windowsize,:);
149C(end-windowsize+1:end,:)=ones(windowsize,1)*C(end-windowsize,:);
150C=tanh(B./(2*C));
151[n,c]=hist(reshape(C,1,[]),100);
152% figure;plot(c,n);
153
154[m,i]=max(n);
155c_max=c(i);
156[dummy,index]=sort(abs(c-c(i)));
157n=n(index);
158c=c(index);
159i_select = find(cumsum(n)<0.95*sum(n));
160if isempty(i_select)
161    i_select = 1:length(c);
162end
163c_select=c(i_select);
164n_select=n(i_select);
165cmin=min(c_select);
166cmax=max(c_select);
167C=(C-cmin)/(cmax-cmin)*256;
168C=uint8(C);
169
170%'imadoc2struct': reads the xml file for image documentation
171%------------------------------------------------------------------------
172% function [s,errormsg]=imadoc2struct(ImaDoc,option)
173%
174% OUTPUT:
175% s: structure representing ImaDoc
176%   s.Heading: information about the data hierarchical structure
177%   s.Time: matrix of times
178%   s.TimeUnit
179%  s.GeometryCalib: substructure containing the parameters for geometric calibration
180% errormsg: error message
181%
182% INPUT:
183% ImaDoc: full name of the xml input file with head key ImaDoc
184% option: ='GeometryCalib': read  the data of GeometryCalib, including source point coordinates
185
186function [s,errormsg]=imadoc2struct(ImaDoc,option)
187%% default input and output
188if ~exist('option','var')
189    option='*';
190end
191errormsg=[];%default
192s.Heading=[];%default
193s.Time=[]; %default
194s.TimeUnit=[]; %default
195s.GeometryCalib=[];
196tsai=[];%default
197
198%% opening the xml file
199if exist(ImaDoc,'file')~=2, errormsg=[ ImaDoc ' does not exist']; return;end;%input file does not exist
200try
201    t=xmltree(ImaDoc);
202catch
203    errormsg={[ImaDoc ' is not a valid xml file']; lasterr};
204    display(errormsg);
205    return
206end
207uid_root=find(t,'/ImaDoc');
208if isempty(uid_root), errormsg=[ImaDoc ' is not an image documentation file ImaDoc']; return; end;%not an ImaDoc .xml file
209
210
211%% Heading
212uid_Heading=find(t,'/ImaDoc/Heading');
213if ~isempty(uid_Heading),
214    uid_Campaign=find(t,'/ImaDoc/Heading/Campaign');
215    uid_Exp=find(t,'/ImaDoc/Heading/Experiment');
216    uid_Device=find(t,'/ImaDoc/Heading/Device');
217    uid_Record=find(t,'/ImaDoc/Heading/Record');
218    uid_FirstImage=find(t,'/ImaDoc/Heading/ImageName');
219    s.Heading.Campaign=get(t,children(t,uid_Campaign),'value');
220    s.Heading.Experiment=get(t,children(t,uid_Exp),'value');
221    s.Heading.Device=get(t,children(t,uid_Device),'value');
222    if ~isempty(uid_Record)
223        s.Heading.Record=get(t,children(t,uid_Record),'value');
224    end
225    s.Heading.ImageName=get(t,children(t,uid_FirstImage),'value');
226end
227
228%% Camera  and timing
229if strcmp(option,'*') || strcmp(option,'Camera')
230    uid_Camera=find(t,'/ImaDoc/Camera');
231    if ~isempty(uid_Camera)
232        uid_ImageSize=find(t,'/ImaDoc/Camera/ImageSize');
233        if ~isempty(uid_ImageSize);
234            ImageSize=get(t,children(t,uid_ImageSize),'value');
235            xindex=findstr(ImageSize,'x');
236            if length(xindex)>=2
237                s.Npx=str2double(ImageSize(1:xindex(1)-1));
238                s.Npy=str2double(ImageSize(xindex(1)+1:xindex(2)-1));
239            end
240        end
241        uid_TimeUnit=find(t,'/ImaDoc/Camera/TimeUnit');
242        if ~isempty(uid_TimeUnit)
243            s.TimeUnit=get(t,children(t,uid_TimeUnit),'value');
244        end
245        uid_BurstTiming=find(t,'/ImaDoc/Camera/BurstTiming');
246        if ~isempty(uid_BurstTiming)
247            for k=1:length(uid_BurstTiming)
248                subt=branch(t,uid_BurstTiming(k));%subtree under BurstTiming
249                % reading Dtk
250                Frequency=get_value(subt,'/BurstTiming/FrameFrequency',1);
251                Dtj=get_value(subt,'/BurstTiming/Dtj',[]);
252                Dtj=Dtj/Frequency;%Dtj converted from frame unit to TimeUnit (e.g. 's')
253                NbDtj=get_value(subt,'/BurstTiming/NbDtj',1);
254                %%%% correction RDvision %%%%
255                NbDtj=NbDtj/numel(Dtj);
256                s.NbDtj=NbDtj;
257                %%%%
258                Dti=get_value(subt,'/BurstTiming/Dti',[]);
259                Dti=Dti/Frequency;%Dtj converted from frame unit to TimeUnit (e.g. 's')
260                NbDti=get_value(subt,'/BurstTiming/NbDti',1);
261                Time_val=get_value(subt,'/BurstTiming/Time',0);%time in TimeUnit
262                if ~isempty(Dti)
263                    Dti=reshape(Dti'*ones(1,NbDti),NbDti*numel(Dti),1); %concatene Dti vector NbDti times
264                    Time_val=[Time_val;Time_val(end)+cumsum(Dti)];%append the times defined by the intervals  Dti
265                end
266                if ~isempty(Dtj)
267                    Dtj=reshape(Dtj'*ones(1,NbDtj),1,NbDtj*numel(Dtj)); %concatene Dtj vector NbDtj times
268                    Dtj=[0 Dtj];
269                    Time_val=Time_val*ones(1,numel(Dtj))+ones(numel(Time_val),1)*cumsum(Dtj);% produce a time matrix with Dtj
270                end
271                % reading Dtk
272                Dtk=get_value(subt,'/BurstTiming/Dtk',[]);
273                NbDtk=get_value(subt,'/BurstTiming/NbDtk',1);
274                %%%% correction RDvision %%%%
275                NbDtk=-1+(NbDtk+1)/(NbDti+1)
276                s.NbDtk=NbDtk;
277                %%%%%
278                if isempty(Dtk)
279                    s.Time=[s.Time;Time_val];
280                else
281                    for kblock=1:NbDtk+1
282                        Time_val_k=Time_val+(kblock-1)*Dtk;
283                        s.Time=[s.Time;Time_val_k];
284                    end
285                end
286            end
287        end
288    end
289end
290
291%% motor
292if strcmp(option,'*') || strcmp(option,'GeometryCalib')
293    uid_subtree=find(t,'/ImaDoc/TranslationMotor');
294    if length(uid_subtree)==1
295        subt=branch(t,uid_subtree);%subtree under GeometryCalib
296       [s.TranslationMotor,errormsg]=read_subtree(subt,{'Nbslice','ZStart','ZEnd'},[1 1 1],[1 1 1]);
297    end
298end
299%%  geometric calibration
300if strcmp(option,'*') || strcmp(option,'GeometryCalib')
301    uid_GeometryCalib=find(t,'/ImaDoc/GeometryCalib');
302    if ~isempty(uid_GeometryCalib)
303        if length(uid_GeometryCalib)>1
304            errormsg=['More than one GeometryCalib in ' filecivxml];
305            return
306        end
307        subt=branch(t,uid_GeometryCalib);%subtree under GeometryCalib
308        cont=get(subt,1,'contents');
309        if ~isempty(cont)
310            uid_CalibrationType=find(subt,'/GeometryCalib/CalibrationType');
311            if isequal(length(uid_CalibrationType),1)
312                tsai.CalibrationType=get(subt,children(subt,uid_CalibrationType),'value');
313            end
314            uid_CoordUnit=find(subt,'/GeometryCalib/CoordUnit');
315            if isequal(length(uid_CoordUnit),1)
316                tsai.CoordUnit=get(subt,children(subt,uid_CoordUnit),'value');
317            end
318            uid_fx_fy=find(subt,'/GeometryCalib/fx_fy');
319            focal=[];%default fro old convention (Reg Wilson)
320            if isequal(length(uid_fx_fy),1)
321                tsai.fx_fy=str2num(get(subt,children(subt,uid_fx_fy),'value'));
322            else %old convention (Reg Wilson)
323                uid_focal=find(subt,'/GeometryCalib/focal');
324                uid_dpx_dpy=find(subt,'/GeometryCalib/dpx_dpy');
325                uid_sx=find(subt,'/GeometryCalib/sx');
326                if ~isempty(uid_focal) && ~isempty(uid_dpx_dpy) && ~isempty(uid_sx)
327                    dpx_dpy=str2num(get(subt,children(subt,uid_dpx_dpy),'value'));
328                    sx=str2num(get(subt,children(subt,uid_sx),'value'));
329                    focal=str2num(get(subt,children(subt,uid_focal),'value'));
330                    tsai.fx_fy(1)=sx*focal/dpx_dpy(1);
331                    tsai.fx_fy(2)=focal/dpx_dpy(2);
332                end
333            end
334            uid_Cx_Cy=find(subt,'/GeometryCalib/Cx_Cy');
335            if ~isempty(uid_Cx_Cy)
336                tsai.Cx_Cy=str2num(get(subt,children(subt,uid_Cx_Cy),'value'));
337            end
338            uid_kc=find(subt,'/GeometryCalib/kc');
339            if ~isempty(uid_kc)
340                tsai.kc=str2double(get(subt,children(subt,uid_kc),'value'));
341            else %old convention (Reg Wilson)
342                uid_kappa1=find(subt,'/GeometryCalib/kappa1');
343                if ~isempty(uid_kappa1)&& ~isempty(focal)
344                    kappa1=str2double(get(subt,children(subt,uid_kappa1),'value'));
345                    tsai.kc=-kappa1*focal*focal;
346                end
347            end
348            uid_Tx_Ty_Tz=find(subt,'/GeometryCalib/Tx_Ty_Tz');
349            if ~isempty(uid_Tx_Ty_Tz)
350                tsai.Tx_Ty_Tz=str2num(get(subt,children(subt,uid_Tx_Ty_Tz),'value'));
351            end
352            uid_R=find(subt,'/GeometryCalib/R');
353            if ~isempty(uid_R)
354                RR=get(subt,children(subt,uid_R),'value');
355                if length(RR)==3
356                    tsai.R=[str2num(RR{1});str2num(RR{2});str2num(RR{3})];
357                end
358            end
359           
360            %look for laser plane definitions
361            uid_Angle=find(subt,'/GeometryCalib/PlaneAngle');
362            uid_Pos=find(subt,'/GeometryCalib/SliceCoord');
363            if isempty(uid_Pos)
364                uid_Pos=find(subt,'/GeometryCalib/PlanePos');%old convention
365            end
366            if ~isempty(uid_Angle)
367                tsai.PlaneAngle=str2num(get(subt,children(subt,uid_Angle),'value'));
368            end
369            if ~isempty(uid_Pos)
370                for j=1:length(uid_Pos)
371                    tsai.SliceCoord(j,:)=str2num(get(subt,children(subt,uid_Pos(j)),'value'));
372                end
373                uid_DZ=find(subt,'/GeometryCalib/SliceDZ');
374                uid_NbSlice=find(subt,'/GeometryCalib/NbSlice');
375                if ~isempty(uid_DZ) && ~isempty(uid_NbSlice)
376                    DZ=str2double(get(subt,children(subt,uid_DZ),'value'));
377                    NbSlice=get(subt,children(subt,uid_NbSlice),'value');
378                    if isequal(NbSlice,'volume')
379                        tsai.NbSlice='volume';
380                        NbSlice=NbDtj+1;
381                    else
382                        tsai.NbSlice=str2double(NbSlice);
383                    end
384                    tsai.SliceCoord=ones(NbSlice,1)*tsai.SliceCoord+DZ*(0:NbSlice-1)'*[0 0 1];
385                end
386            end   
387            tsai.SliceAngle=get_value(subt,'/GeometryCalib/SliceAngle',[0 0 0]);
388            tsai.VolumeScan=get_value(subt,'/GeometryCalib/VolumeScan','n');
389            tsai.InterfaceCoord=get_value(subt,'/GeometryCalib/InterfaceCoord',[0 0 0]);
390            tsai.RefractionIndex=get_value(subt,'/GeometryCalib/RefractionIndex',1);
391           
392            if strcmp(option,'GeometryCalib')
393                tsai.PointCoord=get_value(subt,'/GeometryCalib/SourceCalib/PointCoord',[0 0 0 0 0]);
394            end
395            s.GeometryCalib=tsai;
396        end
397    end
398end
399
400%--------------------------------------------------
401%  read a subtree
402% INPUT:
403% t: xltree
404% head_element: head elelemnt of the subtree
405% Data, structure containing
406%    .Key: element name
407%    .Type: type of element ('charg', 'float'....)
408%    .NbOccur: nbre of occurrence, NaN for un specified number
409function [s,errormsg]=read_subtree(subt,Data,NbOccur,NumTest)
410%--------------------------------------------------
411s=[];%default
412errormsg='';
413head_element=get(subt,1,'name');
414    cont=get(subt,1,'contents');
415    if ~isempty(cont)
416        for ilist=1:length(Data)
417            uid_key=find(subt,[head_element '/' Data{ilist}]);
418            if ~isequal(length(uid_key),NbOccur(ilist))
419                errormsg=['wrong number of occurence for ' Data{ilist}];
420                return
421            end
422            for ival=1:length(uid_key)
423                val=get(subt,children(subt,uid_key(ival)),'value');
424                if ~NumTest(ilist)
425                    eval(['s.' Data{ilist} '=val;']);
426                else
427                    eval(['s.' Data{ilist} '=str2double(val);'])
428                end
429            end
430        end
431    end
432
433
434%--------------------------------------------------
435%  read an xml element
436function val=get_value(t,label,default)
437%--------------------------------------------------
438val=default;
439uid=find(t,label);%find the element iud(s)
440if ~isempty(uid) %if the element named label exists
441   uid_child=children(t,uid);%find the children
442   if ~isempty(uid_child)
443       data=get(t,uid_child,'type');%get the type of child
444       if iscell(data)% case of multiple element
445           for icell=1:numel(data)
446               val_read=str2num(get(t,uid_child(icell),'value'));
447               if ~isempty(val_read)
448                   val(icell,:)=val_read;
449               end
450           end
451%           val=val';
452       else % case of unique element value
453           val_read=str2num(get(t,uid_child,'value'));
454           if ~isempty(val_read)
455               val=val_read;
456           else
457              val=get(t,uid_child,'value');%char string data
458           end
459       end
460   end
461end
Note: See TracBrowser for help on using the repository browser.