You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

基于Python的旋转后图像及单点坐标计算方法咨询

计算点旋转后的坐标(Python实现)

2D场景下的点旋转

在2D画布上旋转点,核心是利用旋转矩阵,步骤分为三步:

  1. 若旋转中心不是坐标原点,先将点平移到以旋转中心为原点的坐标系
  2. 应用2D旋转矩阵计算旋转后的点
  3. 将结果平移回原坐标系

2D旋转公式

假设:

  • 原始点坐标为 (x, y)
  • 旋转中心为 (cx, cy)
  • 旋转角度为 theta(弧度制,顺时针旋转为负,逆时针为正)

旋转后的坐标 (x', y') 计算公式:

dx = x - cx
dy = y - cy
x' = dx * cos(theta) - dy * sin(theta) + cx
y' = dx * sin(theta) + dy * cos(theta) + cy

Python代码实现

import math

def rotate_2d_point(x, y, cx, cy, theta):
    # 将角度转换为弧度
    theta_rad = math.radians(theta)
    cos_theta = math.cos(theta_rad)
    sin_theta = math.sin(theta_rad)
    
    # 平移到旋转中心为原点
    dx = x - cx
    dy = y - cy
    
    # 计算旋转后的坐标
    x_rot = dx * cos_theta - dy * sin_theta + cx
    y_rot = dx * sin_theta + dy * cos_theta + cy
    
    return (x_rot, y_rot)

# 示例:点(2,3)绕(1,1)逆时针旋转90度
original_point = (2, 3)
center = (1, 1)
angle = 90
rotated_point = rotate_2d_point(*original_point, *center, angle)
print(f"原始点: {original_point}, 旋转后点: {rotated_point}")

3D场景下的点旋转(可选)

如果是3D空间中的旋转,可使用Rodrigues旋转公式处理任意轴的旋转,或针对X/Y/Z轴使用对应的旋转矩阵。以下是绕任意轴旋转的实现:

Python代码实现

import math
import numpy as np

def rotate_3d_point(point, axis, angle):
    # 转换为numpy数组方便计算
    point = np.array(point, dtype=np.float64)
    axis = np.array(axis, dtype=np.float64)
    axis = axis / np.linalg.norm(axis)  # 归一化旋转轴
    
    theta_rad = math.radians(angle)
    cos_theta = math.cos(theta_rad)
    sin_theta = math.sin(theta_rad)
    
    # Rodrigues旋转公式
    rotated_point = (
        point * cos_theta
        + np.cross(axis, point) * sin_theta
        + axis * np.dot(axis, point) * (1 - cos_theta)
    )
    
    return tuple(rotated_point)

# 示例:点(1,0,0)绕(0,0,1)轴逆时针旋转90度
original_3d_point = (1, 0, 0)
rotation_axis = (0, 0, 1)
angle_3d = 90
rotated_3d_point = rotate_3d_point(original_3d_point, rotation_axis, angle_3d)
print(f"原始3D点: {original_3d_point}, 旋转后3D点: {rotated_3d_point}")

批量处理图像坐标

如果要处理图像上的多个点(比如图像的四个顶点),只需遍历所有点调用上述函数即可,再根据旋转后的顶点坐标重新绘制图像。

内容的提问来源于stack exchange,提问作者UnknownProgrammer

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.12 23:50:37