在上一篇基础上改进为一个计量模型上进行线段距离测量。
(上篇忘说,使用时在要测得的黑白临界线画两直线即可测得)
同一张测试图片,如上篇。
代码如下:
dev_update_off ()
*Im1 Image1 Image2
read_image (Image,'C:/Users/连山人/Desktop/标定板图片/Image_01.bmp')
dev_close_window ()
get_image_size (Image,Width, Height)
dev_open_window_fit_image (Image, 0, 0, Width, Height, WindowHandle)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_set_color ('lime green')
dev_set_line_width (3)
dev_display (Image)
*------------------步骤2:画矩形得直线1----------------------
*创建计量模型需要的数据结构:MetroLineModel;创建的用于测量的数据结构
create_metrology_model (MetroLineModel1)
* 设置计量对象的图像大小(不是必须的,但可以提高第一次测量的运行时间)
set_metrology_model_image_size (MetroLineModel1, Width, Height)
**------------------步骤1:----------------------
*画一条线
draw_line (WindowHandle, LStartRow1, LStartColumn1, LEndRow1, LEndColumn1)
*画一条线
draw_line (WindowHandle, LStartRow2, LStartColumn2, LEndRow2, LEndColumn2)
Line1 := [LStartRow1, LStartColumn1, LEndRow1, LEndColumn1]
Line2 := [LStartRow2, LStartColumn2, LEndRow2, LEndColumn2]
add_metrology_object_generic (MetroLineModel1, 'line', [Line1,Line2], 7, 5, 1, 30, [], [], LineIndices)
apply_metrology_model (Image, MetroLineModel1)
*获得ContoursLine1(计量模型区域)
get_metrology_object_measures (ContoursLine1, MetroLineModel1, 'all', 'all', RowLine1, ColumnLine1)
*为每个输入点生成一个十字形状的XLD轮廓。
*(十字轮廓,行列坐标,长度,方向)
gen_cross_contour_xld (CrossLine1, RowLine1, ColumnLine1, 6, 0.785398)
* 查询测量对象的结果轮廓。
*(给定计量对象的结果轮廓,模型句柄,计量对象的索引,计量对象的实例,相邻轮廓点之间的距离)
get_metrology_object_result_contour (ContoursfittingLine, MetroLineModel1, 'all', 'all', 1.5)
* 获取每条测量线的起点和终点ParamLine1=='all_param'=((Rsta1,Csta1)(Rend1,Cwend1))
get_metrology_object_result (MetroLineModel1, LineIndices[0], 'all', 'result_type', 'all_param', PLine1)
get_metrology_object_result (MetroLineModel1, LineIndices[1], 'all', 'result_type', 'all_param', PLine2)
Row:=PLine1[0]+ PLine1[2]
Column:=PLine1[1]+ PLine1[3]
*重点到直线
distance_pl( Row/2, Column/2, PLine2[0],PLine2[1], PLine2[2],PLine2[3] ,Distance)
*线段到线段
distance_ss (PLine1[0],PLine1[1], PLine1[2],PLine1[3],PLine2[0],PLine2[1], PLine2[2],PLine2[3], DistanceMin, DistanceMax)
dev_close_window ()
dev_open_window_fit_image (Image, 0, 0, 640, 640, WindowHandle)
disp_obj (Image, WindowHandle)
dev_display (ContoursfittingLine)
dev_display (CrossLine1)
Message0 := '点到直线距离'+Distance
Message1 := '线段间最小距离'+DistanceMin
disp_message (WindowHandle, Message0+'\n'+Message1, 'window', -1, -1, 'red', 'true')
stop()
在这里,使用了两种方法获得距离值,用于比较值对不对。分别为线段与线段最小距离distance_ss;中点到直线的距离distance_pl