Image Stitching
Add example
Computer Vision
Jia-Bin Huang, Virginia Tech
Many slides from S. Seitz and D. Hoiem
Administrative stuffs
• HW 3 will be out Oct 3 (Wed), due Oct 17 (Wed)
• Getting help?
• Piazza
• Jia-Bin’s office hour: 3:30 – 4:30 PM Monday (440
Whittemore Hall)
• Accommodation
• Send me an email
Review: Camera Projection Matrix R
jw
t
kw
Ow
iw
X
u f s u0 r11 r12 r13 tx
x K R t X
Y
w v 0 f v0 r21 r22 r23 ty
Z
1 0 0 1 r31 r32 r33 t z
1
3
Review: Camera Calibration
Method 1: Use an object (calibration grid) with
known geometry
• Correspond image points to 3d points
• Get least squares solution (or non-linear solution)
Known 2d image Known 3d
coordinates locations
X
wu m11 m12 m13 m14 Y
wv m m22 m23 m24
21 Z
w m31 m32 m33 m34
1
Unknown Camera Parameters 4
Unknown Camera Parameters
X
su m11 m12 m13 m14
Known 2d sv m Y Known 3d
m22 m23 m24
image coords 21 Z locations
s m31 m32 m33 m34
1
0 m11 X m12Y m13 Z m14 m31uX m32uY m33uZ m34u
0 m21 X m22Y m23 Z m24 m31vX m32 vY m33vZ m34 v
• Homogeneous linear system.
m11
Solve for m’s entries using linear m
12
least squares m13
m14
X 1 Y1 Z1 1 0 0 0 0 u1 X 1 u1Y1 u1Z1 u1 0
0 0 m21
0 0 X1 Y1 Z1 1 v1 X 1 v1Y1 v1Z1 v1 0 [U, S, V] = svd(A);
m
22 M = V(:,end);
m
X n Yn Zn 1 0 0 0 0 un X n unYn un Z n un 23 0
m M = reshape(M,[],3)';
0 0 0 0 X n Yn Zn 1 vn X n vnYn vn Z n vn 24 0
m
31
m32
m33
m34
Review: Calibration by vanishing points
⊤
VP (2D) VP (3D) Orthogonality constraints 𝑿 𝒊 𝑿 𝒋=𝟎
•
Unknown camera parameters
𝒑𝒊 = 𝑲𝑹 𝑿 𝒊 𝑓 0 𝑢0
𝑿
𝒊 = 𝑹 −𝟏
𝑲 −𝟏
𝒑𝒊
𝑲= 0
0
[ 1
𝑓
0
𝑣0
1
𝑢0
]
[ ]
0 −
𝒑⊤
𝒊
( 𝑲 −𝟏 )⊤ ( 𝑹 −𝟏 )⊤ (𝑹¿¿ − 𝟏)(𝑲 ¿ ¿− 𝟏) 𝒑 𝒋=𝟎 ¿ ¿ 𝑓 𝑓
𝑲 − 1= 1 𝑣
0 − 0
𝑓 𝑓
Constraints for , 0 0 1
=0
… Eqn (1)
=0
… Eqn (2)
=0
… Eqn (3)
Eqn (1) – Eqn (2)
Eqn (2) – Eqn (3)
Solve for
Review: Calibration by vanishing points
•Rotation
matrix Unknown camera parameters
Set directions of vanishing points
Special properties of R
• inv(R)=RT
• Each row and column of
R has unit length
Slide by Steve Seitz
Measuring height vz
r
vanishing line (horizon)
t0 t
vx v vy
H R H
b0
b
t b vZ r H
r b vZ t R
image cross ratio
8
This class: Image Stitching
• Combine two or more overlapping images to make
one larger image
Add example
Slide credit: Vaibhav Vaish
Concepts introduced/reviewed in
today’s lecture
• Camera model (Lecture 11)
• Homographies (Lecture 9)
• Solving homogeneous systems of linear equations
(Lecture 12)
• Keypoint-based alignment (Lecture 9)
• RANSAC (Lecture 8)
• Blending (Lecture 5)
• How the iphone stitcher works
Illustration
Camera Center
Problem set-up
.X
• x = K [R t] X x
• x' = K' [R' t'] X
• t=t'=0 x'
f f'
• x'=Hx where H = K' R' R-1 K-1
• Typically only R and f will change (4 parameters),
but, in general, H has 8 parameters
Homography
• Definition
• General mathematics:
homography = projective linear transformation
• Vision (most common usage):
homography = linear transformation between two
image planes
• Examples
• Project 3D surface into frontal view
• Relate two views that differ only by rotation
Homography example:
Image rectification
p’
p
To unwarp (rectify) an image solve for homography H
given p and p’: wp’=Hp
Homography example:
Planar mapping
Freedom HP Commercial
Image Stitching Algorithm Overview
1. Detect keypoints (e.g., SIFT)
2. Match keypoints (e.g., 1st/2nd NN < thresh)
3. Estimate homography with four matched
keypoints (using RANSAC)
4. Combine images
Computing homography
Assume we have four matched points: How do we
compute homography H?
Direct Linear Transformation (DLT)
w' u'
h1 h2 h3
H h4 h6
x ' Hx x ' w' v '
h5
w' h7 h8 h9
h1
h
2
h3
u v 1 0 0 0 uu vu u
h4
0 h0 h h5
0 0 u v 1 uv vv v
h6
h7
h8
h
9
Computing homography
Direct Linear Transform
u1 v1 1 0 0 0 u1u1 v1u1 u1
0 0 0 u1 v1 1 u1 v1 v1 v1 v1
h 0 Ah 0
0 0 0 un vn 1 u n v n v n v n v n
• Apply SVD: UDVT = A
• h = Vsmallest (column of V corr. to smallest singular value)
h1 Matlab
h h1 h2 h3
h H h4 h5 h6
2 [U, S, V] = svd(A);
h = V(:, end);
h7 h8 h9
h9
Explanations of SVD and solving homogeneous linear systems
Computing homography
• Assume we have four matched points: How do we
compute homography H?
Normalized DLT
1. Normalize coordinates for each image
a) Translate for zero mean
~
b) Scale so that average
x Tx ~ to origin is ~sqrt(2)
distance
x Tx
– This makes problem better behaved numerically
~107-108)
H
(see HZ p.
1 ~
2. Compute using DLT
H T normalized
in HT coordinates
3. Unnormalize: x i Hx i
Computing homography
• Assume we have matched points with outliers: How do
we compute homography H?
Automatic Homography Estimation with RANSAC
1. Choose number of samples N
HZ Tutorial ‘99
Computing homography
• Assume we have matched points with outliers: How do we
compute homography H?
Automatic Homography Estimation with RANSAC
1. Choose number of samples N
2. Choose 4 random potential matches
3. Compute H using normalized DLT
4. Project points from
x i x to
Hxx’ for each potentially matching
i
pair:
5. Count points with projected distance < t
• E.g., t = 3 pixels
6. Repeat steps 2-5 N times
• Choose H with most inliers
HZ Tutorial ‘99
Automatic Image Stitching
1. Compute interest points on each image
2. Find candidate matches
3. Estimate homography H using matched points and
RANSAC with normalized DLT
4. Project each image onto the same surface and
blend
• Matlab: maketform, imtransform
RANSAC for Homography
Initial Matched Points
RANSAC for Homography
Final Matched Points
RANSAC for Homography
Choosing a Projection Surface
Many to choose: planar, cylindrical, spherical, cubic,
etc.
Planar Mapping
x
x
f f
1) For red image: pixels are already on the planar surface
2) For green image: map to first image plane
Planar Projection
Planar
Photos by Russ Hewett
Planar Projection
Planar
Cylindrical Mapping
x
x
f
f
1) For red image: compute h, theta on cylindrical surface from (u, v)
2) For green image: map to first image plane, than map to cylindrical surface
Cylindrical Projection
Cylindrical
Cylindrical Projection
Cylindrical
Planar
Cylindrical
Recognizing Panoramas
Some of following material from Brown and Lowe 2003 talk Brown and Lowe 2003, 2007
Recognizing Panoramas
Input: N images
1. Extract SIFT points, descriptors from all images
2. Find K-nearest neighbors for each point (K=4)
3. For each image
a) Select M candidate matching images by counting
matched keypoints (m=6)
b) Solve homography Hij for each matched image
Recognizing Panoramas
Input: N images
1. Extract SIFT points, descriptors from all images
2. Find K-nearest neighbors for each point (K=4)
3. For each image
a) Select M candidate matching images by counting
matched keypoints (m=6)
b) Solve homography Hij for each matched image
c) Decide if match is valid (ni > 8 + 0.3 nf )
# keypoints in
# inliers
overlapping area
Recognizing Panoramas (cont.)
(now we have matched pairs of images)
4. Find connected components
Finding the panoramas
Finding the panoramas
Recognizing Panoramas (cont.)
(now we have matched pairs of images)
4. Find connected components
5. For each connected component
a) Perform bundle adjustment to solve for rotation (θ1,
θ2, θ3) and focal length f of all cameras
b) Project to a surface (plane, cylinder, or sphere)
c) Render with multiband blending
Bundle adjustment for stitching
• Non-linear minimization of re-projection error
• xˆ Hx where H = K’ R’ R-1 K-1
N Mi
error dist ( x , xˆ )
1 j k
• Solve non-linear least squares
(Levenberg-Marquardt algorithm)
• See paper for details
Bundle Adjustment
• New images initialised with rotation, focal
length of best matching image
Bundle Adjustment
• New images initialised with rotation, focal
length of best matching image
Details to make it look good
• Choosing seams
• Blending
Choosing seams
• Easy method
• Assign each pixel to image with nearest center
im1 im2
x x
Image 2
Image 1
Choosing seams
• Easy method
• Assign each pixel to image with nearest center
• Create a mask:
• mask(y, x) = 1 iff pixel should come from im1
• Smooth boundaries (called “feathering”):
• mask_sm = imfilter(mask, gausfil);
• Composite
• imblend = im1_c.*mask + im2_c.*(1-mask);
im1 im2
x x
Image 2
Image 1
Choosing seams
• Better method: dynamic program to find seam
along well-matched regions
Illustration: http://en.wikipedia.org/wiki/File:Rochester_NY.jpg
Gain compensation
• Simple gain adjustment
• Compute average RGB intensity of each image in overlapping
region
• Normalize intensities by ratio of averages
Multi-band Blending
• Burt & Adelson 1983
• Blend frequency bands over range l
Multiband Blending with
Laplacian Pyramid
• At low frequencies, blend slowly
• At high frequencies, blend quickly
1
0
1
Left pyramid blend Right pyramid
Multiband blending Laplacian pyramids
1. Compute Laplacian
pyramid of images and
mask
2. Create blended image
at each level of
pyramid
3. Reconstruct complete
image
Blending comparison (IJCV
2007)
Blending Comparison
Further reading
• DLT algorithm: HZ p. 91 (alg 4.2), p. 585
• Normalization: HZ p. 107-109 (alg 4.2)
• RANSAC: HZ Sec 4.7, p. 123, alg 4.6
• Rick Szeliski’s alignment/stitching tutorial
• Recognising Panoramas: Brown and Lowe, IJCV 2007
(also bundle adjustment)
How does iphone panoramic stitching work?
• Capture images at 30 fps
• Stitch the central 1/8 of a selection of images
• Select which images to stitch using the accelerometer and frame-to-
frame matching
• Faster and avoids radial distortion that often occurs towards corners of
images
• Alignment
• Initially, perform cross-correlation of small patches aided by
accelerometer to find good regions for matching
• Register by matching points (KLT tracking or RANSAC with FAST (similar
to SIFT) points) or correlational matching
• Blending
• Linear (or similar) blending, using a face detector to avoid blurring face
regions and choose good face shots (not blinking, etc)
http://www.patentlyapple.com/patently-apple/2012/11/apples-cool-iphone-5-panorama-app-revealed-in-5-patents.html
Things to remember
• Homography relates rotating cameras
• Recover homography using RANSAC and normalized
DLT
• Bundle adjustment minimizes reprojection error for
set of related images
• Details to make it look nice (e.g., blending)
See you on Thrusday
• Next class: Epipolar Geometry and Stereo Vision