-
Notifications
You must be signed in to change notification settings - Fork 0
/
fundamental_ransac.m
59 lines (51 loc) · 1.96 KB
/
fundamental_ransac.m
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
% fundamental_ransac.m
%
% Applies RANSAC to the fundamental matrix
%
% Input:
% - x1[n, 1]: x coordinates of feature points in the initial image
% - y1[n, 1]: y coordinates of feature points in the initial image
% - x2[n, 1]: x coordinates of feature points in the matching image
% - y2[n, 1]: y coordinates of feature points in the matching image
% - N: number of RANSAC iterations
% - threshold: inlier threshold for RANSAC
%
% Output:
% - F[3, 3]: Fundamental matrix after RANSAC
% - inliers: array containing the indices of the inliers within the set
% of matching feature points.
%
% Authors:
% - Bas Buller 4166566
% - Rick Feith 4218272
function [F, inliers] = fundamental_ransac(x1,y1,x2,y2,N,threshold)
%% Initialize parameters
inliers = [];
total_inliers = 0;
P = 8;
%% Loop to find optimal Fundamental Matrix
for run = 1:N
seed = randperm(max(size(x1)), P); % select P random feature point matches
A = zeros(P,9);
for i = seed
A(i,:) = [x2(i)*x1(i) x2(i)*y1(i) x2(i) y2(i)*x1(i) y2(i)*y1(i) y2(i) x1(i) y1(i) 1];
end
Fi = fundamental_matrix(A); % Initial fundamental matrix
p1 = [x1;y1;ones(1,length(x1))];
p2 = [x2;y2;ones(1,length(x2))];
Fp1 = Fi*p1;
FTp1 = Fi'*p1;
num = diag(p2'*Fi*p1).^2; % Sampson distance numerator
den = (Fp1(1,:)).^2 + (Fp1(2,:)).^2 + (FTp1(1,:)).^2 + (FTp1(2,:)).^2; % Sampson distance denominator
inl = find(num'./den < threshold);
if length(inl) > total_inliers
total_inliers = length(inl);
inliers = inl;
end
end
A = zeros(total_inliers,9);
for i = 1:total_inliers
A(i,:) = [x2(inliers(i))*x1(inliers(i)) x2(inliers(i))*y1(inliers(i)) x2(inliers(i)) y2(inliers(i))*x1(inliers(i)) y2(inliers(i))*y1(inliers(i)) y2(inliers(i)) x1(inliers(i)) y1(inliers(i)) 1];
end
F = fundamental_matrix(A);
end