Halcon视觉实战例程之边缘对距离测量measure_pairs

测量图中工件的水平边缘对距离,应用到的主要算子:measure_pairs

measure_pairs算子参数:图像、测量句柄、平滑参数(越大越平滑)、边缘幅度最小值、边缘类型(positive正边缘:由暗到亮、negative负边缘:由亮到暗)、选哪条边、第一个边缘中心行列坐标、第一条边边缘幅度、第二个边缘中心行列坐标、第二条边边缘幅度、一个边缘对的两条边之间的距离、两组边缘对的中心点距离。

上代码。

*测量边缘对 2020/04/01

*更新
dev_update_window('off')
dev_update_pc ('off')
dev_update_var ('off')
dev_close_window ()

*读入图片
read_image (MetalParts10, 'C:/metal-parts-10.png')
get_image_size (MetalParts10, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_open_window (Height + 70, 0, Width, 120, 'black', WindowHandleText)
dev_set_window (WindowHandle)
dev_display (MetalParts10)
threshold (MetalParts10, Regions, 115, 255)
connection (Regions, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 1836.92, 17069.9)
shape_trans (SelectedRegions, RegionTrans, 'rectangle2')
orientation_region (RegionTrans, Phi)
area_center (RegionTrans, Area, Row, Column)
vector_angle_to_rigid (Row, Column, Phi, Row, Column, rad(90), HomMat2D)
affine_trans_image (MetalParts10, ImageAffineTrans, HomMat2D, 'constant', 'false')
set_display_font (WindowHandle, 16, 'mono', 'false', 'false')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()

*形成矩形 
Row := 205
Column := 209
Length1 := 80
Length2 := 10
Angle := rad(0)
dev_set_draw ('margin')
dev_set_color ('red')
dev_set_line_width (2)
gen_rectangle2 (ROI, Row, Column, Angle, Length1, Length2)

*得到测量矩形
gen_measure_rectangle2 (Row, Column, Angle, Length1, Length2, Width, Height, 'bilinear', MeasureHandle)
dev_display (ROI)
stop ()

*测量
count_seconds (Seconds)
measure_pairs (ImageAffineTrans, MeasureHandle, 1, 50, 'all', 'all', RowEdgeFirst, ColumnEdgeFirst, AmplitudeFirst, RowEdgeSecond, ColumnEdgeSecond, AmplitudeSecond, IntraDistance, InterDistance)
count_seconds (Seconds1)
time:= (Seconds1-Seconds)*1000
avgPinWidth := sum(IntraDistance) / |IntraDistance|
disp_continue_message (WindowHandle, 'black', 'true')
stop ()

*显示
if(|InterDistance| > 0)
    for i := 0 to |RowEdgeFirst| - 1 by 1
       
        gen_contour_polygon_xld (EdgeFirst, [-sin(Angle + rad(90)) * Length2 + RowEdgeFirst[i],-sin(Angle - rad(90)) * Length2 + RowEdgeFirst[i]], [cos(Angle + rad(90)) * Length2 + ColumnEdgeFirst[i],cos(Angle - rad(90)) * Length2 + ColumnEdgeFirst[i]])
        gen_contour_polygon_xld (EdgeSecond, [-sin(Angle + rad(90)) * Length2 + RowEdgeSecond[i],-sin(Angle - rad(90)) * Length2 + RowEdgeSecond[i]], [cos(Angle + rad(90)) * Length2 + ColumnEdgeSecond[i],cos(Angle - rad(90)) * Length2 + ColumnEdgeSecond[i]])
       
        dev_set_color ('cyan')
        dev_display (EdgeFirst)
        dev_set_color ('magenta')
        dev_display (EdgeSecond)
        
        dev_set_color ('cyan')
        if(i=0)
            set_tposition (WindowHandle, RowEdgeFirst[i] +60, ColumnEdgeFirst[i] -50)
            write_string (WindowHandle, 'The first distance: '+IntraDistance[i]$'5.2f'+' pix')
        endif
        if(i=1)
            set_tposition (WindowHandle, RowEdgeFirst[i] + 20, ColumnEdgeFirst[i] + 20)
            write_string (WindowHandle, 'The second distance: '+IntraDistance[i]$'5.2f'+' pix')
        endif
        
    endfor
    set_display_font (WindowHandleText, 20, 'mono', 'true', 'false')
    disp_message (WindowHandleText, 'center distance: '+InterDistance$'5.2f'+' pix' , 'image', 20, 20, 'yellow', 'false')
    disp_message (WindowHandleText, 'average distance: '+avgPinWidth$'5.2f'+' pix' , 'image', 50, 20, 'green', 'false')
    disp_message (WindowHandleText, 'Measuring time: '+time$'5.2f'+' ms' , 'image', 80, 20, 'blue', 'false')
    
endif
disp_continue_message (WindowHandle, 'black', 'true')
stop ()

*关闭测量句柄
close_measure (MeasureHandle) 
dev_update_window ('on')
dev_clear_window ()

测量结果
在这里插入图片描述
原图
在这里插入图片描述

  • 4
    点赞
  • 19
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值