46
Warping and Morphing

Warping and Morphing. Warping There are two ways to warp an image. The first, called forward mapping, scans through the source image pixel by pixel, and

Embed Size (px)

Citation preview

Page 1: Warping and Morphing. Warping There are two ways to warp an image. The first, called forward mapping, scans through the source image pixel by pixel, and

Warping and Morphing

Page 2: Warping and Morphing. Warping There are two ways to warp an image. The first, called forward mapping, scans through the source image pixel by pixel, and

Warping

There are two ways to warp an image. The first, called forward mapping, scans through the source image pixel by pixel, and copies them to the appropriate place in the destination image. The second, reverse mapping, goes through the destination image pixel by pixel, and samples the correct pixel from the source image. The most important feature of inverse mapping is that every pixel in the destination image gets set to something appropriate. In the forward mapping case, some pixels in the destination might not get painted, and would have to be interpolated. We calculate the image deformation as a reverse mapping. The problem can be stated "Which pixel coordinate in the source image do we sample for each pixel in the destination image?"

Page 3: Warping and Morphing. Warping There are two ways to warp an image. The first, called forward mapping, scans through the source image pixel by pixel, and
Page 4: Warping and Morphing. Warping There are two ways to warp an image. The first, called forward mapping, scans through the source image pixel by pixel, and
Page 5: Warping and Morphing. Warping There are two ways to warp an image. The first, called forward mapping, scans through the source image pixel by pixel, and

Image Warping• Move pixels of image

– Mapping– Resampling

Page 6: Warping and Morphing. Warping There are two ways to warp an image. The first, called forward mapping, scans through the source image pixel by pixel, and

Mapping• Define transformation

– Describe the destination (x, y) for every location (u, v) in the source image(or vice-versa, if invertible)

Page 7: Warping and Morphing. Warping There are two ways to warp an image. The first, called forward mapping, scans through the source image pixel by pixel, and

Example Mappings• Scale by factor

x = factor * u

y = factor * v

Page 8: Warping and Morphing. Warping There are two ways to warp an image. The first, called forward mapping, scans through the source image pixel by pixel, and

Example Mappings• Rotate by degrees

x = ucos - vsiny = usin + vcos

Page 9: Warping and Morphing. Warping There are two ways to warp an image. The first, called forward mapping, scans through the source image pixel by pixel, and

Other Mappings• Any function of u and v

x = fx(u, v)

y = fy(u, v)

Page 10: Warping and Morphing. Warping There are two ways to warp an image. The first, called forward mapping, scans through the source image pixel by pixel, and

Forward Mapping •Traverse input pixels

•Miss/overlap output pixels

W

Page 11: Warping and Morphing. Warping There are two ways to warp an image. The first, called forward mapping, scans through the source image pixel by pixel, and

Forward Mapping

• Iterate over source image– Some destination pixels may not be covered– Many source pixels can map to the same

destination pixel

Page 12: Warping and Morphing. Warping There are two ways to warp an image. The first, called forward mapping, scans through the source image pixel by pixel, and

Inverse Mapping

•Traverse output pixels

•Does not waste work W-1

Page 13: Warping and Morphing. Warping There are two ways to warp an image. The first, called forward mapping, scans through the source image pixel by pixel, and

Reverse Mapping• Iterate over destination image

– Must resample source– May oversample, but much simpler!

Page 14: Warping and Morphing. Warping There are two ways to warp an image. The first, called forward mapping, scans through the source image pixel by pixel, and

Resampling

• Evaluate source image at arbitrary (u, v)– (u, v) does not usually have integer coordinates

• Some kinds of resampling– Point resampling– Triangle filter– Gaussian filter

Source Image Destination Image

Page 15: Warping and Morphing. Warping There are two ways to warp an image. The first, called forward mapping, scans through the source image pixel by pixel, and

Point Sampling

• Take value at closest pixel– int iu = trunc(u + 0.5);– int iv = trunc(v + 0.5);– dst(x, y) = src(iu, iv);

• Simple, but causes aliasing

Page 16: Warping and Morphing. Warping There are two ways to warp an image. The first, called forward mapping, scans through the source image pixel by pixel, and

Triangle Filter

• Convolve with triangle filter

Page 17: Warping and Morphing. Warping There are two ways to warp an image. The first, called forward mapping, scans through the source image pixel by pixel, and

Triangle Filter

• Bilinearly interpolate four closest pixelsa = linear interpolation of src(u1, v2) and src(u2, v2)

b = linear interpolation of src(u1, v1) and src(u2, v1)

dst(x, y) = linear interpolation of ‘a’ and ‘b’

Page 18: Warping and Morphing. Warping There are two ways to warp an image. The first, called forward mapping, scans through the source image pixel by pixel, and

Gaussian Filter

• Convolve with Gaussian filter

Width of Gaussian kernel affects bluriness

Page 19: Warping and Morphing. Warping There are two ways to warp an image. The first, called forward mapping, scans through the source image pixel by pixel, and

Filtering Method Comparison

• Trade-offs– Aliasing versus blurring– Computation speed

Page 20: Warping and Morphing. Warping There are two ways to warp an image. The first, called forward mapping, scans through the source image pixel by pixel, and

Image Warping Implementation• Reverse mapping

for (int x = 0; x < xmax; x++) {

for (int y = 0; y < ymax; y++) {

float u = fx-1(x, y);

float v = fy-1(x, y);

dst(x, y) = resample_src(u, v, w);

}

}

Source Image Destination Image

Page 21: Warping and Morphing. Warping There are two ways to warp an image. The first, called forward mapping, scans through the source image pixel by pixel, and

Polynomial Transformation

Geometric correction requires a spatial transformation to invert an unknown distortion function. The mapping functions, U and V , have been universally chosen to be global bivariate polynomial transformations of the form:

jiN

i

iN

jij yxau

0 0

jiN

iij

N

j

yxbv

0

1

0

where aij and bij are constant polynomial coefficients

A first degree ( N=1) bivariate polynomial defines those mapping functions that are exactly given by a general 3 x 3 affine transformation matrix .

210 ayaxau 210 bybxbv

(20)

Page 22: Warping and Morphing. Warping There are two ways to warp an image. The first, called forward mapping, scans through the source image pixel by pixel, and

Polynomial Warping

In the remote sensing, the polynomial coefficients are not given directly. Instead spatial information is supplied by means of control points, corresponding positions in the input and output images whose coordinates can be defined precisely. In these cases, the central task of the spatial transformation stage is to infer the coefficients of the polynomial that models the unknown distortion. Once these coefficients are known, Eq.20 is fully specified and it is used to map the observed (x,y) points onto the reference ( u,v) coordinate system. This is also called polynomial warping. It is practical to use polynomials up to the fifth order for the transformation function.

Page 23: Warping and Morphing. Warping There are two ways to warp an image. The first, called forward mapping, scans through the source image pixel by pixel, and
Page 24: Warping and Morphing. Warping There are two ways to warp an image. The first, called forward mapping, scans through the source image pixel by pixel, and

Least – Squares with Ordinary

Polynomials From Equation (20) with N=2, coefficients aij can be determined by minimizing

2

1

20211011000

2

11

2 ),(

M

kkkkkkk

M

kkk

M

k

k uyayxayaxaayxUE

This is achieved by determining the partial derivatives of E with respect to coefficients aij, and equating them to zero. For each coefficient aij, we have :

021

ij

kM

kk

ij da

d

da

dE

By considering the partial derivative of E with respect to all six coefficients, we obtain the system of linear equations.

Page 25: Warping and Morphing. Warping There are two ways to warp an image. The first, called forward mapping, scans through the source image pixel by pixel, and

Weighted Least Squares As you see the least-squares formulation is global error measure - distance between control points ( xk, yk) and approximation points ( x,y).

The least –squares method may be localized by introducing a weighting function Wk that represents the contribution of control point (xk,yk) on point (x,y)22 )()(

1

kk

kyyxx

W

where determines the influence of distant control points and approximating points

),(),(),(2

1

yxWuyxUyxE k

M

kkkk

Page 26: Warping and Morphing. Warping There are two ways to warp an image. The first, called forward mapping, scans through the source image pixel by pixel, and

Pseudoinverse SolutionLet a correspondence be established between M points in the observed and reference images. The spatial transformation that approximates this correspondence is chosen to be a polynomial of degree N. In two variables ( x and y) , such a polynomial has K coefficients where

2

)2)(1(1

0 0

NNK

N

i

N

j

For example, a second-degree approximation requires only six coefficients to be solved. In this case , N=2 and K=6.

02

20

11

01

10

00

22

3

2222

211

21111

2

1

1

....1

.....1

....1

..1

1

.

.

.

a

a

a

a

a

a

yxyxyx

x

yxyx

yxyxyx

u

u

u

MMMMMMM

In matrix form:

U=WA ; V=WB

 WTU = WTWA A = (WTW)-1WTU ; B =(WTW)-1WTV

Page 27: Warping and Morphing. Warping There are two ways to warp an image. The first, called forward mapping, scans through the source image pixel by pixel, and

Warping Summary

• Reverse mapping is simpler to implement

• Different filters trade-off speed and aliasing/blurring

• Fun and creative warps are easy to implement

Page 28: Warping and Morphing. Warping There are two ways to warp an image. The first, called forward mapping, scans through the source image pixel by pixel, and

Image Morphing

• Animate transition between two images

Page 29: Warping and Morphing. Warping There are two ways to warp an image. The first, called forward mapping, scans through the source image pixel by pixel, and

Cross-Dissoving

• Blend imagesblend(i, j) = (1-t)src(i, j) + tdst(i, j)

(0 t 1)

Page 30: Warping and Morphing. Warping There are two ways to warp an image. The first, called forward mapping, scans through the source image pixel by pixel, and

Image Morphing

• Combines warping and cross-dissolving

Page 31: Warping and Morphing. Warping There are two ways to warp an image. The first, called forward mapping, scans through the source image pixel by pixel, and

Image Morphing• Warping step is the hard one

– Aim to align features in images

Page 32: Warping and Morphing. Warping There are two ways to warp an image. The first, called forward mapping, scans through the source image pixel by pixel, and

Image MorphingThere are two necessary components to morphing algorithms:

1. the user must have a mechanism to establish correspondence between the two images - perhaps as simple as silhouettes

2. from this correspondence, the algorithm must determine a dense pixel correspondence which is the warp algorithm

A common technique often used for facial morphing is named feature-based morphing. Important feature are chosen rather than a grid of points that must cover "unimportant" areas. Typical features might be ears, nose, and eyebrows. Lines are drawn in each image and are associated with each other. If there is only one pair of lines, the algorithm produces a purely local change. Therefore, a high spatial density is achieved in areas of interest.

Page 33: Warping and Morphing. Warping There are two ways to warp an image. The first, called forward mapping, scans through the source image pixel by pixel, and

Feature-based Morphing• Beier & Neeley use pairs of lines to specify

warp– Given p in dst image, where is p’ in source

image?

u is a fractionv is a length(in pixels)

Page 34: Warping and Morphing. Warping There are two ways to warp an image. The first, called forward mapping, scans through the source image pixel by pixel, and

Feature-based MorphingThe mapping function is simple. An inverse mapping function is used.                                                                   

Page 35: Warping and Morphing. Warping There are two ways to warp an image. The first, called forward mapping, scans through the source image pixel by pixel, and

Feature-based MorphingIf more than one pair of lines is used, the transformations must be blended. Beier and Neeley of Pacific Data Images who developed the technique suggested the use of weight. (NOTE: Beier-92.pdf work).

Page 36: Warping and Morphing. Warping There are two ways to warp an image. The first, called forward mapping, scans through the source image pixel by pixel, and

Warping with One Line Pair

• What happens to the ‘F’?

Page 37: Warping and Morphing. Warping There are two ways to warp an image. The first, called forward mapping, scans through the source image pixel by pixel, and

Warping with Multiple Line Pairs

• Use weighted combination of points defined by each pair of corresponding lines

p’ is a weighted average

Page 38: Warping and Morphing. Warping There are two ways to warp an image. The first, called forward mapping, scans through the source image pixel by pixel, and

Weighting Effect of Each Line Pair

• To weight the contribution of each line pair

– where• Length[i] is the length of L[i]

• dist[i] is the distance from X to L[i]

• a, b, p are constants that control the warp

bp

idista

ilengthiweight

][

][][

Page 39: Warping and Morphing. Warping There are two ways to warp an image. The first, called forward mapping, scans through the source image pixel by pixel, and

Warping Pseudocode

WarpImage(Image, L’[], L[])begin foreach destination pixel p do psum = (0, 0) wsum = (0, 0) foreach line L[i] in destination do p’[i] = p transformed by (L[i], L’[i]) psum = psum + p’[i] * weight[i] wsum += weight[i] end p’ = psum / wsum Result(p) = Image(p’) endend

Page 40: Warping and Morphing. Warping There are two ways to warp an image. The first, called forward mapping, scans through the source image pixel by pixel, and

Morphing Pseudocode

GenerateAnimation(Image0, L0[], Image1, L1[])

begin

foreach intermediate frame time t do

for i = 1 to number of line pairs do

L[i] = line t-th of the way from L0[i] to L1[i]

end

Warp0 = WarpImage(Image0, L0, L)

Warp1 = WarpImage(Image1, L1, L)

foreach pixel p in FinalImage do

Result(p) = (1-t)Warp0 + tWarp1

end

end

Page 41: Warping and Morphing. Warping There are two ways to warp an image. The first, called forward mapping, scans through the source image pixel by pixel, and

Morphing Summary

• Specifying correspondences

• Warping

• Blending

Page 42: Warping and Morphing. Warping There are two ways to warp an image. The first, called forward mapping, scans through the source image pixel by pixel, and

Graphical Objects and Metamorphosis

MetamorphosisObject O1 Object O2

Geometry Alignment

Attribute Interpolation

Geometric

Data Set

Geometric

Data Set

Attributes Attributes

Page 43: Warping and Morphing. Warping There are two ways to warp an image. The first, called forward mapping, scans through the source image pixel by pixel, and

Warping Pipeline

Deform Adjust

Transformation of geometry Generation of attributes

Page 44: Warping and Morphing. Warping There are two ways to warp an image. The first, called forward mapping, scans through the source image pixel by pixel, and

Morphing Pipeline

Deform Adjust

Deform Adjust

Blend

Page 45: Warping and Morphing. Warping There are two ways to warp an image. The first, called forward mapping, scans through the source image pixel by pixel, and

Warping and Morphing

Warping•Single object

•Specification of original and deformed states

Morphing

•Two objects

•Specification of initial and final states