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

Last change on this file since 604 was 604, checked in by sommeria, 8 years ago

bugs corrected in series, ima_levels cleaned as exemple

File size: 9.0 KB
Line 
1%'ima_levels': rescale the image intensity to reduce strong luminosity peaks (their blinking effects often perturbs PIV))
2% this function can be used as a template for applying a transform (here 'levels.m') to each image of a series
3%------------------------------------------------------------------------
4% function GUI_input=ima_levels(Param)
5%
6%------------------------------------------------------------------------
7%%%%%%%%%%% GENERAL TO ALL SERIES ACTION FCTS %%%%%%%%%%%%%%%%%%%%%%%%%%%
8%
9%OUTPUT
10% ParamOut: sets options in the GUI series.fig needed for the function
11%
12%INPUT:
13% In run mode, the input parameters are given as a Matlab structure Param copied from the GUI series.
14% In batch mode, Param is the name of the corresponding xml file containing the same information
15% when Param.Action.RUN=0 (as activated when the current Action is selected
16% in series), the function ouput paramOut set the activation of the needed GUI elements
17%
18% Param contains the elements:(use the menu bar command 'export/GUI config' in series to
19% see the current structure Param)
20%    .InputTable: cell of input file names, (several lines for multiple input)
21%                      each line decomposed as {RootPath,SubDir,Rootfile,NomType,Extension}
22%    .OutputSubDir: name of the subdirectory for data outputs
23%    .OutputDirExt: directory extension for data outputs
24%    .Action: .ActionName: name of the current activated function
25%             .ActionPath:   path of the current activated function
26%             .ActionExt: fct extension ('.m', Matlab fct, '.sh', compiled   Matlab fct
27%             .RUN =0 for GUI input, =1 for function activation
28%             .RunMode='local','background', 'cluster': type of function  use
29%             
30%    .IndexRange: set the file or frame indices on which the action must be performed
31%    .FieldTransform: .TransformName: name of the selected transform function
32%                     .TransformPath:   path  of the selected transform function
33%    .InputFields: sub structure describing the input fields withfields
34%              .FieldName: name(s) of the field
35%              .VelType: velocity type
36%              .FieldName_1: name of the second field in case of two input series
37%              .VelType_1: velocity type of the second field in case of two input series
38%              .Coord_y: name of y coordinate variable
39%              .Coord_x: name of x coordinate variable
40%    .ProjObject: %sub structure describing a projection object (read from ancillary GUI set_object)
41%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
42 
43function ParamOut=ima_levels (Param)
44
45%% set the input elements needed on the GUI series when the action is selected in the menu ActionName
46if isstruct(Param) && isequal(Param.Action.RUN,0)
47    ParamOut.NbViewMax=1;% max nbre of input file series (default , no limitation)
48    ParamOut.AllowInputSort='off';% allow alphabetic sorting of the list of input file SubDir (options 'off'/'on', 'off' by default)
49    ParamOut.WholeIndexRange='off';% prescribes the file index ranges from min to max (options 'off'/'on', 'off' by default)
50    ParamOut.NbSlice='off'; %nbre of slices ('off' by default)
51    ParamOut.VelType='off';% menu for selecting the velocity type (options 'off'/'one'/'two',  'off' by default)
52    ParamOut.FieldName='one';% menu for selecting the field (s) in the input file(options 'off'/'one'/'two', 'off' by default)
53    ParamOut.FieldTransform = 'off';%can use a transform function
54    ParamOut.ProjObject='off';%can use projection object(option 'off'/'on',
55    ParamOut.Mask='off';%can use mask option   (option 'off'/'on', 'off' by default)
56    ParamOut.OutputDirExt='.lev';%set the output dir extension
57    ParamOut.OutputFileMode='NbInput';% ='=NbInput': 1 output file per input file index, '=NbInput_i': 1 file per input file index i, '=NbSlice': 1 file per slice
58    %check the type of the existence and type of the first input file:
59    Param.IndexRange.last_i=Param.IndexRange.first_i;%keep only the first index in the series
60    Param.IndexRange.last_j=Param.IndexRange.first_j;
61    filecell=get_file_series(Param);
62    if ~exist(filecell{1,1},'file')
63        msgbox_uvmat('WARNING','the first input file does not exist')
64    else
65        FileType=get_file_type(filecell{1,1});
66        if isempty(find(strcmp(FileType,{'image','multimage','mmreader','video'})));% =1 for images
67            msgbox_uvmat('ERROR',['bad input file type for ' mfilename ': an image is needed'])
68        end
69    end
70return
71end
72
73%%%%%%%%%%%% STANDARD PART (DO NOT EDIT) %%%%%%%%%%%%
74%% read input parameters from an xml file if input is a file name (batch mode)
75checkrun=1;
76if ischar(Param)
77    Param=xml2struct(Param);% read Param as input file (batch case)
78    checkrun=0;
79end
80
81%% root input file names and nomenclature type (cell arrays with one element)
82RootPath=Param.InputTable(:,1);
83RootFile=Param.InputTable(:,3);
84SubDir=Param.InputTable(:,2);
85NomType=Param.InputTable(:,4);
86FileExt=Param.InputTable(:,5);
87
88%% subdirectory for output files
89SubdirOut=[Param.OutputSubDir Param.OutputDirExt];
90
91%% get the set of input file names (cell array filecell), and file indices
92[filecell,i1_series,i2_series,j1_series,j2_series]=get_file_series(Param);
93% filecell{iview,fileindex}: cell array representing the list of file names
94%        iview: line in the table corresponding to a given file series
95%        fileindex: file index within  the file series,
96% i1_series(iview,ref_j,ref_i)... are the corresponding arrays of indices i1,i2,j1,j2, depending on the input line iview and the two reference indices ref_i,ref_j
97% i1_series(iview,fileindex) expresses the same indices as a 1D array in file indices
98nbfield_j=size(j1_series{1},1); %nb of fields for the j index (bursts or volume slices)
99nbfield_i=size(i1_series{1},2); %nb of fields for the i index
100nbfield=nbfield_j*nbfield_i; %total number of fields
101[FileType{1},FileInfo{1},VideoObject{1}]=get_file_type(filecell{1,1});% type of input file
102
103%% frame index for movie or multimage file input 
104if ~isempty(j1_series{1})
105    frame_index{1}=j1_series{1};
106else
107    frame_index{1}=i1_series{1};
108end
109
110%% calibration data and timing: read the ImaDoc files
111%not relevant for this function
112
113%% check coincidence in time for several input file series
114%not relevant for this function
115
116%% coordinate transform or other user defined transform
117%not relevant for this function
118
119%%%%%%%%%%%% END STANDARD PART  %%%%%%%%%%%%
120 % EDIT FROM HERE
121
122 %% Extension and indexing nomenclature for output file
123 FileExtOut='.png'; % write result as .png images for image inputs
124 if strcmpi(NomType{1}(end),'a')
125     NomTypeOut=NomType{1};
126 else
127     NomTypeOut='_1_1';
128 end
129
130%% Set field names and velocity types
131%not relevant for this function
132
133%% Initiate output fields
134%not relevant for this function
135
136%% set processing parameters
137% not needed for this function
138
139%% update the xml file
140% not needed for this function
141
142%% main loop on images
143j1=[];%default
144for ifile=1:nbfield
145    if checkrun
146        stopstate=get(Param.RUNHandle,'BusyAction');
147        update_waitbar(Param.WaitbarHandle,ifile/nbfield)
148    else
149        stopstate='queue';
150    end
151    if ~isequal(stopstate,'queue') % enable STOP command
152        break
153    end
154    if ~isempty(j1_series)&&~isequal(j1_series,{[]})
155        j1=j1_series{1}(ifile);
156    end
157    filename=fullfile_uvmat(RootPath{1},SubDir{1},RootFile{1},FileExt{1},NomType{1},i1_series{1}(ifile),[],j1);
158    A=read_image(filename,FileType{1},VideoObject{1},frame_index{1}(ifile));
159    if ndims(A)==3;%color images
160        A=sum(double(Aread),3);% take the sum of color components
161    end
162    % operation on images
163    A=levels(A);
164    % write output file
165    filename_new=fullfile_uvmat(RootPath{1},SubdirOut,RootFile{1},FileExtOut,NomTypeOut,i1_series{1}(ifile),[],j1);
166    imwrite(A,filename_new)
167    display([filename_new ' written'])
168end
169%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
170
171
172function C=levels(A)
173%whos A;
174B=double(A(:,:,1));
175windowsize=round(min(size(B,1),size(B,2))/20);
176windowsize=floor(windowsize/2)*2+1;
177ix=[1/2-windowsize/2:-1/2+windowsize/2];%
178%del=np/3;
179%fct=exp(-(ix/del).^2);
180fct2=cos(ix/(windowsize-1)/2*pi/2);
181%Mfiltre=(ones(5,5)/5^2);
182%Mfiltre=fct2';
183Mfiltre=fct2'*fct2;
184Mfiltre=Mfiltre/(sum(sum(Mfiltre)));
185
186C=filter2(Mfiltre,B);
187C(:,1:windowsize)=C(:,windowsize)*ones(1,windowsize);
188C(:,end-windowsize+1:end)=C(:,end-windowsize+1)*ones(1,windowsize);
189C(1:windowsize,:)=ones(windowsize,1)*C(windowsize,:);
190C(end-windowsize+1:end,:)=ones(windowsize,1)*C(end-windowsize,:);
191C=tanh(B./(2*C));
192[n,c]=hist(reshape(C,1,[]),100);
193% figure;plot(c,n);
194
195[m,i]=max(n);
196c_max=c(i);
197[dummy,index]=sort(abs(c-c(i)));
198n=n(index);
199c=c(index);
200i_select = find(cumsum(n)<0.95*sum(n));
201if isempty(i_select)
202    i_select = 1:length(c);
203end
204c_select=c(i_select);
205n_select=n(i_select);
206cmin=min(c_select);
207cmax=max(c_select);
208C=(C-cmin)/(cmax-cmin)*256;
209C=uint8(C);
Note: See TracBrowser for help on using the repository browser.