<Halcon> 计算任意矩形四个顶点坐标

计算任意矩形四个顶点坐标

1、计算矩形与水平方向的夹角Phi(smallest_rectangle2:矩形长与水平的夹角,- pi / 2 < Phi <= pi / 2);
2、若夹角大于45°或者小于-45°,角度转换为矩形宽与水平的夹角,长宽互换;
<Halcon> 计算任意矩形四个顶点坐标_第1张图片
<Halcon> 计算任意矩形四个顶点坐标_第2张图片

Halcon通用代码

*计算矩形的中间点、角度、及长宽(一半)
smallest_rectangle2 (Rectangle, Row, Column, Phi, Length1, Length2)

if(abs(deg(Phi))>45)
    Phi:=rad(deg(Phi)-90*(Phi/abs(Phi)))
    Tmp:=Length1
    Length1:=Length2
    Length2:=Tmp
endif

tuple_sin (Phi, Sin)
tuple_cos (Phi, Cos)

*左上角
TopLeft_X := -Length1*Cos - Length2*Sin
TopLeft_Y := -Length1*Sin + Length2*Cos
TopLeft_Row := Row - TopLeft_Y 
TopLeft_Col := Column + TopLeft_X 

*右上角
TopRight_X := Length1*Cos - Length2*Sin
TopRight_Y := Length1*Sin + Length2*Cos
TopRight_Row := Row - TopRight_Y 
TopRight_Col := Column + TopRight_X 

*右下角
LowerRight_X := Length1*Cos + Length2*Sin
LowerRight_Y := Length1*Sin - Length2*Cos
LowerRight_Row := Row - LowerRight_Y 
LowerRight_Col := Column + LowerRight_X 
 
*左下角
LowerLeft_X := -Length1*Cos + Length2*Sin
LowerLeft_Y := -Length1*Sin - Length2*Cos
LowerLeft_Row := Row - LowerLeft_Y 
LowerLeft_Col := Column + LowerLeft_X 

示例

示例图
绿色圈:左上角
蓝色圈:右上角
黄色圈:右下角
白色圈:左下角

<Halcon> 计算任意矩形四个顶点坐标_第3张图片
示例代码

dev_set_color('red')
gen_rectangle2 (ROI_0, 250, 250, rad(20), 100, 20)
* gen_rectangle2 (ROI_1, 250, 250, rad(70), 100, 20)
* gen_rectangle2 (ROI_2, 250, 250, rad(-20), 100, 20)
* gen_rectangle2 (ROI_3, 250, 250, rad(-70), 100, 20)

smallest_rectangle2 (ROI_0, Row, Column, Phi, Length1, Length2)
* smallest_rectangle2 (ROI_1, Row, Column, Phi, Length1, Length2)
* smallest_rectangle2 (ROI_2, Row, Column, Phi, Length1, Length2)
* smallest_rectangle2 (ROI_3, Row, Column, Phi, Length1, Length2)

if(abs(deg(Phi))>45)
    Phi:=rad(deg(Phi)-90*(Phi/abs(Phi)))
    Tmp:=Length1
    Length1:=Length2
    Length2:=Tmp
endif

tuple_sin (Phi, Sin)
tuple_cos (Phi, Cos)

*左上角
dev_set_color('green')
TopLeft_X := -Length1*Cos - Length2*Sin
TopLeft_Y := -Length1*Sin + Length2*Cos
TopLeft_Row := Row - TopLeft_Y 
TopLeft_Col := Column+TopLeft_X 
gen_circle (Circle, TopLeft_Row, TopLeft_Col, 10)

*右上角
dev_set_color('blue')
TopRight_X := Length1*Cos - Length2*Sin
TopRight_Y := Length1*Sin + Length2*Cos
TopRight_Row:=Row-TopRight_Y 
TopRight_Col:=Column+TopRight_X 
gen_circle (Circle, TopRight_Row, TopRight_Col, 10)

*右下角
dev_set_color('yellow')
LowerRight_X := Length1*Cos + Length2*Sin
LowerRight_Y := Length1*Sin - Length2*Cos
LowerRight_Row:=Row-LowerRight_Y 
LowerRight_Col:=Column+LowerRight_X 
gen_circle (Circle, LowerRight_Row, LowerRight_Col, 10)
 
*左下角
dev_set_color('white')
LowerLeft_X := -Length1*Cos + Length2*Sin
LowerLeft_Y := -Length1*Sin - Length2*Cos
LowerLeft_Row:=Row-LowerLeft_Y 
LowerLeft_Col:=Column+LowerLeft_X 
gen_circle (Circle, LowerLeft_Row, LowerLeft_Col, 10)

你可能感兴趣的:(Halcon,Halcon,几何学)