1 | % SELECTSEG - Interactive selection of linesegments with mouse. |
---|
2 | % |
---|
3 | % Usage: segs = selectseg(seglist); |
---|
4 | % |
---|
5 | % seglist - an Nx4 array storing line segments in the form |
---|
6 | % [x1 y1 x2 y2 |
---|
7 | % x1 y1 x2 y2 |
---|
8 | % . . ] etc |
---|
9 | % |
---|
10 | % |
---|
11 | % See also: EDGELINK, LINESEG, MAXLINEDEV, MERGESEG |
---|
12 | % |
---|
13 | |
---|
14 | % Copyright (c) 2000-2005 Peter Kovesi |
---|
15 | % School of Computer Science & Software Engineering |
---|
16 | % The University of Western Australia |
---|
17 | % http://www.csse.uwa.edu.au/ |
---|
18 | % |
---|
19 | % Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
20 | % of this software and associated documentation files (the "Software"), to deal |
---|
21 | % in the Software without restriction, subject to the following conditions: |
---|
22 | % |
---|
23 | % The above copyright notice and this permission notice shall be included in |
---|
24 | % all copies or substantial portions of the Software. |
---|
25 | % |
---|
26 | % The Software is provided "as is", without warranty of any kind. |
---|
27 | |
---|
28 | % December 2000 |
---|
29 | |
---|
30 | function segs = selectseg(seglist); |
---|
31 | |
---|
32 | segs = []; |
---|
33 | selectedSegs = [NaN]; |
---|
34 | |
---|
35 | figure(1), clf, drawseg(seglist,1); |
---|
36 | |
---|
37 | |
---|
38 | Nseg = size(seglist,1); |
---|
39 | |
---|
40 | fprintf('Select segments by clicking with the left mouse button\n'); |
---|
41 | fprintf('Last segment is indicated by clicking with any other mouse button\n'); |
---|
42 | |
---|
43 | count = 0; |
---|
44 | but = 1; |
---|
45 | while but==1 & count < Nseg |
---|
46 | [xp,yp,but] = ginput(1); % Get digitised point |
---|
47 | count = count + 1; |
---|
48 | rmin = Inf; |
---|
49 | for s = 1:Nseg |
---|
50 | r = segdist(xp,yp,seglist(s,:)); |
---|
51 | % if distance is closest so far and segment is not already |
---|
52 | % selected... |
---|
53 | if r < rmin & ~any(selectedSegs==s) |
---|
54 | rmin = r; |
---|
55 | closestseg = seglist(s,:); |
---|
56 | smin = s; |
---|
57 | end |
---|
58 | end |
---|
59 | |
---|
60 | segs = [segs; closestseg]; % Build up list of segments |
---|
61 | selectedSegs = [selectedSegs smin]; % Remeber selected seg Nos |
---|
62 | |
---|
63 | line([closestseg(1) closestseg(3)], [closestseg(2) closestseg(4)], ... |
---|
64 | 'Color',[1 0 0]); |
---|
65 | text((closestseg(1)+closestseg(3))/2, ... |
---|
66 | (closestseg(2)+closestseg(4))/2, sprintf('%d',count)); |
---|
67 | |
---|
68 | end |
---|
69 | |
---|
70 | function r = segdist(xp,yp,seg) |
---|
71 | |
---|
72 | % Function returns distance from point (xp,yp) to line defined by end |
---|
73 | % points (x1,y1) (x2,y2) |
---|
74 | % |
---|
75 | % |
---|
76 | % Eqn of line joining end pts (x1 y1) and (x2 y2) can be parameterised by |
---|
77 | % |
---|
78 | % x*(y1-y2) + y*(x2-x1) + y2*x1 - y1*x2 = 0 |
---|
79 | % |
---|
80 | % (See Jain, Rangachar and Schunck, "Machine Vision", McGraw-Hill |
---|
81 | % 1996. pp 194-196) |
---|
82 | |
---|
83 | x1=seg(1);y1=seg(2); |
---|
84 | x2=seg(3);y2=seg(4); |
---|
85 | |
---|
86 | y1my2 = y1-y2; % Pre-compute parameters |
---|
87 | x2mx1 = x2-x1; |
---|
88 | C = y2*x1 - y1*x2; |
---|
89 | D = norm([x1 y1] - [x2 y2]); % Distance between end points |
---|
90 | |
---|
91 | r = abs(xp*y1my2 + yp*x2mx1 + C)/D; % Perp. distance from line to (xp,yp) |
---|
92 | |
---|
93 | % Correct the distance if (xp,yp) is `outside' the ends of the segment |
---|
94 | d1 = [xp yp]-[x1 y1]; |
---|
95 | d2 = [xp yp]-[x2 y2]; |
---|
96 | if dot(d1,d2) > 0 % (xp,yp) is not `between' the end points of the |
---|
97 | % segment |
---|
98 | |
---|
99 | r = min(norm(d1),norm(d2)); % return distance to closest end |
---|
100 | % point |
---|
101 | end |
---|
102 | |
---|