python opencv实现任意角度的透视变换实例代码
作者:ShellCollector 时间:2023-11-09 15:14:42
本文主要分享的是一则python+opencv实现任意角度的透视变换的实例,具体如下:
# -*- coding:utf-8 -*-
import cv2
import numpy as np
def rad(x):
return x * np.pi / 180
img = cv2.imread("6.jfif")
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
while 1:
# 镜头与图像间的距离,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)
result = cv2.warpPerspective(img, warpR, (h, w))
cv2.imshow("result", result)
c = cv2.waitKey(30)
# anglex += 3 #auto rotate
# anglez += 1 #auto rotate
# angley += 2 #auto rotate
# 键盘控制
if 27 == c: # Esc quit
break;
if c == ord('w'):
anglex += 1
if c == ord('s'):
anglex -= 1
if c == ord('a'):
angley += 1
# 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('q'):
print("======================================")
print('旋转矩阵:\n', r)
print("angle alpha: ", anglex, 'angle beta: ', angley, "dz: ", anglez, ": ", z)
cv2.destroyAllWindows()
来源:http://blog.csdn.net/jacke121/article/details/78996288
标签:python,opencv,实例
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
mvn 打包报错:no compiler is provided in this environment
2023-10-31 00:47:17
![](https://img.aspxhome.com/file/2023/3/132463_0s.png)
基于PHP RSA密文过长加密解密 越过1024的解决方法
2023-09-07 02:57:56
Javascript实现动态菜单添加的实例代码
2024-04-22 22:23:25
![](https://img.aspxhome.com/file/2023/7/135827_0s.jpg)
使用apiDoc实现python接口文档编写
2023-10-23 21:28:40
在SQL server2005数据库下创建计划任务
2008-12-26 09:19:00
![](https://img.aspxhome.com/file/UploadPic/200812/20081231122259529.jpg)
使用k8s部署Django项目的方法步骤
2022-12-30 01:59:56
如何通过python检查文件是否被占用
2023-03-20 12:25:00
Python split() 函数拆分字符串将字符串转化为列的方法
2022-02-12 00:14:19
人工智能Text Generation文本生成原理示例详解
2022-01-16 22:45:28
MySQL 8.0 redo log的深入解析
2024-01-12 23:44:22
![](https://img.aspxhome.com/file/2023/2/102362_0s.png)
pycharm 使用心得(二)设置字体大小
2022-02-19 15:37:04
![](https://img.aspxhome.com/file/2023/9/101729_0s.jpg)
用python监控服务器的cpu,磁盘空间,内存,超过邮件报警
2023-04-22 07:13:38
Numpy中扁平化函数ravel()和flatten()的区别详解
2022-09-23 08:52:23
Oracle数据库由dataguard备库引起的log file sync等待问题
2023-07-17 07:35:25
![](https://img.aspxhome.com/file/2023/1/63371_0s.png)
python3排序的实例方法
2023-01-17 11:05:28
![](https://img.aspxhome.com/file/2023/0/105320_0s.png)
Nodejs之TCP服务端与客户端聊天程序详解
2024-05-03 15:55:48
![](https://img.aspxhome.com/file/2023/7/129357_0s.png)
uniapp项目打包为桌面应用的方法步骤
2024-04-29 13:16:04
![](https://img.aspxhome.com/file/2023/1/132781_0s.png)
解决python中 f.write写入中文出错的问题
2021-01-02 04:57:44
SQL Server连接失败错误及解决
2008-01-28 21:09:00
![](https://img.aspxhome.com/file/UploadPic/20081/28/2008128211959685s.jpg)
网页设计进阶之六-- 守住那些不能丢的东西
2008-06-12 13:06:00