透视变换

透视变换(Perspective Transformation)是将图片投影到一个新的视平面(Viewing Plane),也称作投影映射(Projective Mapping)。如下图所示

示例代码

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
/**
 图片的透视变换

 # 左上角
pt1 = (120,90)
# 右上角
pt2 = (350,90)

# 左下角
pt3 = (60,470)
# 右下角
pt4 = (430,470)
 */
#include <iostream>
#include <opencv2/opencv.hpp>

using namespace std;
using namespace cv;

int main(int argc,char** argv){
    string path = "/home/kaijun/Pictures/zhengjianzhao.png";

    Mat src = imread(path,IMREAD_COLOR);

    imshow("src",src);

    vector<Point2f> sourcePoint={Point2f(120,90),Point2f(350,90),Point2f(60,470),Point2f(430,470)};

    // 处理完成之后,将图像映射到新的空间中去
    vector<Point2f> dstPoint = {Point2f(0,0),Point2f(480,0),Point2f(0,600),Point2f(480,600)};

    // 计算透视变换的转换矩阵
    Mat matrix = getPerspectiveTransform(sourcePoint,dstPoint);

    // 调用转换函数
    Mat dst;
    warpPerspective(src,dst,matrix,Size(480,600));

    imshow("dst",dst);

    waitKey(0);
    return 0;
}