#图像处理# 将四边形–透视投影变换为标准矩形
1、什么是透视变换?
透视变换(Perspective Transformation)是将图片投影到一个新的视平面(Viewing Plane),也称作投影映射(Projective Mapping)。
2、透视原理有很多文章介绍读者可自行查询理解。
cv2.getPerspectiveTransform 透视变换
先来效果图
Before:
After:
(由于此处只在意所选四边形的变换实现,没有剔除新透视视角下标准矩阵中周围的图像)
代码实现:
```python
# 功能:将图像中某一四边形变换为矩阵
import cv2
import numpy as np
img = cv2.imread('C:/Users/Administrator/Desktop/testphoto/computer.jpg')
#获取源图像宽和高
w = img.shape[0]
h = img.shape[1]
#源图像中四边形坐标点(获取坐标点方法可参照我上篇博文)
point1 = np.array([[320,132],[1240,111],[414,800],[1351,738]],dtype = "float32")
#转换后得到矩形的坐标点
point2 = np.array([[0,0],[320,0],[0,180],[320,180]],dtype = "float32")
# point2 = np.array([[0,180],[320,180],[0,0],[320,0]],dtype = "float32")
M = cv2.getPerspectiveTransform(point1,point2)
out_img = cv2.warpPerspective(img,M,(w,h))
cv2.imshow("img",out_img)
cv2.waitKey(0)
以上代码仅供参考