source: proiecte/pmake3d/make3d_original/Make3dSingleImageStanford_version0.1/third_party/kovesi/transfundmatrix.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: 3.9 KB
Line 
1% TRANSFUNDMATRIX - computes fundamental matrix from 8 or more points
2%
3% Function computes the fundamental matrix from 8 or more matching points in
4% a stereo pair of images.  The normalised 8 point algorithm given by
5% Hartley and Zisserman p265 is used.  To achieve accurate results it is
6% recommended that 12 or more points are used
7%
8% Usage:   [F, e1, e2] = fundmatrix(x1, x2)
9%          [F, e1, e2] = fundmatrix(x)
10%
11% Arguments:
12%          x1, x2 - Two sets of corresponding 3xN set of homogeneous
13%          points.
14%         
15%          x      - If a single argument is supplied it is assumed that it
16%                   is in the form x = [x1; x2]
17% Returns:
18%          F      - The 3x3 fundamental matrix such that x2'*F*x1 = 0.
19%          e1     - The epipole in image 1 such that F*e1 = 0
20%          e2     - The epipole in image 2 such that F'*e2 = 0
21%
22
23% Copyright (c) 2002-2005 Peter Kovesi
24% School of Computer Science & Software Engineering
25% The University of Western Australia
26% http://www.csse.uwa.edu.au/
27%
28% Permission is hereby granted, free of charge, to any person obtaining a copy
29% of this software and associated documentation files (the "Software"), to deal
30% in the Software without restriction, subject to the following conditions:
31%
32% The above copyright notice and this permission notice shall be included in
33% all copies or substantial portions of the Software.
34%
35% The Software is provided "as is", without warranty of any kind.
36
37% Feb 2002  - Original version.
38% May 2003  - Tidied up and numerically improved.
39% Feb 2004  - Single argument allowed to enable use with RANSAC.
40% Mar 2005  - Epipole calculation added, 'economy' SVD used.
41% Aug 2005  - Octave compatibility
42
43function [F,e1,e2] = transfundmatrix(varargin)
44   
45    [x1, x2, npts] = checkargs(varargin(:));
46    v = version; Octave = v(1)<'5';  % Crude Octave test
47   
48    % Normalise each set of points so that the origin
49    % is at centroid and mean distance from origin is sqrt(2).
50    % normalise2dpts also ensures the scale parameter is 1.
51    lx1 = size(x1,2);
52    [points, T1] = normalise2dpts([x1,x2]);
53    x1 = points(:,1:lx1); x2 = points(:,lx1+1:end);
54%    [x2, T2] = normalise2dpts(x2);
55   
56    % Build the constraint matrix
57    A = [x2(2,:)'-x1(2,:)'  x1(1,:)'-x2(1,:)' ...
58         x2(1,:)'.*x1(2,:)'-x2(2,:)'.*x1(1,:)'];       
59
60    if Octave
61        [U,D,V] = svd(A);   % Don't seem to be able to use the economy
62                            % decomposition under Octave here
63    else
64        [U,D,V] = svd(A,0); % Under MATLAB use the economy decomposition
65    end
66
67    % Extract fundamental matrix from the column of V corresponding to
68    % smallest singular value.
69    F = [      0,-V(3,3), V(2,3);
70          V(3,3),      0,-V(1,3);
71         -V(2,3), V(1,3),      0];
72   
73    % Enforce constraint that fundamental matrix has rank 2 by performing
74    % a svd and then reconstructing with the two largest singular values.
75%    [U,D,V] = svd(F,0);
76%    F = U*diag([D(1,1) D(2,2) 0])*V';
77   
78    % Denormalise
79    F = T1'*F*T1;
80   
81    if nargout == 3     % Solve for epipoles
82        [U,D,V] = svd(F,0);
83        e1 = hnormalise(V(:,3));
84        e2 = hnormalise(U(:,3));
85    end
86   
87%--------------------------------------------------------------------------
88% Function to check argument values and set defaults
89
90function [x1, x2, npts] = checkargs(arg);
91   
92    if length(arg) == 2
93        x1 = arg{1};
94        x2 = arg{2};
95        if ~all(size(x1)==size(x2))
96            error('x1 and x2 must have the same size');
97        elseif size(x1,1) ~= 3
98            error('x1 and x2 must be 3xN');
99        end
100       
101    elseif length(arg) == 1
102        if size(arg{1},1) ~= 6
103            error('Single argument x must be 6xN');
104        else
105            x1 = arg{1}(1:3,:);
106            x2 = arg{1}(4:6,:);
107        end
108    else
109        error('Wrong number of arguments supplied');
110    end
111     
112    npts = size(x1,2);
113    if npts < 8
114        error('At least 8 points are needed to compute the fundamental matrix');
115    end
116   
Note: See TracBrowser for help on using the repository browser.