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. 思路分析
解决这个问题的基础是饼干部分灰度值比较高,碎裂的部分灰度值低。
- 自动阈值分割
- 形态学处理
- 计算孔洞面积
- 计算矩形的形状因子
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