source: trunk/src/series/ima2vol.m @ 73

Last change on this file since 73 was 73, checked in by sommeria, 14 years ago

ima2vol: provides volume images in 16 bit (presently needed for the software 3D3C)
set_grid: small bug repairs
uvmat: small bug repairs.
civ: bug repair following previous changes (RUN and BATCH functions merged)
plot_field: cleaning of parameter transmission. Introduction of a parameter handle of a text box (not yet used)

File size: 4.2 KB
Line 
1% relabel_i_j: relabel an image series with two indices, according to the time matrix given by ImaDoc
2%----------------------------------------------------------------------
3function GUI_input=ima2vol(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)
5if ~exist('num_i1','var')
6    GUI_input={};
7    return %exit the function
8end
9
10%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%enable waitbar
11hseries=guidata(Series.hseries);%handles of the GUI series
12WaitbarPos=get(hseries.waitbar_frame,'Position');
13%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
14
15basename=fullfile(Series.RootPath,Series.RootFile) ;
16
17%create dir of the new images
18[dir_images,namebase]=fileparts(basename);
19[path,subdir_ima]=fileparts(dir_images);
20curdir=pwd;
21cd(path);
22mkdir([subdir_ima '_vol']);
23cd(curdir);
24basename_new=fullfile(path,[subdir_ima '_vol'],namebase);
25
26% read imadoc
27[XmlData,warntext]=imadoc2struct([basename '.xml']);
28nbfield1=size(XmlData.Time,1)
29nbfield2=size(XmlData.Time,2)
30
31answer=msgbox_uvmat('INPUT_Y-N','apply image rescaling function levels.m')
32test_level=isequal(answer,'Yes')
33
34%copy the xml file
35if exist([basename '.xml'],'file')
36    copyfile([basename '.xml'],[basename_new '.xml']);% copy the .civ file
37    t=xmltree([basename_new '.xml']);
38   
39    %update information on the first image name in the series
40    uid_Heading=find(t,'ImaDoc/Heading');
41    if isempty(uid_Heading)
42        [t,uid_Heading]=add(t,1,'element','Heading');
43    end   
44    uid_ImageName=find(t,'ImaDoc/Heading/ImageName');
45    ImageName=name_generator(basename_new,num_i1(1),num_j1(1),'.png','_i_j');
46    [pth,ImageName]=fileparts(ImageName);
47    ImageName=[ImageName '.png']
48    if isempty(uid_ImageName)
49       [t,uid_ImageName]=add(t,uid_Heading,'element','ImageName');
50    end
51    uid_value=children(t,uid_ImageName);
52    if isempty(uid_value)
53        t=add(t,uid_ImageName,'chardata',ImageName)%indicate  name of the first image, with ;png extension
54    else
55        t=set(t,uid_value(1),'value',ImageName)%indicate  name of the first image, with ;png extension
56    end 
57    save(t,[basename_new '.xml'])
58end
59
60%main loop
61 vol=[];
62 for ifile=1:nbfield1*nbfield2
63     update_waitbar(hseries.waitbar,WaitbarPos,ifile/(nbfield1*nbfield2))
64     stopstate=get(hseries.RUN,'BusyAction');
65     if isequal(stopstate,'queue') % enable STOP command
66         filename=name_generator(basename,ifile-1,1,Series.FileExt,Series.NomType);
67         num_j=mod(ifile-1,nbfield2)+1;
68         num_i=floor((ifile-1)/nbfield2)+1;
69         A=imread(filename);
70         Atype=class(A);
71         if test_level
72             A=levels(A,16);
73             display(num2str(num_i))
74         end
75         vol=[vol;A];%concacene along y
76         if num_j==nbfield2
77             filename_new=name_generator(basename_new,num_i,1,'.vol','_i');
78             imwrite(vol,filename_new,'png','BitDepth',16)% WRITE IN 16 bits: needed for the current version of civ3C3D
79             display([filename_new 'written (16bits image)'])
80             vol=[];
81         end
82     end
83 end
84
85
86
87function C=levels(A,bitdepth)
88%whos A;
89B=double(A(:,:,1));
90windowsize=round(min(size(B,1),size(B,2))/20);
91windowsize=floor(windowsize/2)*2+1;
92ix=[1/2-windowsize/2:-1/2+windowsize/2];%
93%del=np/3;
94%fct=exp(-(ix/del).^2);
95fct2=cos(ix/(windowsize-1)/2*pi/2);
96%Mfiltre=(ones(5,5)/5^2);
97%Mfiltre=fct2';
98Mfiltre=fct2'*fct2;
99Mfiltre=Mfiltre/(sum(sum(Mfiltre)));
100
101C=filter2(Mfiltre,B);
102C(:,1:windowsize)=C(:,windowsize)*ones(1,windowsize);
103C(:,end-windowsize+1:end)=C(:,end-windowsize+1)*ones(1,windowsize);
104C(1:windowsize,:)=ones(windowsize,1)*C(windowsize,:);
105C(end-windowsize+1:end,:)=ones(windowsize,1)*C(end-windowsize,:);
106C=tanh(B./(2*C));
107[n,c]=hist(reshape(C,1,[]),100);
108% figure;plot(c,n);
109
110[m,i]=max(n);
111c_max=c(i);
112[dummy,index]=sort(abs(c-c(i)));
113n=n(index);
114c=c(index);
115i_select = find(cumsum(n)<0.95*sum(n));
116if isempty(i_select)
117    i_select = 1:length(c);
118end
119c_select=c(i_select);
120n_select=n(i_select);
121cmin=min(c_select);
122cmax=max(c_select);
123if isequal(bitdepth,16)
124    C=((C-cmin)/(cmax-cmin))*256*256;
125    C=uint16(C);
126else
127    C=((C-cmin)/(cmax-cmin))*256;
128    C=uint8(C);
129end
Note: See TracBrowser for help on using the repository browser.