[37] | 1 | function result = homogWarp(im, H, szIm, grayDefault) |
---|
| 2 | %% result = homogWarp(im, H, szIm, grayDefault) |
---|
| 3 | %% Warp the input image by the 2D homography H. The output |
---|
| 4 | %% image, result, is to be of size szIm, with x going from 0 to szIm(2) |
---|
| 5 | %% and y going from 0 to szIm(1). By default, szIm = size(im). |
---|
| 6 | %% grayDefault is the gray level to use for points in the new image |
---|
| 7 | %% that cannot be mapped from the original image im. |
---|
| 8 | %% Note, the warp H is arranged such that a pixel (x,y) in |
---|
| 9 | %% the result image comes from pixel (p1/p3, p2/p3) in the original |
---|
| 10 | %% image im, where (p1 p2 p3)^T = H * (x,y,1)^T. (If you don't get |
---|
| 11 | %% the desired result, try using the inverse of H.) |
---|
| 12 | |
---|
| 13 | if nargin < 3 |
---|
| 14 | % size of the image |
---|
| 15 | szIm = size(im); |
---|
| 16 | end |
---|
| 17 | if nargin < 4 |
---|
| 18 | % default gray value |
---|
| 19 | grayDefault = 127; |
---|
| 20 | end |
---|
| 21 | |
---|
| 22 | % pixel coordinates in original image |
---|
| 23 | [x,y] = meshgrid(1:size(im,2),1:size(im,1)); |
---|
| 24 | |
---|
| 25 | % pixel coordinates in result image |
---|
| 26 | if any(szIm ~= size(im)) |
---|
| 27 | [xp,yp] = meshgrid(1:szIm(2),1:szIm(1)); |
---|
| 28 | pix = [xp(:)'; yp(:)']; |
---|
| 29 | else |
---|
| 30 | pix = [x(:)'; y(:)']; |
---|
| 31 | end |
---|
| 32 | |
---|
| 33 | % homogeneous pixels in result frame. |
---|
| 34 | hPixels = [ pix; ones(1,prod(szIm))]; |
---|
| 35 | |
---|
| 36 | % corresponding warped points in original frame |
---|
| 37 | hScene = H*hPixels; |
---|
| 38 | |
---|
| 39 | % pixel coords in original frame. |
---|
| 40 | xprime=(hScene(1,:)./(hScene(3,:)))'; |
---|
| 41 | yprime=(hScene(2,:)./(hScene(3,:)))'; |
---|
| 42 | xprime = reshape(xprime, szIm); |
---|
| 43 | yprime = reshape(yprime, szIm); |
---|
| 44 | |
---|
| 45 | % Warping an image according to the homography |
---|
| 46 | result = interp2(x,y,im,xprime,yprime, '*linear'); |
---|
| 47 | result(isnan(result)) = grayDefault; |
---|
| 48 | result = reshape(result,szIm); |
---|
| 49 | |
---|