-
Notifications
You must be signed in to change notification settings - Fork 0
/
warp.m
61 lines (51 loc) · 1.5 KB
/
warp.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
#------------------------------------------------------------------------------
# File: warp.m
# Author: Igor Janjic
# Description: Octave script that warps an image to another image frame.
##-----------------------------------------------------------------------------
function [A2, oi] = warp(H, A1)
# Determine the dimensions of the images.
[m, n, l] = size(A1);
# Find the corner points (x, y) of the original image.
s = zeros(4, 2);
s(1, :) = [1, 1];
s(2, :) = [n, 1];
s(3, :) = [n, m];
s(4, :) = [1, m];
# Map the corners to the second image frame using forward warping.
sh = [s'; ones(1, 4)];
sph = H*sh;
sphh = sph./sph(3, :);
sp = sphh(1:2, :)';
# Get the origin.
oi = [sp(1, 1); sp(1, 2)];
#oi = [0; 0]
# Get the output image dimensions.
minxp = min(sp(:, 1));
maxxp = max(sp(:, 1));
minyp = min(sp(:, 2));
maxyp = max(sp(:, 2));
np = round(maxxp - minxp);
mp = round(maxyp - minyp);
lp = l;
# Initialize the output image.
B = zeros(mp, np, lp);
[X, Y] = meshgrid(1:np, 1:mp);
X = reshape(X, [1, prod(size(X))]);
Y = reshape(Y, [1, prod(size(Y))]);
# Construct the points to be inverse warpped.
P = [X; Y];
P = P + oi;
Ph = [P; ones(1, length(X))];
# Do the inverse warping.
PPh = inv(H)*Ph;
PPhh = PPh./PPh(3, :);
PP = PPhh(1:2, :);
Xp = PP(1, :);
Yp = PP(2, :);
Xp = reshape(Xp, [mp, np]);
Yp = reshape(Yp, [mp, np]);
# Get the color from the original image frame.
A2(:, :, 1) = interp2(A1(:, :, 1), Xp, Yp);
A2(:, :, 2) = interp2(A1(:, :, 2), Xp, Yp);
A2(:, :, 3) = interp2(A1(:, :, 3), Xp, Yp);