索引地址:系列索引
透视变换(Perspective Transformation)是将图片投影到一个新的视平面(Viewing Plane),也称作投影映射(Projective Mapping)。如下图所示。
通用变换公式为:
[x′y′w′][uvw]∗⎣⎡a11a21a31a12a22a32a13a23a33⎦⎤
(u,v)为原始图像像素坐标,(x=x’/w’,y=y’/w’)为变换之后的图像像素坐标。透视变换矩阵图解如下:
Transform=⎣⎡a11a21a31a12a22a32a13a23a33⎦⎤=[T1T3T2a33]T1=[a11a21a12a22](表示图像线性变换)T2=[a13a23]T(用于产生图像透视变换)T3=[a31a32](用于产生图像平移)
仿射变换(Affine Transformation)可以理解为透视变换的特殊形式。透视变换的数学表达式为:
x=w′x′=a13u+a23v+a33a11u+a21v+a31y=w′y′=a13u+a23v+a33a12u+a22v+a32
所以,给定透视变换对应的四对像素点坐标,即可求得透视变换矩阵;反之,给定透视变换矩阵,即可对图像或像素点坐标完成透视变换,如下图所示:
函数说明:
1 2 3 4 5 6 7 8 9 10 11 12 13 14
| Mat getPerspectiveTransform(const Point2f* src, const Point2f* dst)
void warpPerspective(InputArray src, OutputArray dst, InputArray M, Size dsize, int flags=INTER_LINEAR, int borderMode=BORDER_CONSTANT, const Scalar& borderValue=Scalar())
|
测试代码:
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
| #include "opencv2/opencv.hpp"
using namespace cv;
int main( int argc, char** argv ) { Mat src; src = imread(argv[1]);
Mat dst; Point2f src_verts[4]; src_verts[2] = Point(195, 140); src_verts[3] = Point(410, 120); src_verts[1] = Point(220, 750); src_verts[0] = Point(400, 750); Point2f dst_verts[4]; dst_verts[2] = Point(160, 100); dst_verts[3] = Point(530, 120); dst_verts[1] = Point(220, 750); dst_verts[0] = Point(400, 750);
Mat M = getPerspectiveTransform(src_verts,dst_verts); warpPerspective(src,dst,M,src.size());
namedWindow( " ORIGINAL ", WINDOW_AUTOSIZE ); imshow( " ORIGINAL ", src ); namedWindow( " PERSPECTIVE ", WINDOW_AUTOSIZE ); imshow( " PERSPECTIVE ", dst );
waitKey(); return 0; }
|
测试结果: