Opencv-Python图像透视变换cv2.warpPerspective的示例
作者:-牧野- 时间:2023-06-27 19:03:18
Opencv-Python图像透视变换cv2.warpPerspective
代码如下:
# -*- coding:utf-8 -*-
import cv2
import numpy as np
import sys
img = cv2.imread('test.jpg')
# cv2.imshow("original", img)
# 可选,扩展图像,保证内容不超出可视范围
img = cv2.copyMakeBorder(img, 200, 200, 200, 200, cv2.BORDER_CONSTANT, 0)
w, h = img.shape[0:2]
anglex = 0
angley = 30
anglez = 0 # 是旋转
fov = 42
r = 0
def rad(x):
return x * np.pi / 180
def get_warpR():
global anglex,angley,anglez,fov,w,h,r
# 镜头与图像间的距离,21为半可视角,算z的距离是为了保证在此可视角度下恰好显示整幅图像
z = np.sqrt(w ** 2 + h ** 2) / 2 / np.tan(rad(fov / 2))
# 齐次变换矩阵
rx = np.array([[1, 0, 0, 0],
[0, np.cos(rad(anglex)), -np.sin(rad(anglex)), 0],
[0, -np.sin(rad(anglex)), np.cos(rad(anglex)), 0, ],
[0, 0, 0, 1]], np.float32)
ry = np.array([[np.cos(rad(angley)), 0, np.sin(rad(angley)), 0],
[0, 1, 0, 0],
[-np.sin(rad(angley)), 0, np.cos(rad(angley)), 0, ],
[0, 0, 0, 1]], np.float32)
rz = np.array([[np.cos(rad(anglez)), np.sin(rad(anglez)), 0, 0],
[-np.sin(rad(anglez)), np.cos(rad(anglez)), 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]], np.float32)
r = rx.dot(ry).dot(rz)
# 四对点的生成
pcenter = np.array([h / 2, w / 2, 0, 0], np.float32)
p1 = np.array([0, 0, 0, 0], np.float32) - pcenter
p2 = np.array([w, 0, 0, 0], np.float32) - pcenter
p3 = np.array([0, h, 0, 0], np.float32) - pcenter
p4 = np.array([w, h, 0, 0], np.float32) - pcenter
dst1 = r.dot(p1)
dst2 = r.dot(p2)
dst3 = r.dot(p3)
dst4 = r.dot(p4)
list_dst = [dst1, dst2, dst3, dst4]
org = np.array([[0, 0],
[w, 0],
[0, h],
[w, h]], np.float32)
dst = np.zeros((4, 2), np.float32)
# 投影至成像平面
for i in range(4):
dst[i, 0] = list_dst[i][0] * z / (z - list_dst[i][2]) + pcenter[0]
dst[i, 1] = list_dst[i][1] * z / (z - list_dst[i][2]) + pcenter[1]
warpR = cv2.getPerspectiveTransform(org, dst)
return warpR
def control():
global anglex,angley,anglez,fov,r
# 键盘控制
if 27 == c: # Esc quit
sys.exit()
if c == ord('w'):
anglex += 1
if c == ord('s'):
anglex -= 1
if c == ord('a'):
angley += 1
print(angley)
# dx=0
if c == ord('d'):
angley -= 1
if c == ord('u'):
anglez += 1
if c == ord('p'):
anglez -= 1
if c == ord('t'):
fov += 1
if c == ord('r'):
fov -= 1
if c == ord(' '):
anglex = angley = anglez = 0
if c == ord('e'):
print("======================================")
print('Rotation Matrix:')
print(r)
print('angle alpha(anglex):')
print(anglex)
print('angle beta(angley):')
print(angley)
print('dz(anglez):')
print(anglez)
while True:
warpR = get_warpR()
result = cv2.warpPerspective(img, warpR, (h, w))
cv2.namedWindow('result',2)
cv2.imshow("result", result)
c = cv2.waitKey(30)
control()
cv2.destroyAllWindows()
运行效果:
控制:
s控制垂直方向上的形变
a和d控制水平方向上的行变
u和p控制角度旋转
e 输出当前旋转矩阵参数
来源:https://blog.csdn.net/dcrmg/article/details/80273818
标签:opencv,python,图像,透视变换
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python yield迭代器详解
2023-12-06 09:36:50
python编程学习使用管道Pipe编写优化代码
2023-08-20 04:08:45
![](https://img.aspxhome.com/file/2023/9/90419_0s.png)
so easy!10行代码写个"狗屁不通"文章生成器功能
2023-03-19 06:53:30
![](https://img.aspxhome.com/file/2023/8/131778_0s.jpg)
Python-for循环的内部机制
2023-02-02 20:47:18
Python FTP操作类代码分享
2022-09-07 17:59:23
perl特殊符号及默认的内部变量
2023-08-08 02:13:20
Python中最大递归深度值的探讨
2021-01-16 14:21:21
![](https://img.aspxhome.com/file/2023/8/80608_0s.png)
学习SQL语句(强大的group by与select from模式)
2011-11-03 16:46:09
python中property属性的介绍及其应用详解
2022-09-26 03:21:36
asp无限级分类加js收缩伸展功能代码
2009-12-08 12:25:00
python实现批量转换图片为黑白
2023-03-14 15:13:11
![](https://img.aspxhome.com/file/2023/4/118534_0s.jpg)
vue项目两种方式实现竖向表格的思路分析
2024-06-05 15:31:13
![](https://img.aspxhome.com/file/2023/1/123131_0s.png)
Python3.8安装Pygame教程步骤详解
2022-05-11 15:26:15
![](https://img.aspxhome.com/file/2023/4/66284_0s.png)
Python实现随机取一个矩阵数组的某几行
2021-10-04 16:45:52
MySQL制作具有千万条测试数据的测试库的方法
2024-01-22 07:00:36
Linux下修改MySQL数据库数据文件路径的步骤
2024-01-19 13:53:41
如何实现SQL Server的分页显示?
2010-05-18 18:36:00
Jquery AJAX POST与GET之间的区别
2024-04-29 13:58:32
解决Python 中JSONDecodeError: Expecting value: line 1 column 1 (char 0)错误
2023-10-30 22:42:03
![](https://img.aspxhome.com/file/2023/6/71426_0s.png)
python代码的几种常见加密方式分享
2021-05-19 02:14:56
![](https://img.aspxhome.com/file/2023/1/90781_0s.png)