-
Notifications
You must be signed in to change notification settings - Fork 1
/
Melina_branchStats.m
227 lines (168 loc) · 6.75 KB
/
Melina_branchStats.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
function Melina_branchStats()
detection.trimNeuriteSize = 12;
try
curPath = pwd();
old = load('SynD_config.mat');
filePath = old.old.exportPath;
cd(filePath)
catch
disp('Unable to load SynD configuration file')
end
[fileName,filePath] = uigetfile('Select a mat file to load','*.mat');
cd(curPath);
tmp = load(strcat(filePath,fileName));
data = tmp.old;
data.height = size(data.image,1);
data.width = size(data.image,2);
clear tmp;
data.skeleton = bwmorph(data.neuriteMask-data.somaMask>0,'skel',inf);
trimSkeleton();
distMask = makeDistMask(data.somaMask,data.neuriteMask);
endPoints = bwmorph(data.skeleton,'endpoints');
% Remove the end points that are linking to the soma
pad = strel('disk',10);
endPoints = endPoints & ~imdilate(data.somaMask,pad);
% Remove end points that are too close together
minDist = 10;
[ey,ex] = find(endPoints);
for i = 1:numel(ex)
for j = i+1:numel(ex)
d = sqrt((ex(i)-ex(j))^2 + (ey(i)-ey(j))^2);
if(d < minDist)
if(distMask(ey(i),ex(i)) < distMask(ey(j),ex(j)))
endPoints(ey(i),ex(i)) = 0;
else
endPoints(ey(j),ex(j)) = 0;
end
end
end
end
[ey,ex] = find(endPoints);
longestNeuriteLength = max(distMask(endPoints));
nEndPoints = nnz(endPoints);
fprintf('Longest neurite is %f micrometer\n', ...
longestNeuriteLength*1e6)
fprintf('%d end points found.\n', nEndPoints)
figure
imagesc(data.neuriteMask + data.somaMask + data.skeleton + endPoints*2)
hold on
plot(ex,ey,'wo')
title(sprintf('%d end points, longest neurite %.3f micrometer', ...
nEndPoints, longestNeuriteLength*1e6))
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Helper function taken from SynD_extract.m
function distMask = makeDistMask(somaMask, neuriteMask)
% Create a padded version of the masks, this allows us to avoid
% range checks at edges
somaMaskPadded = zeros(size(somaMask)+2);
somaMaskPadded(2:end-1,2:end-1) = somaMask;
neuriteMaskPadded = zeros(size(neuriteMask)+2);
neuriteMaskPadded(2:end-1,2:end-1) = neuriteMask;
distMaskPadded = inf*ones(size(somaMaskPadded));
distMaskPadded(find(somaMaskPadded)) = 0;
% Find all the pixels that we need to calculate the distance for
[yNeurite,xNeurite] = ind2sub(size(neuriteMaskPadded), ...
find(neuriteMaskPadded));
[ySoma,xSoma] = ind2sub(size(somaMaskPadded), ...
find(somaMaskPadded));
% Seed the algorithm with the pixels belonging to the soma
% Then we want to add all the immediate neighbours to those pixels
% and then keep adding their neighbours, etc, while keeping track of
% the distance.
xPrev = xSoma;
yPrev = ySoma;
allDone = 0;
neighOfsX = [-1 0 1 -1 0 1 -1 0 1];
neighOfsY = [1 1 1 0 0 0 -1 -1 -1];
neighDist = [sqrt(2) 1 sqrt(2) 1 0 1 sqrt(2) 1 sqrt(2)]*data.xyRes;
idxNew = NaN;
iter = 0;
while(~isempty(idxNew))
idxNew = [];
iter = iter + 1;
% Get the neighbours to the previously updated pixels
for i = 1:length(xPrev)
xN = xPrev(i) + neighOfsX;
yN = yPrev(i) + neighOfsY;
dN = distMaskPadded(yPrev(i),xPrev(i)) + neighDist;
for j = 1:length(xN)
% Was the neighbour part of the neuron, and did we find
% a shorter path to this pixel than it had before?
if(neuriteMaskPadded(yN(j),xN(j)) > 0 ...
& distMaskPadded(yN(j),xN(j)) > dN(j))
distMaskPadded(yN(j),xN(j)) = dN(j);
% Keep track of the newest added
idxNew = [idxNew, yN(j) + (xN(j)-1)*size(somaMaskPadded,1)];
end
end
end
idxNew = unique(idxNew);
% fprintf('Iter: %d, num changed: %d\n', iter, length(idxNew))
% Set the x and y values of the pixels previously updated
[yPrev,xPrev] = ind2sub(size(somaMaskPadded),idxNew);
end
% Extract the original part of the mask
distMask = distMaskPadded(2:end-1,2:end-1);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
% Helper functions taken from SynD_extract.m
function trimSkeleton(nIter)
oldSkel = data.skeleton;
if(~exist('nIter'))
nIter = 3;
else
if(nIter < 1)
return
end
end
fprintf('Trimming skeleton: %d\n',nIter)
branchPoints = bwmorph(data.skeleton,'branchpoints');
endPoints = bwmorph(data.skeleton,'endpoints');
endPointIdx = find(endPoints);
% We want to remove all the mini-branches smaller than the trim size.
% To find them we remove the branch points, then calculate the locate
% all the connected components smaller than the trim size, and if
% they only have one branch point around them (imdilate 1),
% then we remove it.
trimMask = double(data.skeleton - bwmorph(branchPoints,'dilate') > 0);
mCC = bwconncomp(trimMask);
for i = 1:length(mCC.PixelIdxList)
if(length(mCC.PixelIdxList{i}) < detection.trimNeuriteSize)
% Does it contain an endpoint?
if(nnz(ismember(mCC.PixelIdxList{i},endPointIdx)))
[y,x] = ind2sub(size(data.skeleton),mCC.PixelIdxList{i});
yAll = max(1,min([y+1;y+1;y+1;y;y;y;y-1;y-1;y-1],data.height));
xAll = max(1,min([x+1;x;x-1;x+1;x;x-1;x+1;x;x-1],data.width));
trimIdx = sub2ind(size(data.skeleton),yAll,xAll);
%fprintf('Trimming away %d pixels.\n', length(mCC.PixelIdxList{i}))
% Yes it does, remove pixel from skeleton
%data.skeleton(mCC.PixelIdxList{i}) = 0;
data.skeleton(trimIdx) = 0;
end
end
end
% UPDATE --- Finally we recalulate the end points
% and see if there are any end points neighbouring branch points
% if so remove those end points.
ep = bwmorph(data.skeleton,'endpoints');
[epY,epX] = find(ep);
[bpY,bpX] = find(branchPoints);
d = sqrt((kron(epY,ones(1,length(bpY))) ...
- kron(ones(length(epY),1),transpose(bpY))).^2 ...
+ (kron(epX,ones(1,length(bpX))) ...
- kron(ones(length(epX),1),transpose(bpX))).^2);
dMin = min(d,[],2);
epNeigh = find(dMin <= sqrt(2));
if(~isempty(epNeigh))
fprintf('Found %d neighbours.\n', length(epNeigh))
data.skeleton(sub2ind(size(data.skeleton),...
epY(epNeigh),epX(epNeigh))) = 0;
end
% Remove non-endpoint neighbours
data.skeleton = bwmorph(data.skeleton,'skel',inf);
fprintf('Modifying %d pixels\n', nnz(oldSkel - data.skeleton))
% Previously tried with multiple iterations
% trimSkeleton(nIter-1);
end
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
end