Lecture 4.3 Estimating homographies from feature ... - UiO

29
Lecture 4.3 Estimating homographies from feature correspondences Thomas Opsahl

Transcript of Lecture 4.3 Estimating homographies from feature ... - UiO

Page 1: Lecture 4.3 Estimating homographies from feature ... - UiO

Lecture 4.3 Estimating homographies from feature correspondences Thomas Opsahl

Page 2: Lecture 4.3 Estimating homographies from feature ... - UiO

Homographies induced by central projection

2

𝐻 𝐻

β€’ Homography 𝐻𝒖� = 𝒖�′

𝐻 =β„Ž1 β„Ž2 β„Ž3β„Ž4 β„Ž5 β„Ž6β„Ž7 β„Ž8 β„Ž9

β€’ Point-correspondences can be determined automatically β€’ Erroneous correspondences are common β€’ Robust estimation is required to find 𝐻

1 1 2 2

2 3 1

𝒖 𝒖′

Page 3: Lecture 4.3 Estimating homographies from feature ... - UiO

Homographies induced by central projection

3

𝐻

1 2 3

𝐻1←3 𝐻1←2

𝐻 1 2 1 2

β€’ Homography 𝐻𝒖� = 𝒖�′

𝐻 =β„Ž1 β„Ž2 β„Ž3β„Ž4 β„Ž5 β„Ž6β„Ž7 β„Ž8 β„Ž9

β€’ Point-correspondences can be determined automatically β€’ Erroneous correspondences are common β€’ Robust estimation is required to find 𝐻

𝒖 𝒖′

Page 4: Lecture 4.3 Estimating homographies from feature ... - UiO

Estimating the homography between overlapping images

β€’ Establish point correspondences 𝒖𝑖 ↔ 𝒖′𝑖 – Find key points 𝒖𝑖 ∈ Img1 and 𝒖′𝑖 ∈ Img2 – Represent key points by suitable descriptors – Determine correspondences 𝒖𝑖 ↔ 𝒖′𝑖 by matching descriptors – Some wrong correspondences are to be expected

β€’ Estimate the homography 𝐻 such that 𝒖′𝑖 = 𝐻𝒖𝑖 βˆ€π‘–

– Robust estimation with RANSAC – Improved estimation based on RANSAC inliers

β€’ This homography enables us to compose the images into a

larger image – Image mosaicing – Panorama

4

Page 5: Lecture 4.3 Estimating homographies from feature ... - UiO

Adaptive RANSAC

Objective To robustly fit a model π’š = 𝑓 𝒙;𝜢 to a data set 𝑆 containing outliers Algorithm 1. Let 𝑁 = ∞, 𝑆𝐼𝐼 = βˆ… and #𝑖𝑖𝑖𝑖𝑖𝑖𝑖𝑖𝑖𝑖 = 0

2. while 𝑁 > #𝑖𝑖𝑖𝑖𝑖𝑖𝑖𝑖𝑖𝑖 repeat 3-5

3. Estimate parameters πœΆπ‘‘π‘‘π‘‘ from a random 𝑖-tuple from 𝑆

4. Determine inlier set 𝑆𝑑𝑑𝑑, i.e. data points within a distance 𝑖 of the model π’š = 𝑓 𝒙;πœΆπ‘‘π‘‘π‘‘

5. If 𝑆𝑑𝑑𝑑 > 𝑆𝐼𝐼 , set 𝑆𝐼𝐼 = 𝑆𝑑𝑑𝑑, 𝜢 = πœΆπ‘‘π‘‘π‘‘, πœ” = 𝑆𝐼𝐼

𝑆 and 𝑁 = 𝑙𝑙𝑙 1βˆ’π‘

𝑙𝑙𝑙 1βˆ’πœ”π‘› with 𝑝 = 0.99 Increase #𝑖𝑖𝑖𝑖𝑖𝑖𝑖𝑖𝑖𝑖 by 1

5

Page 6: Lecture 4.3 Estimating homographies from feature ... - UiO

Estimating the homography

β€’ Estimating the homography in a RANSAC scheme requires 1. A basic homography estimation method for 𝑖 point-correspondences 2. A way to determine the inlier set of point-correspondences for a given homography

6

Page 7: Lecture 4.3 Estimating homographies from feature ... - UiO

Estimating the homography

β€’ Estimating the homography in a RANSAC scheme requires 1. A basic homography estimation method for 𝒏 point-correspondences 2. A way to determine the inlier set of point-correspondences for a given homography

β€’ The homography has 8 degrees of freedom, but it is custom to treat all 9 entries of the matrix

as unknowns instead of setting one of the entries to 1 which excludes all potential solutions where this entry is 0

β€’ Let us solve the equation 𝐻𝒖� = 𝒖�′ for the entries of the homography matrix 𝐻𝒖� = 𝒖�′ β„Ž1 β„Ž2 β„Ž3β„Ž4 β„Ž5 β„Ž6β„Ž7 β„Ž8 β„Ž9

𝑒𝑣1

=𝑒′𝑣′1

7

Page 8: Lecture 4.3 Estimating homographies from feature ... - UiO

Basic homography estimation

8

1

2

3

1 2 3 1 2 3 4

4 5 6 4 5 6 5

7 8 9 7 8 9 6

7

8

9

0 0 0 11 0 0 0

1 1 1 0 0 0

hhh

h h h u u uh vh h u u v v u v v v hh h h v v uh vh h v u v u u u v u hh h h uh vh h v u v v v u u u v u h

hhh

β€² β€² β€² β€² β€²+ + = βˆ’ βˆ’ βˆ’ β€² β€² β€² β€² β€²= ⇔ + + = ⇔ βˆ’ βˆ’ βˆ’ β€² β€² β€² β€² β€² β€² + + = βˆ’ βˆ’ βˆ’

000

A

= ⇔ =

h 0

Page 9: Lecture 4.3 Estimating homographies from feature ... - UiO

Basic homography estimation

Observe that the third row in 𝐴 is a linear combination of the first and second row π‘–π‘–π‘Ÿ3 = βˆ’π‘’β€² βˆ™ π‘–π‘–π‘Ÿ1 βˆ’ 𝑣′ βˆ™ π‘–π‘–π‘Ÿ2

Hence every correspondence 𝒖𝑖 ↔ 𝒖′𝑖 contribute with 2 equations in the 9 unknown entries

9

1

2

3

1 2 3 1 2 3 4

4 5 6 4 5 6 5

7 8 9 7 8 9 6

7

8

9

0 0 0 11 0 0 0

1 1 1 0 0 0

hhh

h h h u u uh vh h u u v v u v v v hh h h v v uh vh h v u v u u u v u hh h h uh vh h v u v v v u u u v u h

hhh

β€² β€² β€² β€² β€²+ + = βˆ’ βˆ’ βˆ’ β€² β€² β€² β€² β€²= ⇔ + + = ⇔ βˆ’ βˆ’ βˆ’ β€² β€² β€² β€² β€² β€² + + = βˆ’ βˆ’ βˆ’

000

A

= ⇔ =

h 0

Page 10: Lecture 4.3 Estimating homographies from feature ... - UiO

Basic homography estimation

β€’ Since 𝐻 (and thus 𝒉) is homogeneous, we only need the matrix 𝐴 to have rank 8 in order to determine 𝒉 up to scale

β€’ It is sufficient with 4 point correspondences where no 3 points are collinear

β€’ We can calculate the non-trivial solution to the equation 𝐴𝒉 = 𝟎 by SVD

svd 𝐴 = π‘ˆπ‘†π‘‰π‘‡

β€’ The solution is given by the right singular vector without a singular value which is the last column of 𝑉, i.e. 𝒉 = 𝒗9

10

1

2

31 1 1 1 1 1 1

41 1 1 1 1 1 1

52 2 2 2 2 2 2

62 2 2 2 2 2 2

7

8

9

0 0 0 1 01 0 0 0 0

0 0 0 1 01 0 0 0 0

hhhu v v u v v vhu v u u u v uhu v v u v v vhu v u u u v uhhhA

β€² β€² β€² βˆ’ βˆ’ βˆ’ β€² β€² β€²βˆ’ βˆ’ βˆ’ β€² β€² β€² =βˆ’ βˆ’ βˆ’ β€² β€² β€²βˆ’ βˆ’ βˆ’

=

h 0

Page 11: Lecture 4.3 Estimating homographies from feature ... - UiO

Basic homography estimation

β€’ Estimating the homography in a RANSAC scheme requires 1. A basic homography estimation method for 𝒏 point-correspondences 2. A way to determine which of the point correspondences that are inliers for a given homography

Direct Linear Transform

1. Build the matrix 𝐴 from at least 4 point-correspondences 𝑒𝑖 ,𝑣𝑖 ↔ 𝑒′𝑖 ,𝑣′𝑖 2. Obtain the SVD of 𝐴: 𝐴 = π‘ˆπ‘†π‘‰π‘‡ 3. If 𝑆 is diagonal with positive values in descending order along the main diagonal,

then 𝒉 equals the last column of 𝑉 4. Reconstruct 𝐻 from 𝒉

1 1 1 1 1 1 1

1 1 1 1 1 1 1

0 0 0 11 0 0 0

u v v u v v vA u v u u u v u

β€² β€² β€²βˆ’ βˆ’ βˆ’ β€² β€² β€²= βˆ’ βˆ’ βˆ’

11

Page 12: Lecture 4.3 Estimating homographies from feature ... - UiO

Basic homography estimation

β€’ The basic DLT algorithm is never used with more than 4 point-correspondences β€’ This is because the algorithm performs better when all the terms of 𝐴 has a similar scale

– Note that some of the terms will always be of scale 1 β€’ To achieve this, it is common to extend the algorithm with a normalization and a

denormalization step

Normalized Direct Linear Transform 1. Normalize the set of points 𝒖𝑖 = 𝑒𝑖 , 𝑣𝑖 𝑇 by computing a similarity transform 𝑇 that

translates the centroid to the origin and scales such that the average distance from the origin is 2

2. In the same way normalize the set of points 𝒖′𝑖 = 𝑒′𝑖 ,𝑣′𝑖 𝑇 by computing a similarity transform 𝑇′

3. Apply the basic DLT algorithm on the normalized points to obtain a homography 𝐻� 4. Denormalize to get the homography: 𝐻 = π‘‡β€²βˆ’1𝐻�𝑇

12

Page 13: Lecture 4.3 Estimating homographies from feature ... - UiO

Basic homography estimation

β€’ Estimating the homography in a RANSAC scheme requires 1. A basic homography estimation method for 𝑖 point-correspondences 2. A way to determine the inlier set of point-correspondences for a given homography

β€’ For a point correspondence 𝑒𝑖 , 𝑣𝑖 ↔ 𝑒′𝑖 , 𝑣′𝑖 and homography 𝐻, we can choose from

several errors – Algebraic error: πœ€π‘– = 𝐴𝑖𝒉 where

– Geometric errors: 1. πœ€π‘– = 𝑑 𝐻𝒖𝑖 ,𝒖′𝑖 + 𝑑 𝒖𝑖 ,π»βˆ’1𝒖′𝑖 (Reprojection error) 2. πœ€π‘– = 𝑑 𝒖𝑖 ,π»βˆ’1𝒖′𝑖 3. πœ€π‘– = 𝑑 𝐻𝒖𝑖 ,𝒖′𝑖

0 0 0 11 0 0 0

i i i i i i ii

i i i i i i i

u v v u v v vA

u v u u u v uβ€² β€² β€²βˆ’ βˆ’ βˆ’

= β€² β€² β€²βˆ’ βˆ’ βˆ’

13

Notation

Euclidean distance 𝑑 βˆ™,βˆ™

Inhomogenous 𝐻𝒖�𝑖 𝐻𝒖𝑖

Inhomogeneous π»βˆ’1𝒖�′𝑖 π»βˆ’1𝒖′𝑖

Page 14: Lecture 4.3 Estimating homographies from feature ... - UiO

Robust homography estimation

β€’ Finally we would typically re-estimate 𝐻 from all correspondences in 𝑆𝐼𝐼 – Normalized DLT – Minimize Ο΅ = βˆ‘πœ–π‘– in an iterative optimization method like Levenberg Marquardt

RANSAC estimation of homography For a set of point-correspondences S = π’–π’Š ↦ 𝒖′𝑖 , perform 𝑁 iterations, where 𝑁 is determined adaptively

1. Estimate 𝐻𝑑𝑑𝑑 from 4 random correspondences π’–π’Š ↦ 𝒖′𝑖 using the basic DLT algorithm 2. Determine the set of inlier-correspondences 𝑆𝑑𝑑𝑑 = π’–π’Š ↦ 𝒖′𝑖 π‘–π‘’π‘ β„Ž π‘–β„Žπ‘–π‘– πœ–π‘– < 𝑖

Here one can choose πœ–π‘– = 𝑑 𝐻𝒖𝑖 ,𝒖′𝑖 + 𝑑 𝒖𝑖 ,π»βˆ’1𝒖′𝑖 and 𝑖 = 5.99𝜎 where 𝜎 is the expected uncertainty in key-point positions

3. If 𝑆𝑑𝑑𝑑 > 𝑆𝐼𝐼 update 𝑁, homography and inlier set: 𝐻 = 𝐻𝑑𝑑𝑑, 𝑆𝐼𝐼 = 𝑆𝑑𝑑𝑑

14

Page 15: Lecture 4.3 Estimating homographies from feature ... - UiO

Image mosaicing

15

β€’ Let us compose these two images into a larger image

Page 16: Lecture 4.3 Estimating homographies from feature ... - UiO

Image mosaicing

β€’ Find key points and represent by descriptors

16

Page 17: Lecture 4.3 Estimating homographies from feature ... - UiO

Image mosaicing

β€’ Establish point-correspondences by matching descriptors β€’ Several wrong correspondences

17

Page 18: Lecture 4.3 Estimating homographies from feature ... - UiO

Image mosaicing

β€’ Establish point-correspondences by matching descriptors β€’ Several wrong correspondences

18

Page 19: Lecture 4.3 Estimating homographies from feature ... - UiO

Image mosaicing

β€’ Estimate homography 𝐻𝒖� = 𝒖�′ – OpenCV

#include "opencv2/calib3d.hpp" cv::findHomography(srcPoints, dstPoints, CV_RANSAC);

– Matlab tform = estimateGeometricTransform(srcPoints,dstPoints,’projective’);

19

𝐻

Page 20: Lecture 4.3 Estimating homographies from feature ... - UiO

Image mosaicing

β€’ Represent the images in common coordinates (Note the additional translation!) – OpenCV

#include "opencv2/calib3d.hpp" cv::warpPerspective(img1, img2, H, output_size);

– Matlab img2 = imwarp(img1,tform);

20

Page 21: Lecture 4.3 Estimating homographies from feature ... - UiO

Image mosaicing

β€’ Now we can compose the images

21

Page 22: Lecture 4.3 Estimating homographies from feature ... - UiO

22

Overwriting

Page 23: Lecture 4.3 Estimating homographies from feature ... - UiO

23

Blending with a ramp

Page 24: Lecture 4.3 Estimating homographies from feature ... - UiO

24

Blending with a ramp + histogram equalization

Page 25: Lecture 4.3 Estimating homographies from feature ... - UiO

SVD How to use β€’ Matlab

[U,S,V] = svd(A); Right singular vectors are columns in V

β€’ OpenCV cv::SVD::compute(A, S, U, Vtranspose, cv::SVD::FULL_UV); Right singular vectors are rows in Vtranspose

β€’ Eigen Eigen::JacobiSVD<Eigen::MatrixXd> svd(A, Eigen::ComputeFullU | Eigen::ComputeFullV); Right singular vectors are columns in svd.matrixV()

25

Singular Value Decomposition The singular value decomposition of a real π‘š Γ— 𝑖 matrix 𝐴 is a factorization 𝐴 = π‘ˆS𝑉𝑇 Here π‘ˆ is a orthogonal π‘š Γ— π‘š matrix, 𝑉 is a orthogonal 𝑖 Γ— 𝑖 matrix and 𝑆 is a real positive diagonal π‘š Γ— 𝑖 matrix The diagonal entries of 𝑆 = diag 𝑖1, … , 𝑖min π‘š,𝑛 are known as the singular values of 𝐴 and the columns of π‘ˆ = 𝒖1, … ,π’–π‘š and 𝑉 = 𝒗1, … ,𝒗𝑛 are known as the left and right singular vectors of 𝐴 respectively The nullspace of A is the span of the right singular vectors 𝒗𝑖 that corresponds to a zero singular value 𝑖𝑖 (or does not have a corresponding singular value)

Page 26: Lecture 4.3 Estimating homographies from feature ... - UiO

SVD Singular Value Decomposition The singular value decomposition of a real π‘š Γ— 𝑖 matrix 𝐴 is a factorization 𝐴 = π‘ˆS𝑉𝑇 Here π‘ˆ is a orthogonal π‘š Γ— π‘š matrix, 𝑉 is a orthogonal 𝑖 Γ— 𝑖 matrix and 𝑆 is a real positive diagonal π‘š Γ— 𝑖 matrix The diagonal entries of 𝑆 = diag 𝑖1, … , 𝑖min π‘š,𝑛 are known as the singular values of 𝐴 and the columns of π‘ˆ = 𝒖1, … ,π’–π‘š and 𝑉 = 𝒗1, … ,𝒗𝑛 are known as the left and right singular vectors of 𝐴 respectively The nullspace of A is the span of the right singular vectors 𝒗𝑖 that corresponds to a zero singular value 𝑖𝑖 (or does not have a corresponding singular value)

Applications of SVD Solving homogeneous linear equations like

𝐴𝒉 = 𝟎 Method For theoretical problems, 𝐑 ∈ null 𝐴 so 𝒉 is a linear combination of the right singular vectors 𝒗𝑖 that correspond to a zero singular value 𝑖𝑖

𝒉 = οΏ½π‘˜π‘–π’—π‘– ; π‘˜π‘– ∈ ℝ, 𝑖𝑖 = 0 (or missing)

For practical problems, the presence of noise force us to expand the solution by including those right singular vectors that correspond to small singular values 𝑖𝑖 β‰ˆ 0

𝒉 = οΏ½π‘˜π‘–π’—π‘– ; π‘˜π‘– ∈ ℝ, 𝑖𝑖 β‰ˆ 0 (or missing)

26

Page 27: Lecture 4.3 Estimating homographies from feature ... - UiO

SVD Example

From [U,S,V] = svd(A); we get

From this we see that A has: β€’ 2 left singular vectors

β€’ 2 nonzero singular values β€’ 3 right singular vectors Since 𝒗3 does not have a corresponding singular value, 𝒙 = 𝒗3 is a non-trivial solution to A𝒙 = 0 and 𝒙 = π‘˜ βˆ™ 𝒗3; π‘˜ ∈ ℝ\ 0 is the family of all non-trivial solutions

27

1 2 3 04 5 6 0

Axyz

=

=

x 0

0.3863 0.9224 9.5080 0 00.9224 0.3863 0 0.7729 0

0.4287 0.8060 0.40820.5663 0.1124 0.81650.7039 0.5812 0.4082

U S

V

βˆ’ βˆ’ = = βˆ’

βˆ’ = βˆ’ βˆ’ βˆ’ βˆ’

1 2 3

0.4287 0.8060 0.40820.5663 0.1124 0.81650.7039 0.5812 0.4082

βˆ’ = βˆ’ = = βˆ’ βˆ’ βˆ’

v v v

1 29.5080 0.7729s s= =

1 2

0.3863 0.92240.9224 0.3863βˆ’ βˆ’

= = βˆ’ u u

Page 28: Lecture 4.3 Estimating homographies from feature ... - UiO

SVD Example

This time singular value decomposition give us the following singular values and right singular vectors:

This time all right singular vectors correspond to a non-zero singular value, so the equation does not have any non-trivial solutions!

If this equation came from a practical problem, instead of looking for solutions to 𝐴𝒙 = 0, we might be looking for the 𝒙 that minimize 𝐴𝒙 Since 𝑖1 ≉ 0, 𝑖2 ≉ 0, 𝑖3 β‰ˆ 0, we would conclude that 𝒙 = 𝒗3 solves the equation in a least-squares sense Check:

28

1.0792 2.0656 3.08494.0959 5.0036 6.0934 01.0679 2.0743 3.0655 04.0758 5.0392 6.0171

A

xyz

=

=

x 0

1 2 3

1 2 3

13.6295 1.0849 0.05060.4336 0.8103 0.39420.5635 0.0975 0.82030.7032 0.5778 0.4143

s s s= = =

βˆ’ βˆ’ = βˆ’ = βˆ’ = βˆ’ βˆ’

v v v 3

0.00910.42880.01050.0341

A

= βˆ’ βˆ’

v

Page 29: Lecture 4.3 Estimating homographies from feature ... - UiO

Summary

β€’ Homography 𝐻𝒖� = 𝒖�′

𝐻 =β„Ž1 β„Ž2 β„Ž3β„Ž4 β„Ž5 β„Ž6β„Ž7 β„Ž8 β„Ž9

β€’ Automatic point-correspondences

β€’ Wrong correspondences are common

β€’ RANSAC estimation

– Basic DLT (Direct Linear Transform) on 4 random correspondences

– Inliers determined from the reprojection error πœ–π‘– = 𝑑 𝐻𝒖𝑖 ,𝒖′𝑖 + 𝑑 𝒖𝑖 ,π»βˆ’1𝒖′𝑖

β€’ Improve estimate by normalized DLT on inliers or iterative methods for an even better estimate

β€’ Additional reading – Szeliski: 6.1.1 – 6.1.3

29