python opencv implements perspective transform instance code for any Angle
- 2020-06-23 01:09:38
- OfStack
This paper mainly shares 1 example of python+opencv to realize perspective transformation from any Angle, specifically as follows:
# -*- 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)
# Extend the image to keep the content within visual range
img = cv2.copyMakeBorder(img, 200, 200, 200, 200, cv2.BORDER_CONSTANT, 0)
w, h = img.shape[0:2]
anglex = 0
angley = 30
anglez = 0 # Is rotating
fov = 42
while 1:
# The distance between the lens and the image, 21 Is a semi-viewable Angle, calculate z The distance is to ensure that the entire image is displayed exactly at this visual Angle
z = np.sqrt(w ** 2 + h ** 2) / 2 / np.tan(rad(fov / 2))
# Homogeneous transformation matrix
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)
# 4 The generation of the point
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)
# Project onto the image plane
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
# Keyboard control
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(' Rotation matrix: \n', r)
print("angle alpha: ", anglex, 'angle beta: ', angley, "dz: ", anglez, ": ", z)
cv2.destroyAllWindows()
conclusion
That's the end of this article on the implementation of python opencv perspective transformation at any Angle of the sample code, I hope to help you. Interested friends can continue to refer to other related topics in this site, if there is any deficiency, welcome to comment out. Thank you for your support!