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

时间:2022-11-25 16:41:57

opencv-python图像透视变换cv2.warpperspective

代码如下:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
# -*- 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 输出当前旋转矩阵参数

总结

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

原文链接:https://blog.csdn.net/dcrmg/article/details/80273818