Python 按y轴旋转图像
我有一个函数Python 按y轴旋转图像,python,opencv,rotation,Python,Opencv,Rotation,我有一个函数rotate,它是通过y轴在输入角度上完美地旋转输入图像,但我面临一个问题。输出图像的角被裁剪,我认为图像边界有问题,但我不确定。 我的职能 import numpy as np import cv2 import math def rotate_y(img, y): # change size of image width = img.shape[1] height = img.shape[0] # 2d to 3d (projection)
rotate
,它是通过y轴在输入角度上完美地旋转输入图像,但我面临一个问题。输出图像的角被裁剪,我认为图像边界有问题,但我不确定。
我的职能
import numpy as np
import cv2
import math
def rotate_y(img, y):
# change size of image
width = img.shape[1]
height = img.shape[0]
# 2d to 3d (projection) , and -> rotation point - center point (origin point)
proj2dto3d = np.array([[1, 0, -img.shape[1] / 2],
[0, 1, -img.shape[0] / 2],
[0, 0, 0],
[0, 0, 1]], np.float32)
# 3d matrix in y
ry = np.array([[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 0],
[0, 0, 0, 1]], np.float32)
trans = np.array([[1, 0, 0, 0],
[0, 1, 0, 0],
[0, 0, 1, 100],
[0, 0, 0, 1]], np.float32)
proj3dto2d = np.array([[100, 0, img.shape[1] / 2, 0],
[0, 100, img.shape[0] / 2, 0],
[0, 0, 1, 0]], np.float32)
ay = float(y * (math.pi / 180))
ry[0, 0] = math.cos(ay)
ry[0, 2] = -math.sin(ay)
ry[2, 0] = math.sin(ay)
ry[2, 2] = math.cos(ay)
final = proj3dto2d.dot(trans.dot(ry.dot(proj2dto3d)))
# rotation calculates the cos and sin, taking absolutes of those.
abs_cos = abs(math.cos(ay))
abs_sin = abs(math.sin(ay))
# find the new width and height bounds
bound_w = int(height * abs_sin + width * abs_cos)
bound_h = int(height * abs_cos + width * abs_sin)
dst = cv2.warpPerspective(img, final, (bound_w, bound_h), None, cv2.INTER_LINEAR
, cv2.BORDER_CONSTANT, (0, 0, 0))
return dst
我想收到一张未剪切的图片。请参见和@fmw42,是的,我已经阅读了这些文章,但只有关于x轴旋转的文章,我需要3d旋转(y轴)