Abhigyan Singh
Abhigyan Singh

Reputation: 167

Applying homography to image

I am stuck at this homography problem. I have to apply homography and then 2d bilinear interpolation to render a image so that I see a birds eye view.

I successfully computed the homography(I guess). I am unable to figure out how to apply this homography matrix to the source image to generate output image over blank 940x500 image.

Here is my code so far:

I = imread('image.png');
imshow(I)

% Mark more than 4 input points
[x,y] = getpts

xp = [0; 720; 720; 0;];
yp = [0; 0; 1280; 1280;];

for i=1:4
    A(2*i-1,:) = [x(i), y(i), 1, 0, 0, 0, -x(i)*xp(i), -xp(i)*y(i), -xp(i)];
    A(2*i,:) = [0, 0, 0, x(i), y(i), 1, -x(i)*yp(i), -yp(i)*y(i), -yp(i)];
end

[U,S,V] = svd(A);
h = V(:,9);

H = reshape(h,3,3);

How do I apply this H matrix to the source image?

Upvotes: 1

Views: 2734

Answers (1)

Rotem
Rotem

Reputation: 32084

You can use projective2d and imwarp:

tform = projective2d(H);
outputImage = imwarp(I, tform);

I don't understand the example you gave, so I can't say if you need to invert H.

It could be that my answer is wrong - I can't execute your code sample for verifying the solution.

Please let me know if it solves your problem.

Upvotes: 1

Related Questions