python opencv实现任意角度的透视变换实例代码

yipeiwu_com5年前Python基础

本文主要分享的是一则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()

总结

以上就是本文关于python opencv实现任意角度的透视变换实例代码的全部内容,希望对大家有所帮助。感兴趣的朋友可以继续参阅本站其他相关专题,如有不足之处,欢迎留言指出。感谢朋友们对本站的支持!

相关文章

python判断无向图环是否存在的示例

暂时是一个手动设置无向图中的边,用一个二维数组表示,后面会改进为用户自己定义无向图的边。 学习python的新手,若大佬有解决的办法,希望不吝赐教 #无向图判断环是否存在 def d...

python生成器/yield协程/gevent写简单的图片下载器功能示例

本文实例讲述了python生成器/yield协程/gevent写简单的图片下载器功能。分享给大家供大家参考,具体如下: 1、生成器: '''第二种生成器''' # 函数只有有yiel...

Python3中详解fabfile的编写

fab命令好似结合我们编写的fabfile.py(其它文件名必须添加-f filename应用)来搭配使用的,部分命令行参数可以通过相应的方法来替代,使之更加灵活,例如"-H 192.1...

ubuntu安装sublime3并配置python3环境的方法

最近有一些烦,虚拟机跑代码,跑着跑着存储不够,我就去扩大磁盘,结果虚拟机崩了,试了一上午的修复办法,仍然无法修复,于是只能重装虚拟机,配置各种环境,这里总结一下Ubuntu中配置subl...

深入理解python中sort()与sorted()的区别

Python list内置sort()方法用来排序,也可以用python内置的全局sorted()方法来对可迭代的序列排序生成新的序列 一,最简单的排序 1.使用sort排序 my_...