首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >旋转图像坐标的确定

问旋转图像坐标的确定
EN

Stack Overflow用户
提问于 2022-11-09 06:54:18
回答 1查看 26关注 0票数 0

我使用python,我在画布上有一些图片,它们是以不同的角度旋转的。我想做的是得到他们的坐标,而我知道他们以前的位置,轴和他们旋转的角度。

如果我能在旋转后找到一个点的坐标,那也就足够了。

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2022-11-16 06:49:43

为了在坐标旋转后得到一个坐标,我将使用矮胖和旋转矩阵来导出它的位置:

​

​

代码语言:javascript
复制
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}).
""")
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/74370956

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档