1 | % RANSACFITAFFINEFUND - fits affine fundamental matrix using RANSAC |
---|
2 | % |
---|
3 | % Usage: [F, inliers] = ransacfitaffinefund(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 | % F - The 3x3 fundamental matrix such that x2'Fx1 = 0. |
---|
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, FUNDMATRIX AFFINEFUNDMATRIX |
---|
25 | |
---|
26 | % Copyright (c) 2004-2005 Peter Kovesi |
---|
27 | % School of Computer Science & Software Engineering |
---|
28 | % The University of Western Australia |
---|
29 | % http://www.csse.uwa.edu.au/ |
---|
30 | % |
---|
31 | % Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
32 | % of this software and associated documentation files (the "Software"), to deal |
---|
33 | % in the Software without restriction, subject to the following conditions: |
---|
34 | % |
---|
35 | % The above copyright notice and this permission notice shall be included in |
---|
36 | % all copies or substantial portions of the Software. |
---|
37 | % |
---|
38 | % The Software is provided "as is", without warranty of any kind. |
---|
39 | |
---|
40 | % February 2004 Original version |
---|
41 | % August 2005 Distance error function changed to match changes in RANSAC |
---|
42 | |
---|
43 | function [F, inliers] = ransacfitaffinefund(x1, x2, t, feedback) |
---|
44 | |
---|
45 | if ~all(size(x1)==size(x2)) |
---|
46 | error('Data sets x1 and x2 must have the same dimension'); |
---|
47 | end |
---|
48 | |
---|
49 | if nargin == 3 |
---|
50 | feedback = 0; |
---|
51 | end |
---|
52 | |
---|
53 | [rows,npts] = size(x1); |
---|
54 | if rows~=2 & rows~=3 |
---|
55 | error('x1 and x2 must have 2 or 3 rows'); |
---|
56 | end |
---|
57 | |
---|
58 | if rows == 2 % Pad data with homogeneous scale factor of 1 |
---|
59 | x1 = [x1; ones(1,npts)]; |
---|
60 | x2 = [x2; ones(1,npts)]; |
---|
61 | end |
---|
62 | |
---|
63 | % Normalise each set of points so that the origin is at centroid and |
---|
64 | % mean distance from origin is sqrt(2). normalise2dpts also ensures the |
---|
65 | % scale parameter is 1. Note that 'fundmatrix' will also call |
---|
66 | % 'normalise2dpts' but the code in 'ransac' that calls the distance |
---|
67 | % function will not - so it is best that we normalise beforehand. |
---|
68 | [x1, T1] = normalise2dpts(x1); |
---|
69 | [x2, T2] = normalise2dpts(x2); |
---|
70 | |
---|
71 | s = 4; % Number of points needed to fit an affine fundamental matrix. |
---|
72 | |
---|
73 | fittingfn = @affinefundmatrix; |
---|
74 | distfn = @funddist; |
---|
75 | degenfn = @isdegenerate; |
---|
76 | % x1 and x2 are 'stacked' to create a 6xN array for ransac |
---|
77 | [F, inliers] = ransac([x1; x2], fittingfn, distfn, degenfn, s, t, feedback); |
---|
78 | |
---|
79 | % Now do a final least squares fit on the data points considered to |
---|
80 | % be inliers. |
---|
81 | F = affinefundmatrix(x1(:,inliers), x2(:,inliers)); |
---|
82 | |
---|
83 | % Denormalise |
---|
84 | F = T2'*F*T1; |
---|
85 | |
---|
86 | %-------------------------------------------------------------------------- |
---|
87 | % Function to evaluate the first order approximation of the geometric error |
---|
88 | % (Sampson distance) of the fit of a fundamental matrix with respect to a |
---|
89 | % set of matched points as needed by RANSAC. See: Hartley and Zisserman, |
---|
90 | % 'Multiple View Geometry in Computer Vision', page 270. |
---|
91 | |
---|
92 | function [inliers, F] = funddist(F, x, t); |
---|
93 | |
---|
94 | x1 = x(1:3,:); % Extract x1 and x2 from x |
---|
95 | x2 = x(4:6,:); |
---|
96 | |
---|
97 | x2tFx1 = zeros(1,length(x1)); |
---|
98 | for n = 1:length(x1) |
---|
99 | x2tFx1(n) = x2(:,n)'*F*x1(:,n); |
---|
100 | end |
---|
101 | |
---|
102 | Fx1 = F*x1; |
---|
103 | Ftx2 = F'*x2; |
---|
104 | |
---|
105 | % Evaluate distances |
---|
106 | d = x2tFx1.^2 ./ ... |
---|
107 | (Fx1(1,:).^2 + Fx1(2,:).^2 + Ftx2(1,:).^2 + Ftx2(2,:).^2); |
---|
108 | |
---|
109 | inliers = find(abs(d) < t); % Indices of inlying points |
---|
110 | |
---|
111 | |
---|
112 | %---------------------------------------------------------------------- |
---|
113 | % (Degenerate!) function to determine if a set of matched points will result |
---|
114 | % in a degeneracy in the calculation of a fundamental matrix as needed by |
---|
115 | % RANSAC. This function assumes this cannot happen... |
---|
116 | |
---|
117 | function r = isdegenerate(x) |
---|
118 | r = 0; |
---|
119 | |
---|