-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathmakeGraphMinDist.m
44 lines (40 loc) · 1.24 KB
/
makeGraphMinDist.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
function [edges] = makeGraphMinDist(emap,params)
%% Check Inputs
if nargin<2
thresh=10;
else
thresh = params.graphDistOffset;
end
%% Assign Labels and Generate Sparse Map for fast computation
emap = emap>0;
[rows,cols] = size(emap);
emapClus = sparse(bwlabel(emap,8));
cc = bwconncomp(emap,8);
numComp = cc.NumObjects;
%% Generate Edges for Graph
if (numComp==1)
edges = [1,1];
return
end
edges = false(numComp);
for i = 1:numComp
[r,c] = ind2sub([rows,cols],cc.PixelIdxList{i});
currLabel = emapClus(r(1),c(1));
r = sort(r); rmin = max(1,r(1)-thresh); rmax = min(rows,r(end)+thresh);
c = sort(c); cmin = max(1,c(1)-thresh); cmax = min(cols,c(end)+thresh);
box = emapClus(rmin:rmax,cmin:cmax);
labels = unique(box((box>0) & (box~=currLabel)));
while isempty(labels)
rmin = max(1,rmin-2); rmax = min(rows,rmax+2);
cmin = max(1,cmin-2); cmax = min(cols,cmax+2);
box = emapClus(rmin:rmax,cmin:cmax);
labels = unique(box((box>0) & (box~=currLabel)));
if rmin==1 && rmax==rows && cmax==cols && cmin==1
break;
end
end
edges(i,labels) = true;
end
edgesMat = triu((edges+edges')>0);
[node1,node2] = ind2sub([numComp,numComp],find(edgesMat));
edges = [node1,node2];