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 命令行非阻塞输入的小例子
Sep 27 Python
python日志记录模块实例及改进
Feb 12 Python
Python内置函数——__import__ 的使用方法
Nov 24 Python
Python编写合并字典并实现敏感目录的小脚本
Feb 26 Python
python导入pandas具体步骤方法
Jun 23 Python
利用python实现汉字转拼音的2种方法
Aug 12 Python
python递归下载文件夹下所有文件
Aug 31 Python
python 实现图片上传接口开发 并生成可以访问的图片url
Dec 18 Python
Python使用Tkinter实现滚动抽奖器效果
Jan 06 Python
Python-opencv 双线性插值实例
Jan 17 Python
jupyter notebook 调用环境中的Keras或者pytorch教程
Apr 14 Python
Python自省及反射原理实例详解
Jul 06 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
双料怀旧--SHARP GF515的维护、修理和简单调试
2021/03/02 无线电
基于PHP对XML的操作详解
2013/06/07 PHP
PHP批量删除、清除UTF-8文件BOM头的代码实例
2014/04/14 PHP
浅谈htmlentities 、htmlspecialchars、addslashes的使用方法
2016/12/09 PHP
PHP实现微信公众号验证Token的示例代码
2019/12/16 PHP
游戏人文件夹程序 ver 3.0
2006/07/14 Javascript
ajax 文件上传应用简单实现
2009/03/03 Javascript
JavaScript 语言的递归编程
2010/05/18 Javascript
ASP.NET jQuery 实例1(在TextBox里面创建一个默认提示)
2012/01/13 Javascript
DOM节点的替换或修改函数replaceChild()用法实例
2015/01/12 Javascript
jquery实现表格隔行换色效果
2015/11/19 Javascript
基于javascript如何传递特殊字符
2015/11/30 Javascript
快速学习jQuery插件 jquery.validate.js表单验证插件使用方法
2015/12/01 Javascript
微信小程序 progress组件详解及实例代码
2016/10/25 Javascript
实例解析Array和String方法
2016/12/14 Javascript
vue图片加载与显示默认图片实例代码
2017/03/16 Javascript
详解Vue用cmd创建项目
2019/02/12 Javascript
详解vue使用$http服务端收不到参数
2019/04/19 Javascript
Vue v-for中的 input 或 select的值发生改变时触发事件操作
2020/08/31 Javascript
从零学python系列之浅谈pickle模块封装和拆封数据对象的方法
2014/05/23 Python
python实现的守护进程(Daemon)用法实例
2015/06/02 Python
Python使用Pycrypto库进行RSA加密的方法详解
2016/06/06 Python
Python Grid使用和布局详解
2018/06/30 Python
Django如何自定义分页
2018/09/25 Python
django-crontab 定时执行任务方法的实现
2019/09/06 Python
Django 实现Admin自动填充当前用户的示例代码
2019/11/18 Python
利用三角函数在canvas上画虚线的方法
2018/01/11 HTML / CSS
实习心得体会
2014/01/02 职场文书
小学生期末自我鉴定
2014/01/19 职场文书
会计毕业生自荐书
2014/06/12 职场文书
2014年办公室个人工作总结
2014/11/12 职场文书
处级干部考察材料
2014/12/24 职场文书
会议邀请函
2015/01/30 职场文书
稽核岗位职责范本
2015/04/13 职场文书
员工工作心得体会
2019/05/07 职场文书
一文搞懂MySQL索引页结构
2022/02/28 MySQL