forked from carmlingling/PeGSVector
-
Notifications
You must be signed in to change notification settings - Fork 0
/
particleDetect.m
executable file
·287 lines (233 loc) · 9.93 KB
/
particleDetect.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
%function particle_detect(directory)
% A script to find particle locations
function particleDetect(directory, imname, radiusRange, boundaryType, verbose)
% directory = './';
% imname = 'test.jpg';
% radiusRange = [40, 57];
% boundaryType = "annulus";
% verbose = false;
if not(isfolder(append(directory,'particles'))) %make a new folder with particle centers
mkdir(append(directory,'particles'));
end
if boundaryType == "annulus"
images=dir([directory, '/warpedimg/',imname(1:end-4),'warped.tif']);
nFrames = length(images);
cen = [2710, 2768]; %measure the center of annulus in images taken by the camera
rad = [2830/2, 5330/2];
% cen = [71+5313/2, 110+5313/2];
%rad = [2783/2, 5313/2]; %measured in imageJ, pixels in untransformed image should be same as preprocess
R_overlap = 15; %how much do the particle have to overlap by to remove double detected particles
dtol = 25; %distance from edge
for frame = 1:nFrames
frame
im = imread([images(frame).folder,'/', images(frame).name]);
red = im(:,:,1);
green = im(:,:,2);
red = imsubtract(red, green*0.2); %this works for the annulus images, removes excess green
red = imadjust(red, [0.20,0.65]); %this works for annulus, might need to tweak, brightens image
sigma = 50; % chosen by visual inspection
G = fspecial('gaussian', 3*sigma+1, sigma);
yb = imfilter(red, G, 'replicate'); %removes large scale image features like bright spots
red = bsxfun(@minus, red,yb);
% if you want to check out the images
if verbose == true
h1 = figure(1);
ax1 = axes('Parent', h1);
%subplot(1, 2, 1)
imshow(red)
%subplot(1, 2, 2)
%green =imadjust(green);
%imshow(green);
hold on;
axis on
end
[centers,radii,metrics]=imfindcircles(red,radiusRange,'objectpolarity','dark','sensitivity',0.945,'method','twostage','EdgeThreshold',0.02);%values found by tweaking
%%
xt = centers(:,1);
yt = centers(:,2);
rt = radii;
%binarize radius
rt(rt<49) = 44;
rt(rt>49) = 55;
%% %beginning cleaning section
%convert back to real space
[midx,midy] = size(red);
[theta,r] = cart2pol(xt-midx/2,yt-midy/2);
d = -6.5*r.^2/(200*(925+6.5)); %6.5 is the thickness of the particles in mm, 925 is distance between particles and camera lens in mm
rmax = max(r(:));
s1 = d+r;
[ut,vt] = pol2cart(theta,s1);
ut = ut + midx/2;
vt = vt + midy/2;
ifcn = @(c) [ut(:) vt(:)];
tform = geometricTransform2d(ifcn);
[uv] = transformPointsInverse(tform, [0,0]); %particle original coordinates
u = uv(:,1)-400;
v = uv(:,2)-400;
% figure;
% imold = imread([directory, '/', images(frame).name]);
% imshow(imold)
% if verbose
% viscircles([u, v], rt)
% N = length(uv);
% for n=1:N
% text(u(n),v(n),num2str(n),'Color','w');
% end
% hold on;
% end
%remove some of the misfound particles too close to the center
radialPos = sqrt((u-cen(1)).^2+(v-cen(2)).^2);
closeind = find(radialPos <= rad(1)+15 );
closeind = sortrows(closeind, 'descend');
xt(closeind) = [];
yt(closeind) = [];
radii(closeind) = [];
rt(closeind) = [];
metrics(closeind)= [];
u(closeind) = [];
v(closeind) = [];
if verbose
viscircles([xt, yt], rt,'EdgeColor', 'b')
end
%%
%now we look for particles with a dramatic overlap
dmat = pdist2([u,v],[u,v]); %Creates a distance matrix for particle center locations
rmat = rt + rt'; %Makes a combination of radii for each particle
friendmat = dmat < (rmat - 25) & dmat~=0; %Logical "friend" matrix
[f1, f2] = find(friendmat == 1);
badind = zeros(length(f1),1);
M = length(f1);
%this picks out the worse circle
for n=1:M
if metrics(f1(n)) > metrics(f2(n))
badind(n) = f2(n);
else
badind(n) = f1(n);
end
end
badind = badind(badind~=0);
badind = unique(badind);
badind = sortrows(badind, 'descend');
xt(badind) = [];
yt(badind) = [];
radii(badind) = [];
rt(badind) = [];
metrics(badind)= [];
u(badind) = [];
v(badind) = [];
if verbose
viscircles([xt, yt], rt,'EdgeColor','g'); %draw particle outline
hold on;
end
%%
dmat = pdist2([u,v],[u,v]);
rmat = rt + rt';
friendmat = dmat < (rmat -8 ) & dmat~=0; %Logical "friend" matrix
% %friendmat = triu(friendmat); %Only examine the upper triangle portion (no repeats)
[f1, f2] = find(friendmat == 1);
% [f3, f4] = find(friendmat2 == 1);
badind = unique(f2);
M = length(badind);
toobig = zeros(M, 1);
%
badin2 = unique(f2);
for n=1:M
sum(f2 == badin2(n));
if sum(f2 == badin2(n))>2
if rt(badind(n)) > 49
rt(badind(n)) = 44;
toobig(n) = badind(n);
end
end
end
toobig = toobig(toobig~=0);
%viscircles([u(toobig),v(toobig)], rt(toobig), edgecolor = 'y');
%%
radialPos = sqrt((u-cen(1)).^2+(v-cen(2)).^2);
owi= find(radialPos <= rad(2)+2.5*dtol &radialPos >=rad(2)-2.5*dtol);
iwi = find(radialPos <= rad(1)+3.5*dtol &radialPos >=rad(1)-2.5*dtol);
edges = zeros(length(u), 1);
edges(owi) = 1;
edges(iwi) = -1;
particle = [xt, yt, rt, edges];
writematrix(particle,[directory,'particles/', images(frame).name(1:end-4),'_centers.txt'])
end
elseif boundaryType == "airtable"
dtol = 10;
images=dir([directory,imname]);
nFrames = length(images);
for frame = 1:nFrames
%for frame = 1:1
%frame = 663
im = imread([directory,images(frame).name]);
red = im(:,:,1);
green = im(:,:,2);
red = imsubtract(red, green*0.05);
%imfindcircles(red,radius,'objectpolarity','dark','sensitivity',0.945,'method','twostage','EdgeThreshold',0.02)
[centers, radii, metrics] = imfindcircles(red,radiusRange,'ObjectPolarity','bright','Method','TwoStage','Sensitivity',0.945);
% for r=1:length(radii) %binarize radius
% if radii(r)>65
% radii(r) = 70;
% else
% radii(r) = 49;
% end
% end
x = centers(:,1);
y = centers(:,2); %for brevity in coding the next bit
dmat = pdist2([x,y],[x,y]); %Creates a distance matrix for particle center locations
rmat = radii + radii'; %Makes a combination of radii for each particle
friendmat = dmat < (rmat - 25) & dmat~=0; %Logical "friend" matrix
[f1, f2] = find(friendmat == 1);
badind = zeros(length(f1),1);
M = length(f1);
for n=1:M
if metrics(f1(n)) > metrics(f2(n))
badind(n) = f2(n);
else
badind(n) = f1(n);
end
end
badind = badind(badind~=0);
badind = unique(badind);
badind = sortrows(badind, 'descend');
centers(badind,:) = [];
radii(badind) = [];
imshow(red);
viscircles(centers, radii);
if length(radii)>29
for m = 1:length(radii)-29
text(centers(m+29,1), centers(m+29,2), num2str(m+29) )
end
end
drawnow
if length(radii)>29
for m = length(radii)-29:-1:1
centers(m+29,:)=[];
radii(m+29)=[];
end
end
lpos = min(centers(:,1)-radii);
rpos = max(centers(:,1)+radii);
upos = max(centers(:,2)+radii);
bpos = min(centers(:,2)-radii);
lwi = find(centers(:,1)-radii <= lpos+dtol);
rwi = find(centers(:,1)+radii >= rpos-dtol);
uwi = find(centers(:,2)+radii >= upos-dtol);
bwi = find(centers(:,2)-radii <= bpos+dtol); %need to add edge case of corner particle
edges = zeros(length(radii), 1);
edges(rwi) = 1;
edges(lwi) = -1;
edges(uwi) = 2;
edges(bwi) = -2;
for q = 1:length(uwi)
d = centers(uwi(q),2)+radii(uwi(q))-799;
if d>=0
centers(uwi(q),2)=centers(uwi(q),2)-d-0.5;
end
end
particle = [centers(:,1), centers(:,2), radii, edges];
[directory,'particles/', images(frame).name(1:end-4),'_centers.txt']
dlmwrite([directory,'particles/', images(frame).name(1:end-4),'_centers.txt'], particle);
end
end
%dlmwrite([directory,images(frame).name(1:end-4),'centers_Improved.txt'],particle)