1 | % RANSACFITHOMOGRAPHY - fits 2D homography using RANSAC |
---|
2 | % |
---|
3 | % Usage: [H, inliers] = ransacfithomography_vgg(x1, x2, t) |
---|
4 | % |
---|
5 | % Arguments: |
---|
6 | % x1 - 2xN or 3xN set of homogeneous points. If the data is |
---|
7 | % 2xN it is assumed the homogeneous scale factor is 1. |
---|
8 | % x2 - 2xN or 3xN set of homogeneous points such that x1<->x2. |
---|
9 | % t - The distance threshold between data point and the model |
---|
10 | % used to decide whether a point is an inlier or not. |
---|
11 | % Note that point coordinates are normalised to that their |
---|
12 | % mean distance from the origin is sqrt(2). The value of |
---|
13 | % t should be set relative to this, say in the range |
---|
14 | % 0.001 - 0.01 |
---|
15 | % |
---|
16 | % Note that it is assumed that the matching of x1 and x2 are putative and it |
---|
17 | % is expected that a percentage of matches will be wrong. |
---|
18 | % |
---|
19 | % Returns: |
---|
20 | % H - The 3x3 homography such that x2 = H*x1. |
---|
21 | % inliers - An array of indices of the elements of x1, x2 that were |
---|
22 | % the inliers for the best model. |
---|
23 | % |
---|
24 | % See Also: ransac, homography2d, homography1d |
---|
25 | |
---|
26 | % Peter Kovesi |
---|
27 | % School of Computer Science & Software Engineering |
---|
28 | % The University of Western Australia |
---|
29 | % pk at csse uwa edu au |
---|
30 | % http://www.csse.uwa.edu.au/~pk |
---|
31 | % |
---|
32 | % February 2004 - original version |
---|
33 | % July 2004 - error in denormalising corrected (thanks to Andrew Stein) |
---|
34 | |
---|
35 | % Adapted to use vgg functions by Peter Kovesi and Andrew Zisserman |
---|
36 | |
---|
37 | function [H, inliers] = ransacfithomography_vgg(x1, x2, t) |
---|
38 | |
---|
39 | if ~all(size(x1)==size(x2)) |
---|
40 | error('Data sets x1 and x2 must have the same dimension'); |
---|
41 | end |
---|
42 | |
---|
43 | [rows,npts] = size(x1); |
---|
44 | if rows~=2 & rows~=3 |
---|
45 | error('x1 and x2 must have 2 or 3 rows'); |
---|
46 | end |
---|
47 | |
---|
48 | if npts < 4 |
---|
49 | error('Must have at least 4 points to fit homography'); |
---|
50 | end |
---|
51 | |
---|
52 | if rows == 2 % Pad data with homogeneous scale factor of 1 |
---|
53 | x1 = [x1; ones(1,npts)]; |
---|
54 | x2 = [x2; ones(1,npts)]; |
---|
55 | end |
---|
56 | |
---|
57 | % Normalise each set of points so that the origin is at centroid and |
---|
58 | % mean distance from origin is sqrt(2). normalise2dpts also ensures the |
---|
59 | % scale parameter is 1. Note that 'homography2d' will also call |
---|
60 | % 'normalise2dpts' but the code in 'ransac' that calls the distance |
---|
61 | % function will not - so it is best that we normalise beforehand. |
---|
62 | [x1, T1] = normalise2dpts(x1); |
---|
63 | [x2, T2] = normalise2dpts(x2); |
---|
64 | |
---|
65 | s = 4; % Minimum No of points needed to fit a homography. |
---|
66 | |
---|
67 | fittingfn = @wrap_vgg_homography2d; |
---|
68 | distfn = @homogdist2d; |
---|
69 | degenfn = @isdegenerate; |
---|
70 | % x1 and x2 are 'stacked' to create a 6xN array for ransac |
---|
71 | [H, inliers] = ransac([x1; x2], fittingfn, distfn, degenfn, s, t); |
---|
72 | |
---|
73 | % Now do a final least squares fit on the data points considered to |
---|
74 | % be inliers. |
---|
75 | Hlin = vgg_H_from_x_lin(x1(:,inliers), x2(:,inliers)); |
---|
76 | H = vgg_H_from_x_nonlin(Hlin,x1(:,inliers), x2(:,inliers)); |
---|
77 | |
---|
78 | % Denormalise |
---|
79 | H = T2\H*T1; |
---|
80 | |
---|
81 | %---------------------------------------------------------------------- |
---|
82 | % Function to evaluate the symmetric transfer error of a homography with |
---|
83 | % respect to a set of matched points as needed by RANSAC. |
---|
84 | |
---|
85 | function d2 = homogdist2d(H, x); |
---|
86 | |
---|
87 | x1 = x(1:3,:); % Extract x1 and x2 from x |
---|
88 | x2 = x(4:6,:); |
---|
89 | |
---|
90 | % Calculate, in both directions, the transfered points |
---|
91 | Hx1 = H*x1; |
---|
92 | invHx2 = H\x2; |
---|
93 | |
---|
94 | % Normalise so that the homogeneous scale parameter for all coordinates |
---|
95 | % is 1. |
---|
96 | |
---|
97 | x1 = hnormalise(x1); |
---|
98 | x2 = hnormalise(x2); |
---|
99 | Hx1 = hnormalise(Hx1); |
---|
100 | invHx2 = hnormalise(invHx2); |
---|
101 | |
---|
102 | d2 = sum((x1-invHx2).^2) + sum((x2-Hx1).^2); |
---|
103 | |
---|
104 | |
---|
105 | %---------------------------------------------------------------------- |
---|
106 | % Function to determine if a set of 4 pairs of matched points give rise |
---|
107 | % to a degeneracy in the calculation of a homography as needed by RANSAC. |
---|
108 | % This involves testing whether any 3 of the 4 points in each set is |
---|
109 | % colinear. |
---|
110 | |
---|
111 | function r = isdegenerate(x) |
---|
112 | |
---|
113 | x1 = x(1:3,:); % Extract x1 and x2 from x |
---|
114 | x2 = x(4:6,:); |
---|
115 | |
---|
116 | r = ... |
---|
117 | iscolinear(x1(:,1),x1(:,2),x1(:,3)) | ... |
---|
118 | iscolinear(x1(:,1),x1(:,2),x1(:,4)) | ... |
---|
119 | iscolinear(x1(:,1),x1(:,3),x1(:,4)) | ... |
---|
120 | iscolinear(x1(:,2),x1(:,3),x1(:,4)) | ... |
---|
121 | iscolinear(x2(:,1),x2(:,2),x2(:,3)) | ... |
---|
122 | iscolinear(x2(:,1),x2(:,2),x2(:,4)) | ... |
---|
123 | iscolinear(x2(:,1),x2(:,3),x2(:,4)) | ... |
---|
124 | iscolinear(x2(:,2),x2(:,3),x2(:,4)); |
---|
125 | |
---|
126 | function H = wrap_vgg_homography2d(x) |
---|
127 | |
---|
128 | xs1 = x(1:3,:); |
---|
129 | xs2 = x(4:6,:); |
---|
130 | H = vgg_H_from_x_lin(xs1,xs2); |
---|