计算机视觉 -- Homography单应性矩阵
Homography单应性矩阵Homography 单应性概念考虑 同一个平面(比如书皮)的两张图片,红点表示同一个物理坐标点在两张图片上的各自位置。在 CV 术语中,我们称之为对应点。Homography 就是将一张图像上的点映射到另一张图像上对应点的3x3变换矩阵.因为 Homography 是一个 3x3 的 矩阵,所以我们可以把它写成:H=「h11 h12 h13h21
Homography单应性矩阵
1. Homography 单应性概念
考虑 同一个平面(比如书皮)的两张图片,红点表示同一个物理坐标点在两张图片上的各自位置。在 CV 术语中,我们称之为对应点。
Homography 就是将一张图像上的点映射到另一张图像上对应点的3x3变换矩阵.
因为 Homography 是一个 3x3 的 矩阵,所以我们可以把它写成:
H
=
[
h
11
h
12
h
13
h
21
h
22
h
23
h
31
h
32
h
33
]
.
H =\begin{bmatrix} h11&h12&h13\\ h21&h22&h23\\ h31&h32&h33\\ \end{bmatrix}.
H=⎣⎡h11h21h31h12h22h32h13h23h33⎦⎤.
对于图中的一对儿对应点,位于图一的 (x1, y1) 和 位于图二的 (x2, y2). H 把二者映射关系建立起来:
[
x
1
y
1
1
]
=
[
h
11
h
12
h
13
h
21
h
22
h
23
h
31
h
32
h
33
]
∗
[
x
2
y
2
1
]
.
\begin{bmatrix} x1\\ y1\\ 1\\ \end{bmatrix} =\begin{bmatrix} h11&h12&h13\\ h21&h22&h23\\ h31&h32&h33\\ \end{bmatrix} * \begin{bmatrix} x2\\ y2\\ 1\\ \end{bmatrix}.
⎣⎡x1y11⎦⎤=⎣⎡h11h21h31h12h22h32h13h23h33⎦⎤∗⎣⎡x2y21⎦⎤.
对于所有的对应点,只要它们都位于同一个物理平面上,上述 Homography 就是成立的。换句话说,就是可以把图一中书皮上的所有点都映射到图二的书皮上,也就是看起来,图一中的书皮和图二中的书皮对齐了!
那么对于不在此平面上的点呢?这时再应用 Homography 就无法再对齐到对应点了。比如 上图中的 桌面,地面,橱柜面。对于这种图像中有多个平面的情况,我们就需要针对每一个平面使用其对应的Homography了。
2. 如何计算 Homography?
对于 H 矩阵,一般设 H22 为 1, 所以 H 有 8 个未知参数。至少需要8 个等式才能求解。而一组对应点可以提供 2 个等式,所以,至少需要 4 组对应点(任意三点不共线)来求得 H。 如果有更多组对应点,效果更佳。 OpenCV 可以鲁棒地计算出一个最好地拟合所有对应点的 Homography。通常,图像间的这些对应点通过 SIFT 或者 SURF 这样算法进行自动特征提取和匹配。当然,对于简单的demo,手动选取对应点就足够了。
//pts_src : 源图像点坐标
//pts_dst : 结果图像坐标
// 数据类型都是 vector<Point2f>.
// 需要至少4组对应点.
Mat h = findHomography(pts_src, pts_dst);
//im_src : 源图像
// im_dst : 结果图像
// h: 上一步计算得到的 Homography
// size : im_dst 的 大小(宽度,高度)
// 将 im_src 通过 h warp 到 im_dst 上去
warpPerspective(im_src, im_dst, h, size);
// warp points, not full image
vector<Point2f> dstPoints, srcPoints;
srcPoints.push_back(Point2f(1,1));
cv::perspectiveTransform(srcPoints,dstPoints,warpMatrix);
for example
#include "opencv2/opencv.hpp"
using namespace cv;
using namespace std;
int main( int argc, char** argv)
{
// Read source image.
Mat im_src = imread("book2.jpg");
// Four corners of the book in source image
vector<Point2f> pts_src;
pts_src.push_back(Point2f(141, 131));
pts_src.push_back(Point2f(480, 159));
pts_src.push_back(Point2f(493, 630));
pts_src.push_back(Point2f(64, 601));
// Read destination image.
Mat im_dst = imread("book1.jpg");
// Four corners of the book in destination image.
vector<Point2f> pts_dst;
pts_dst.push_back(Point2f(318, 256));
pts_dst.push_back(Point2f(534, 372));
pts_dst.push_back(Point2f(316, 670));
pts_dst.push_back(Point2f(73, 473));
// Calculate Homography
Mat h = findHomography(pts_src, pts_dst);
// Output image
Mat im_out;
// Warp source image to destination based on homography
warpPerspective(im_src, im_out, h, im_dst.size());
// Display images
imshow("Source Image", im_src);
imshow("Destination Image", im_dst);
imshow("Warped Source Image", im_out);
waitKey(0);
}
3. Homography,Essential 和 Fundamental matrix
单应 ( homography )也叫射影映射,保线变换或射影变换,射影变换可以看作是一些列变换的组合:
H
=
H
s
∗
H
a
∗
H
p
=
[
s
R
t
0
1
]
∗
[
K
0
0
1
]
∗
[
I
0
V
v
]
H = Hs * Ha *Hp = \begin{bmatrix} sR&t\\ 0&1\\ \end{bmatrix} *\begin{bmatrix} K&0\\ 0&1\\ \end{bmatrix}*\begin{bmatrix} I&0\\ V&v\\ \end{bmatrix}
H=Hs∗Ha∗Hp=[sR0t1]∗[K001]∗[IV0v]
其中Hs是相似变换矩阵,Ha是仿射变换矩阵,Hp也是一个射影变换矩阵。
本质矩阵(Essential matrix)是归一化图像坐标下的基本矩阵的特殊形式。其参数由运动的pose决定,与相机内参无关;本质矩阵在位姿估计和相机标定上很有用。
假设在一个图中的点x下的坐标是( u , v , 1 ) T,与其相对应匹配点x ′的坐标是( u ′ , v ′ , 1 ) T ,单应性矩阵H,则有
x
T
E
x
′
=
0
x^T Ex' = 0
xTEx′=0即
[
u
v
1
]
[
e
11
e
12
e
13
e
21
e
22
e
23
e
31
e
32
e
33
]
[
u
′
v
′
1
]
=
0
\begin{bmatrix} u&v&1\\ \end{bmatrix} \begin{bmatrix} e11&e12&e13\\ e21&e22&e23\\ e31&e32&e33\\ \end{bmatrix}\begin{bmatrix} u'\\ v'\\ 1\\ \end{bmatrix} = 0
[uv1]⎣⎡e11e21e31e12e22e32e13e23e33⎦⎤⎣⎡u′v′1⎦⎤=0
因为
E
=
t
R
E=t^R
E=tR由于平移和旋转各3个自由度共有6个自由,但考虑到尺度等价性,故E实际上有5个自由度。求出E 后就可以采用奇异值分解(SVD)恢复出相机的运动R、t。
Fundamental matrix:对两幅图像中任何一对对应点x和x′基本矩阵(Fundamental matrix)F都满足条件:
x
T
F
x
′
=
0
x^TFx' =0
xTFx′=0
E和F关系:
F
=
K
−
T
E
K
−
1
F=K^{-T}EK^{-1}
F=K−TEK−1
其中K为相机内参矩阵。因为E和F差个相机内参,而SLAM中相机通常都标定过,内参已知。所以实践中常用E。
更多推荐
所有评论(0)