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(Param)
|
---|
16 | %requests for the visibility of input windows in the GUI series (activated directly by the selection in the menu ACTION)
|
---|
17 | if ~exist('Param','var')
|
---|
18 | GUI_input={'OutputDirExt';'.lev'};
|
---|
19 | return %exit the function
|
---|
20 | end
|
---|
21 |
|
---|
22 | %% input parameters
|
---|
23 | % read the xml file for batch case
|
---|
24 | if ischar(Param) && ~isempty(find(regexp('Param','.xml$')))
|
---|
25 | Param=xml2struct(Param);
|
---|
26 | checkrun=0;
|
---|
27 | else % RUN case: parameters introduced as the input structure Param
|
---|
28 | hseries=guidata(Param.hseries);%handles of the GUI series
|
---|
29 | WaitbarPos=get(hseries.waitbar_frame,'Position');
|
---|
30 | checkrun=1;
|
---|
31 | end
|
---|
32 | %filebase=fullfile(Param.InputTable{1,1},Param.InputTable{1,3});
|
---|
33 | RootPath=Param.InputTable{1,1};
|
---|
34 | Subdir=Param.InputTable{1,2};
|
---|
35 | RootFile=Param.InputTable{1,3};
|
---|
36 | NomType=Param.InputTable{1,4};
|
---|
37 | FileExt=Param.InputTable{1,5};
|
---|
38 | [filecell,i1_series,i2_series,j1_series]=get_file_series(Param);% list of input files and indices
|
---|
39 | if size(filecell,1)>1
|
---|
40 | msgbox_uvmat('WARNING','This function uses only the first input image series')
|
---|
41 | return
|
---|
42 | end
|
---|
43 |
|
---|
44 | %% determine input image type
|
---|
45 | [FileType,FileInfo,MovieObject]=get_file_type(filecell{1,1});
|
---|
46 | ListTypes={'image','multimage','mmreader','video'};
|
---|
47 |
|
---|
48 | if isempty(strcmp(FileType,ListTypes))% if the detected FileType is not in the list for images
|
---|
49 | msgbox_uvmat('ERROR',['invalid file extension ' FileExt ': this function only accepts image or movie input'])
|
---|
50 | return
|
---|
51 | end
|
---|
52 |
|
---|
53 | %% create dir of the new images
|
---|
54 | SubdirResult=[Param.InputTable{1,2} '.lev'];% add the suffix '.lev' to the name of the image folder
|
---|
55 | try
|
---|
56 | mkdir(fullfile(Param.InputTable{1,1},SubdirResult));
|
---|
57 | catch ME
|
---|
58 | msgbox_uvmat('ERROR',['error in creating result directory: ' ME.message]);%display error msg for directory creation if fails
|
---|
59 | return
|
---|
60 | end
|
---|
61 | [xx,msg2] = fileattrib(fullfile(Param.InputTable{1,1},SubdirResult),'+w','g'); %yield writing access (+w) to user group (g)
|
---|
62 | if ~strcmp(msg2,'')
|
---|
63 | msgbox_uvmat('ERROR',['pb of permission for ' fullfile(Param.InputTable{1,1},SubdirResult) ': ' msg2])%error message for directory creation
|
---|
64 | return
|
---|
65 | end
|
---|
66 | msgbox_uvmat('CONFIRMATION','apply image rescaling function levels.m ');
|
---|
67 |
|
---|
68 | %copy the xml file
|
---|
69 | % if exist([basename '.xml'],'file')
|
---|
70 | % copyfile([basename '.xml'],[basename_new '.xml']);% copy the .civ file
|
---|
71 | % end
|
---|
72 |
|
---|
73 | %% main loop
|
---|
74 | nbfield=size(i1_series{1},2);
|
---|
75 | nbfield2=size(i1_series{1},1);
|
---|
76 | for ifile=1:nbfield
|
---|
77 | if checkrun
|
---|
78 | % update_waitbar(hseries.waitbar,WaitbarPos,ifile/nbfield)
|
---|
79 | update_waitbar(hseries.waitbar_frame,WaitbarPos,ifile/nbfield)
|
---|
80 | stopstate=get(hseries.RUN,'BusyAction');
|
---|
81 | else
|
---|
82 | stopstate='queue';
|
---|
83 | end
|
---|
84 | if isequal(stopstate,'queue') % enable STOP command
|
---|
85 | for jfile=1:nbfield2
|
---|
86 | %filename=name_generator(basename,num_i1(jfile,ifile),num_j1(jfile,ifile),Series.FileExt,Series.NomType);
|
---|
87 | %filename_new=name_generator(basename_new,num_i1(jfile,ifile),num_j1(jfile,ifile),'.png',Series.NomType);
|
---|
88 | filename=fullfile_uvmat(RootPath,Subdir,RootFile,FileExt,NomType,i1_series{1}(jfile,ifile),[],j1_series{1}(jfile,ifile));
|
---|
89 | switch FileType
|
---|
90 | case {'video','mmreader'}
|
---|
91 | A=read(MovieObject,i1_series{1}(jfile,ifile));
|
---|
92 | if strcmp(NomType,'*')
|
---|
93 | A=read(MovieObject,i1_series{1}(jfile,ifile));
|
---|
94 | NomType_out='_1';
|
---|
95 | else
|
---|
96 | A=imread(filename,j1_series{1}(jfile,ifile));
|
---|
97 | NomType_out='_1_1';
|
---|
98 | end
|
---|
99 | case {'vol','image'}
|
---|
100 | A=imread(filename);
|
---|
101 | NomType_out='_1';
|
---|
102 | case 'multimage'
|
---|
103 | if strcmp(NomType,'*')
|
---|
104 | A=imread(filename,i1_series{1}(jfile,ifile));
|
---|
105 | NomType_out='_1';
|
---|
106 | else
|
---|
107 | A=imread(filename,j1_series{1}(jfile,ifile));
|
---|
108 | NomType_out='_1_1';
|
---|
109 | end
|
---|
110 | end
|
---|
111 | C=levels(A);
|
---|
112 | filename_new=fullfile_uvmat(RootPath,SubdirResult,RootFile,'.png',NomType_out,i1_series{1}(jfile,ifile),[],j1_series{1}(jfile,ifile));
|
---|
113 | imwrite(C,filename_new)
|
---|
114 | display([filename_new ' written'])
|
---|
115 | end
|
---|
116 | end
|
---|
117 | end
|
---|
118 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
---|
119 |
|
---|
120 | function C=levels(A)
|
---|
121 | %whos A;
|
---|
122 | B=double(A(:,:,1));
|
---|
123 | windowsize=round(min(size(B,1),size(B,2))/20);
|
---|
124 | windowsize=floor(windowsize/2)*2+1;
|
---|
125 | ix=[1/2-windowsize/2:-1/2+windowsize/2];%
|
---|
126 | %del=np/3;
|
---|
127 | %fct=exp(-(ix/del).^2);
|
---|
128 | fct2=cos(ix/(windowsize-1)/2*pi/2);
|
---|
129 | %Mfiltre=(ones(5,5)/5^2);
|
---|
130 | %Mfiltre=fct2';
|
---|
131 | Mfiltre=fct2'*fct2;
|
---|
132 | Mfiltre=Mfiltre/(sum(sum(Mfiltre)));
|
---|
133 |
|
---|
134 | C=filter2(Mfiltre,B);
|
---|
135 | C(:,1:windowsize)=C(:,windowsize)*ones(1,windowsize);
|
---|
136 | C(:,end-windowsize+1:end)=C(:,end-windowsize+1)*ones(1,windowsize);
|
---|
137 | C(1:windowsize,:)=ones(windowsize,1)*C(windowsize,:);
|
---|
138 | C(end-windowsize+1:end,:)=ones(windowsize,1)*C(end-windowsize,:);
|
---|
139 | C=tanh(B./(2*C));
|
---|
140 | [n,c]=hist(reshape(C,1,[]),100);
|
---|
141 | % figure;plot(c,n);
|
---|
142 |
|
---|
143 | [m,i]=max(n);
|
---|
144 | c_max=c(i);
|
---|
145 | [dummy,index]=sort(abs(c-c(i)));
|
---|
146 | n=n(index);
|
---|
147 | c=c(index);
|
---|
148 | i_select = find(cumsum(n)<0.95*sum(n));
|
---|
149 | if isempty(i_select)
|
---|
150 | i_select = 1:length(c);
|
---|
151 | end
|
---|
152 | c_select=c(i_select);
|
---|
153 | n_select=n(i_select);
|
---|
154 | cmin=min(c_select);
|
---|
155 | cmax=max(c_select);
|
---|
156 | C=(C-cmin)/(cmax-cmin)*256;
|
---|
157 | C=uint8(C);
|
---|