forked from petercorke/machinevision-toolbox-matlab
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhomog_solve.m
More file actions
159 lines (142 loc) · 3.47 KB
/
Copy pathhomog_solve.m
File metadata and controls
159 lines (142 loc) · 3.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
%HOMOG_SOLVE various linear equation solvers used by fmatrix and homography
%
% Copyright (C) 1993-2011, by Peter I. Corke
%
% This file is part of The Machine Vision Toolbox for Matlab (MVTB).
%
% MVTB is free software: you can redistribute it and/or modify
% it under the terms of the GNU Lesser General Public License as published by
% the Free Software Foundation, either version 3 of the License, or
% (at your option) any later version.
%
% MVTB is distributed in the hope that it will be useful,
% but WITHOUT ANY WARRANTY; without even the implied warranty of
% MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
% GNU Lesser General Public License for more details.
%
% You should have received a copy of the GNU Leser General Public License
% along with MVTB. If not, see <http://www.gnu.org/licenses/>.
function x = homog_solve(how, A, b)
switch how,
case 'eigsol',
x = eigsol(A);
case 'lsqsvd',
x = lsqsvd(A, b);
case 'psinvsol',
x = psinvsol(A,b);
case 'psinvsvd',
x = psinvsvd(A,b);
otherwise,
error('bad method');
end
function x = eigsol(A);
% EIGSOL Calculate the eigen value solution of the system Ax=0.
% x = eigsol(A);
%
% input : Coeficient Matrix of the system, A
% output : Solution vector, x
%
% Nuno Alexandre Cid Martins
% Coimbra, Sep 29, 1998
% I.S.R.
if (nargin~=1),
error('Requires one input argument.');
else
if (isstr(A)),
error('Requires one matrix as input arguments.');
else
[V D]=eig(A'*A);
m=1.0e+308;
j=1;
for i=1:size(D,1),
if (m>D(i,i)),
m=D(i,i);
j=i;
end;
end;
x=V(:,j);
end;
end;
function x = lsqsvd(A,b);
% LSQSVD Computes the least-squares solution x of the system Ax=b,
% using the SVD of A.
% x = lsqsvd(A,b);
%
% input : Coeficient Matrix of the system, A
% Result vector of the system, b
% output : Solution vector, x
%
% Nuno Alexandre Cid Martins
% Coimbra, Sep 29, 1998
% I.S.R.
if (nargin~=2),
error('Requires two input arguments.');
else
if (isstr(A) | isstr(b)),
error('Requires one matrix and one vector as input arguments.');
else
[U,S,V]=svd(A);
ub=U'*b;
y=zeros(size(A,2),1);
% for i=1:size(A,2), % mod by pic, was size(A,2)
for i=1:min(size(A)), % mod by pic, was size(A,2)
if (S(i,i)~=0),
y(i)=ub(i)/S(i,i);
else
y(i)=1.0e+308;
end;
end;
x=V*y;
end;
end;
function x = psinvsol(A,b);
% PSINVSOL Computes pseudo-inverse solution x of the system Ax=b.
% x = psinvsol(A,b);
%
% input : Coeficient Matrix of the system, A
% Result vector of the system, b
% output : Solution vector, x
%
% Nuno Alexandre Cid Martins
% Coimbra, Sep 29, 1998
% I.S.R.
if (nargin~=2),
error('Requires two input arguments.');
else
if (isstr(A) | isstr(b)),
error('Requires one matrix and one vector as input arguments.');
else
x=inv(A'*A)*A'*b;
end;
end;
function x = psinvsvd(A,b);
% PSINVSVD Computes pseudo-inverse solution x of the system Ax=b,
% using the SVD of A.
% x = psinvsvd(A,b);
%
% input : Coeficient Matrix of the system, A
% Result vector of the system, b
% output : Solution vector, x
%
% Nuno Alexandre Cid Martins
% Coimbra, Sep 29, 1998
% I.S.R.
if (nargin~=2),
error('Requires two input arguments.');
else
if (isstr(A) | isstr(b)),
error('Requires one matrix and one vector as input arguments.');
else
[U,S,V]=svd(A);
Sp=zeros(size(S));
for i=1:size(S,2),
if (S(i,i)~=0),
Sp(i,i)=1/S(i,i);
else
Sp(i,i)=0;
end;
end;
Ap=V*Sp'*U';
x=Ap*b;
end;
end;