Halcon示例之根据矩形中心和旋转角度求矩形角点

得益于(鸟叔)咋咋鸟的视觉平台和众网友的无私奉献,学习了不少知识和技巧;也学习下大神们的分享和奉献精神,分享一个实例源代码:根据矩形的中心坐标和矩形的旋转角度求矩形四个角点坐标;
需要说明的是:我们是处理图像中的信息,即输入信息和输出信息都是以图像坐标系为参考的,而图像坐标系常见的样子是水平(X轴)向右为正,垂直(Y轴)向下为正,其实这是所谓的2D笛卡尔坐标系,属于左手坐标系;但是矩形的旋转角度,却是以另外的坐标系来描述的,即水平(X轴)向右为正,垂直(Y轴)向上为正,这是我们在学习中常用坐标系,也是2D笛卡尔坐标系,属于右手坐标系,我们更习惯于在右手坐标系里求解数学问题,这个实例中涉及到的知识点主要是坐标变换(仿射变换)、其次坐标等;我们以图像坐标系进行输入信息并观察在此坐标系中的效果,因此需要在右手坐标系中求解,然后在图像坐标系观察。背景简介完毕,我来大致描述一下实现过程:我们假定左手坐标系和右手坐标系共用原点,首先根据矩形中心在左手坐标系中的坐标,得到平行坐标轴时的矩形角点坐标,然后将矩形中心以及角点坐标转换为右手坐标系中的坐标;再根据坐标变换,在右手坐标系中求解绕矩形中心旋转Phi角度后的角点坐标;最后将在右手坐标系中的角点坐标转换为左手坐标系中的坐标,即观察效果。
详细求解过程请自行尝试,若有疑问请私信!
以下是Halcon 下的代码(本地函数文件形式):(英文描述可以提高逼格哦!)

*Initialize the variable for coordinate of vertexes of rectangle2
VertexesY:=[]
VertexesX:=[]
*Initialize the temperary variables
RowTem:=0
ColTem:=0
*Judge the rectangle if it is available
if(Len1<0 or Len2<0)
    return()
endif
*Compute the sine and cosine of tuple Phi
tuple_cos(Phi,Cos)
tuple_sin(Phi,Sin)
*Compute the coordinate of the upper-right vertex of rectangle
RowTem:=CenterY-Len1*Sin-Len2*Cos
ColTem:=CenterX+Len1*Cos-Len2*Sin
VertexesY:=[VertexesY,RowTem]
VertexesX:=[VertexesX,ColTem]

*Compute the coordinate of the upper-left vertex of rectangle
RowTem:=CenterY+Len1*Sin-Len2*Cos
ColTem:=CenterX-Len1*Cos-Len2*Sin
VertexesY:=[VertexesY,RowTem]
VertexesX:=[VertexesX,ColTem]

*Compute the coordinate of the bottom-left vertex of rectangle
RowTem:=CenterY+Len1*Sin+Len2*Cos
ColTem:=CenterX-Len1*Cos+Len2*Sin
VertexesY:=[VertexesY,RowTem]
VertexesX:=[VertexesX,ColTem]

*Compute the coordinate of the bottom-right vertex of rectangle
RowTem:=CenterY-Len1*Sin+Len2*Cos
ColTem:=CenterX+Len1*Cos+Len2*Sin
VertexesY:=[VertexesY,RowTem]
VertexesX:=[VertexesX,ColTem]
return ()
发布了455 篇原创文章 · 获赞 535 · 访问量 324万+

猜你喜欢

转载自blog.csdn.net/libaineu2004/article/details/103852125