-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathsaddlefront_base.m
74 lines (58 loc) · 1.9 KB
/
saddlefront_base.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
%% SADDLEFRONT_BASE - Saddle points of a labelled image.
%
%% Description
% Compute saddle points of an map of influence zones.
%
%% Syntax
% [vertex,faces] = SADDLEFRONT_BASE(D, Q);
% [vertex,faces] = SADDLEFRONT_BASE(D, Q, mask);
%
%% Inputs
% *|D|* : distance function associated to the Voronoi map.
%
% *|Q|* : Voronoi-like (labelled) index map.
%% Function implementation
function [vertex,faces] = saddlefront_base(D, Q, mask)
if nargin==3 && ~isempty(mask)
Q(mask==0) = -1;
end
Q1 = zeros(size(Q)+2)-1;
Q1(2:end-1,2:end-1) = Q;
% all combinaisons of labels found in (2x2)-neighbourhoods
v = Q1(1:end-1,1:end-1); V = v(:);
v = Q1(2:end,1:end-1); V = [V v(:)];
v = Q1(1:end-1,2:end); V = [V v(:)];
v = Q1(2:end,2:end); V = [V v(:)];
V = sort(V,2); % order depending on the columns
% V = unique(V, 'rows'); % keep only one sample for each row
% V = V( prod(V,2)>0 ,:); % keep only vertex with 'non far' neigbour
% where cells meet: d>=1 (d=0: interior of a cell)
d = (V(:,1)~=V(:,2)) + (V(:,2)~=V(:,3)) + (V(:,3)~=V(:,4));% + (V(:,1)~=V(:,4));
V = V';
% all pixels in the (2x2)-neighbourhood have a different label, ie. belong
% to a different Voronoi cell
I = find(d>=1);
[vx,vy] = ind2sub(size(Q)+1, I);
vx = min(max(vx,1),size(Q,1));
vy = min(max(vy,1),size(Q,2));
J = vx + (vy-1)*size(Q,1);
% sort according to distance
[~,s] = sort(D(J), 1, 'descend');
I = I(s);
[vx,vy] = ind2sub(size(Q)+1, I);
vx = min(max(vx,1),size(Q,1));
vy = min(max(vy,1),size(Q,2));
vertex = cat(1,vx',vy');
V = sort(V, 1, 'descend'); %#ok
faces = V(1:3, I);
if isempty(vertex)
% add farthest point
[~,I] = max( D(:) );
[vx,vy] = ind2sub(size(D), I(1));
vertex = [vx;vy];
faces = [-1 -1 -1]';
end
I = faces(1,:)<0 | faces(2,:)<0 | faces(3,:)<0;
J = faces(1,:)>0 & faces(2,:)>0 & faces(3,:)>0;
faces = cat(2, faces(:,I), faces(:,J) );
end % end of saddlefront_base