一、我们都知道,点到直线最短距离就是垂线。Halcon也有现成的算子可以计算点到直线的距离
distance_pl( : : Row, Column, Row1, Column1, Row2, Column2 : Distance)。但是此算子不会返回具体的垂点坐标。当我们希望显示垂线的时候是需要知道垂点坐标,才可以绘制该直线的。
二、计算垂线方程代码
dev_close_window ()
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
read_image (Image01, 'D:/01.png')
*画点
row_cross1:=287
col_cross1:=243
gen_cross_contour_xld (Cross, row_cross1 ,col_cross1 , 26, 0.785398)
*画直线
row1_Line1:=507
col1_Line1:=241
row2_Line1:=336
col2_Line1:=561
gen_region_line (RegionLines, row1_Line1, col1_Line1, row2_Line1, col2_Line1)
*计算直线与水平线的角度
angle_lx (row1_Line1, col1_Line1, row2_Line1, col2_Line1, Angle_Line1)
*计算与该直线垂直的直线的方程
angleH:=Angle_Line1
k:=tan(angleH)
b:=col_cross1-k*row_cross1
*任意给一个x坐标,计算y
xTest:=600
yTest:=k*xTest + b
*画垂线
gen_region_line (RegionLines1, row_cross1 ,col_cross1, xTest, yTest)
*计算直线与其垂线的夹角,验证是否是90度
angle_ll (row_cross1 ,col_cross1, xTest, yTest, row1_Line1, col1_Line1, row2_Line1, col2_Line1, Angle1)
*计算直线与垂线的交点
intersection_lines (row_cross1 ,col_cross1, xTest, yTest, row1_Line1, col1_Line1, row2_Line1, col2_Line1, Row, Column, IsOverlapping)
gen_cross_contour_xld (Cross1, Row, Column, 26, 0.785398)
*连接点与垂点
gen_region_line (RegionLines2, row_cross1 ,col_cross1, Row, Column)
*刷新显示
dev_set_line_width (5)
dev_display (Image01)
dev_set_color ('red')
dev_display (Cross)
dev_set_color ('green')
dev_display (Cross1)
dev_set_color ('blue')
dev_display (RegionLines2)
dev_set_color ('orange')
dev_display (RegionLines)