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

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

bug corrected: update the xml file

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