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

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

functions updated in series for the new file configuration

File size: 5.8 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={};
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                case {'vol','image'}
93                    A=imread(filename);
94                case 'multimage'
95                    A=imread(filename,i1_series{1}(jfile,ifile));
96            end
97            C=levels(A);
98            filename_new=fullfile_uvmat(RootPath,SubdirResult,RootFile,'.png',NomType,i1_series{1}(jfile,ifile),[],j1_series{1}(jfile,ifile));
99            imwrite(C,filename_new)
100            display([filename_new ' written'])
101        end
102    end
103end
104%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
105
106function C=levels(A)
107%whos A;
108B=double(A(:,:,1));
109windowsize=round(min(size(B,1),size(B,2))/20);
110windowsize=floor(windowsize/2)*2+1;
111ix=[1/2-windowsize/2:-1/2+windowsize/2];%
112%del=np/3;
113%fct=exp(-(ix/del).^2);
114fct2=cos(ix/(windowsize-1)/2*pi/2);
115%Mfiltre=(ones(5,5)/5^2);
116%Mfiltre=fct2';
117Mfiltre=fct2'*fct2;
118Mfiltre=Mfiltre/(sum(sum(Mfiltre)));
119
120C=filter2(Mfiltre,B);
121C(:,1:windowsize)=C(:,windowsize)*ones(1,windowsize);
122C(:,end-windowsize+1:end)=C(:,end-windowsize+1)*ones(1,windowsize);
123C(1:windowsize,:)=ones(windowsize,1)*C(windowsize,:);
124C(end-windowsize+1:end,:)=ones(windowsize,1)*C(end-windowsize,:);
125C=tanh(B./(2*C));
126[n,c]=hist(reshape(C,1,[]),100);
127% figure;plot(c,n);
128
129[m,i]=max(n);
130c_max=c(i);
131[dummy,index]=sort(abs(c-c(i)));
132n=n(index);
133c=c(index);
134i_select = find(cumsum(n)<0.95*sum(n));
135if isempty(i_select)
136    i_select = 1:length(c);
137end
138c_select=c(i_select);
139n_select=n(i_select);
140cmin=min(c_select);
141cmax=max(c_select);
142C=(C-cmin)/(cmax-cmin)*256;
143C=uint8(C);
Note: See TracBrowser for help on using the repository browser.