1 | % MAXLINEDEV - Find max deviation from a line in an edge contour. |
---|
2 | % |
---|
3 | % Function finds the point of maximum deviation from a line joining the |
---|
4 | % endpoints of an edge contour. |
---|
5 | % |
---|
6 | % Usage: [maxdev, index, D, totaldev] = maxlinedev(x,y) |
---|
7 | % |
---|
8 | % Arguments: |
---|
9 | % x, y - arrays of x,y (col,row) indicies of connected pixels |
---|
10 | % on the contour. |
---|
11 | % Returns: |
---|
12 | % maxdev - Maximum deviation of contour point from the line |
---|
13 | % joining the end points of the contour (pixels). |
---|
14 | % index - Index of the point having maxdev. |
---|
15 | % D - Distance between end points of the contour so that |
---|
16 | % one can calculate maxdev/D - the normalised error. |
---|
17 | % totaldev - Sum of the distances of all the pixels from the |
---|
18 | % line joining the endpoints. |
---|
19 | % |
---|
20 | % See also: EDGELINK, LINESEG, MERGESEG, DRAWSEG |
---|
21 | % |
---|
22 | |
---|
23 | % Peter Kovesi |
---|
24 | % School of Computer Science & Software Engineering |
---|
25 | % The University of Western Australia |
---|
26 | % pk @ csse uwa edu au |
---|
27 | % http://www.csse.uwa.edu.au/~pk |
---|
28 | % |
---|
29 | % December 2000 - Original version |
---|
30 | % February 2003 - Added calculation of total deviation |
---|
31 | % August 2006 - Avoid degeneracy when endpoints are coincident |
---|
32 | |
---|
33 | function [maxdev, index, D, totaldev] = maxlinedev(x,y) |
---|
34 | |
---|
35 | Npts = length(x); |
---|
36 | |
---|
37 | if Npts == 1 |
---|
38 | warning('Contour of length 1'); |
---|
39 | maxdev = 0; index = 1; |
---|
40 | D = 1; totaldev = 0; |
---|
41 | return; |
---|
42 | elseif Npts == 0 |
---|
43 | error('Contour of length 0'); |
---|
44 | end |
---|
45 | |
---|
46 | % Eqn of line joining end pts (x1 y1) and (x2 y2) can be parameterised by |
---|
47 | % |
---|
48 | % x*(y1-y2) + y*(x2-x1) + y2*x1 - y1*x2 = 0 |
---|
49 | % |
---|
50 | % (See Jain, Rangachar and Schunck, "Machine Vision", McGraw-Hill |
---|
51 | % 1996. pp 194-196) |
---|
52 | |
---|
53 | y1my2 = y(1)-y(Npts); % Pre-compute parameters |
---|
54 | x2mx1 = x(Npts)-x(1); |
---|
55 | C = y(Npts)*x(1) - y(1)*x(Npts); |
---|
56 | |
---|
57 | D = norm([x(1) y(1)] - [x(Npts) y(Npts)]); % Distance between end points |
---|
58 | |
---|
59 | % If end points are coincident set D to 1 to avoid degeneracy below |
---|
60 | D = max(D,1); |
---|
61 | |
---|
62 | d = abs(x*y1my2 + y*x2mx1 + C)/D; % Distance from line |
---|
63 | % segment for each |
---|
64 | % contour point |
---|
65 | [maxdev, index] = max(d); |
---|
66 | |
---|
67 | totaldev = sum(d.^2); |
---|
68 | |
---|
69 | |
---|
70 | |
---|
71 | |
---|
72 | |
---|
73 | |
---|
74 | |
---|
75 | |
---|