-
Notifications
You must be signed in to change notification settings - Fork 0
/
fsmPrepMainSecondarySpeckles.m
333 lines (284 loc) · 11.7 KB
/
fsmPrepMainSecondarySpeckles.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
function [IMfinal,candsTot]=fsmPrepMainSecondarySpeckles(I,strg,counter,noiseParam,Speckles,enhTriang,fsmParam,oI)
% fsmPrepMainSecondarySpeckles is the main function of the fsmPrepSecondarySpeckles sub-module
%
% Plots PSFs on the positions of the primary speckles (found by locmax operator) and substructs
% them from the filtered data. Appled again on the resulting image, the locmax-operator finds
% new (secondary) speckles (intensity and distance significance tests applied)
%
%
% SYNOPSIS [IMfinal,candsTot]=fmsPrepMainSecondarySpeckles(I,strg,counter,noiseParam,Speckles,enhTriang)
%
% INPUT I : filtered image
% strg : format string for the correct file numbering
% counter : image number
% noiseParam : noise parameters for statistical speckle selection
% Speckles : (1) contains information for the hierarchical level
% (2) minimal increase in (%) of new speckles
% (before stopping)
% enhTriang : turns on enhanced triangulation for Matlab Version < 6.5
% fsmParam : (optional) fsmParam structure for SpeckTackle
% oI : original image necessary for subpixel accuracy
% determination; is normalized, but NOT filtered
%
% OUTPUT candsTot : augmented cands structure (see fsmPrepTestLocalMaxima.m)
% IMfinal : local maxima map
%
%
%
% DEPENDENCES fsmPrepMainSecondarySpeckles uses { fsmPrepConfirmSpeckles, fsmPrepSubstructMaxima,
% fsmPrepCheckDistance, fsmPrepUpdateImax, fsmPrepCheckInfo}
% fsmPrepMainSecondarySpeckles is used by { fsmPrepMain }
%
% Alexandre Matov, November 7th, 2002
if nargin==0
locDEBUG=1;
DEBUG=0;
Speckles=[3 0.0/100];
% Speckles=[8 0.0/100]; % default
% load first image
[fileName,dirName] = uigetfile('*.tif','Choose an image');
I=imread([dirName,filesep,fileName]);
SIG=1.88;
enhTriang=0;
% I=double(I); %??
IG=prepareRowData(I,SIG);
strg=[];
counter=0;
shift=4;%15
% format: [Q/GaussRatio sDN beta I0 Q]
noiseParam=[1.4700 0.0001 0.0002 0.0313 1.9600]; % 488 S1 AOTF488_15_1.tif
SAVEINFO=0;
else
IG=I;
SAVEINFO=1;
if strg == 0 % if you provide all the fields but dont wanna write to disc
SAVEINFO=0;
end
locDEBUG=0;
DEBUG=0;
end
if nargin==6
fsmParam=[];
userROIbw=[];
end
if ~isempty(fsmParam)
if fsmParam.prep.drawROI~=0 % Either 1 (drawn) or 2 (loaded)
% Load user-defined ROI from disk
ROIname=[fsmParam.main.path,filesep,'userROI.mat'];
if exist(ROIname)==2 % File found
tmp=load(ROIname);
userROIbw=tmp.userROIbw;
clear tmp;
else
userROIbw=[];
end
else
userROIbw=[];
end
end
% SIG=1.60;
% SIG=1.77;
SIG=1.88; % for the twice convolved image (or 1.77)
% local minima
Imin=locmin2d(IG,[3,3]);
% intial (filtered) image
[yi,xi,y,x,Imax,candsP,triMin,pMin]=fsmPrepConfirmSpeckles(IG,Imin,noiseParam,enhTriang,userROIbw); % TO DO: update cands
aux=length(candsP);
for i=1:aux
candsP(i).speckleType=1;
end
candsTot=candsP;
Inew=IG;
candsS=candsP;
HierLevel=2;
while HierLevel<=Speckles(1) & length(candsS)>(Speckles(2)*length(candsTot)) & length(find([candsS.status]==1))>0
[Inew,Imaxima,nmB]=fsmPrepSubstructMaxima(Inew,Imax,SIG,candsS); % prednite Cands
[yni,xni,yn,xn,Imax,candsS]=fsmPrepConfirmLoopSpeckles(Inew,noiseParam,enhTriang,triMin,pMin,IG,userROIbw);
% [yni,xni,yn,xn,Imax,candsS]=fsmPrepConfirmSpeckles(Inew,Imin,noiseParam,enhTriang);
aux=length(candsS);
for i=1:aux
candsS(i).speckleType=HierLevel; % type flag
end
candsS=fsmPrepCheckDistance(candsS,candsTot);
HierLevel=HierLevel+1;
if ~isempty(candsS)
candsTot=cat(2,candsTot,candsS); % concatenating secondary and primary cands structures
end
end
% remove repetitions because of secondary speckles apearing on the same positions as primary (because of floating background)
candsTot=fsmPrepCheckInfo(candsTot);
% obtain updated IM from candstTot
IMfinal=zeros(size(IG));
for i=1:length(candsTot)
if candsTot(i).status==1
IMfinal(candsTot(i).Lmax(1),candsTot(i).Lmax(2))=candsTot(i).ILmax;
end
end
[yMfinal,xMfinal]=find(ne(IMfinal,0));
% Save speckle information (cands and locMax) to disk for future use
if SAVEINFO==1
locMax=IMfinal;
cands=candsTot;
indxStr=sprintf(strg,counter);
eval(strcat('save cands',filesep,'cands',indxStr,'.mat cands;')); % Save speckle info
eval(strcat('save locMax',filesep,'locMax',indxStr,'.mat locMax;')); % Save loc max positions
end
%-----------------------------------------
% Estimate subpixel positions of speckles
%-----------------------------------------
% The size of the GaussKernel used for filtering the image (in
% fsmPrepareImage) has, as of March 2005, been set to the real value of
% psfsigma as determined by the physical parameters,
% psfsigma=0.21*(lambda/NA)/pixelsize
% Thus, the the mixture model fitting may be performed without loss of
% information on the filtered image (called I), rather than the original
% image (called oI)
%
% However, it is important to note that performing the Gauss fit in the
% mixture model on the filtered image (which is broadened), rather than on
% the original one, requires to modify the sigma of the mixture-model fit!
if fsmParam.prep.subpixel==1
cands=candsTot;
psfsigma = fsmParam.prep.psfSigma; % true physical sigma of the image point-spread function, caluclated by sigma=0.21*(lambda/NA)/pixelsize
filtersigma = fsmParam.prep.filterSigma; % sigma used for the low-pass filtering; except where specifically
% stated differently by the user, filtersigma should have the same value as psfsigma;
% for filtersigma>psfsigma, image information is lost during filtering!! % same value as
%mixture model Gauss sigma (mmsigma) is calculated from psfsigma and
%filtersigma; in the usual case where psfsigma=filtersigma, then
%mmsigma=sqrt(2)*psfsigma
mmsigma=sqrt(psfsigma^2+filtersigma^2);
image=I;
disp(['psfsigma=',num2str(psfsigma),' filtersigma=',num2str(filtersigma),' mixmodsigma=',num2str(mmsigma)]);
disp('calculating sub-pixel locations...');
[candsSP] = candsToSubpixelN(image,cands,mmsigma);
eval( (strcat('save cands',filesep,'cands',indxStr,'_spa.mat candsSP;')) );
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% DEBUG FIGURES:
if locDEBUG==1
if DEBUG==1
% length(find(IM~=0))
length(candsTot)
c1=0;
for i=1:length(candsTot)
if candsTot(i).status==1
c1=c1+1;
SNR(c1)=candsTot(i).deltaI/candsTot(i).sigmaLmax;
end
end
figure(100),plot(SNR);
MEAN_SNR=mean(SNR)
c2=0;
for j=1:length(candsTot)
if candsTot(j).status==0
c2=c2+1;
SNRins(c2)=candsTot(j).deltaI/candsTot(j).sigmaLmax;
end
end
figure(101),plot(SNRins);
MEAN_SNR_ins=mean(SNRins)
PRIMARY=size(y,1)
Primary_rejected_noiseModel=size(yi,1)-size(y,1)
PRIMARY_rejected_procentage=Primary_rejected_noiseModel/size(yi,1)*100
% the figure is showing all local maxima and also only the significant ones
figure(1);
imshow(IG(1+shift:end-shift,1+shift:end-shift),[]);
hold on;
plot(xi-shift,yi-shift,'y*');
plot(x-shift,y-shift,'r.');
hold off;
title('all the local maxima in yellow and the significant ones in red asteriques');
% local maxima points in the raw data
figure(2);
surf(Imax);
title('poinst at positions of the local maxima but with intensity DELTA.I');
figure(19);
imshow(IG(1+shift:end-shift,1+shift:end-shift),[]);
hold on;
plot(x-shift,y-shift,'r*');
plot(xn-shift,yn-shift,'g*');
hold off;
title('red asteriques are the initial local maxima and the green dots are the secondary speckles found');
figure(20);
imshow(IG(1+shift:end-shift,1+shift:end-shift),[]);
hold on;
plot(x-shift,y-shift,'r*');
hold off;
title('red asteriques are the initial local maxima and the green and blue dots are the secondary speckles found');
figure(21);
imshow(IG(1+shift:end-shift,1+shift:end-shift),[]);
hold on;
plot(x-shift,y-shift,'r*');
hold off;
title('red asteriques are the initial local maxima and the green dots are the secondary speckles found');
figure(22);
imshow(IG(1+shift:end-shift,1+shift:end-shift),[]);
hold on;
for i=1:length(candsTot)
if candsTot(i).status==1
plot(candsTot(i).Lmax(2)-shift,candsTot(i).Lmax(1)-shift,'y.');
end
end
hold off;
title('red asteriques - IM; yellow dots - cands');
end
% figure
% imshow(IG(1+shift:end-shift,1+shift:end-shift),[]);
% hold on
% for i=1:length(candsTot)
% if candsTot(i).status==1
% plot(candsTot(i).Lmax(2)-shift,candsTot(i).Lmax(1)-shift,'y.');
% end
% end
% hold off
% title('all speckles yellow');
% END WAS HERE
%end
SECONDARY=0;
PRIMARY=0;
TERTIARY=0;
QUATTRO=0;
CINQUE=0;
SHEST=0;
SEDEM=0;
OSEM=0;
shift=4;%15
figure,imshow(IG(1+shift:end-shift,1+shift:end-shift),[]);
hold on
for i=1:length(candsTot)
if candsTot(i).status==1
switch candsTot(i).speckleType
case 1
plot(candsTot(i).Lmax(2)-shift,candsTot(i).Lmax(1)-shift,'g.'); PRIMARY=PRIMARY+1;
case 2
plot(candsTot(i).Lmax(2)-shift,candsTot(i).Lmax(1)-shift,'r.'); SECONDARY=SECONDARY+1;
case 3
plot(candsTot(i).Lmax(2)-shift,candsTot(i).Lmax(1)-shift,'b.'); TERTIARY=TERTIARY+1;
case 4
plot(candsTot(i).Lmax(2)-shift,candsTot(i).Lmax(1)-shift,'c*'); QUATTRO=QUATTRO+1;
case 5
plot(candsTot(i).Lmax(2)-shift,candsTot(i).Lmax(1)-shift,'m*'); CINQUE=CINQUE+1;
case 6
plot(candsTot(i).Lmax(2)-shift,candsTot(i).Lmax(1)-shift,'y.'); SHEST=SHEST+1;
case 7
plot(candsTot(i).Lmax(2)-shift,candsTot(i).Lmax(1)-shift,'y*'); SEDEM=SEDEM+1;
case 8
plot(candsTot(i).Lmax(2)-shift,candsTot(i).Lmax(1)-shift,'y*'); OSEM=OSEM+1;
otherwise
error('wrong classification of secondary speckles');
end
end
end
hold off
title('the green spots denote significant speckles');
title('green-primary red-secondary blue-tertiary cyan-quattro magenta-cinque yellow-shest');
PRIMARY
SECONDARY
TERTIARY
QUATTRO
CINQUE
SHEST
SEDEM
OSEM
% MOVE IT BACK DOWN
end