-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfindendsjunctions.m
99 lines (83 loc) · 3.3 KB
/
findendsjunctions.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
% FINDENDSJUNCTIONS - find junctions and endings in a line/edge image
%
% Usage: [rj, cj, re, ce] = findendsjunctions(edgeim, disp)
%
% Arguments: edgeim - A binary image marking lines/edges in an image. It is
% assumed that this is a thinned or skeleton image (or
% nearly so).
% disp - An optional flag 0/1 to indicate whether the edge
% image should be plotted with the junctions and endings
% marked. This defaults to 0.
%
% Returns: rj, cj - Row and column coordinates of junction points in the
% image.
% re, ce - Row and column coordinates of end points in the
% image.
% Copyright (c) 2006 Peter Kovesi
% School of Computer Science & Software Engineering
% The University of Western Australia
% pk at csse uwa edu au
% http://www.csse.uwa.edu.au/
%
% Permission is hereby granted, free of charge, to any person obtaining a copy
% of this software and associated documentation files (the "Software"), to deal
% in the Software without restriction, subject to the following conditions:
%
% The above copyright notice and this permission notice shall be included in
% all copies or substantial portions of the Software.
%
% The Software is provided "as is", without warranty of any kind.
% November 2006
function [rj, cj, re, ce] = findendsjunctions(edgeim, disp)
if nargin == 1
disp = 0;
end
% Ensure edge/line image really is thinnned otherwise tests for junctions
% and endings may fail.
b = bwmorph(edgeim,'skel',Inf);
% Set up look up table to find junctions. To do this we use the function
% defined at the end of this file to test that the centre pixel within a 3x3
% neighbourhood is a junction.
lut = makelut(@junction, 3);
junctions = applylut(b, lut);
[rj,cj] = find(junctions);
% Set up a look up table to find endings.
lut = makelut(@ending, 3);
ends = applylut(b, lut);
[re,ce] = find(ends);
if disp
show(edgeim,1), hold on
plot(cj,rj,'r+')
plot(ce,re,'g+')
end
%----------------------------------------------------------------------
% Function to test whether the centre pixel within a 3x3 neighbourhood is a
% junction. The centre pixel must be set and the number of transitions/crossings
% between 0 and 1 as one traverses the perimeter of the 3x3 region must be 6 or
% 8.
%
% Pixels in the 3x3 region are numbered as follows
%
% 1 4 7
% 2 5 8
% 3 6 9
function b = junction(x)
a = [x(1) x(2) x(3) x(6) x(9) x(8) x(7) x(4)];
b = [x(2) x(3) x(6) x(9) x(8) x(7) x(4) x(1)];
crossings = sum(abs(a-b));
b = x(5) && crossings >= 6;
%----------------------------------------------------------------------
% Function to test whether the centre pixel within a 3x3 neighbourhood is an
% ending. The centre pixel must be set and the number of transitions/crossings
% between 0 and 1 as one traverses the perimeter of the 3x3 region must be 2.
%
% Pixels in the 3x3 region are numbered as follows
%
% 1 4 7
% 2 5 8
% 3 6 9
function b = ending(x)
a = [x(1) x(2) x(3) x(6) x(9) x(8) x(7) x(4)];
b = [x(2) x(3) x(6) x(9) x(8) x(7) x(4) x(1)];
crossings = sum(abs(a-b));
b = x(5) && crossings == 2;