1 | % NORMALISE2DPTS - normalises 2D homogeneous points |
---|
2 | % |
---|
3 | % Function translates and normalises a set of 2D homogeneous points |
---|
4 | % so that their centroid is at the origin and their mean distance from |
---|
5 | % the origin is sqrt(2). This process typically improves the |
---|
6 | % conditioning of any equations used to solve homographies, fundamental |
---|
7 | % matrices etc. |
---|
8 | % |
---|
9 | % Usage: [newpts, T] = normalise2dpts(pts) |
---|
10 | % |
---|
11 | % Argument: |
---|
12 | % pts - 3xN array of 2D homogeneous coordinates |
---|
13 | % |
---|
14 | % Returns: |
---|
15 | % newpts - 3xN array of transformed 2D homogeneous coordinates. The |
---|
16 | % scaling parameter is normalised to 1 unless the point is at |
---|
17 | % infinity. |
---|
18 | % T - The 3x3 transformation matrix, newpts = T*pts |
---|
19 | % |
---|
20 | % If there are some points at infinity the normalisation transform |
---|
21 | % is calculated using just the finite points. Being a scaling and |
---|
22 | % translating transform this will not affect the points at infinity. |
---|
23 | |
---|
24 | % Peter Kovesi |
---|
25 | % School of Computer Science & Software Engineering |
---|
26 | % The University of Western Australia |
---|
27 | % pk at csse uwa edu au |
---|
28 | % http://www.csse.uwa.edu.au/~pk |
---|
29 | % |
---|
30 | % May 2003 - Original version |
---|
31 | % February 2004 - Modified to deal with points at infinity. |
---|
32 | |
---|
33 | |
---|
34 | function [newpts, T] = normalise2dpts(pts) |
---|
35 | |
---|
36 | if size(pts,1) ~= 3 |
---|
37 | error('pts must be 3xN'); |
---|
38 | end |
---|
39 | |
---|
40 | % Find the indices of the points that are not at infinity |
---|
41 | finiteind = find(abs(pts(3,:)) > eps); |
---|
42 | |
---|
43 | if length(finiteind) ~= size(pts,2) |
---|
44 | warning('Some points are at infinity'); |
---|
45 | end |
---|
46 | |
---|
47 | % For the finite points ensure homogeneous coords have scale of 1 |
---|
48 | pts(1,finiteind) = pts(1,finiteind)./pts(3,finiteind); |
---|
49 | pts(2,finiteind) = pts(2,finiteind)./pts(3,finiteind); |
---|
50 | pts(3,finiteind) = 1; |
---|
51 | |
---|
52 | c = mean(pts(1:2,finiteind)')'; % Centroid of finite points |
---|
53 | newp(1,finiteind) = pts(1,finiteind)-c(1); % Shift origin to centroid. |
---|
54 | newp(2,finiteind) = pts(2,finiteind)-c(2); |
---|
55 | |
---|
56 | meandist = mean(sqrt(newp(1,finiteind).^2 + newp(2,finiteind).^2)); |
---|
57 | |
---|
58 | scale = sqrt(2)/meandist; |
---|
59 | |
---|
60 | T = [scale 0 -scale*c(1) |
---|
61 | 0 scale -scale*c(2) |
---|
62 | 0 0 1 ]; |
---|
63 | |
---|
64 | newpts = T*pts; |
---|
65 | |
---|
66 | |
---|
67 | |
---|