source: trunk/src/series/ima_levels.m @ 447

Last change on this file since 447 was 447, checked in by sommeria, 12 years ago

aver_stat modified in the new rationalized form.
OTHER series FCTS NOT OPERATIONAL

File size: 6.4 KB
Line 
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%----------------------------------------------------------------------
15function 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)
17if ~exist('Param','var')
18    GUI_input={'OutputDirExt';'.lev'};
19    return %exit the function
20end
21
22%% input parameters
23% read the xml file for batch case
24if ischar(Param) && ~isempty(find(regexp('Param','.xml$')))
25    Param=xml2struct(Param);
26    checkrun=0;
27else %  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;
31end
32%filebase=fullfile(Param.InputTable{1,1},Param.InputTable{1,3});
33RootPath=Param.InputTable{1,1};
34Subdir=Param.InputTable{1,2};
35RootFile=Param.InputTable{1,3};
36NomType=Param.InputTable{1,4};
37FileExt=Param.InputTable{1,5};
38[filecell,i1_series,i2_series,j1_series]=get_file_series(Param);% list of input files and indices
39if size(filecell,1)>1
40    msgbox_uvmat('WARNING','This function uses only the first input image series')
41    return
42end
43
44%% determine input image type
45[FileType,FileInfo,MovieObject]=get_file_type(filecell{1,1});
46ListTypes={'image','multimage','mmreader','video'};
47
48if 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
51end
52
53%% create dir of the new images
54SubdirResult=[Param.InputTable{1,2} '.lev'];% add the suffix '.lev' to the name of the image folder
55try
56    mkdir(fullfile(Param.InputTable{1,1},SubdirResult));
57catch ME
58    msgbox_uvmat('ERROR',['error in creating result directory: ' ME.message]);%display error msg for directory creation if fails
59    return
60end
61[xx,msg2] = fileattrib(fullfile(Param.InputTable{1,1},SubdirResult),'+w','g'); %yield writing access (+w) to user group (g)
62if ~strcmp(msg2,'')
63    msgbox_uvmat('ERROR',['pb of permission for ' fullfile(Param.InputTable{1,1},SubdirResult) ': ' msg2])%error message for directory creation
64    return
65end
66msgbox_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
74nbfield=size(i1_series{1},2);
75nbfield2=size(i1_series{1},1);
76for 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
117end
118%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
119
120function C=levels(A)
121%whos A;
122B=double(A(:,:,1));
123windowsize=round(min(size(B,1),size(B,2))/20);
124windowsize=floor(windowsize/2)*2+1;
125ix=[1/2-windowsize/2:-1/2+windowsize/2];%
126%del=np/3;
127%fct=exp(-(ix/del).^2);
128fct2=cos(ix/(windowsize-1)/2*pi/2);
129%Mfiltre=(ones(5,5)/5^2);
130%Mfiltre=fct2';
131Mfiltre=fct2'*fct2;
132Mfiltre=Mfiltre/(sum(sum(Mfiltre)));
133
134C=filter2(Mfiltre,B);
135C(:,1:windowsize)=C(:,windowsize)*ones(1,windowsize);
136C(:,end-windowsize+1:end)=C(:,end-windowsize+1)*ones(1,windowsize);
137C(1:windowsize,:)=ones(windowsize,1)*C(windowsize,:);
138C(end-windowsize+1:end,:)=ones(windowsize,1)*C(end-windowsize,:);
139C=tanh(B./(2*C));
140[n,c]=hist(reshape(C,1,[]),100);
141% figure;plot(c,n);
142
143[m,i]=max(n);
144c_max=c(i);
145[dummy,index]=sort(abs(c-c(i)));
146n=n(index);
147c=c(index);
148i_select = find(cumsum(n)<0.95*sum(n));
149if isempty(i_select)
150    i_select = 1:length(c);
151end
152c_select=c(i_select);
153n_select=n(i_select);
154cmin=min(c_select);
155cmax=max(c_select);
156C=(C-cmin)/(cmax-cmin)*256;
157C=uint8(C);
Note: See TracBrowser for help on using the repository browser.