source: proiecte/pmake3d/make3d_original/Make3dSingleImageStanford_version0.1/third_party/kovesi/ransacfittransfundmatrix.m @ 37

Last change on this file since 37 was 37, checked in by (none), 14 years ago

Added original make3d

  • Property svn:executable set to *
File size: 5.5 KB
Line 
1% RANSACFITTRANSFUNDMATRIX - fits fundamental matrix using RANSAC
2%
3% Usage:   [F, inliers] = ransacfitfundmatrix(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
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
43function [F, inliers] = ransacfitfundmatrix(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    lx1 = size(x1,2);
69    [points, T1] = normalise2dpts([x1,x2]);
70    x1 = points(:,1:lx1); x2 = points(:,lx1+1:end);
71%    [x1, T1] = normalise2dpts(x1);
72%    [x2, T2] = normalise2dpts(x2);
73
74    s = 8;  % Number of points needed to fit a fundamental matrix. Note that
75            % only 7 are needed but the function 'fundmatrix' only
76            % implements the 8-point solution.
77   
78    fittingfn = @transfundmatrix;
79    distfn    = @funddist;
80    degenfn   = @isdegenerate;
81    % x1 and x2 are 'stacked' to create a 6xN array for ransac
82    [F, inliers] = ransac([x1; x2], fittingfn, distfn, degenfn, s, t, feedback);
83
84    % Now do a final least squares fit on the data points considered to
85    % be inliers.
86    F = transfundmatrix(x1(:,inliers), x2(:,inliers));
87   
88    % Denormalise
89    F = T1'*F*T1;
90   
91   
92%--------------------------------------------------------------------------
93% Function to evaluate the first order approximation of the geometric error
94% (Sampson distance) of the fit of a fundamental matrix with respect to a
95% set of matched points as needed by RANSAC.  See: Hartley and Zisserman,
96% 'Multiple View Geometry in Computer Vision', page 270.
97%
98% Note that this code allows for F being a cell array of fundamental matrices of
99% which we have to pick the best one. (A 7 point solution can return up to 3
100% solutions)
101
102function [bestInliers, bestF] = funddist(F, x, t);
103
104x1 = x(1:3,:);    % Extract x1 and x2 from x
105x2 = x(4:6,:);
106
107
108if iscell(F)  % We have several solutions each of which must be tested
109
110  nF = length(F);   % Number of solutions to test
111  bestF = F{1};     % Initial allocation of best solution
112  ninliers = 0;     % Number of inliers
113
114  for k = 1:nF
115    x2tFx1 = zeros(1,length(x1));
116    for n = 1:length(x1)
117      x2tFx1(n) = x2(:,n)'*F{k}*x1(:,n);
118    end
119
120    Fx1 = F{k}*x1;
121    Ftx2 = F{k}'*x2;
122
123    % Evaluate distances
124    d =  x2tFx1.^2 ./ ...
125      (Fx1(1,:).^2 + Fx1(2,:).^2 + Ftx2(1,:).^2 + Ftx2(2,:).^2);
126
127    inliers = find(abs(d) < t);     % Indices of inlying points
128
129    if length(inliers) > ninliers   % Record best solution
130      ninliers = length(inliers);
131      bestF = F{k};
132      bestInliers = inliers;
133    end
134  end
135
136else     % We just have one solution
137  x2tFx1 = zeros(1,length(x1));
138  for n = 1:length(x1)
139    x2tFx1(n) = x2(:,n)'*F*x1(:,n);
140  end
141
142  Fx1 = F*x1;
143  Ftx2 = F'*x2;
144
145  % Evaluate distances
146  d =  x2tFx1.^2 ./ ...
147    (Fx1(1,:).^2 + Fx1(2,:).^2 + Ftx2(1,:).^2 + Ftx2(2,:).^2);
148
149  bestInliers = find(abs(d) < t);     % Indices of inlying points
150  bestF = F;                          % Copy F directly to bestF
151
152end
153
154
155
156%----------------------------------------------------------------------
157% (Degenerate!) function to determine if a set of matched points will result
158% in a degeneracy in the calculation of a fundamental matrix as needed by
159% RANSAC.  This function assumes this cannot happen...
160     
161function r = isdegenerate(x)
162    r = 0;   
163   
Note: See TracBrowser for help on using the repository browser.