几何变换类型
- 保距变换 isometry
- 相似变换 similarity
- 仿射变换 affine
- 射影变换 projective -> homography
What is Homography?
A Homography is a transformation ( a 3×3 matrix ) that maps the points in one image to the corresponding points in the other image.
- homography只针对同一平面
How to calculate a Homography ?
摄影变换的自由度为8,一对点能产生两个方程,共需要4对对应点,即可求取H矩阵;如超过4对,通过最小二乘法或RANSAC求取最优参数
理论推导
单应性(homography)变换的推导 - flyinsky518 - 博客园
假设一对对应点 a = ( x , y , 1 ) a = (x,y,1) a=(x,y,1)和 a , = ( x , , y , , 1 ) a^,=(x^,, y^,,1) a,=(x,,y,,1)有 A h = 0 Ah=0 Ah=0,其中
A = [ − x − y − 1 0 0 0 x x 1 y x 1 x 1 0 0 0 − x − y − 1 x y 1 y y 1 y 1 ] A=\left[\begin{array}{ccccccccc}-x & -y & -1 & 0 & 0 & 0 & x x_{1} & y x_{1} & x_{1} \\ 0 & 0 & 0 & -x & -y & -1 & x y_{1} & y y_{1} & y_{1}\end{array}\right] A=[−x0−y0−100−x0−y0−1xx1xy1yx1yy1x1y1]
h = [ h 11 , h 12 , h 13 , h 21 , h 22 , h 23 , h 31 , h 32 , h 33 ] T h=\left[h_{11}, h_{12}, h_{13}, h_{21}, h_{22}, h_{23}, h_{31}, h_{32}, h_{33}\right]^{T} h=[h11,h12,h13,h21,h22,h23,h31,h32,h33]T
当有n对点时, A ∈ R 2 n × 9 A \in R^{2n\times9} A∈R2n×9,对A进行SVD分解,即 U ∗ ∑ ∗ V T U * \sum * V^T U∗∑∗VT,取 V V V的最后一列求解h,再转换成 3 ∗ 3 3*3 3∗3矩阵即得到 H H H
[U,S,V]=svd(A);
h=V(:,9);
H= reshape(h,3,3);
工程实践
If you have more than 4 corresponding points, it is even better. OpenCV will robustly estimate a homography that best fits all corresponding points.
Usually, these point correspondences are found automatically by matching features like SIFT or SURF between the images.
'''
pts_src and pts_dst are numpy arrays of points
in source and destination images. We need at least
4 corresponding points.
'''
h, status = cv2.findHomography(pts_src, pts_dst)
'''
The calculated homography can be used to warp
the source image to destination. Size is the
size (width,height) of im_dst
'''
im_dst = cv2.warpPerspective(im_src, h, size)
Application
- 图像矫正
- 图像扫描
- 虚拟广告牌