-
Notifications
You must be signed in to change notification settings - Fork 50
/
Copy pathseg2edge.m
executable file
·66 lines (58 loc) · 2.15 KB
/
seg2edge.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
% This function takes an input segment and produces binary bdrys.
% Multi-channel input segments are supported by the function.
function [idxEdge] = seg2edge(seg, radius, labelIgnore, edge_type)
% Get dimensions
[height, width, chn] = size(seg);
if(~isempty(labelIgnore))
if(chn~=size(labelIgnore, 2))
error('Channel dimension not matching ignored label dimension!')
end
end
% Set the considered neighborhood
radius_search = max(ceil(radius), 1);
[X, Y] = meshgrid(1:width, 1:height);
[x, y] = meshgrid(-radius_search:radius_search, -radius_search:radius_search);
% Columnize everything
X = X(:); Y = Y(:);
x = x(:); y = y(:);
if(chn == 1)
seg = seg(:);
else
seg = reshape(seg, [height*width chn]);
end
% Build circular neighborhood
idxNeigh = sqrt(x.^2 + y.^2) <= radius;
x = x(idxNeigh); y = y(idxNeigh);
numPxlImg = length(X);
numPxlNeigh = length(x);
% Compute Gaussian weight
idxEdge = false(numPxlImg, 1);
for i = 1:numPxlNeigh
XNeigh = X+x(i);
YNeigh = Y+y(i);
idxValid = find( XNeigh >= 1 & XNeigh <= width & YNeigh >=1 & YNeigh <= height );
XCenter = X(idxValid);
YCenter = Y(idxValid);
XNeigh = XNeigh(idxValid);
YNeigh = YNeigh(idxValid);
LCenter = seg(sub2ind([height width], YCenter, XCenter), :);
LNeigh = seg(sub2ind([height width], YNeigh, XNeigh), :);
if(strcmp(edge_type, 'regular'))
idxDiff = find(any(LCenter~=LNeigh, 2));
elseif(strcmp(edge_type, 'inner'))
idxDiff = find(any(LCenter~=LNeigh, 2) & any(LCenter~=0, 2) & all(LNeigh==0, 2) );
elseif(strcmp(edge_type, 'outer'))
idxDiff = find(any(LCenter~=LNeigh, 2) & all(LCenter==0, 2) & any(LNeigh~=0, 2) );
else
error('Wrong edge type input!');
end
LCenterEdge = LCenter(idxDiff, :);
LNeighEdge = LNeigh(idxDiff, :);
idxIgnore2 = false(length(idxDiff), 1);
for j = 1:size(labelIgnore, 1)
idxIgnore2 = idxIgnore2 | ( all(bsxfun(@eq, LCenterEdge, labelIgnore(j, :)), 2) | all(bsxfun(@eq, LNeighEdge, labelIgnore(j, :)), 2) );
end
idxDiffGT = idxDiff(~idxIgnore2);
idxEdge(idxValid(idxDiffGT)) = true;
end
idxEdge = reshape(idxEdge, [height, width]);