1 | %vgg_F_from_7pts_2img Computes fundamental matrix from 7 points across 2 images.
|
---|
2 | %
|
---|
3 | % [P,X] = vgg_F_from_7pts_2img(x), where
|
---|
4 | % x ... double(3,7,2) or cell{2} of double(3,7), 7 homogeneous points in 2 images
|
---|
5 | % F ... double(3,3), fundamental matrix
|
---|
6 | % There are 0 to 3 solutions for F. Solutions are pruned by requirement that
|
---|
7 | % scalars s in all equations s*cross(e1,x1)==F*x2 are positive.
|
---|
8 | % In case of multiple solutions, F has one dimension
|
---|
9 | % more such that F(:,:,n) is the n-th solution.
|
---|
10 | %
|
---|
11 | % Also the form F = vgg_F_from_7pts_2img(x1,x2) is accepted.
|
---|
12 |
|
---|
13 | function F = vgg_F_from_7pts_2img(x1,x2)
|
---|
14 |
|
---|
15 | if nargin==1
|
---|
16 | if iscell(x1)
|
---|
17 | x2 = x1{2};
|
---|
18 | x1 = x1{1};
|
---|
19 | else
|
---|
20 | x2 = x1(:,:,2);
|
---|
21 | x1 = x1(:,:,1);
|
---|
22 | end
|
---|
23 | end
|
---|
24 | if any(size(x1)~=[3 7]) | any(size(x2)~=[3 7])
|
---|
25 | error('Wrong size of input points.');
|
---|
26 | end
|
---|
27 |
|
---|
28 | % Linear step
|
---|
29 | A = vgg_vec_swap(x1,x2)';
|
---|
30 | [u,s,v] = svd(A,0);
|
---|
31 | FF{1} = reshape(v(:,end-1),[3 3]);
|
---|
32 | FF{2} = reshape(v(:,end ),[3 3]);
|
---|
33 |
|
---|
34 | % Solving cubic equation and getting 1 or 3 solutions for F
|
---|
35 | a = vgg_singF_from_FF(FF);
|
---|
36 | F = [];
|
---|
37 | for i = 1:length(a)
|
---|
38 | Fi = a(i)*FF{1} + (1-a(i))*FF{2};
|
---|
39 | %for n = 1:7, disp(norm(x(:,n,1)'*Fi*x(:,n,2))), end % test code
|
---|
40 | if signs_OK(Fi,x1,x2)
|
---|
41 | F = cat(3, F, Fi);
|
---|
42 | end
|
---|
43 | end
|
---|
44 |
|
---|
45 | return
|
---|
46 |
|
---|
47 | %%%%%%%%%%%%%%%%%%%%%%%%%
|
---|
48 |
|
---|
49 | % Checks sign consistence of F and x
|
---|
50 | function OK = signs_OK(F,x1,x2)
|
---|
51 | [u,s,v] = svd(F');
|
---|
52 | e1 = v(:,3);
|
---|
53 | l1 = vgg_contreps(e1)*x1;
|
---|
54 | s = sum( (F*x2) .* l1 );
|
---|
55 | OK = all(s>0) | all(s<0);
|
---|
56 | return |
---|