Skip to content

Instantly share code, notes, and snippets.

@messa
Last active August 29, 2015 14:18
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save messa/3c7b437a92a40aa645cf to your computer and use it in GitHub Desktop.
Save messa/3c7b437a92a40aa645cf to your computer and use it in GitHub Desktop.
homography
function homography_example
close all
hold on
axis([-100 800 -100 800]);
axis square;
set(gca, 'ydir', 'reverse');
in = [ ...
92 711 601 188; ...
398 398 93 90];
out = [ ...
10 710 710 10; ...
710 710 10 10];
plot( ...
[in(1,:); ...
out(1,:)], ...
[in(2,:); ...
out(2,:)], ...
'y--');
plot(in(1,:), in(2,:), 'bo');
plot(out(1,:), out(2,:), 'bx');
v = homography_solve(in, out)
in2 = [ ...
114 199 297 395 495 592 213 305 396 487 587 226 310 397 482 565 238 317 396 476 555 249 324 396 471 547 91 131 149 163 177 189 258 328 396 467 538 602 617 632 645 664 684 707 606 503 396 289 183; ...
325 329 332 333 334 334 267 269 269 270 270 214 213 214 217 218 166 167 169 170 171 125 126 126 129 130 394 265 211 166 125 93 92 93 92 93 94 95 131 173 219 272 333 398 400 401 401 400 398];
out2 = homography_transform(in2, v)
plot( ...
[in2(1,:); ...
out2(1,:)], ...
[in2(2,:); ...
out2(2,:)], ...
'y--');
plot(in2(1,:), in2(2,:), 'go');
plot(out2(1,:), out2(2,:), 'rx');
end
% ------------------------------------------------------
% following two functions copied from
% http://www.mathworks.com/matlabcentral/answers/26141-homography-matrix
function v = homography_solve(pin, pout)
% HOMOGRAPHY_SOLVE finds a homography from point pairs
% V = HOMOGRAPHY_SOLVE(PIN, POUT) takes a 2xN matrix of input vectors and
% a 2xN matrix of output vectors, and returns the homogeneous
% transformation matrix that maps the inputs to the outputs, to some
% approximation if there is noise.
%
% This uses the SVD method of
% http://www.robots.ox.ac.uk/%7Evgg/presentations/bmvc97/criminispaper/node3.html
% David Young, University of Sussex, February 2008
if ~isequal(size(pin), size(pout))
error('Points matrices different sizes');
end
if size(pin, 1) ~= 2
error('Points matrices must have two rows');
end
n = size(pin, 2);
if n < 4
error('Need at least 4 matching points');
end
% Solve equations using SVD
x = pout(1, :); y = pout(2,:); X = pin(1,:); Y = pin(2,:);
rows0 = zeros(3, n);
rowsXY = -[X; Y; ones(1,n)];
hx = [rowsXY; rows0; x.*X; x.*Y; x];
hy = [rows0; rowsXY; y.*X; y.*Y; y];
h = [hx hy];
if n == 4
[U, ~, ~] = svd(h);
else
[U, ~, ~] = svd(h, 'econ');
end
v = (reshape(U(:,9), 3, 3)).';
end
function y = homography_transform(x, v)
% HOMOGRAPHY_TRANSFORM applies homographic transform to vectors
% Y = HOMOGRAPHY_TRANSFORM(X, V) takes a 2xN matrix, each column of which
% gives the position of a point in a plane. It returns a 2xN matrix whose
% columns are the input vectors transformed according to the homography
% V, represented as a 3x3 homogeneous matrix.
q = v * [x; ones(1, size(x,2))];
p = q(3,:);
y = [q(1,:)./p; q(2,:)./p];
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment