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.
就是两幅图的对应点的变换关系(是一个矩阵),对应点以上图,四个不同颜色点为例。
如
以红色点为例(x1,y1)<-->(x2,y2),有如下变换关系
利用单应性进行图像匹配(配准)
全景:透视应用
在前一节中,我们知道,如果两个图像之间的单应性已知,我们可以将一个图像扭曲到另一个图像上。然而,有一个大的警告。图像必须包含一个平面(一本书的顶部),只有平面部分被正确对齐。事实证明,如果你拍第一张照片,任何场景(不只是一个平面),然后通过旋转相机拍了第二图像,两图像将被联系通过单应性。
计算透视变换矩阵
为了计算两幅图像之间的单应性,需要知道两幅图像之间至少有4点对应。如果你有4个以上的对应点,那就更好了。OpenCV将稳健估计单应性,最适合所有对应点。通常,这些点对应是通过匹配图像之间的SIFT或SURF等特征自动发现的。
C++
1 2 3 4 5 6 7 8 9 10 |
|
Python
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
|
OpenCV的C++单应性实例
在图2中的图像。可以使用下面的C++代码生成。下面的代码显示了如何在两幅图像中获取四个对应点,并将扭曲图像带到另一个图像上。
#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);
}
python
#!/usr/bin/env python
import cv2
import numpy as np
if __name__ == '__main__' :
# Read source image.
im_src = cv2.imread('book2.jpg')
# Four corners of the book in source image
pts_src = np.array([[141, 131], [480, 159], [493, 630],[64, 601]])
# Read destination image.
im_dst = cv2.imread('book1.jpg')
# Four corners of the book in destination image.
pts_dst = np.array([[318, 256],[534, 372],[316, 670],[73, 473]])
# Calculate Homography
h, status = cv2.findHomography(pts_src, pts_dst)
# Warp source image to destination based on homography
im_out = cv2.warpPerspective(im_src, h, (im_dst.shape[1],im_dst.shape[0]))
# Display images
cv2.imshow("Source Image", im_src)
cv2.imshow("Destination Image", im_dst)
cv2.imshow("Warped Source Image", im_out)
cv2.waitKey(0)