iis服务器助手广告
返回顶部
首页 > 资讯 > 后端开发 > Python >已知中心点、长宽和旋转角度,求矩形的四个顶点坐标(Python)
  • 653
分享到

已知中心点、长宽和旋转角度,求矩形的四个顶点坐标(Python)

python线性代数矩阵 2023-09-30 08:09:20 653人浏览 泡泡鱼

Python 官方文档:入门教程 => 点击学习

摘要

已知中心点、长宽和旋转角度,求矩形的四个顶点坐标 理论基础情况一: θ ∈ [ 0

已知中心点、长宽和旋转角度,求矩形的四个顶点坐标

理论基础

本次实现有几个前提:

  • 已知的信息如下形式:[x_center, y_center, w, h, angle],其中默认 w w w是矩形最长的边,即 w>h w > h w>h
  • 已知的旋转角度 θ \theta θ是矩形的最长边 w w w相对于 x x x坐标轴的旋转角度
  • 旋转角度 θ \theta θ的旋转区间在 [0,π] [0, \pi] [0,π]

可以将情况分为两种,即 θ ∈ [ 0 , π / 2 ] \theta \in [0, \pi/2] θ[0,π/2] θ ∈ [ π / 2 , π ] \theta \in [\pi/2, \pi] θ[π/2,π]

情况一: θ ∈ [ 0 , π / 2 ] \theta \in [0, \pi/2] θ[0,π/2]

先看第一种情况 θ ∈ [ 0 , π / 2 ] \theta \in [0, \pi/2] θ[0,π/2]
在这里插入图片描述
已知矩形的中心点 ( x , y ) (x, y) (x,y),旋转角度 θ \theta θ在图中用橙色标注。

先来求 ( x1 , y1 ) (x_1,y_1) (x1,y1),用到图中绿色的辅助线,用到的三角形都标注了角 θ \theta θ

  • x 1 =x+cosθ∗w/2−sinθ∗h/2 x_1=x+cos\theta*w/2-sin\theta*h/2 x1=x+cosθw/2sinθh/2
  • y 1 =y+sinθ∗w/2+cosθ∗h/2 y_1=y+sin\theta*w/2+cos\theta*h/2 y1=y+sinθw/2+cosθh/2

再来求 ( x2 , y2 ) (x_2,y_2) (x2,y2),用到图中紫色的辅助线:

  • x 2 =x+cosθ∗w/2+sinθ∗h/2 x_2=x+cos\theta*w/2+sin\theta*h/2 x2=x+cosθw/2+sinθh/2
  • y 2 =y+sinθ∗w/2−cosθ∗h/2 y_2=y+sin\theta*w/2-cos\theta*h/2 y2=y+sinθw/2cosθh/2

接下来的 ( x3 , y3 ) (x_3,y_3) (x3,y3) ( x4 , y4 ) (x_4,y_4) (x4,y4)就是 ( x1 , y1 ) (x_1,y_1) (x1,y1) ( x2 , y2 ) (x_2,y_2) (x2,y2)关于 ( x , y ) (x, y) (x,y)的对称点,只需要将正项变为负项,负项变为正项即可:

  • x 3 =x−cosθ∗w/2+sinθ∗h/2 x_3=x-cos\theta*w/2+sin\theta*h/2 x3=xcosθw/2+sinθh/2
  • y 3 =y−sinθ∗w/2−cosθ∗h/2 y_3=y-sin\theta*w/2-cos\theta*h/2 y3=ysinθw/2cosθh/2
  • x 4 =x−cosθ∗w/2−sinθ∗h/2 x_4=x-cos\theta*w/2-sin\theta*h/2 x4=xcosθw/2sinθh/2
  • y 4 =y−sinθ∗w/2+cosθ∗h/2 y_4=y-sin\theta*w/2+cos\theta*h/2 y4=ysinθw/2+cosθh/2

情况二: θ ∈ [ π / 2 , π ] \theta \in [ \pi/2,\pi] θ[π/2,π]

在这里插入图片描述
同理,此时旋转角度 θ \theta θ大于 π / 2 \pi/2 π/2,所以用到的辅助三角形的角度标注为 π − θ \pi -\theta πθ

先来求 ( x1 , y1 ) (x_1,y_1) (x1,y1),用到图中绿色的辅助线,用到的三角形都标注了角 π − θ \pi -\theta πθ

  • x 1 =x−cos(π−θ)∗w/2+sin(π−θ)∗h/2=x+cosθ∗w/2+sinθ∗h/2 x_1=x-cos(\pi-\theta)*w/2+sin(\pi-\theta)*h/2=x+cos\theta*w/2+sin\theta*h/2 x1=xcos(πθ)w/2+sin(πθ)h/2=x+cosθw/2+sinθh/2
  • y 1 =y−sin(π−θ)∗w/2−cos(π−θ)∗h/2=y−sinθ∗w/2+cosθ∗h/2 y_1=y-sin(\pi-\theta)*w/2-cos(\pi-\theta)*h/2=y-sin\theta*w/2+cos\theta*h/2 y1=ysin(πθ)w/2cos(πθ)h/2=ysinθw/2+cosθh/2
  • x 2 =x−cos(π−θ)∗w/2−sin(π−θ)∗h/2=x+cosθ∗w/2−sinθ∗h/2 x_2=x-cos(\pi-\theta)*w/2-sin(\pi-\theta)*h/2=x+cos\theta*w/2-sin\theta*h/2 x2=xcos(πθ)w/2sin(πθ)h/2=x+cosθw/2sinθh/2
  • y 2 =y+sin(π−θ)∗w/2−cos(π−θ)∗h/2=y+sinθ∗w/2+cosθ∗h/2 y_2=y+sin(\pi-\theta)*w/2-cos(\pi-\theta)*h/2=y+sin\theta*w/2+cos\theta*h/2 y2=y+sin(πθ)w/2cos(πθ)h/2=y+sinθw/2+cosθh/2

( x3 , y3 ) (x_3,y_3) (x3,y3) ( x4 , y4 ) (x_4,y_4) (x4,y4)就是 ( x1 , y1 ) (x_1,y_1) (x1,y1) ( x2 , y2 ) (x_2,y_2) (x2,y2)关于 ( x , y ) (x, y) (x,y)的对称点。

可以看到两种情况下,得到的四个点的值是一样的,比如情况一里 ( x1 , y1 ) (x_1,y_1) (x1,y1)和情况二里 ( x2 , y2 ) (x_2,y_2) (x2,y2)一样,所以在代码实现里可以不分情况讨论,不影响最终结果。

python代码实现

def get_corners(box):  #这里本人项目yaw [-pi/4, 3*pi/4),需要映射到[0, pi)    box = box.detach().cpu().numpy()    x = box[0]    y = box[1]    w = box[2]    l = box[3]    yaw = box[4]    if yaw <0: #用来映射        yaw = yaw + np.pi    bev_corners = np.zeros((4, 2), dtype=np.float32)    cos_yaw = np.cos(yaw)    sin_yaw = np.sin(yaw)     bev_corners[0, 0] = (w / 2) * cos_yaw - (l / 2) * sin_yaw +x    bev_corners[0, 1] = (w / 2)* sin_yaw + (l / 2) * cos_yaw +y    bev_corners[1, 0] = (l / 2) * sin_yaw + (w / 2) * cos_yaw +x    bev_corners[1, 1] = (w / 2)* sin_yaw - (l / 2) * cos_yaw +y    bev_corners[2, 0] = (-w / 2) * cos_yaw - (-l / 2) * sin_yaw +x    bev_corners[2, 1] = (-w / 2)* sin_yaw + (-l / 2) * cos_yaw +y    bev_corners[3, 0] = (-l / 2) * sin_yaw + (-w / 2) * cos_yaw +x    bev_corners[3, 1] = (-w / 2)* sin_yaw - (-l / 2) * cos_yaw +y    return bev_corners

来源地址:https://blog.csdn.net/weixin_45453121/article/details/129582622

--结束END--

本文标题: 已知中心点、长宽和旋转角度,求矩形的四个顶点坐标(Python)

本文链接: https://www.lsjlt.com/news/421772.html(转载时请注明来源链接)

有问题或投稿请发送至: 邮箱/279061341@qq.com    QQ/279061341

本篇文章演示代码以及资料文档资料下载

下载Word文档到电脑,方便收藏和打印~

下载Word文档
猜你喜欢
软考高级职称资格查询
编程网,编程工程师的家园,是目前国内优秀的开源技术社区之一,形成了由开源软件库、代码分享、资讯、协作翻译、讨论区和博客等几大频道内容,为IT开发者提供了一个发现、使用、并交流开源技术的平台。
  • 官方手机版

  • 微信公众号

  • 商务合作