-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathfind_projection_matrix.m
69 lines (61 loc) · 1.7 KB
/
find_projection_matrix.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
60
61
62
63
64
65
66
67
68
function [ H ] = find_projection_matrix(original,projective)
% find_projection_matrix calculate the projective transformation matrix H
% A transformation that maps lines to lines (but does not necessarily
% preserve parallelism) is a projective transformation.
%
% In projective transformation, given 4 non-collinear points in original
% image and the corresponding 4 points in transformed image,
% calculate the non-singular 3 × 3 matrix H that express the plane
% projective transformation.
%
% SYNTAX
% [H] = find_projection_matrix(original,projective)
%
% INPUT
% original - x, y coordinates of 4 points in original image
% - [x1,x2,x3,x4; y1,y2,y3,y4]
%
% projective - x, y coordinates of 4 points in transformed image
% - [u1,u2,u3,u4; v1,v2,v3,v4]
%
% OUTPUT
% H: - 3 × 3 projective matrix
%
% REFERENCES:
% Hartley, R., & Zisserman, A. (2015). Multiple view geometry in computer
% vision. Cambridge: Cambridge Univ. Press.
%
% Copyright 2017, yzhang559
% Licensed for use under Apache License, Version 2. See LICENSE for
% details.
% check input
[om,on]=size(original);
[cm,cn]=size(projective);
if ((om ~= 2) || (on ~= 4))
error('Invalid input original points matrix');
end
if ((cm ~= 2) || (cn ~= 4))
error('Invalid input projective points matrix');
end
% calculate in homogeneous coordinates
app=[1 1 1];
O=[original(:,(1:3));app];
O1=[original(:,4);1];
p1=O\O1;
Pro=[projective(:,(1:3));app];
Pro1=[projective(:,4);1];
p2=Pro\Pro1;
A=zeros(3,3);
for i = 1:3
for j = 1:3
A(i,j)=O(i,j)*p1(j);
end
end
B=zeros(3,3);
for i = 1:3
for j = 1:3
B(i,j)=Pro(i,j)*p2(j);
end
end
H=B/A;
end