2D算法 Halcon 测量液体线高度

本案例通过测量矩形测量液位线的位置来判断液体是装多了还是装少了。(测量矩形使用形状模板匹配定位跟随测量)

整体思路:

以瓶底为模板进行模板匹配
设定标准液线,高液线,低液线(瓶内液体在该范围内判定合格)
将测量矩形移动到测量位置进行测量
显示
dev_get_window (WindowHandle)
set_display_font (WindowHandle, 15, ‘mono’, ‘true’, ‘false’)
read_image (Image, ‘ampoules/ampoules_01’)

  • 创建模板
    get_image_size (Image, Width, Height)
    gen_rectangle1 (ModelRegion, 264, 54, 321, 100)
    reduce_domain (Image, ModelRegion, TemplateImage)
    create_shape_model (TemplateImage, 3, rad(-5), rad(10), ‘auto’, [‘none’,‘no_pregeneration’], ‘use_polarity’, [25,54,4], 4, ModelID)
    get_shape_model_contours (ModelContours, ModelID, 1)
    NumImages := 8
    for Index := 1 to NumImages by 1
    read_image (Image, ‘ampoules/ampoules_’ + Index$‘.2d’)
    * 寻找实例
    find_shape_model (Image, ModelID, rad(-5), rad(10), 0.7, 0, 0.5, ‘least_squares’, [3,1], 0.75, Row, Column, Angle, Score)
    MeanRows:=mean(Row)
    Length1:=52
    Length2:=20
    gen_measure_rectangle2 (0, 0, rad(90), Length1, Length2,Width, Height, ‘nearest_neighbor’, MeasureHandle)
    * 设置两条参考线
    MeasureRow:=MeanRows-180
    standard:=120//标准液线
    offset:=20//允许液线偏移量
    RefLineHigh:=standard-offset//高液线
    RefLineLow:=standard+offset//低液线
    dev_set_color (‘cyan’)
    dev_set_line_width (1)
    set_line_style (WindowHandle, 10)
    gen_contour_polygon_xld (ContourLineHigh, [RefLineHigh,RefLineHigh], [0,Width])
    gen_contour_polygon_xld (ContourLineLow, [RefLineLow,RefLineLow], [0,Width])
    gen_contour_polygon_xld (ContourStand, [standard,standard], [0,Width])
    dev_display (Image)
    dev_display (ContourStand)
    dev_display (ContourLineHigh)
    dev_display (ContourLineLow)
    for I := 0 to |Score| - 1 by 1
    * 将测量矩形移动到测量位置
    dev_set_line_width (3)
    set_line_style (WindowHandle, 0)
    * 转换度量对象
    translate_measure (MeasureHandle, MeasureRow, Column[I])
    measure_pos (Image, MeasureHandle, 2.6, 7, ‘all’, ‘all’, RowEdge, ColumnEdge, Amplitude, Distance)
    if(|RowEdge|>0)
    if(RowEdge<RefLineHigh)
    dev_set_color (‘red’)
    gen_contour_polygon_xld (Contour, [RowEdge,RowEdge], [ColumnEdge-24,ColumnEdge+24])
    dev_display (Contour)
    disp_message (WindowHandle, ‘超出’+(RefLineHigh-RowEdge), ‘image’, RowEdge, ColumnEdge-30, ‘red’, ‘false’)
    elseif(RowEdge>RefLineLow)
    dev_set_color (‘red’)
    gen_contour_polygon_xld (Contour, [RowEdge,RowEdge], [ColumnEdge-24,ColumnEdge+24])
    dev_display (Contour)
    disp_message (WindowHandle, ‘低出’+(RowEdge-RefLineLow), ‘image’, RowEdge, ColumnEdge-30, ‘red’, ‘false’)
    else
    dev_set_color (‘green’)
    gen_contour_polygon_xld (Contour, [RowEdge,RowEdge], [ColumnEdge-24,ColumnEdge+24])
    dev_display (Contour)
    endif
    endif
    endfor
    stop()
    endfor

在这里插入图片描述
在这里插入图片描述

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值