Opencv-Python图像透视变换cv2.warpPerspective的示例


Posted in Python onApril 11, 2019

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()

运行效果:

Opencv-Python图像透视变换cv2.warpPerspective的示例

控制:

  • s控制垂直方向上的形变
  • a和d控制水平方向上的行变
  • u和p控制角度旋转
  • e 输出当前旋转矩阵参数

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作具有一定的参考学习价值,谢谢大家对三水点靠木的支持。如果你想了解更多相关内容请查看下面相关链接

Python 相关文章推荐
python操作MySQL数据库的方法分享
May 29 Python
python通过scapy获取局域网所有主机mac地址示例
May 04 Python
实例讲解Python中global语句下全局变量的值的修改
Jun 16 Python
python结合selenium获取XX省交通违章数据的实现思路及代码
Jun 26 Python
基于Linux系统中python matplotlib画图的中文显示问题的解决方法
Jun 15 Python
对python中的 os.mkdir和os.mkdirs详解
Oct 16 Python
numpy concatenate数组拼接方法示例介绍
May 27 Python
python实现对图片进行旋转,放缩,裁剪的功能
Aug 07 Python
python程序中的线程操作 concurrent模块使用详解
Sep 23 Python
Python箱型图绘制与特征值获取过程解析
Oct 22 Python
浅谈tensorflow中张量的提取值和赋值
Jan 19 Python
Python的Tqdm模块实现进度条配置
Feb 24 Python
Opencv+Python实现图像运动模糊和高斯模糊的示例
Apr 11 #Python
详解python执行shell脚本创建用户及相关操作
Apr 11 #Python
python中aioysql(异步操作MySQL)的方法
Apr 11 #Python
很酷的python表白工具 你喜欢我吗
Apr 11 #Python
2019 Python最新面试题及答案16道题
Apr 11 #Python
【python】matplotlib动态显示详解
Apr 11 #Python
python爬虫之验证码篇3-滑动验证码识别技术
Apr 11 #Python
You might like
Yii实现文章列表置顶功能示例
2016/10/18 PHP
php字符串截取函数mb_substr用法实例分析
2019/06/25 PHP
PHP实现简单登录界面
2019/10/23 PHP
ExtJS 2.0实用简明教程 之获得ExtJS
2009/04/29 Javascript
Array.prototype 的泛型应用分析
2010/04/30 Javascript
jquery上传插件fineuploader上传文件使用方法(jquery图片上传插件)
2013/12/05 Javascript
jQuery动态修改超链接地址的方法
2015/02/13 Javascript
js图片模糊切换显示特效的方法
2015/02/17 Javascript
javascript组合使用构造函数模式和原型模式实例
2015/06/04 Javascript
js不间断滚动的简单实现
2016/06/03 Javascript
js实现当鼠标移到表格上时显示这一格全部内容的代码
2016/06/12 Javascript
JS实现图片延迟加载并淡入淡出效果的简单方法
2016/08/25 Javascript
js判断请求的url是否可访问,支持跨域判断的实现方法
2016/09/17 Javascript
认识jQuery的Promise的具体使用方法
2017/10/10 jQuery
element-ui使用导航栏跳转路由的用法详解
2018/08/22 Javascript
js取0-9随机取4个数不重复的数字代码实例
2019/03/27 Javascript
javascript中的闭包概念与用法实践分析
2019/07/26 Javascript
Python运算符重载用法实例分析
2015/06/01 Python
Python使用Windows API创建窗口示例【基于win32gui模块】
2018/05/09 Python
使用Python3内置文档高效学习以及官方中文文档
2019/05/19 Python
pycharm工具连接mysql数据库失败问题
2020/04/01 Python
如何利用python正则表达式匹配版本信息
2020/12/09 Python
python基于Kivy写一个图形桌面时钟程序
2021/01/28 Python
定制iPhone和Macbook保护壳:Slick Case
2018/11/21 全球购物
异常和异常类的概念
2014/09/12 面试题
交通安全演讲稿
2014/01/07 职场文书
自考生自我评价分享
2014/01/18 职场文书
房地产财务管理制度
2014/02/02 职场文书
合作协议书怎么写
2014/04/18 职场文书
商业街策划方案
2014/05/31 职场文书
区政府领导班子个人对照检查材料
2014/09/25 职场文书
安全责任书
2015/01/29 职场文书
Java8中Stream的一些神操作
2021/11/02 Java/Android
MySQL中B树索引和B+树索引的区别详解
2022/03/03 MySQL
Python装饰器详细介绍
2022/03/25 Python
Python探索生命起源 matplotlib细胞自动机动画演示
2022/04/21 Python