目录
1.2.1、检测饼干的完整性(check_hazelnut_wafers.hdev)
2.2.1、方案一:匹配灰度图片中的区域案例(exhaustive_match.hdev)
2.2.1、方案一:相关性匹配区域案例(find_ncc_model_exposure.hdev)
1、blob分析+特征
1.1、整体思路
- 采集图像,将图像灰度化
- 使用二值化进行图像分割
1.2、案例分析
1.2.1、检测饼干的完整性(check_hazelnut_wafers.hdev)
该例程用到了Blob分析+特征,将一个或多个图像进行初步处理,去找到明显的不同。进而鉴定出有缺陷的物体。
该例程检测缺陷的整体思路:
- read_image—— 读取待检测图片
- binary_threshold —— 获取自动化阈值分割后的区域
- opening_circle—— 进行开运算处理(腐蚀+膨胀;断开细小的地方,所以叫开),使孔洞相对平滑
- area_holes—— 计算孔洞面积
- rectangularity—— 计算区域的矩形度
- 根据面积或矩形度判断是否存在缺陷
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)
*这里判断存在空洞面积大于300或者矩形度小于0.92的为不合格
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
相关算子分析
- read_image—— 读取待检测图片
read_image( : Image : FileName : )
说明: 读取不同文件格式的图像。
参数:
Image:输出图像名称;
FileName:输入图像文件名称(适用多种图像文件格式);
- binary_threshold —— 获取二进制阈值分割后的区域
binary_threshold(Image : Region : Method, LightDark : UsedThreshold)
说明:获取二进制阈值分割后的区域
参数:
Image:需要进行阈值的图像
Region:处理后的区域
Method:分割方法('max_separability':最大限度的可分性, 'smooth_histo':直方图平滑)
LightDark:提取的是黑色部分还是白色部分
UsedThreshold:自动阈值使用的阈值值
- opening_circle(进行开运算处理,使孔洞相对平滑)
注:腐蚀+膨胀,断开细小的地方,所以叫开;膨胀+腐蚀;链接细小的地方,所以叫闭
opening_circle(Region : RegionOpening : Radius : )
说明:用于消除小区域(小于圆形结构元素)并平滑区域
参数表
Region(输入参数):要打开的区域
RegionOpening(输出参数):打开的区域
Radius(输入参数):圆形结构元素的半径
默认值:3.5
建议的值: 1.5, 2.5, 3.5, 4.5, 5.5, 7.5, 9.5, 12.5, 15.5, 19.5, 25.5, 33.5, 45.5, 60.5, 110.5
取值范围: (lin)0.5 ≤ Radius ≤ 511.5
最小增量:1.0
建议的增量:1.0
- area_holes(计算孔洞面积)
area_holes(Regions : : : Area)
说明:如果一个区域有多个孔,则为所有孔的面积之和返回
参数
Regions (输入参数):要检查的区域
Area(输出参数):区域的孔的面积
- rectangularity(计算区域的矩形度)
rectangularity(Regions: : : Rectangularity)
说明:
参数:
Regions(输入参数):要检查的区域。
Rectangularity(输出参数):输入区域的矩形度,值越接近于1,越接近完美矩形,否则偏离。
断言:0 <= Rectangularity && Rectangularity <= 1.0
1.2.2、检查产品毛刺缺陷 (fin.dhev)
该例程用到了Blob分析+特征+差分思想,将一个或多个图像进行初步处理,将处理后的区域与原始区域相减。进而鉴定出有缺陷的物体。
该例程检测缺陷的整体思路:
- read_image—— 读取待检测图片
- binary_threshold —— 获取二进制阈值分割后的区域
- closing_circle—— 闭运算处理(膨胀+腐蚀;链接细小的地方,所以叫闭)
- difference—— 区域相减,获取差异的图像区域(差分思想)
- opening_rectangle1 —— 开运算(腐蚀+膨胀)这里使用开运算主要是清除上面闭运算增加的部分毛点
* Blob+特征+差分
dev_update_window ('off')
read_image (Fins, 'fin' + [1:3])
get_image_size (Fins, Width, Height)
dev_close_window ()
dev_open_window (0, 0, Width[0], Height[0], 'black', WindowID)
set_display_font (WindowID, 14, 'mono', 'true', 'false')
for I := 1 to 3 by 1
select_obj (Fins, Fin, I)
dev_display (Fin)
*二进制阈值分割
binary_threshold (Fin, Background, 'max_separability', 'light', UsedThreshold)
dev_set_color ('blue')
dev_set_draw ('margin')
dev_set_line_width (4)
dev_display (Background)
disp_continue_message (WindowID, 'black', 'true')
stop ()
*闭运算,膨胀+腐蚀,
closing_circle (Background, ClosedBackground, 250)
dev_set_color ('green')
dev_display (ClosedBackground)
disp_continue_message (WindowID, 'black', 'true')
stop ()
*区域相减,获取差异的图像区域(差分思想)
difference (ClosedBackground, Background, RegionDifference)
*开运算,腐蚀+膨胀,这里使用开运算主要是清除上面闭运算增加的部分毛点
opening_rectangle1 (RegionDifference, FinRegion, 5, 5)
dev_display (Fin)
dev_set_