-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathfindClosestGM.m
347 lines (324 loc) · 12.7 KB
/
findClosestGM.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
334
335
336
337
338
339
340
341
342
343
344
345
346
347
function [closeGM sertoliLbl centers GMandSertoli posNeg] = findClosestGM(sertoli, connectedGerm, ND, Im, thre)
% function [closeGM sertoliLbl centers GMandSertoli posNeg] ...
% = findClosestGM(sertoli, connectedGerm, ND, Im, thre)
% This will get the Sertoli cells and germ-mass to build a data structure
% that hold infomation such as closest germ-mass, neighboring sertoli cells
% etc. More information can be found in 5.1 "Assign non-germ cells to germ-mass"
% Input;
% sertoli, a binary image contains the Sertoli cells.
% connectedGerm, a binary image of germ-mass.
% ND, an integer, maximum neighborhood distance.
% Im (optional), an image with the same size as sertoli.
% thre(optional needed if Im is given), a threshold value for
% thresholding the Im to count the number crosses when connecting a cell
% to an germ-mass.
% Ouput:
% closeGM, is a cell array that has entry for each Sertoli cell and each
% entry is array of 3 cell; First, a column array stores the label indices
% of neighboring Sertoli cells in the specified neighborhood distance;
% Second 2D array that each row has germ-mass label, distance to closest
% point in the border of germ-mass, row and column indices of that point,
% and the number of white pixels thresholded Im if it has given; I need
% to mention that this matrix is sorted based on the cell's distance to
% the germ-mass;Third one has tha same number of row as the second one
% and store the indices of srtoli celles that are in-between the current
% sertoli and germ mass;
% Note: it is good to replace the cell array with structure array which
% is more convinient.
% sertoliLbl, an image contains the labeled Sertoli cells.
% centers, a 2D array that each row has the centroid of the corresponding
% Sertoli cells.
% GMandSertoli, a label matrix that the Sertoli cells are labeled as
% sertoliLbl and the Germ-mass regions are labled with negetive numbers.
% posNeg, an structure that have two field 'pos' and 'neg' that contains
% information for all neighbors of the Sertoli cells; the connection
% between the sertoli cell and the Germ-mass splite the plane into two
% negetive and positive plane that the neighbors of the cell in each
% plane are stored in corresponding feild.
%
% Author: A.Rahim Kadkhodamohammadi ([email protected])
% 01 June 2012 CBA, Uppsala University
%--------------------------------------------------------------
sertoliLbl = bwlabel(sertoli,4);
germBoundary = imdilate(connectedGerm,strel('disk',1)) - connectedGerm;
germBoundaryLbl = bwlabel(germBoundary,8);
stats = regionprops(sertoliLbl, 'Centroid');
num = numel(stats);
% cell of 2D that the first column store a list of direct germ mass
% neighbors and their distance. The second column store the list of germ
% Mass that connected to the current sertoli cell via another sertoli
% cells. So, it includes germ mass label, distance and connecting sertoli.
closeGM = cell(num,1);
posNeg = cell(num,1);
GMandSertoli = sertoliLbl - germBoundaryLbl;
[row, col] = size(GMandSertoli);
centers = round(cat(1,stats.Centroid));
switch nargin
case 3
for i = 1 : num
cntr = centers(i,:);
NS = neighboringSertoli(i, cntr, row,col,GMandSertoli,ND);
if numel(NS{1}) > 0
directNS = directNeighbor(i, centers(i,:), NS{1}, centers, ...
sertoliLbl, row, col);
NS{1} = directNS;
end
closeGM{i} = NS;
end
for i = 1 : num
cntr = centers(i,:);
NS = closeGM{i};
if numel(NS{1}) > 0
[nGermMass ~] = size(NS{2});
if nGermMass > 0
posNeg{i} = separateNeighbor(nGermMass, NS, cntr, centers, closeGM);
end
end
end
case 5
% give each sertoli germ mass a weight which is the portion of pixels
% above threshold per length of connecting line
BW = Im > thre;
for i = 1 : num
cntr = centers(i,:);
NS = neighboringSertoliWeight(i, cntr, row,col,GMandSertoli,ND, BW);
if numel(NS{1}) > 0
directNS = directNeighbor(i, centers(i,:), NS{1}, centers, ...
sertoliLbl, row, col);
NS{1} = directNS;
end
closeGM{i} = NS;
end
for i = 1 : num
cntr = centers(i,:);
NS = closeGM{i};
if numel(NS{1}) > 0
[nGermMass ~] = size(NS{2});
if nGermMass > 0
posNeg{i} = separateNeighbor(nGermMass, NS, cntr, centers, closeGM);
end
end
end
otherwise
error('Please check the function for correct number of input');
end
end
function NS = neighboringSertoli(currentR, centroid, row,col,lbl,ND)
currentBox = lbl(max(1,centroid(2)-ND): min(centroid(2)+ND, row), ...
max(1,centroid(1)-ND): min(centroid(1)+ND, col));
[~, ~, v] = find(currentBox);
NS = {[], [], []};
v = sort(v);
for j = 1 : numel(v)-1
if (v(j) ~= currentR && v(j) ~= v(j+1))
if v(j) > 0
NS{1} = [NS{1}, v(j)];
else
NS{2} = [NS{2}, v(j)];
end
end
end
if (numel(v) > 0 && v(end) ~= currentR )
if v(end) > 0
NS{1} = [NS{1}, v(end)];
else
NS{2} = [NS{2}, v(end)];
end
end
numGM = numel(NS{2});
if numGM > 0
tmpNS = zeros(numGM,4);
pixelPos = zeros(numGM,2);
cntrInROI = min(centroid, [1 1]+ND);
for l = 1 : numGM
[r,c] = find(currentBox == NS{2}(l));
D = sqrt((r-cntrInROI(2)).^2 + (c-cntrInROI(1)).^2);
[val, ind] =min(D);
tmpNS(l,1) = - NS{2}(l); % germ mass label
tmpNS(l,2) = val; % ditance of centriod and nearest pixel in boundary
% first coordinate of nearest pixel or row
tmpNS(l,3) = r(ind)+ max(1,centroid(2)-ND)- 1;
% second coordinate of nearest pixel or column
tmpNS(l,4) = c(ind)+max(1,centroid(1)-ND) - 1;
pixelPos(l, :) = [c(ind) r(ind)];
end
% sort neighboring germ mass based on distance
[~, sortTmpNS] = sort(tmpNS(:,2));
NS{2} = tmpNS(sortTmpNS, : );
pixelPos = pixelPos(sortTmpNS, :);
NS{3} = checkBridgeRegion(currentR, cntrInROI, NS{2},pixelPos, currentBox);
end
end
function NS = neighboringSertoliWeight(currentR, centroid, row,col,lbl,ND, BW)
currentBox = lbl(max(1,centroid(2)-ND): min(centroid(2)+ND, row), ...
max(1,centroid(1)-ND): min(centroid(1)+ND, col));
[~, ~, v] = find(currentBox);
NS = {[], [], []};
v = sort(v);
for j = 1 : numel(v)-1
if (v(j) ~= currentR && v(j) ~= v(j+1))
if v(j) > 0
NS{1} = [NS{1}, v(j)];
else
NS{2} = [NS{2}, v(j)];
end
end
end
if (numel(v) > 0 && v(end) ~= currentR )
if v(end) > 0
NS{1} = [NS{1}, v(end)];
else
NS{2} = [NS{2}, v(end)];
end
end
numGM = numel(NS{2});
if numGM > 0
tmpNS = zeros(numGM,5);
pixelPos = zeros(numGM,2);
cntrInROI = min(centroid, [1 1]+ND);
for l = 1 : numGM
[r,c] = find(currentBox == NS{2}(l));
D = sqrt((r-cntrInROI(2)).^2 + (c-cntrInROI(1)).^2);
[val, ind] =min(D);
tmpNS(l,1) = - NS{2}(l); % germ mass label
tmpNS(l,2) = val; % ditance of centriod and nearest pixel in boundary
% first coordinate of nearest pixel or row
tmpNS(l,3) = r(ind)+ max(1,centroid(2)-ND)- 1;
% second coordinate of nearest pixel or column
tmpNS(l,4) = c(ind)+max(1,centroid(1)-ND) - 1;
pixelPos(l, :) = [c(ind) r(ind)];
% weight sertoli-germMass connection by portion of set pixel in BW
% per line length.
[~, ~, cp] = improfile(BW, ...
[centroid(1), tmpNS(l,4)], [centroid(2), tmpNS(l,3)]);
setP = find(cp);
tmpNS(l,5) = length(setP); %/length(cp);
end
% sort neighboring germ mass based on distance
[~, sortTmpNS] = sort(tmpNS(:,2));
NS{2} = tmpNS(sortTmpNS, : );
pixelPos = pixelPos(sortTmpNS, :);
NS{3} = checkBridgeRegion(currentR, cntrInROI, NS{2},pixelPos, currentBox);
end
end
function BR = checkBridgeRegion(currentR, centroid, GM, pixelPos, lbl)
%neighborhood distance of centroid that is applied to check direct conection
n= 5;
numGM = size(GM,1);
BR = cell(numGM,1);
[row col] = size(lbl);
for i = 1 : numGM
% collecting some pixels in between regions
connectingPixels = [];
NearstP = pixelPos(i,:);
[x, y, cp] = improfile(lbl, ...
[centroid(1), NearstP(1)], [centroid(2), NearstP(2)]);
connectingPixels = [connectingPixels; cp];
cp = improfile(lbl, ...
max([centroid(1)-n, NearstP(1)],1), [centroid(2), NearstP(2)]);
connectingPixels = [connectingPixels; cp];
cp = improfile(lbl, ...
min([centroid(1)+n, NearstP(1)], col), [centroid(2), NearstP(2)]);
connectingPixels = [connectingPixels; cp];
cp = improfile(lbl, ...
[centroid(1), NearstP(1)], max([centroid(2)-n, NearstP(2)], 1));
connectingPixels = [connectingPixels; cp];
cp = improfile(lbl, ...
[centroid(1), NearstP(1)], min([centroid(2)+n, NearstP(2)], row));
connectingPixels = sort([connectingPixels; cp]);
for j = 1 : numel(connectingPixels)-1
if (connectingPixels(j) ~= 0 && connectingPixels(j) ~= currentR ...
&& connectingPixels(j) ~= connectingPixels(j+1) ...
&& connectingPixels(j) ~= -GM(i,1) )
BR{i} = [BR{i} connectingPixels(j)];
end
end
if (connectingPixels(end) ~= 0 && connectingPixels(end) ~= currentR ...
&& connectingPixels(j) ~= -GM(i,1) )
BR{i} = [BR{i} connectingPixels(end)];
end
end
end
function directNS = directNeighbor(currentR, centroid, NS, centers, lbl, row, col)
%neighborhood distance of centroid that is applied to check direct conection
n= 5;
directNS = [];
for i = 1 : numel(NS)
% collecting some pixels in between regions
connectingPixels = [];
NCentroid = centers(NS(i), :);
[x, y, cp] = improfile(lbl, ...
[centroid(1), NCentroid(1)], [centroid(2), NCentroid(2)]);
connectingPixels = [connectingPixels; cp];
cp = improfile(lbl, ...
max([centroid(1)-n, NCentroid(1)-n],1), [centroid(2), NCentroid(2)]);
connectingPixels = [connectingPixels; cp];
cp = improfile(lbl, ...
min([centroid(1)+n, NCentroid(1)+n], col), [centroid(2), NCentroid(2)]);
connectingPixels = [connectingPixels; cp];
cp = improfile(lbl, ...
[centroid(1), NCentroid(1)], max([centroid(2)-n, NCentroid(2)-n], 1));
connectingPixels = [connectingPixels; cp];
cp = improfile(lbl, ...
[centroid(1), NCentroid(1)], min([centroid(2)+n, NCentroid(2)+n], row));
connectingPixels = [connectingPixels; cp];
for j = 1 : numel(connectingPixels)
if (connectingPixels(j) ~= 0 && connectingPixels(j) ~= currentR ...
&& connectingPixels(j) ~= NS(i))
break;
end
end
if j == numel(connectingPixels)
directNS = [directNS, NS(i)];
end
end
end
function np = separateNeighbor(nGermMass, NS, cntr, centers, closeGM)
np = repmat(struct('neg',{},'pos',{}),nGermMass,1);
% compute the line between Sertoli's center and Germ mass border and divide
% neighboring Sertoli to two classes negetive(left) and positive(right)
nNbSertoli = size(NS{1},2);
for i = 1 : nGermMass
p = NS{2}(i,[4,3]);
m= (cntr(1) - p(1))/(cntr(2) - p(2));
b = cntr(1) - m*cntr(2);
neg = [];
negGMlbl = [];
negGMDist = [];
pos =[];
posGMlbl = [];
posGMDist = [];
for j = 1 : nNbSertoli
Ncntr = centers(NS{1}(j),:);
if (Ncntr(1)- m *Ncntr(2))< b
neg = [neg, j];
[gmL gmD]=getGMlbl(NS{1}(j), closeGM);
negGMlbl = [negGMlbl, gmL];
negGMDist = [negGMDist, gmD];
else
pos= [pos, j];
[gmL gmD]=getGMlbl(NS{1}(j), closeGM);
posGMlbl = [posGMlbl, gmL];
posGMDist = [posGMDist, gmD];
end
end
np(i).neg = neg;
np(i).negGMlbl = negGMlbl;
np(i).negGMDist = negGMDist;
np(i).pos = pos;
np(i).posGMlbl = posGMlbl;
np(i).posGMDist = posGMDist;
end
end
function [germlbl DD]= getGMlbl(sID,closeGM)
NS = closeGM{sID};
germlbl = 0;
DD = 0;
for j =1 : size((NS{2}),1)
if isempty(NS{3}{j})
germlbl = NS{2}(j,1);
DD = NS{2}(j,2);
return
end
end
end