简体   繁体   English

找到旋转的图像坐标

[英]finding rotated image coordinates

I'm using python and I have some images on my canvas that are rotated by different angles.我使用的是 python,我的 canvas 上有一些旋转了不同角度的图像。 What I want to do is to get their coordinates while I know their previous position, axis and the angle they were rotated by.我想做的是在我知道他们之前的 position、轴和他们旋转的角度的同时获取他们的坐标。

It would also be enough if I can find coordinates of just one point after rotation如果我能在旋转后找到一个点的坐标就足够了

To get a coordinate after it has been rotated, I would use numpy and a rotation matrix to derive its position:为了在旋转后获得坐标,我将使用numpy 和旋转矩阵来导出其 position:

在此处输入图像描述

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}).
""")

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM