我使用python,我在画布上有一些图片,它们是以不同的角度旋转的。我想做的是得到他们的坐标,而我知道他们以前的位置,轴和他们旋转的角度。
如果我能在旋转后找到一个点的坐标,那也就足够了。
发布于 2022-11-16 06:49:43
为了在坐标旋转后得到一个坐标,我将使用矮胖和旋转矩阵来导出它的位置:

import numpy as np
# define parameters
pixel_column = 10
pixel_row = 20
center_column = 50
center_row = 50
# angle goes counter-clockwise!
rotation_angle_deg = 90
def get_rotation_matrix(angle_deg: float):
theta = np.radians(angle_deg)
cos_theta, sin_theta = np.cos(theta), np.sin(theta)
rotation = np.array(((cos_theta, -sin_theta), (sin_theta, cos_theta)))
return rotation
def rotate_coordinate(coordinate: np.array, angle_deg: float) -> np.array:
rotation = get_rotation_matrix(angle_deg)
rotated = rotation.dot(coordinate)
return rotated
def get_new_coordinate(original_coordinate: np.array, center_of_rotation: np.array, angle_deg: float):
delta = original_coordinate - center_of_rotation
delta_rotated = rotate_coordinate(delta, angle_deg)
new_coordinate = center_of_rotation + delta_rotated
return new_coordinate
# calculate new rotated coordinate
rotated_coordinate = get_new_coordinate(
np.array((pixel_row, pixel_column)),
np.array((center_row, center_column)),
rotation_angle_deg
)
new_pixel_row, new_pixel_column = [int(val) for val in rotated_coordinate]
# show as text
print(f"""
After rotating the image at an angle of {rotation_angle_deg}° around ({center_row}, {center_column}),
the pixel located previously at ({pixel_row}, {pixel_column}) is now at ({new_pixel_row}, {new_pixel_column}).
""")https://stackoverflow.com/questions/74370956
复制相似问题