1 | %'ima_levels': rescale the image intensity to reduce strong luminosity peaks
|
---|
2 | %------------------------------------------------------------------------
|
---|
3 | % function GUI_input=ima_levels(num_i1,num_i2,num_j1,num_j2,Series)
|
---|
4 | %
|
---|
5 | %OUTPUT
|
---|
6 | % GUI_input=list of options in the GUI series.fig needed for the function
|
---|
7 | %
|
---|
8 | %INPUT:
|
---|
9 | %num_i1: series of first indices i (given from the series interface as first_i:incr_i:last_i, mode and list_pair_civ)
|
---|
10 | %num_i2: series of second indices i (given from the series interface as first_i:incr_i:last_i, mode and list_pair_civ)
|
---|
11 | %num_j1: series of first indices j (given from the series interface as first_j:incr_j:last_j, mode and list_pair_civ )
|
---|
12 | %num_j2: series of second indices j (given from the series interface as first_j:incr_j:last_j, mode and list_pair_civ)
|
---|
13 | %Series: Matlab structure containing information set by the series interface% relabel_i_j: relabel an image series with two indices, according to the time matrix given by ImaDoc
|
---|
14 | %----------------------------------------------------------------------
|
---|
15 | function GUI_input=ima_levels(num_i1,num_i2,num_j1,num_j2,Series)
|
---|
16 | %requests for the visibility of input windows in the GUI series (activated directly by the selection in the menu ACTION)
|
---|
17 | if ~exist('num_i1','var')
|
---|
18 | GUI_input={};
|
---|
19 | return %exit the function
|
---|
20 | end
|
---|
21 |
|
---|
22 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%enable waitbar
|
---|
23 | hseries=guidata(Series.hseries);%handles of the GUI series
|
---|
24 | WaitbarPos=get(hseries.waitbar_frame,'Position');
|
---|
25 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
---|
26 |
|
---|
27 | basename=fullfile(Series.RootPath,Series.RootFile) ;
|
---|
28 |
|
---|
29 | %create dir of the new images
|
---|
30 | [dir_images,namebase]=fileparts(basename);
|
---|
31 | [path,subdir_ima]=fileparts(dir_images);
|
---|
32 | dircur=pwd;
|
---|
33 | cd(path);
|
---|
34 | mkdir([subdir_ima '_levels']);
|
---|
35 | [xx,msg2] = fileattrib(subdir_ima,'+w','g'); %yield writing access (+w) to user group (g)
|
---|
36 | if ~strcmp(msg2,'')
|
---|
37 | msgbox_uvmat('ERROR',['pb of permission for ' subdir_ima ': ' msg2])%error message for directory creation
|
---|
38 | cd(dircur)
|
---|
39 | return
|
---|
40 | end
|
---|
41 | cd(dircur);
|
---|
42 | basename_new=fullfile(path,[subdir_ima '_levels'],namebase);
|
---|
43 |
|
---|
44 | % read imadoc
|
---|
45 | %[XmlData,warntext]=imadoc2struct([basename '.xml']);
|
---|
46 | % nbfield1=size(XmlData.Time,1);
|
---|
47 | % nbfield2=size(XmlData.Time,2);
|
---|
48 |
|
---|
49 | msgbox_uvmat('CONFIRMATION','apply image rescaling function levels.m ');
|
---|
50 |
|
---|
51 | %copy the xml file
|
---|
52 | if exist([basename '.xml'],'file')
|
---|
53 | copyfile([basename '.xml'],[basename_new '.xml']);% copy the .civ file
|
---|
54 | end
|
---|
55 |
|
---|
56 | %main loop
|
---|
57 | nbfield=size(num_i1,2);
|
---|
58 | nbfield2=size(num_i1,1);
|
---|
59 | for ifile=1:nbfield
|
---|
60 | update_waitbar(hseries.waitbar,WaitbarPos,ifile/nbfield)
|
---|
61 | stopstate=get(hseries.RUN,'BusyAction');
|
---|
62 | if isequal(stopstate,'queue') % enable STOP command
|
---|
63 | for jfile=1:nbfield2
|
---|
64 | filename=name_generator(basename,num_i1(jfile,ifile),num_j1(jfile,ifile),Series.FileExt,Series.NomType);
|
---|
65 | filename_new=name_generator(basename_new,num_i1(jfile,ifile),num_j1(jfile,ifile),'.png',Series.NomType);
|
---|
66 | A=imread(filename);
|
---|
67 | C=levels(A);
|
---|
68 | imwrite(C,filename_new)
|
---|
69 | end
|
---|
70 | end
|
---|
71 | end
|
---|
72 |
|
---|
73 |
|
---|
74 |
|
---|
75 | function C=levels(A)
|
---|
76 | %whos A;
|
---|
77 | B=double(A(:,:,1));
|
---|
78 | windowsize=round(min(size(B,1),size(B,2))/20);
|
---|
79 | windowsize=floor(windowsize/2)*2+1;
|
---|
80 | ix=[1/2-windowsize/2:-1/2+windowsize/2];%
|
---|
81 | %del=np/3;
|
---|
82 | %fct=exp(-(ix/del).^2);
|
---|
83 | fct2=cos(ix/(windowsize-1)/2*pi/2);
|
---|
84 | %Mfiltre=(ones(5,5)/5^2);
|
---|
85 | %Mfiltre=fct2';
|
---|
86 | Mfiltre=fct2'*fct2;
|
---|
87 | Mfiltre=Mfiltre/(sum(sum(Mfiltre)));
|
---|
88 |
|
---|
89 | C=filter2(Mfiltre,B);
|
---|
90 | C(:,1:windowsize)=C(:,windowsize)*ones(1,windowsize);
|
---|
91 | C(:,end-windowsize+1:end)=C(:,end-windowsize+1)*ones(1,windowsize);
|
---|
92 | C(1:windowsize,:)=ones(windowsize,1)*C(windowsize,:);
|
---|
93 | C(end-windowsize+1:end,:)=ones(windowsize,1)*C(end-windowsize,:);
|
---|
94 | C=tanh(B./(2*C));
|
---|
95 | [n,c]=hist(reshape(C,1,[]),100);
|
---|
96 | % figure;plot(c,n);
|
---|
97 |
|
---|
98 | [m,i]=max(n);
|
---|
99 | c_max=c(i);
|
---|
100 | [dummy,index]=sort(abs(c-c(i)));
|
---|
101 | n=n(index);
|
---|
102 | c=c(index);
|
---|
103 | i_select = find(cumsum(n)<0.95*sum(n));
|
---|
104 | if isempty(i_select)
|
---|
105 | i_select = 1:length(c);
|
---|
106 | end
|
---|
107 | c_select=c(i_select);
|
---|
108 | n_select=n(i_select);
|
---|
109 | cmin=min(c_select);
|
---|
110 | cmax=max(c_select);
|
---|
111 | C=(C-cmin)/(cmax-cmin)*256;
|
---|
112 | C=uint8(C);
|
---|