-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathspotDetector.m
234 lines (189 loc) · 7.02 KB
/
spotDetector.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
% [frameInfo imgDenoised] = detectSpotsWT(img, S, dthreshold, postProcLevel)
%
% Performs detection of local intensity clusters through a combination of
% multiscale products and denoising by iterative filtering from
% significant coefficients:
% Olivo-Marin, "Extraction of spots in biological images using multiscale products," Pattern Recoginition 35, pp. 1989-1996, 2002.
% Starck et al., "Image Processing and Data Analysis," Section 2.3.4, p. 73
%
% INPUTS: img : input image (2D array)
% {S} : postprocessing level.
% {dthreshold} : minimum allowed distance of secondary maxima in large clusters
% {postProcLevel} : morphological post processing level for mask
% Parts of this function are based on code by Henry Jaqaman.
% Francois Aguet, March 2010
function [frameInfo imgDenoised] = spotDetector(img, S, dthreshold, postProcLevel)
if nargin<2
S = 4;
end
if nargin<3
dthreshold = 5;
end
if nargin<4
postProcLevel = 1;
end
maxI = max(img(:));
minI = min(img(:));
[ny nx] = size(img);
%===================================================
% Iterative filtering from significant coefficients
%===================================================
imgDenoised = significantCoefficientDenoising(img, S);
res = img - imgDenoised; % residuals
sigma_res0 = std(res(:));
delta = 1;
while delta > 0.002
resDenoised = significantCoefficientDenoising(res, S);
imgDenoised = imgDenoised + resDenoised; % add significant residuals
res = img - imgDenoised;
sigma_res1 = std(res(:));
delta = abs(sigma_res0/sigma_res1 - 1);
sigma_res0 = sigma_res1;
end
%===================================================
% Multiscale product of wavelet coefficients
%===================================================
% The support of the objects is given by the multiscale product in the wavelet domain.
W = awt(imgDenoised, S);
imgMSP = abs(prod(W(:,:,1:S),3));
%===================================================
% Binary mask
%===================================================
% Establish thresholds
[imAvg imStd] = localAvgStd2D(imgDenoised, 9);
mask = zeros(ny,nx);
mask((imgDenoised >= imAvg+0.5*imStd) & (imgDenoised.*imgMSP >= mean(imgDenoised(:)))) = 1;
% Morphological postprocessing
mask = bwmorph(mask, 'clean'); % remove isolated pixels
mask = bwmorph(mask, 'fill'); % fill isolated holes
mask = bwmorph(mask, 'thicken');
mask = bwmorph(mask, 'spur'); % remove single pixels 8-attached to clusters
mask = bwmorph(mask, 'spur');
mask = bwmorph(mask, 'clean');
if postProcLevel >= 1
mask = bwmorph(mask, 'erode');
if postProcLevel == 2
mask = bwmorph(mask, 'spur');
end
mask = bwmorph(mask, 'clean');
mask = bwmorph(mask, 'thicken');
end
% rescale denoised image
imgDenoised = (imgDenoised-min(imgDenoised(:))) * (maxI-minI) / (max(imgDenoised(:))-min(imgDenoised(:)));
imgDenoised = mask.*imgDenoised;
localMax = locmax2d(imgDenoised, [9 9]);
%===================================================
% Process connected components
%===================================================
[labels, nComp] = bwlabel(mask, 8);
area = zeros(nComp, 1);
totalInt = zeros(nComp, 1);
nMaxima = zeros(nComp, 1);
xmax = zeros(nComp, 1);
ymax = zeros(nComp, 1);
xcom = zeros(nComp, 1);
ycom = zeros(nComp, 1);
labelVect = zeros(nComp, 1);
xmax2 = cell(nComp, 1);
ymax2 = cell(nComp, 1);
area2 = cell(nComp, 1);
totalInt2 = cell(nComp, 1);
labelVect2 = cell(nComp, 1);
% Compute area and center of mass for each component
stats = regionprops(labels, imgDenoised, 'Area', 'WeightedCentroid', 'PixelIdxList');
% component labels of local maxima
maxLabels = labels .* (labels & localMax>0);
maxCoords(1:nComp) = struct('PixelIdxList', []);
mc = regionprops(maxLabels, 'PixelIdxList');
maxCoords(1:length(mc)) = deal(mc);
for n = 1:nComp
%[yi,xi] = find(labels == n); % coordinates of nth component
[yi,xi] = ind2sub([ny nx], stats(n).PixelIdxList);
[ym,xm] = ind2sub([ny nx], maxCoords(n).PixelIdxList);
area(n) = stats(n).Area;
com = stats(n).WeightedCentroid;
xcom(n) = com(1);
ycom(n) = com(2);
values = imgDenoised(stats(n).PixelIdxList);
totalInt(n) = sum(values);
nMaxima(n) = length(xm);
if nMaxima(n)==1
xmax(n) = xm;
ymax(n) = ym;
nMaxima(n) = 1;
labelVect(n) = labels(ym,xm);
elseif nMaxima(n)==0 % no maximum was detected for this cluster
maxValueIdx = find(values == max(values));
xmax(n) = xi(maxValueIdx(1));
ymax(n) = yi(maxValueIdx(1));
nMaxima(n) = 1;
labelVect(n) = labels(ymax(n), xmax(n));
else % resolve multiple maxima cases
maxValues = localMax(sub2ind(size(localMax), ym, xm)); % highest local max
maxIdx = find(maxValues == max(maxValues));
xmax(n) = xm(maxIdx(1));
ymax(n) = ym(maxIdx(1));
labelVect(n) = labels(ymax(n), xmax(n));
% remove highest max from list
xm(maxIdx(1)) = [];
ym(maxIdx(1)) = [];
% compute distance of secondary maxima to primary
dist2max = sqrt((xmax(n)-xm).^2 + (ymax(n)-ym).^2);
dist2com = sqrt((xcom(n)-xm).^2 + (ycom(n)-ym).^2);
mindist = min(dist2max,dist2com);
% retain secondary maxima where mindist > threshold
idx2 = find(mindist > dthreshold);
if ~isempty(idx2)
xmax2{n} = xm(idx2);
ymax2{n} = ym(idx2);
nSecMax = length(idx2);
nMaxima(n) = nSecMax+1;
% split area
area2{n} = area(n)*ones(nSecMax,1)/nMaxima(n);
area(n) = area(n)/nMaxima(n);
labelVect2{n} = labels(sub2ind(size(labels), ymax2{n}, xmax2{n}));
%intensity values
totalInt2{n} = totalInt(n)*ones(nSecMax,1)/nMaxima(n);
totalInt(n) = totalInt(n)/nMaxima(n);
end
end
end
xmax2 = vertcat(xmax2{:});
ymax2 = vertcat(ymax2{:});
totalInt2 = vertcat(totalInt2{:});
area2 = vertcat(area2{:});
labelVect2 = vertcat(labelVect2{:});
% assign results to output structure
frameInfo.xmax = [xmax; xmax2(:)];
frameInfo.ymax = [ymax; ymax2(:)];
frameInfo.xcom = [xcom; xmax2(:)];
frameInfo.ycom = [ycom; ymax2(:)];
frameInfo.totalInt = [totalInt; totalInt2(:)];
frameInfo.area = [area; area2(:)];
frameInfo.nMaxima = nMaxima; % maxima per component
frameInfo.labels = [labelVect; labelVect2(:)];
frameInfo.nComp = nComp;
frameInfo.maxI = maxI;
frameInfo.minI = minI;
% prepare fields for tracker
nObj = length(frameInfo.xmax);
frameInfo.amp = zeros(nObj,2);
frameInfo.xCoord = zeros(nObj,2);
frameInfo.yCoord = zeros(nObj,2);
frameInfo.amp(:,1) = frameInfo.totalInt;
frameInfo.xCoord(:,1) = frameInfo.xcom;
frameInfo.yCoord(:,1) = frameInfo.ycom;
frameInfo.path = [];
frameInfo.maskPath = [];
%=======================
% Subfunctions
%=======================
function result = significantCoefficientDenoising(img, S)
mask = zeros(size(img));
result = zeros(size(img));
W = awt(img, S);
for s = 1:S
tmp = W(:,:,s);
mask(abs(tmp) >= 3*std(tmp(:))) = 1;
result = result + tmp.*mask;
end