halcon例程解析:检查饼干的质量——check_hazelnut_wafers

This example demonstrates a quality inspection on hazelnut wavers. Using the morphology tools the waver is extracted and examined according to a few shape features like Rectangularity and AreaHoles. This program also shows the use of the operator area_holes.

这个例子展示了饼干的质量检验。利用形态学工具,根据矩形和面积孔等一些形状特征提取和检测抖动。这个程序还展示了算子area_holes的用法。

1. 效果展示

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

2. 思路分析

解决这个问题的基础是饼干部分灰度值比较高,碎裂的部分灰度值低。

  1. 自动阈值分割
  2. 形态学处理
  3. 计算孔洞面积
  4. 计算矩形的形状因子

2.1自动阈值分割

* 自动阈值分割
binary_threshold(Image, Foreground, 'smooth_histo', 'light', UsedThreshold)

2.2 形态学处理

* 开运算:消除小区域,平滑边界
opening_circle(Foreground, RegionOpening, 8.5)

2.3 计算孔洞面积

* 计算区域中孔洞的面积
area_holes(RegionOpening, AreaHoles)

2.4 计算矩形的形状因子

* 矩形区域的形状因子,矩形为1
rectangularity(RegionOpening, Rectangularity)

2.5 结果判断

if(AreaHoles > 300 or Rectangularity < 0.92)
    Text := 'Not OK'
else
    Text := 'OK'
endif

3. 全部代码

* This example demonstrates a quality inspection on hazelnut wavers.
* Using the morphology tools the waver is extracted and examined
* according to a few shape features like Rectangularity and AreaHoles.
* This program also shows the use of the operator area_holes.
* 
read_image (Image, 'food/hazelnut_wafer_01')
dev_close_window ()
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
dev_update_window ('off')
dev_set_line_width (3)
dev_set_draw ('margin')
set_display_font (WindowHandle, 20, 'mono', 'true', 'false')
* 
for Index := 1 to 24 by 1
    read_image (Image, 'food/hazelnut_wafer_' + Index$'.02')
    binary_threshold (Image, Foreground, 'smooth_histo', 'light', UsedThreshold)
    opening_circle (Foreground, FinalRegion, 8.5)
    area_holes (FinalRegion, AreaHoles)
    rectangularity (FinalRegion, Rectangularity)
    dev_display (Image)
    if (AreaHoles > 300 or Rectangularity < 0.92)
        dev_set_color ('red')
        Text := 'Not OK'
    else
        dev_set_color ('forest green')
        Text := 'OK'
    endif
    dev_display (FinalRegion)
    disp_message (WindowHandle, Text, 'window', 12, 12, '', 'false')
    if (Index < 24)
        disp_continue_message (WindowHandle, 'black', 'true')
        stop ()
    endif
endfor
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值