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') on each image of a series
|
---|
3 | %------------------------------------------------------------------------
|
---|
4 | % function GUI_input=ima_levels(Param)
|
---|
5 | %
|
---|
6 | %%%%%%%%%%% GENERAL TO ALL SERIES ACTION FCTS %%%%%%%%%%%%%%%%%%%%%%%%%%%
|
---|
7 | %OUTPUT
|
---|
8 | % GUI_input=list of options in the GUI series.fig needed for the function
|
---|
9 | %
|
---|
10 | %INPUT:
|
---|
11 | % In run mode, the input parameters are given as a Matlab structure Param copied from the GUI series.
|
---|
12 | % In batch mode, Param is the name of the corresponding xml file containing the same information
|
---|
13 | % In the absence of input (as activated when the current Action is selected
|
---|
14 | % in series), the function ouput GUI_input set the activation of the needed GUI elements
|
---|
15 | %
|
---|
16 | % Param contains the elements:(use the menu bar command 'export/GUI config' in series to see the current structure Param)
|
---|
17 | % .InputTable: cell of input file names, (several lines for multiple input)
|
---|
18 | % each line decomposed as {RootPath,SubDir,Rootfile,NomType,Extension}
|
---|
19 | % .OutputSubDir: name of the subdirectory for data outputs
|
---|
20 | % .OutputDir: directory for data outputs, including path
|
---|
21 | % .Action: .ActionName: name of the current activated function
|
---|
22 | % .ActionPath: path of the current activated function
|
---|
23 | % .IndexRange: set the file or frame indices on which the action must be performed
|
---|
24 | % .FieldTransform: .TransformName: name of the selected transform function
|
---|
25 | % .TransformPath: path of the selected transform function
|
---|
26 | % .TransformHandle: corresponding function handle
|
---|
27 | % .InputFields: sub structure describing the input fields withfields
|
---|
28 | % .FieldName: name of the field
|
---|
29 | % .VelType: velocity type
|
---|
30 | % .FieldName_1: name of the second field in case of two input series
|
---|
31 | % .VelType_1: velocity type of the second field in case of two input series
|
---|
32 | % .ProjObject: %sub structure describing a projection object (read from ancillary GUI set_object)
|
---|
33 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
---|
34 |
|
---|
35 | function GUI_input=ima_levels (Param)
|
---|
36 |
|
---|
37 | %% set the input elements needed on the GUI series when the action is selected in the menu ActionName
|
---|
38 | if ~exist('Param','var') % case with no input parameter
|
---|
39 | GUI_input={'NbViewMax';1;...% max nbre of input file series (default='' , no limitation)
|
---|
40 | 'AllowInputSort';'off';...% allow alphabetic sorting of the list of input files (options 'off'/'on', 'off' by default)
|
---|
41 | 'NbSlice';'off'; ...%nbre of slices ('off' by default)
|
---|
42 | 'VelType';'off';...% menu for selecting the velocity type (options 'off'/'one'/'two', 'off' by default)
|
---|
43 | 'FieldName';'off';...% menu for selecting the field (s) in the input file(options 'off'/'one'/'two', 'off' by default)
|
---|
44 | 'FieldTransform'; 'off';...%can use a transform function
|
---|
45 | 'ProjObject';'off';...%can use projection object(option 'off'/'on',
|
---|
46 | 'Mask';'off';...%can use mask option (option 'off'/'on', 'off' by default)
|
---|
47 | 'OutputDirExt';'.lev';...%set the output dir extension
|
---|
48 | ''};
|
---|
49 | return
|
---|
50 | end
|
---|
51 |
|
---|
52 | %% get input parameters, file names and indices
|
---|
53 | % BATCH case: read the xml file for batch case
|
---|
54 | if ischar(Param) && ~isempty(find(regexp(Param,'.xml$')))
|
---|
55 | Param=xml2struct(Param);
|
---|
56 | checkrun=0;
|
---|
57 | % RUN case: parameters introduced as the input structure Param
|
---|
58 | else
|
---|
59 | hseries=guidata(Param.hseries);%handles of the GUI series
|
---|
60 | WaitbarPos=get(hseries.waitbar_frame,'Position');%position of the waitbar on the GUI series
|
---|
61 | checkrun=1; % indicate the RUN option is used
|
---|
62 | end
|
---|
63 |
|
---|
64 | % get the set of input file names (cell array filecell), and the lists of
|
---|
65 | [filecell,i1_series,i2_series,j1_series,j2_series]=get_file_series(Param);
|
---|
66 |
|
---|
67 | %% root input file(s) and type
|
---|
68 | RootPath=Param.InputTable(:,1);
|
---|
69 | RootFile=Param.InputTable(:,3);
|
---|
70 | SubDir=Param.InputTable(:,2);
|
---|
71 | NomType=Param.InputTable(:,4);
|
---|
72 | FileExt=Param.InputTable(:,5);
|
---|
73 |
|
---|
74 | % numbers of slices and file indices
|
---|
75 | NbSlice=1;%default
|
---|
76 | if isfield(Param.IndexRange,'NbSlice')
|
---|
77 | NbSlice=Param.IndexRange.NbSlice;
|
---|
78 | end
|
---|
79 | nbview=numel(i1_series);%number of input file series (lines in InputTable)
|
---|
80 | nbfield_j=size(i1_series{1},1); %nb of consecutive fields for the j index (bursts or volume slices)
|
---|
81 | nbfield_i=size(i1_series{1},2); %nb of consecutive fields for the i index
|
---|
82 | nbfield=nbfield_j*nbfield_i; %total number of files or frames
|
---|
83 | nbfield_i=floor(nbfield/NbSlice);%total number of indexes in a slice (adjusted to an integer number of slices)
|
---|
84 | nbfield=nbfield_i*NbSlice; %total number of fields after adjustement
|
---|
85 |
|
---|
86 | %determine the file type on each line from the first input file
|
---|
87 | ImageTypeOptions={'image','multimage','mmreader','video'};%allowed input file types(images)
|
---|
88 | [FileType{1},FileInfo{1},MovieObject{1}]=get_file_type(filecell{1,1});
|
---|
89 | CheckImage{1}=~isempty(find(strcmp(FileType,ImageTypeOptions)));% =1 for images
|
---|
90 | if ~isempty(j1_series{1})
|
---|
91 | frame_index{1}=j1_series{1};
|
---|
92 | else
|
---|
93 | frame_index{1}=i1_series{1};
|
---|
94 | end
|
---|
95 |
|
---|
96 | %% calibration data and timing: read the ImaDoc files
|
---|
97 | %not relevant for this function
|
---|
98 |
|
---|
99 | %% check coincidence in time for several input file series
|
---|
100 | %not relevant for this function
|
---|
101 |
|
---|
102 | %% coordinate transform or other user defined transform
|
---|
103 | %not relevant for this function
|
---|
104 |
|
---|
105 | %%%%%%%%%%%% END STANDARD PART %%%%%%%%%%%%
|
---|
106 | % EDIT FROM HERE
|
---|
107 |
|
---|
108 | %% check the validity of input file types
|
---|
109 | if CheckImage{1}
|
---|
110 | FileExtOut='.png'; % write result as .png images for image inputs
|
---|
111 | if strcmp(lower(NomType{1}(end)),'a')
|
---|
112 | NomTypeOut=NomType{1};
|
---|
113 | else
|
---|
114 | NomTypeOut='_1_1';
|
---|
115 | end
|
---|
116 | else
|
---|
117 | msgbox_uvmat('ERROR',['invalid file type input: ' FileType{1} ' not an image'])
|
---|
118 | return
|
---|
119 | end
|
---|
120 |
|
---|
121 | %% Set field names and velocity types
|
---|
122 | %not relevant for this function
|
---|
123 |
|
---|
124 | %% Initiate output fields
|
---|
125 | %not relevant for this function
|
---|
126 |
|
---|
127 | %% set processing parameters
|
---|
128 | % not needed for this function
|
---|
129 |
|
---|
130 | %% update the xml file
|
---|
131 | % not needed for this function
|
---|
132 |
|
---|
133 | %% main loop on images
|
---|
134 | j1=[];%default
|
---|
135 | for ifile=1:nbfield
|
---|
136 | if checkrun
|
---|
137 | update_waitbar(hseries.waitbar_frame,WaitbarPos,ifile/nbfield)
|
---|
138 | stopstate=get(hseries.RUN,'BusyAction');
|
---|
139 | else
|
---|
140 | stopstate='queue';
|
---|
141 | end
|
---|
142 | if isequal(stopstate,'queue') % enable STOP command
|
---|
143 | if ~isempty(j1_series)
|
---|
144 | j1=j1_series{1}(ifile);
|
---|
145 | end
|
---|
146 | filename=fullfile_uvmat(RootPath{1},SubDir{1},RootFile{1},FileExt{1},NomType{1},i1_series{1}(ifile),[],j1);
|
---|
147 | A=read_image(filename,FileType{1},MovieObject{1},frame_index{1}(ifile));
|
---|
148 | if ndims(A)==3;%color images
|
---|
149 | A=sum(double(Aread),3);% take the sum of color components
|
---|
150 | end
|
---|
151 | % operation on images
|
---|
152 | A=levels(A);
|
---|
153 | filename_new=fullfile_uvmat(RootPath{1},Param.OutputSubDir,RootFile{1},FileExtOut,NomTypeOut,i1_series{1}(ifile),[],j1);
|
---|
154 | imwrite(A,filename_new)
|
---|
155 | display([filename_new ' written'])
|
---|
156 | end
|
---|
157 | end
|
---|
158 | %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
|
---|
159 |
|
---|
160 |
|
---|
161 | function C=levels(A)
|
---|
162 | %whos A;
|
---|
163 | B=double(A(:,:,1));
|
---|
164 | windowsize=round(min(size(B,1),size(B,2))/20);
|
---|
165 | windowsize=floor(windowsize/2)*2+1;
|
---|
166 | ix=[1/2-windowsize/2:-1/2+windowsize/2];%
|
---|
167 | %del=np/3;
|
---|
168 | %fct=exp(-(ix/del).^2);
|
---|
169 | fct2=cos(ix/(windowsize-1)/2*pi/2);
|
---|
170 | %Mfiltre=(ones(5,5)/5^2);
|
---|
171 | %Mfiltre=fct2';
|
---|
172 | Mfiltre=fct2'*fct2;
|
---|
173 | Mfiltre=Mfiltre/(sum(sum(Mfiltre)));
|
---|
174 |
|
---|
175 | C=filter2(Mfiltre,B);
|
---|
176 | C(:,1:windowsize)=C(:,windowsize)*ones(1,windowsize);
|
---|
177 | C(:,end-windowsize+1:end)=C(:,end-windowsize+1)*ones(1,windowsize);
|
---|
178 | C(1:windowsize,:)=ones(windowsize,1)*C(windowsize,:);
|
---|
179 | C(end-windowsize+1:end,:)=ones(windowsize,1)*C(end-windowsize,:);
|
---|
180 | C=tanh(B./(2*C));
|
---|
181 | [n,c]=hist(reshape(C,1,[]),100);
|
---|
182 | % figure;plot(c,n);
|
---|
183 |
|
---|
184 | [m,i]=max(n);
|
---|
185 | c_max=c(i);
|
---|
186 | [dummy,index]=sort(abs(c-c(i)));
|
---|
187 | n=n(index);
|
---|
188 | c=c(index);
|
---|
189 | i_select = find(cumsum(n)<0.95*sum(n));
|
---|
190 | if isempty(i_select)
|
---|
191 | i_select = 1:length(c);
|
---|
192 | end
|
---|
193 | c_select=c(i_select);
|
---|
194 | n_select=n(i_select);
|
---|
195 | cmin=min(c_select);
|
---|
196 | cmax=max(c_select);
|
---|
197 | C=(C-cmin)/(cmax-cmin)*256;
|
---|
198 | C=uint8(C);
|
---|