You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 

60 lines
1.5 KiB

import cv2
import numpy as np
def pingyi(x,y):
# 获取图像的大小
height, width = image.shape[:2]
# 构造平移矩阵:将原点移到图像中心
M_translate = np.float32([
[1, 0, x],
[0, 1, y],
[0, 0, 1]
])
return M_translate
def xuanzhuan_z(z):
# 构造沿Z轴旋转30度的旋转矩阵
theta = np.radians(z)
cos_theta = np.cos(theta)
sin_theta = np.sin(theta)
M_z_rotate = np.float32([
[cos_theta, -sin_theta, 0],
[sin_theta, cos_theta, 0],
[0, 0, 1]
])
return M_z_rotate
def xuanzhuan_y(y):
# 构造沿Z轴旋转30度的旋转矩阵
theta = np.radians(y)
rotation_vector = np.array([0, theta, 0]) # 绕Y轴旋转
# 计算旋转矩阵
rotation_matrix, _ = cv2.Rodrigues(rotation_vector)
return rotation_matrix
if __name__ == '__main__':
# 读取图像
image = cv2.imread("images/trans/transformed_image.jpg")
# 获取图像的大小
height, width = image.shape[:2]
m_pinyi=pingyi(width,0)
# m_xuanzhuan=xuanzhuan_z(30)
m_xuanzhuan = xuanzhuan_y(80)
M_combined = m_pinyi @ m_xuanzhuan
rotation_3d_int = np.clip(M_combined, 0, 255)
# 应用透视变换
warped_image = cv2.warpPerspective(
image,
M_combined,
(width*2, height*2)
)
# 显示结果
cv2.imshow('Original Image', image)
cv2.imshow('Warped Image', warped_image)
cv2.waitKey(0)
cv2.destroyAllWindows()