Image Warping
http://www.jeffrey-martin.com
Many slides are from Alexei Efros (Berkeley) and Steve Seitz (U. of Washington)
CS3335 Visual computing Image Warping
• Parametrictransformations
– Linear transformations of images via 2×2 matrices
(a crash course on basic linear algebra)
– Homographies (3×3 transformation matrices)
• Estimation of parametric transformations (from corresponding points)
• Forwardandinversewarps – bilinear interpolation
Image Warping
image filtering: change range of image
g(x) = T(f(x))
ff xx
image warping: change domain of image g(x) = f(T(x))
ff xx
T
T
Image Warping
image filtering: change range of image g(x) = T(f(x))
fg
image warping: change domain of image g(x) = f(T(x))
T
f
g
T
Parametric (global) warping Examples of parametric warps:
translation rotation
aspect
affine
perspective
cylindrical
Parametric (global) warping
T
p = (x,y) p’ = (x’,y’) Transformation T is a coordinate-changing machine:
What does it mean that T is global?
• Is the same for any point p
• can be described by just a few numbers (parameters)
Let’s represent T as a matrix: p’ = Mp
x’ Mx y ‘ y
g(p’)g(Mp) f(p) f(M1p’)
Scaling
Scaling a coordinate means multiplying each of its components by a scalar
Uniform scaling means this scalar is the same for all components:
2
Scaling
Non-uniform scaling: different scalars per component:
X 2, Y 0.5
Scaling
Scaling operation: Or, in matrix form:
x’ax y’by
x’ a 0x y’ 0 b y
scaling matrix S
What’s inverse of S?
2-D Rotation
(x’, y’)
(x, y)
x’ = x cos() – y sin() y’ = x sin() + y cos()
2-D Rotation
(x’, y’)
(x, y)
x = r cos (f)
y = r sin (f)
x’ = r cos (f + ) y’ = r sin (f + )
Trig Identity…
x’ = r cos(f) cos() – r sin(f) sin() y’ = r cos(f) sin() + r sin(f) cos()
Substitute…
x’ = x cos() – y sin() y’ = x sin() + y cos()
f
2-D Rotation
This is easy to capture in matrix form:
x’cos() sin()x
y’ sin( )
Even though sin() and cos() are nonlinear functions of ,
• x’ is a linear combination of x and y
• y’ is a linear combination of x and y
What is the inverse transformation?
• Rotationby– 1 T
• For rotation matrices R R
cos( ) y
R
2×2 Matrices
What types of transformations can be represented with a 2×2 matrix?
2D Identity?
x’ x x’ 1 y’ y y’ 0
2D Scale around (0,0)?
x’sx *x x’sx y ‘ s y * y y ‘ 0
0x 1y
0x s y y
2×2 Matrices
What types of transformations can be represented with a 2×2 matrix?
2D Rotate around (0,0)?
x’ cos * x sin * y y’ sin * x cos * y
2D Shear?
x’ x shx * y y’shy*xy
x’cos sinx
y’ sin x’ 1
cos y shx x
y’ shy
1y
2×2 Matrices
What types of transformations can be represented with a 2×2 matrix?
2D Mirror about Y axis?
x’ x y’ y
2D Mirror over (0,0)?
x’ x y ‘ y
x’ 1 y’ 0
0x 1y
x’ 1 y ‘ 0
0 x 1 y
2×2 Matrices
What types of transformations can be represented with a 2×2 matrix?
2D Translation?
x’ xtx y’ yty
NO!
Only linear 2D transformations can be represented with a 2×2 matrix
All 2D Linear Transformations
Linear transformations are combinations of …
• Scale,
• Rotation,
• Shear, and
• Mirror
Properties of linear transformations:
• Origin maps to origin
• Lines map to lines
• Parallel lines remain parallel
x’ a y’ c
bx d y
• Distance or length ratios are preserved on parallel lines
– scaling of length/distances depends on (line) orientation only (see next slide)
• Ratios of areas are preserved
• Closed under composition
x’ a bi js y ‘ c d k l r
qx t y
• See pp. 40-41 of Hartley and Zisserman “Multiple View Geometry” (2nd edition)
All 2D Linear Transformations Decomposition into basic geometric transformations
a b 0 ARR1R
c d
0 2
rotation by angle
deformation (non-isotropic scaling)
1
1 f
Scaling directions are always orthogonal.
Areas are scaled (homogeneously over a plane) by a factor of det A = 1 2
2
Linear Transformation as Space Deformation qTp
j =(0,1)
i =(1,0)
p = (4,3)
q
Tp
coordinates of p in basis i,j
qx ux vx 4 qy uy vy 3
coordinates of q in basis i,j qqx iqy j
p4i3j point p is transformed into new point q
Linear Transformation as Change of Basis NOTE: q looks just like p
q = (qx,qy)
v =(vx, vy)
relative to coordinate system u,v q =(4,3)
j =(0,1)
i =(1,0)
vx ivy j
now interpret the columns of matrix T
u=(ux,uy)ux iuy j
as some vectors u and v (their coordinates in basis i, j)
qx ux vx 4 qy uy vy 3
coordinates of q in basis i,j
qqx iqy j ? q4u3v
qy Indeed, q4(ux iuy j)3(vx ivy j) (4ux 3vx)i(4uy 3vy)j
u v qx
coordinates of q in basis u,v
Linear Transformation as Change of Basis
T
q = (qx,qy)
v =(vx, vy)
q =(4,3)
j =(0,1)
i =(1,0)
vx ivy j
now interpret the columns of matrix T
coordinates of q in basis i,j qqx iqy j
u=(ux,uy)ux iuy j
as some vectors u and v (their coordinates in basis i, j)
coordinates of q in basis u,v ? q4u3v
qx ux vx 4 qy uy vy 3
point q represented in different coordinate systems
Linear Transformation as Change of Basis
T
q = (qx,qy)
v =(vx, vy)
q =(4,3)
j =(0,1)
i =(1,0)
vx ivy j
now interpret the columns of matrix T
u=(ux,uy)ux iuy j
as some vectors u and v (their coordinates in basis i, j)
qx ux vx 4 qy uy vy 3
Any matrix can be seen as a (linear) coordinate system basis!!!
Question: What’s the inverse matrix T-1 ? 4 ? ?qx 3 ? ?qy
Linear Transformation as Change of Basis
-1 T
q = (qx,qy)
v =(vx, vy)
q =(4,3)
j =(0,1)
i =(1,0)
vx ivy j
now interpret the columns of matrix T
u=(ux,uy)ux iuy j
as some vectors u and v (their coordinates in basis i, j)
Any matrix can be seen as a (linear) coordinate system basis!!!
Question: What’s the inverse matrix T-1 ?
4 ix 3 iy
jx qx jy qy
coordinates of i and j in basis u,v
iix uiy v j jx u jy v
qx ux vx 4 qy uy vy 3
Linear Transformation as Change of Basis
q
T=?
q
j =(0,1)
v =(vx, vy)
u =(ux, uy)
i =(1,0)
Any matrix can be seen as a (linear) coordinate system basis!!! Question: What if both coordinate systems have ortho-normal basis?
Linear Transformation as Change of Basis
q
T=R
q
j =(0,1)
v =(vx, vy)
u =(ux, uy)
i =(1,0)
Any matrix can be seen as a (linear) coordinate system basis!!!
Question: What if both coordinate systems have ortho-normal basis? Then as a linear transformation matrix T represents rotation
Homogeneous Coordinates
Q: Can we represent translation by matrix multiplication?
x’ xtx y’ yty
very simple, but
not a linear transformation
for 2D points p T ( p q) T ( p) T (q)
T(p) T(p) Answer: Yes, using homogeneous coordinates and 3×3 matrices
Homogeneous coordinates
• represent coordinates in 2 dimensions with a 3-vector
y’ 0 1 t y yt
x’ 1 0 tx x
xtx
yy
1 0 0 1 1 1
Translation matrix (3×3)
x homogeneous x coordinates
y y 1
Translation Example of translation
Homogeneous Coordinates
x’ 1 0 txx xtx y’0 1 t yyt
1 0 0 11 1
yy
tx = 2 ty = 1
Homogeneous Coordinates (in general)
Add a 3rd coordinate to every 2D point
• (x, y, w) represents a point at location (x/w, y/w) • (0, 0, 0) is not allowed
Advantages of homogeneous coordinate system:
y
2 1
12
(2,1,1) or (4,2,2) or (6,3,3)
x
(w2,w1,w)
represent the same 2D point for any value of w
– simple matrix representation of many useful transformations
Homogeneous Coordinates (in general)
Add a 3rd coordinate to every 2D point
• (x, y, w) represents a point at location (x/w, y/w) • (0, 0, 0) is not allowed
• (x, y, 0) represents a point at infinity
Advantages of homogeneous coordinate system:
y
2 1
12
( 2 , 1 , 14 ) ( 2 , 1 , 12 )
(2,1,1)
x
– simple matrix representation of many useful transformations
-allowstoexpandR2 with“pointsatinfinity” (like R1 could be expanded with )
Basic 2D Transformations Basic 2D transformations as 3×3 matrices
x’ 1 0 txx y’ 0 1 ty y
1 0 0 11
Translate
x’ cos sin 0x y’ sin cos 0y
10 0 11
Rotate
x’ sx 0 0x y’ 0 sy 0y 1 0 0 11
Scale
x’ 1 shx 0x y’ shy 1 0y
1 0 0 11
Shear
Affine Transformations
Affine transformations are combinations of … • Linear 2D transformations, and
• Translations
Properties of affine transformations:
• Origin does not necessarily map to origin
x’ a b cx y’d e fy
w 0 0 1w
• Lines map to lines
• Parallel lines remain parallel
• Length/distance ratios are preserved on parallel lines
• Ratios of areas are preserved
• Closed under composition
Affine Transformations
Example of a composition of basic transformations
x’ 1 0 txcos sin 0sx 0 0x y’0 1 tysin cos 00 sy 0y w’0010 01001w
p’ = T(tx,ty)
R()
S(sx,sy) p
Projective Transformations (a.k.a. homographies) transformations in homogeneous coordinate space via general 3×3 matrices
Projective transformations …
• Affine transformations, and
• Projective warps
Properties of projective transformations:
• Origin does not necessarily map to origin
x’ a b cx y’d e fy
w’ g h i 1
• Lines map to lines indeed, points p (in hom. coord.) on a line are a∙p=0. Then, b∙Hp=0 for b=aH-1
• Parallel lines do not necessarily remain parallel
• Non-parallel lines may become parallel
• Distance/length or area ratios are not preserved
• Closed under composition
• Models change of basis
Projective Transformations (a.k.a. homographies)
• •
Parallel lines do not necessarily remain parallel Non-parallel lines may become parallel
H
3 a b c6 2 d e f 5
0 1 1 11
(3,2,0)
y
(6,5,1) y x?x
(3,2,1)
NOTE: “finite” point may transform to “point at infinity”
Projective Transformations (a.k.a. homographies)
• •
Parallel lines do not necessarily remain parallel
Non-parallel lines may become parallel
H-1
12 a’ b’ c’3 10d’ e’ f’2
2 g’ h’
i’ 0
(3,2,0)
y
(6,5,1) y x?x
(3,2,1)
NOTE: or “point at infinity” may transform to “finite” point
Projective Transformations
• Distance/length or area ratios are not preserved
(a.k.a. homographies)
A’
(3,2,0)
B’
C’ ?
xx
3 a b c6 2 d e f 5 0 g h i1
yAy (6,5,1)
B C
(3,2,1)
Example: distance |B’C’| remains finite, while |A’B’| is infinite
Projective Transformations (a.k.a. homographies) General property to keep in mind (Theorem 2.10 from Hartley&Zisserman)
An invertible mapping h from a (homogeneous) plane P2 onto P2 preserves straight lines iff there exists a non-singular 3×3 matrix H s.t.
for any x2
That is, any transformation of a plane onto a plane that preserves straight lines must be a homography.
h( x ) H x
2D image transformations
These transformations are a nested set of groups
• Closed under composition and inverse is a member
See Hartley and Zisserman,
p. 44
Remaining parts of this lecture
• Estimation of parametric transformations (from corresponding points)
• Forward and inverse warps
Recovering Parametric Transformations
?
T(x,y) y y’
x f(x,y) x’ g(x’,y’)
What if we know f and g and want to recover transform T?
• e.g. to better align images (image registration)
• willing to let user provide correspondences
Q: How many pairs of corresponding points do we need?
Translation: # correspondences?
?
T(x,y) y y’
x x’
How many correspondences needed for translation? How many Degrees of Freedom (DOF)?
What is the transformation matrix?
1 0 cp’p
xxx
M M 0 0 1 1 cp ‘ p yyy
0 0 11
Euclidian: # correspondences?
?
T(x,y) y y’
x x’
How many correspondences needed for translation+rotation? How many DOF?
Transformation matrix?
cos sin cx
M sin cos cy
001
Affine: # correspondences?
?
T(x,y) y y’
x x’
How many correspondences needed for affine? How many DOF?
Transformation matrix?
Geometric point of view:
a b c
Mc d f
0 0 1
??? perhaps 3 pairs specifying a maps for 2 lines should be enough to define an affine transform
Algebraic point of view
p’i M pi
p’i M pi x’i a b cxi
for any given pair of corresponding points
(pi,p’i ) 6 unknown
parameters (variables)
y’id e fyi 1 0 0 11
xax by c
iii
y dx ey f iii
gives with known point coordinates for pi and p’i
3 pairs of corresponding points give 3×2 (=6) linear equations allowing to resolve 6 unknown parameters
Each pair of corresponding points (pi ,p’i )
two linear equations w.r.t 6 unknown coefficients of matrix M
Affine: # correspondences?
?
T(x,y) y y’
x x’
How many correspondences needed for affine?
How many DOF? Transformation matrix?
=3
gives 3×2 (6) equations for 6 unknowns
a b c
Mc d f
0 0 1
Projective: # correspondences?
?
T(x,y) y y’
x x’
=4
Easy to check that 4 pairs give only 4×2 (=8) equations! What about 9 unknowns?
a b c
How many correspondences needed for projective?
How many DOF? Transformation matrix?
Homographies have only 8 DOF since scale is irrelevant (multiplying M by any factor does not change the actual transformation). To choose one unique matrix M we can fix one parameter (e.g. i=1)
or restrict matrix norm ||M||F =1 (the sum of squares of all entries). Either way, one can get one additional (9th) constraint for parameters of M.
Md e f
g h i
Example: warping triangles
B
B’
?
T(x,y)
Given two triangles: ABC and A’B’C’ in 2D (3 corresponding pairs)
Need to find (linear) transform T to transfer all pixels from one to the other.
(affine)
a b c
Mc d f
0 0 1
A Source C
C’
What kind of transformation is T?
How can we compute the transformation matrix?
(solve 6 linear equations with 6 unknons)
A’ Destination
Image warping
T(x,y) y y’
x f(x,y) x’ g(x’,y’)
Given a coordinate transform (x’,y’) = T(x,y) and a source image f(x,y), how do we compute a transformed image g(x’,y’) = f (x,y)?
Forward warping
T(x,y) y y’
x f(x,y) x’ g(x’,y’)
Send each pixel (x,y) in the first image to its corresponding
location (x’,y’) = T(x,y) in the second image Q: what if pixel lands “between” two pixels?
Forward warping
T(x,y) y y’
x f(x,y) x’ g(x’,y’)
Send each pixel (x,y) in the first image to its corresponding
location (x’,y’) = T(x,y) in the second image Q: what if pixel lands “between” two pixels?
A: distribute color among neighboring pixels (x’,y’) – Known as “splatting”
Inverse warping
-1
T (x,y)
y y’
x f(x,y) x’ g(x’,y’)
Get each pixel (x’,y’) in the second image from its corresponding location (x,y) = T-1(x’,y’) in the first image
Q: what if pixel comes from “between” two pixels?
Inverse warping
-1
T (x,y)
y y’
x f(x,y) x’ g(x’,y’)
Get each pixel (x’,y’) in the second image from its corresponding location (x,y) = T-1(x’,y’) in the first image
Q: what if pixel comes from “between” two pixels?
A: Interpolate color value from neighbors – nearest neighbor, bilinear, Gaussian, bicubic
Linear interpolation in vector spaces
Q
Any point between P and Q can be obtained as a linear combination
P + (1) Q
v=Q-P
P
e.g.
P + 0.5(Q – P)
P + 0.5v =
= 0.5P + 0.5 Q
Linear interpolation for functions
Assume 1D image (scan line) with intensity f(P) and f(Q) for 2 pixels P and Q
Linear interpolation of function f between P and Q:
f
f(P)
f( P + (1) Q) f(Q)
f( P + (1) Q ) = f(P) + (1) f(Q)
PQ
P + (1) Q
Bilinear interpolation (2 variate image intensity function) Sampling of f at (x,y):
1-a
a
1-a
a
(i, j+1)
(i+1, j+1)
a
b
(x, y)
(i, j)
pixels viewed as points in 2D
(i+1, j)
pixels viewed as square regions in 2D
Interpolated intensity at (x,y) can be seen as a weighted average of 4 near-by pixels intensities where weights based on overlap area
Forward vs. inverse warping Q: which is better?
A: usually inverse—eliminates holes
•however, it requires an invertible warp function—not always possible…
x’
x’
x
x
?
3 pixels
12 pixels