1 | % RANSACFITFUNDMATRIX7 - fits fundamental matrix using RANSAC |
---|
2 | % |
---|
3 | % Usage: [F, inliers] = ransacfitfundmatrix7(x1, x2, t) |
---|
4 | % |
---|
5 | % This function requires Andrew Zisserman's 7 point fundamental matrix code. |
---|
6 | % See: http://www.robots.ox.ac.uk/~vgg/hzbook/code/ |
---|
7 | % |
---|
8 | % Arguments: |
---|
9 | % x1 - 2xN or 3xN set of homogeneous points. If the data is |
---|
10 | % 2xN it is assumed the homogeneous scale factor is 1. |
---|
11 | % x2 - 2xN or 3xN set of homogeneous points such that x1<->x2. |
---|
12 | % t - The distance threshold between data point and the model |
---|
13 | % used to decide whether a point is an inlier or not. |
---|
14 | % Note that point coordinates are normalised to that their |
---|
15 | % mean distance from the origin is sqrt(2). The value of |
---|
16 | % t should be set relative to this, say in the range |
---|
17 | % 0.001 - 0.01 |
---|
18 | % |
---|
19 | % Note that it is assumed that the matching of x1 and x2 are putative and it |
---|
20 | % is expected that a percentage of matches will be wrong. |
---|
21 | % |
---|
22 | % Returns: |
---|
23 | % F - The 3x3 fundamental matrix such that x2'Fx1 = 0. |
---|
24 | % inliers - An array of indices of the elements of x1, x2 that were |
---|
25 | % the inliers for the best model. |
---|
26 | % |
---|
27 | % See Also: RANSAC, FUNDMATRIX, RANSACFITFUNDMATRIX |
---|
28 | |
---|
29 | % Copyright (c) 2004-2005 Peter Kovesi |
---|
30 | % School of Computer Science & Software Engineering |
---|
31 | % The University of Western Australia |
---|
32 | % http://www.csse.uwa.edu.au/ |
---|
33 | % |
---|
34 | % Permission is hereby granted, free of charge, to any person obtaining a copy |
---|
35 | % of this software and associated documentation files (the "Software"), to deal |
---|
36 | % in the Software without restriction, subject to the following conditions: |
---|
37 | % |
---|
38 | % The above copyright notice and this permission notice shall be included in |
---|
39 | % all copies or substantial portions of the Software. |
---|
40 | % |
---|
41 | % The Software is provided "as is", without warranty of any kind. |
---|
42 | |
---|
43 | % February 2004 Original version |
---|
44 | % August 2005 Distance error function changed to match changes in RANSAC |
---|
45 | |
---|
46 | function [F, inliers] = ransacfitfundmatrix7(x1, x2, t, feedback) |
---|
47 | |
---|
48 | if ~all(size(x1)==size(x2)) |
---|
49 | error('Data sets x1 and x2 must have the same dimension'); |
---|
50 | end |
---|
51 | |
---|
52 | if nargin == 3 |
---|
53 | feedback = 0; |
---|
54 | end |
---|
55 | |
---|
56 | [rows,npts] = size(x1); |
---|
57 | if rows~=2 & rows~=3 |
---|
58 | error('x1 and x2 must have 2 or 3 rows'); |
---|
59 | end |
---|
60 | |
---|
61 | if rows == 2 % Pad data with homogeneous scale factor of 1 |
---|
62 | x1 = [x1; ones(1,npts)]; |
---|
63 | x2 = [x2; ones(1,npts)]; |
---|
64 | end |
---|
65 | |
---|
66 | % Normalise each set of points so that the origin is at centroid and |
---|
67 | % mean distance from origin is sqrt(2). normalise2dpts also ensures the |
---|
68 | % scale parameter is 1. Note that 'fundmatrix' will also call |
---|
69 | % 'normalise2dpts' but the code in 'ransac' that calls the distance |
---|
70 | % function will not - so it is best that we normalise beforehand. |
---|
71 | [x1, T1] = normalise2dpts(x1); |
---|
72 | [x2, T2] = normalise2dpts(x2); |
---|
73 | |
---|
74 | s = 7; % Number of points needed to fit a fundamental matrix using |
---|
75 | % a 7 point solution |
---|
76 | |
---|
77 | fittingfn = @vgg_F_from_7pts_wrapper; % Wrapper for AZ's code |
---|
78 | distfn = @funddist; |
---|
79 | degenfn = @isdegenerate; |
---|
80 | % x1 and x2 are 'stacked' to create a 6xN array for ransac |
---|
81 | [F, inliers] = ransac([x1; x2], fittingfn, distfn, degenfn, s, t, feedback); |
---|
82 | |
---|
83 | % Now do a final least squares fit on the data points considered to |
---|
84 | % be inliers. |
---|
85 | F = fundmatrix(x1(:,inliers), x2(:,inliers)); |
---|
86 | |
---|
87 | % Denormalise |
---|
88 | F = T2'*F*T1; |
---|
89 | |
---|
90 | |
---|
91 | %-------------------------------------------------------------------------- |
---|
92 | % Function providing a wrapper for Andrew Zisserman's 7 point fundamental |
---|
93 | % matrix code. See: http://www.robots.ox.ac.uk/~vgg/hzbook/code/ |
---|
94 | % This code takes inputs and returns output according to the requirements of |
---|
95 | % RANSAC |
---|
96 | |
---|
97 | function F = vgg_F_from_7pts_wrapper(x) |
---|
98 | |
---|
99 | Fvgg = vgg_F_from_7pts_2img(x(1:3,:), x(4:6,:)); |
---|
100 | |
---|
101 | if isempty(Fvgg) |
---|
102 | F = []; |
---|
103 | return; |
---|
104 | end |
---|
105 | |
---|
106 | % Store the (potentially) 3 solutions in a cell array |
---|
107 | [rows,cols,Nsolutions] = size(Fvgg); |
---|
108 | for n = 1:Nsolutions |
---|
109 | F{1} = Fvgg(:,:,n); |
---|
110 | end |
---|
111 | |
---|
112 | %-------------------------------------------------------------------------- |
---|
113 | % Function to evaluate the first order approximation of the geometric error |
---|
114 | % (Sampson distance) of the fit of a fundamental matrix with respect to a |
---|
115 | % set of matched points as needed by RANSAC. See: Hartley and Zisserman, |
---|
116 | % 'Multiple View Geometry in Computer Vision', page 270. |
---|
117 | % |
---|
118 | % Note that this code allows for F being a cell array of fundamental matrices of |
---|
119 | % which we have to pick the best one. (A 7 point solution can return up to 3 |
---|
120 | % solutions) |
---|
121 | |
---|
122 | function [bestInliers, bestF] = funddist(F, x, t); |
---|
123 | |
---|
124 | x1 = x(1:3,:); % Extract x1 and x2 from x |
---|
125 | x2 = x(4:6,:); |
---|
126 | |
---|
127 | |
---|
128 | if iscell(F) % We have several solutions each of which must be tested |
---|
129 | |
---|
130 | nF = length(F); % Number of solutions to test |
---|
131 | bestF = F{1}; % Initial allocation of best solution |
---|
132 | ninliers = 0; % Number of inliers |
---|
133 | |
---|
134 | for k = 1:nF |
---|
135 | x2tFx1 = zeros(1,length(x1)); |
---|
136 | for n = 1:length(x1) |
---|
137 | x2tFx1(n) = x2(:,n)'*F{k}*x1(:,n); |
---|
138 | end |
---|
139 | |
---|
140 | Fx1 = F{k}*x1; |
---|
141 | Ftx2 = F{k}'*x2; |
---|
142 | |
---|
143 | % Evaluate distances |
---|
144 | d = x2tFx1.^2 ./ ... |
---|
145 | (Fx1(1,:).^2 + Fx1(2,:).^2 + Ftx2(1,:).^2 + Ftx2(2,:).^2); |
---|
146 | |
---|
147 | inliers = find(abs(d) < t); % Indices of inlying points |
---|
148 | |
---|
149 | if length(inliers) > ninliers % Record best solution |
---|
150 | ninliers = length(inliers); |
---|
151 | bestF = F{k}; |
---|
152 | bestInliers = inliers; |
---|
153 | end |
---|
154 | end |
---|
155 | |
---|
156 | else % We just have one solution |
---|
157 | x2tFx1 = zeros(1,length(x1)); |
---|
158 | for n = 1:length(x1) |
---|
159 | x2tFx1(n) = x2(:,n)'*F*x1(:,n); |
---|
160 | end |
---|
161 | |
---|
162 | Fx1 = F*x1; |
---|
163 | Ftx2 = F'*x2; |
---|
164 | |
---|
165 | % Evaluate distances |
---|
166 | d = x2tFx1.^2 ./ ... |
---|
167 | (Fx1(1,:).^2 + Fx1(2,:).^2 + Ftx2(1,:).^2 + Ftx2(2,:).^2); |
---|
168 | |
---|
169 | bestInliers = find(abs(d) < t); % Indices of inlying points |
---|
170 | bestF = F; % Copy F directly to bestF |
---|
171 | |
---|
172 | end |
---|
173 | |
---|
174 | |
---|
175 | |
---|
176 | %---------------------------------------------------------------------- |
---|
177 | % (Degenerate!) function to determine if a set of matched points will result |
---|
178 | % in a degeneracy in the calculation of a fundamental matrix as needed by |
---|
179 | % RANSAC. This function assumes this cannot happen... |
---|
180 | |
---|
181 | function r = isdegenerate(x) |
---|
182 | r = 0; |
---|
183 | |
---|