****************************通过画线取点*****************************

read_image (Image, 'C:/Users/研发/Pictures/Saved Pictures/1.png')

*在窗口画一条线

draw_line(3600, Row1, Column1, Row2, Column2)

Row:= [Row1,Row2]

Col:=[Column1,Column2]

*保存至本地

write_tuple(Row, 'C:/Users/研发/Desktop/Row.tup')

write_tuple(Col,'C:/Users/研发/Desktop/Col.tup')



*****************通过点进一步定位取点并拟合直线**********************

read_image (Image, 'C:/Users/研发/Pictures/Saved Pictures/1.png')

get_image_size (Image, Width, Height)

dev_set_draw ('margin')

*从本地读取点坐标

read_tuple('C:/Users/研发/Desktop/Row.tup',Row)

read_tuple('C:/Users/研发/Desktop/Col.tup',Col)

*角度计算

angle_lx(Row[0],Col[0],Row[1],Col[1], Angle)

Angle:=Angle+rad(90)

*将输入的直线存成区域

gen_region_line(RegionLines,Row[0],Col[0],Row[1],Col[1])

*区域转轮廓

gen_contour_region_xld (RegionLines, Contours, 'border')

*获取轮廓上点数目

contour_point_num_xld (Contours, Length)

*获取轮廓点坐标

get_contour_xld (Contours, Row, Col)

gen_cross_contour_xld(Cross1, Row, Col, 6, 0.785398)

*轮廓点个数

tuple_length(Col, Length)

*定义在轮廓上取点个数

Num:=30

*保存指定数的点坐标

TupeRow:=[]

TupeCol:=[]

Index:=0

Temp:=Length/Num

*取点个数大于轮廓上点的个数

if(Temp<1)

    Temp:=1

    Num:=Length

endif

*提取部分点

for i:=1 to Length-1 by Temp

    TupeRow:=[TupeRow,Row[i]]

    TupeCol:=[TupeCol,Col[i]]

    Index:=Index+1

    if((Index+1)>Num)

        break

    endif

endfor

dev_set_color('green')

*显示提取的部分点

gen_cross_contour_xld(Cross1, TupeRow, TupeCol, 6, 0.785398)

*本文核心,获取拟合直线的点坐标

RowAfter:=[]

ColAfter:=[]

for Index:=0 to Num-1 by 1

    *获取测量句柄

    gen_measure_rectangle2( TupeRow[Index],TupeCol[Index],Angle, 20, 2, Width, Height, 'nearest_neighbor', MeasureHandle)

    *获取矩形

    gen_rectangle2(Rectangle,TupeRow[Index],TupeCol[Index],Angle, 20, 2)

    *提取直线边缘的垂直矩形或环形弧

    measure_pos (Image, MeasureHandle, 1, 100, 'all', 'first', RowEdge, ColumnEdge, Amplitude, Distance)

    *获取XLD交叉点

    gen_cross_contour_xld(Cross, RowEdge, ColumnEdge, 6, 0.785398)

    *将获取的点存储在元组中

    RowAfter[Index]:=RowEdge

    ColAfter[Index]:=ColumnEdge

endfor


*根据点画出轮廓

gen_contour_polygon_xld(Contour,RowAfter,ColAfter)

*将轮廓拟合成直线

fit_line_contour_xld(Contour, 'tukey', -1, 0, 5, 2, RowBegin, ColBegin, RowEnd, ColEnd, Nr, Nc, Dist)

gen_contour_polygon_xld (ContourD, [RowBegin,RowEnd], [ColBegin,ColEnd])

*根据点到线的距离,筛除一些偏差较大的点

*计算点到线的距离

distance_pl(RowAfter,ColAfter,RowBegin,ColBegin,RowEnd,ColEnd,Distance1)

RowSelect:=[]

ColSelect:=[]

for Index:=0 to Num-1 by 1

    if(Distance1[Index]<0.4)

        RowSelect[Index]:=RowAfter[Index] 

        ColSelect[Index]:=ColAfter[Index]

    endif

endfor

dev_set_color('green')

*筛选后的点再拟合直线

gen_contour_polygon_xld(Contour1,RowSelect,ColSelect)

fit_line_contour_xld(Contour1, 'tukey', -1, 0, 5, 2, RowBegin1, ColBegin1, RowEnd1, ColEnd1, Nr, Nc, Dist)

gen_contour_polygon_xld (ContourRet, [RowBegin1,RowEnd1], [ColBegin1,ColEnd1])