Halcon的angle_ll和angle_lx角度的解析
在Halcon的使用过程中经常要使用到angle_ll和angle_lx两个算子来求解线与线之间的角度以及线与水平轴(x轴)之间的角度,而线线之间和线轴之间的角度正负往往是困扰我们的问题,下面对这两种情况进行解释说明。
1. angle_ll(Row1,Col1,Row2,Col2,Row3,Col3,Row4,Col4,Angle)
该算子的前四个参数分别代表第一条线的起点行列坐标和终点行列坐标,后面四个参数分别代表第二条线的起点行列坐标和终点行列坐标,这里需要注意的是这些参数的输入顺序会影响到直线的方向,直线的方向是由起点指向终点,如下图所示:
angle_ll算子在计算两条直线之间的夹角的方式,可以理解为第一条直线绕着其起点进行旋转,直至第一条直线的方向和第二条直线的方向相同时,第一条直线所旋转的角度即为两条直线的角度,但是第一条直线顺时针和逆时针方向都可以转至和第二条直线相同的方向,应该朝哪个方向转动来计算角度呢?angle_ll算子的角度范围为-Pi~Pi,也就是说第一条直线的转动方向选择的两条直线的夹角比较小的方向,如下图所示:
如果使用angle_ll算子计算图中红色直线与绿色直线的夹角,是应该将红色直线沿着蓝色箭头方向旋转还是黄色箭头方向进行旋转呢?答案是黄色箭头方向,因为黄色的夹角小于蓝色的夹角,蓝色夹角的角度超过了angle_ll算子的取值范围。
说完了旋转的方向,现在来讨论下角度的正负,如果第一条直线旋转至第二条直线的方向为顺时针,那么角度为负值,逆时针则为正值,下面通过几个Halcon的程序来演示。
1 set_display_font (200000, 26, 'mono', 'true', 'false') 2 Row1 := 100 3 Col1 := 100 4 Row2 := 500 5 Col2 := 600 6 Row1Half := (Row1 + Row2)/2 7 Col1Half := (Col1 + Col2)/2 8 9 Row3 := 400 10 Col3 := 100 11 Row4 := 100 12 Col4 := 500 13 Row2Half := (Row3 + Row4)/2 14 Col2Half := (Col3 + Col4)/2 15 16 17 dev_set_color ('red') 18 gen_region_line (RegionLines, Row1, Col1, Row2, Col2) 19 gen_arrow_contour_xld (Arrow1, Row1Half, Col1Half, Row1, Col1, 25, 15) 20 21 dev_set_color ('blue') 22 gen_region_line (RegionLines, Row3, Col3, Row4, Col4) 23 gen_arrow_contour_xld (Arrow1, Row2Half, Col2Half, Row3, Col3, 25, 15) 24 25 26 27 angle_ll (Row4, Col4,Row3, Col3, Row2, Col2,Row1, Col1, Angle) 28 disp_message (200000, '蓝色线转向红色线角度为:' + deg(Angle), 'window', 12, 12, 'black', 'true')
1 set_display_font (200000, 26, 'mono', 'true', 'false') 2 Row1 := 100 3 Col1 := 100 4 Row2 := 500 5 Col2 := 600 6 Row1Half := (Row1 + Row2)/2 7 Col1Half := (Col1 + Col2)/2 8 9 Row3 := 400 10 Col3 := 100 11 Row4 := 100 12 Col4 := 500 13 Row2Half := (Row3 + Row4)/2 14 Col2Half := (Col3 + Col4)/2 15 16 17 18 dev_set_color ('red') 19 gen_region_line (RegionLines, Row1, Col1, Row2, Col2) 20 gen_arrow_contour_xld (Arrow1, Row1Half, Col1Half, Row1, Col1, 25, 15) 21 22 dev_set_color ('blue') 23 gen_region_line (RegionLines, Row3, Col3, Row4, Col4) 24 gen_arrow_contour_xld (Arrow1, Row2Half, Col2Half, Row4, Col4, 25, 15) 25 26 27 28 29 angle_ll (Row3, Col3, Row4, Col4, Row2, Col2,Row1, Col1, Angle) 30 disp_message (200000, '蓝色线转向红色线角度为:' + deg(Angle), 'window', 12, 12, 'black', 'true')
set_display_font (200000, 26, 'mono', 'true', 'false') Row1 := 100 Col1 := 100 Row2 := 500 Col2 := 600 Row1Half := (Row1 + Row2)/2 Col1Half := (Col1 + Col2)/2 Row5 := 250 Col5 := 20 Row6 := 250 Col6 := 550 Row3Half := (Row5 + Row6)/2 Col3Half := (Col5 + Col6)/2 dev_set_color ('red') gen_region_line (RegionLines, Row1, Col1, Row2, Col2) gen_arrow_contour_xld (Arrow1, Row1Half, Col1Half, Row1, Col1, 25, 15) dev_set_color ('yellow') gen_region_line (RegionLines, Row5, Col5, Row6, Col6) gen_arrow_contour_xld (Arrow1, Row3Half, Col3Half, Row6, Col6, 25, 15) angle_ll (Row2, Col2,Row1, Col1,Row5, Col5, Row6, Col6, Angle) disp_message (200000, '红色线转向黄色色线角度为:' + deg(Angle), 'window', 12, 12, 'black', 'true')
1 set_display_font (200000, 26, 'mono', 'true', 'false') 2 Row1 := 100 3 Col1 := 100 4 Row2 := 500 5 Col2 := 600 6 Row1Half := (Row1 + Row2)/2 7 Col1Half := (Col1 + Col2)/2 8 9 10 Row5 := 250 11 Col5 := 20 12 Row6 := 250 13 Col6 := 550 14 Row3Half := (Row5 + Row6)/2 15 Col3Half := (Col5 + Col6)/2 16 17 dev_set_color ('red') 18 gen_region_line (RegionLines, Row1, Col1, Row2, Col2) 19 gen_arrow_contour_xld (Arrow1, Row1Half, Col1Half, Row1, Col1, 25, 15) 20 21 22 23 24 dev_set_color ('yellow') 25 gen_region_line (RegionLines, Row5, Col5, Row6, Col6) 26 gen_arrow_contour_xld (Arrow1, Row3Half, Col3Half, Row6, Col6, 25, 15) 27 28 29 angle_ll (Row5, Col5, Row6, Col6, Row2, Col2,Row1, Col1, Angle) 30 disp_message (200000, '黄色线转向红色线角度为:' + deg(Angle), 'window', 12, 12, 'black', 'true')
2.angle_lx(RowBegin,ColBegin,RowEnd,ColEnd,Angle)
RowBegin——直线起点行坐标
ColEnd——直线起点列坐标
RowEnd——直线终点行坐标
ColEnd——直线终点列坐标
angle_lx关于直线的方向和上文中ange_ll算子的直线方向规定相同,即直线的方向由起点指向终点,x轴的方向规定水平向右,angle_lx的角度范围也为-Pi~Pi,其中,当直线方向与x轴方向构成的夹角位于一二象限时,该直线与x轴的夹角角度范围在0~Pi,当直线方向与x轴方向构成的夹角位于三四象限时,该直线与x轴的夹角范围在-Pi~0,具体可结合下图理解。
结合Halcon示范如下:
1 set_display_font (200000, 26, 'mono', 'true', 'false') 2 Row1 := 100 3 Col1 := 100 4 Row2 := 500 5 Col2 := 600 6 Row1Half := (Row1 + Row2)/2 7 Col1Half := (Col1 + Col2)/2 8 9 dev_set_color ('red') 10 gen_region_line (RegionLines, Row1, Col1, Row2, Col2) 11 gen_arrow_contour_xld (Arrow1, Row1Half, Col1Half, Row1, Col1, 25, 15) 12 13 dev_set_color ('yellow') 14 gen_region_line (ROI_0, 350, 100, 350, 750) 15 XRowHalf := 350 16 XColHalf := (100 + 700)/2 17 gen_arrow_contour_xld (Arrow, XRowHalf, XColHalf,350, 750, 25, 15) 18 19 angle_lx (Row2, Col2, Row1, Col1, Angle) 20 disp_message (200000, '红色线角度为:' + deg(Angle), 'window', 12, 12, 'black', 'true') 21 disp_message (200000, 'X', 'Image', 345, 765, 'yellow', 'false')
1 set_display_font (200000, 26, 'mono', 'true', 'false') 2 Row1 := 100 3 Col1 := 100 4 Row2 := 500 5 Col2 := 600 6 Row1Half := (Row1 + Row2)/2 7 Col1Half := (Col1 + Col2)/2 8 9 dev_set_color ('red') 10 gen_region_line (RegionLines, Row1, Col1, Row2, Col2) 11 gen_arrow_contour_xld (Arrow1, Row1Half, Col1Half, Row2, Col2, 25, 15) 12 13 dev_set_color ('yellow') 14 gen_region_line (ROI_0, 350, 100, 350, 750) 15 XRowHalf := 350 16 XColHalf := (100 + 700)/2 17 gen_arrow_contour_xld (Arrow, XRowHalf, XColHalf,350, 750, 25, 15) 18 19 angle_lx (Row1, Col1,Row2, Col2, Angle) 20 disp_message (200000, '红色线角度为:' + deg(Angle), 'window', 12, 12, 'black', 'true') 21 disp_message (200000, 'X', 'Image', 345, 765, 'yellow', 'false')