1 | % relabel_i_j: relabel an image series with two indices, according to the time matrix given by ImaDoc
|
---|
2 | %----------------------------------------------------------------------
|
---|
3 | function GUI_input=ima_levels(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 | if ~exist('num_i1','var')
|
---|
6 | GUI_input={};
|
---|
7 | return %exit the function
|
---|
8 | end
|
---|
9 |
|
---|
10 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%enable waitbar
|
---|
11 | hseries=guidata(Series.hseries);%handles of the GUI series
|
---|
12 | WaitbarPos=get(hseries.waitbar_frame,'Position');
|
---|
13 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
---|
14 |
|
---|
15 | basename=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);
|
---|
20 | dircur=pwd;
|
---|
21 | cd(path);
|
---|
22 | mkdir([subdir_ima '_levels']);
|
---|
23 | [xx,msg2] = fileattrib(subdir_ima,'+w','g'); %yield writing access (+w) to user group (g)
|
---|
24 | if ~strcmp(msg2,'')
|
---|
25 | msgbox_uvmat('ERROR',['pb of permission for ' subdir_ima ': ' msg2])%error message for directory creation
|
---|
26 | cd(dircur)
|
---|
27 | return
|
---|
28 | end
|
---|
29 | cd(dircur);
|
---|
30 | basename_new=fullfile(path,[subdir_ima '_levels'],namebase);
|
---|
31 |
|
---|
32 | % read imadoc
|
---|
33 | %[XmlData,warntext]=imadoc2struct([basename '.xml']);
|
---|
34 | % nbfield1=size(XmlData.Time,1);
|
---|
35 | % nbfield2=size(XmlData.Time,2);
|
---|
36 |
|
---|
37 | msgbox_uvmat('CONFIRMATION','apply image rescaling function levels.m ');
|
---|
38 |
|
---|
39 | %copy the xml file
|
---|
40 | if exist([basename '.xml'],'file')
|
---|
41 | copyfile([basename '.xml'],[basename_new '.xml']);% copy the .civ file
|
---|
42 | end
|
---|
43 |
|
---|
44 | %main loop
|
---|
45 | nbfield=size(num_i1,2);
|
---|
46 | nbfield2=size(num_i1,1);
|
---|
47 | for ifile=1:nbfield
|
---|
48 | update_waitbar(hseries.waitbar,WaitbarPos,ifile/nbfield)
|
---|
49 | stopstate=get(hseries.RUN,'BusyAction');
|
---|
50 | if isequal(stopstate,'queue') % enable STOP command
|
---|
51 | for jfile=1:nbfield2
|
---|
52 | filename=name_generator(basename,num_i1(jfile,ifile),num_j1(jfile,ifile),Series.FileExt,Series.NomType);
|
---|
53 | filename_new=name_generator(basename_new,num_i1(jfile,ifile),num_j1(jfile,ifile),'.png',Series.NomType);
|
---|
54 | A=imread(filename);
|
---|
55 | C=levels(A);
|
---|
56 | imwrite(C,filename_new)
|
---|
57 | end
|
---|
58 | end
|
---|
59 | end
|
---|
60 |
|
---|
61 |
|
---|
62 |
|
---|
63 | function C=levels(A)
|
---|
64 | %whos A;
|
---|
65 | B=double(A(:,:,1));
|
---|
66 | windowsize=round(min(size(B,1),size(B,2))/20);
|
---|
67 | windowsize=floor(windowsize/2)*2+1;
|
---|
68 | ix=[1/2-windowsize/2:-1/2+windowsize/2];%
|
---|
69 | %del=np/3;
|
---|
70 | %fct=exp(-(ix/del).^2);
|
---|
71 | fct2=cos(ix/(windowsize-1)/2*pi/2);
|
---|
72 | %Mfiltre=(ones(5,5)/5^2);
|
---|
73 | %Mfiltre=fct2';
|
---|
74 | Mfiltre=fct2'*fct2;
|
---|
75 | Mfiltre=Mfiltre/(sum(sum(Mfiltre)));
|
---|
76 |
|
---|
77 | C=filter2(Mfiltre,B);
|
---|
78 | C(:,1:windowsize)=C(:,windowsize)*ones(1,windowsize);
|
---|
79 | C(:,end-windowsize+1:end)=C(:,end-windowsize+1)*ones(1,windowsize);
|
---|
80 | C(1:windowsize,:)=ones(windowsize,1)*C(windowsize,:);
|
---|
81 | C(end-windowsize+1:end,:)=ones(windowsize,1)*C(end-windowsize,:);
|
---|
82 | C=tanh(B./(2*C));
|
---|
83 | [n,c]=hist(reshape(C,1,[]),100);
|
---|
84 | % figure;plot(c,n);
|
---|
85 |
|
---|
86 | [m,i]=max(n);
|
---|
87 | c_max=c(i);
|
---|
88 | [dummy,index]=sort(abs(c-c(i)));
|
---|
89 | n=n(index);
|
---|
90 | c=c(index);
|
---|
91 | i_select = find(cumsum(n)<0.95*sum(n));
|
---|
92 | if isempty(i_select)
|
---|
93 | i_select = 1:length(c);
|
---|
94 | end
|
---|
95 | c_select=c(i_select);
|
---|
96 | n_select=n(i_select);
|
---|
97 | cmin=min(c_select);
|
---|
98 | cmax=max(c_select);
|
---|
99 | C=(C-cmin)/(cmax-cmin)*256;
|
---|
100 | C=uint8(C);
|
---|