halcon计量模型学习2 单计量模型测量获得线段间距离(轮廓拟合效果较好)

本文介绍了如何在已有计量模型的基础上,通过Python实现对黑白图像中线段的精确测量,包括线段到线段距离和中点到直线距离的计算。通过步骤详细展示了从创建模型到获取距离值的全过程,适用于图像处理和测量应用。
摘要由CSDN通过智能技术生成

在上一篇基础上改进为一个计量模型上进行线段距离测量。
(上篇忘说,使用时在要测得的黑白临界线画两直线即可测得)
同一张测试图片,如上篇。
代码如下:

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

  • 2
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值