robotics专项课程Perception Assignment 2: Image Projection(图像投影)
文档中错误更正:1. 公式(14)中x3=1
2. A为8x9矩阵
3. reshape后H矩阵需转置
function [ H ] = est_homography(video_pts, logo_pts)
% est_homography estimates the homography to transform each of the
% video_pts into the logo_pts
% Inputs:
% video_pts: a 4x2 matrix of corner points in the video
% logo_pts: a 4x2 matrix of logo points that correspond to video_pts
% Outputs:
% H: a 3x3 homography matrix such that logo_pts ~ H*video_pts
% est_homography estimates the homography to transform each of the
% video_pts into the logo_pts
% Inputs:
% video_pts: a 4x2 matrix of corner points in the video
% logo_pts: a 4x2 matrix of logo points that correspond to video_pts
% Outputs:
% H: a 3x3 homography matrix such that logo_pts ~ H*video_pts
A = zeros(8,9);
for i = 1:4
A(2*i-1,:)= [-video_pts(i,1) -video_pts(i,2) -1 0 0 0 video_pts(i,1)*logo_pts(i,1) video_pts(i,2)*logo_pts(i,1) logo_pts(i,1)];
A(2*i, :) = [0 0 0 -video_pts(i,1) -video_pts(i,2) -1 video_pts(i,1)*logo_pts(i,2) video_pts(i,2)*logo_pts(i,2) logo_pts(i,2)];
end
[U, S, V] = svd(A);
for i = 1:4
A(2*i-1,:)= [-video_pts(i,1) -video_pts(i,2) -1 0 0 0 video_pts(i,1)*logo_pts(i,1) video_pts(i,2)*logo_pts(i,1) logo_pts(i,1)];
A(2*i, :) = [0 0 0 -video_pts(i,1) -video_pts(i,2) -1 video_pts(i,1)*logo_pts(i,2) video_pts(i,2)*logo_pts(i,2) logo_pts(i,2)];
end
[U, S, V] = svd(A);
H = zeros(3,3);
H = reshape(V(:,9),3,3);
H = H';
H = reshape(V(:,9),3,3);
H = H';
end
function [ warped_pts ] = warp_pts( video_pts, logo_pts, sample_pts)
% warp_pts computes the homography that warps the points inside
% video_pts to those inside logo_pts. It then uses this
% homography to warp the points in sample_pts to points in the logo
% image
% Inputs:
% video_pts: a 4x2 matrix of (x,y) coordinates of corners in the
% video frame
% logo_pts: a 4x2 matrix of (x,y) coordinates of corners in
% the logo image
% sample_pts: a nx2 matrix of (x,y) coordinates of points in the video
% video that need to be warped to corresponding points in the
% logo image
% Outputs:
% warped_pts: a nx2 matrix of (x,y) coordinates of points obtained
% after warping the sample_pts
% warp_pts computes the homography that warps the points inside
% video_pts to those inside logo_pts. It then uses this
% homography to warp the points in sample_pts to points in the logo
% image
% Inputs:
% video_pts: a 4x2 matrix of (x,y) coordinates of corners in the
% video frame
% logo_pts: a 4x2 matrix of (x,y) coordinates of corners in
% the logo image
% sample_pts: a nx2 matrix of (x,y) coordinates of points in the video
% video that need to be warped to corresponding points in the
% logo image
% Outputs:
% warped_pts: a nx2 matrix of (x,y) coordinates of points obtained
% after warping the sample_pts
% Complete est_homography first!
[ H ] = est_homography(video_pts, logo_pts);
[nrows,ncols] = size(sample_pts);
warped_pts = zeros(nrows,ncols);
for i = 1:nrows
factor = H(3,1)*sample_pts(i,1)+H(3,2)*sample_pts(i,2)+H(3,3);
x = (H(1,1)*sample_pts(i,1)+H(1,2)*sample_pts(i,2)+H(1,3))/factor;
y = (H(2,1)*sample_pts(i,1)+H(2,2)*sample_pts(i,2)+H(2,3))/factor;
warped_pts(i,:) = [x y];
end
end