工控编程吧

标题: 工业视觉获取旋转矩形四顶点与四边中点坐标 [打印本页]

作者: qq263946146    时间: 2019-6-7 16:45
标题: 工业视觉获取旋转矩形四顶点与四边中点坐标
在视觉处理,特别是在几何处理计算时,经常要计算出矩形顶点坐标,进行结果好坏的判断,
下面给出了计算方法, 有两种,可根据需求选择使用
根据旋转矩形的参数计算矩形四顶点和四边中点坐标
下面函数可以打包成本地函数使用
*CenterY,CenterX 矩形的中心坐标
*Phi 矩形的旋转角度
*Len1,Len2 矩形长宽一半
*CornerY,CornerX 矩形四顶点坐标
*LineCenterY,LineCenterX, 矩形四边中心坐标

CenterY:=250
CenterX:=250
Phi := rad(45)
Len1 :=100
Len2 :=50

*矩形四顶点与四边中点初始化
CornerY :=[]
CornerX :=[]
LineCenterY :=[]
LineCenterX :=[]
*临时变量初始化
RowT:=0
ColT:=0
*判断矩形是否有效
if(Len1<0 or Len2<0)
    return()
endif
*计算矩形旋转角度正弦,余弦
tuple_sin (Phi, Sin)
tuple_cos (Phi, Cos)
*矩形第一顶点坐标
ColT:=CenterX-Len1*Cos-Len2*Sin
RowT:=CenterY-(-Len1*Sin+Len2*Cos)
CornerY :=[CornerY,RowT]
CornerX :=[CornerX,ColT]
*矩形第二顶点坐标
ColT:=CenterX+Len1*Cos-Len2*Sin
RowT:=CenterY-(Len1*Sin+Len2*Cos)
CornerY :=[CornerY,RowT]
CornerX :=[CornerX,ColT]
*矩形第三顶点坐标
ColT:=CenterX+Len1*Cos+Len2*Sin
RowT:=CenterY-(Len1*Sin-Len2*Cos)
CornerY :=[CornerY,RowT]
CornerX :=[CornerX,ColT]
*矩形第四顶点坐标
ColT:=CenterX-Len1*Cos+Len2*Sin
RowT:=CenterY-(-Len1*Sin-Len2*Cos)
CornerY :=[CornerY,RowT]
CornerX :=[CornerX,ColT]
*矩形第一边中点坐标
LineCenterY[0] := (CornerY[0]+CornerY[1])*0.5
LineCenterX[0] := (CornerX[0]+CornerX[1])*0.5
*矩形第二边中点坐标
LineCenterY[1] := (CornerY[1]+CornerY[2])*0.5
LineCenterX[1] := (CornerX[1]+CornerX[2])*0.5
*矩形第三边中点坐标
LineCenterY[2] := (CornerY[3]+CornerY[2])*0.5
LineCenterX[2] := (CornerX[3]+CornerX[2])*0.5
*矩形第四边中点坐标
LineCenterY[3] := (CornerY[3]+CornerY[0])*0.5
LineCenterX[3] := (CornerX[3]+CornerX[0])*0.5

*显示点,查看效果,可删除
gen_rectangle2_contour_xld (Rectangle, CenterY, CenterX, Phi, Len1, Len2)
gen_cross_contour_xld (Cross, CornerY, CornerX, 6, 0.785398)
gen_cross_contour_xld (Cross1, LineCenterY, LineCenterX, 6, 0.785398)
dev_set_color ('blue')
dev_display (Rectangle)
dev_set_color ('red')
dev_display (Cross)
dev_set_color ('green')
dev_display (Cross1)

*也可使用下面的方法获取旋转矩形中四个顶点坐标,再通过顶点坐标算出四边的中点坐标
if(Len1<0 or Len2<0)
    return()
endif
gen_rectangle2_contour_xld (Rectangle, CenterY, CenterX, Phi, Len1, Len2)
get_contour_xld (Rectangle, RowT, ColT)
CornerY :=[]
CornerX :=[]
for i:=0 to |RowT|-2 by 1
    CornerY := RowT
    CornerX := ColT
endfor
*显示点,查看效果,可删除
gen_cross_contour_xld (Cross, CornerY, CornerX, 6, 0.785398)
dev_set_color ('blue')
dev_display (Rectangle)
dev_set_color ('red')
dev_display (Cross)

结果为
(, 下载次数: 3)