一、缺陷检测概述
缺陷检测是机器视觉重要的应用方向之一,由于在制造产品的过程中,表面缺陷的产生往往是不可避免的,故机器视觉的缺陷检测有较大的市场需求。熟练掌握缺陷检测是视觉工程师的必要技能。 在工业视觉检测当中,常见的工业视觉检测表面缺陷有划伤、划痕、辊印、凹坑、粗糙、波纹等外观缺陷,此外还有像一些非金属产品表面的夹杂、破损、污点,以及纸张表面的色差、压痕等。 相比于人工检测,基于机器视觉的检测有如下优点: ①能24小时不间断工作 ②检测速度快,准确率高 ③检测精度高 ④不受外界因素的干扰,检测结果稳定 ⑤非接触性检测
二、缺陷检测方法
个人总结如下: ①基于颜色的Blob法+差分 ②模板匹配+差分 ③基于轮廓的边缘提取 ④频域+空间结合 ⑤机器学习的方法 ⑥光度立体法
三、模板匹配+差分法
主要检测物品损坏,凸起,破洞,缺失等。先定位模板区域后,求得模板区域的坐标,创建物品的形状模板create_shape_model,注意把模板的旋转角度改为rad(0)和rad(360)。匹配模板find_shape_model时,由于物品的缺陷使形状有局部的改变,所以要把MinScore设置小一点,否则匹配不到模板。并求得匹配项的坐标。关键的一步,将模板区域仿射变换到匹配成功的区域。由于差集运算是在相同的区域内作用的,所以必须把模板区域转换到匹配项的区域。之后求差集,根据差集部分的面积判断该物品是否有缺陷。 图片集链接:https://pan.baidu.com/s/1x1dCrW17GM0d7v9DZPpQZA 提取码:df5u 这是完整形状的模板: 目标检测204这三个字符可能出现的缺陷 检测结果如下,蓝色区域代表的是缺陷: Halcon代码如下:
*CSDN->三元荧
*读入标准模板图片(图片名字为standard.bmp)
read_image (Image, 'D:/halcon用到的照片/standard.bmp')
*设置图像窗口
get_image_size (Image, Width, Height)
dev_close_window ()
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_display (Image)
*选中模板区域
gen_rectangle1 (ROI_0, 201, 244, 385, 526)
reduce_domain (Image, ROI_0, ImageReduced)
*选中模板区域数字
threshold (ImageReduced, Regions, 106, 255)
connection (Regions, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 163.12, 10000)
union1 (SelectedRegions, RegionUnion)
*求模板区域坐标
area_center (RegionUnion, Areastandard, Rowstandard, Columnstandard)
*扣取模板区域图像,创建模板需要的是图像,不是区域
reduce_domain (ImageReduced, RegionUnion, ImageReduced1)
*创建模板
create_shape_model (ImageReduced1, 'auto', rad(0), rad(360), 'auto', 'auto', 'use_polarity', 'auto', 'auto', ModelID)
*读入图像路径,循环遍历每个图像
list_files ('D:/halcon用到的照片/204', ['files','follow_links'], ImageFiles)
tuple_regexp_select (ImageFiles, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles)
for Index := 0 to |ImageFiles| - 1 by 1
read_image (Image, ImageFiles[Index])
*模板匹配
find_shape_model (Image, ModelID, 0, rad(360), 0.5, 1, 0.5, 'least_squares', 0, 0.9, Row, Column, Angle, Score)
*判断匹配是否成功
if (|Score| > 0)
*搜寻当前图像匹配成功的区域并求它的坐标和角度
dev_display_shape_matching_results (ModelID, 'red', Row, Column, Angle, 1, 1, 0)
*将模板区域仿射变换到匹配成功的区域
vector_angle_to_rigid (Rowstandard, Columnstandard, 0, Row, Column, Angle, HomMat2D)
affine_trans_region (RegionUnion, RegionAffineTrans, HomMat2D, 'nearest_neighbor')
dev_display (Image)
*blob方法提取当前图像的数字,并变成一个联通域
threshold (Image, Regions1, 106, 255)
connection (Regions1, ConnectedRegions1)
select_shape (ConnectedRegions1, SelectedRegions1, 'area', 'and', 163.12, 10000)
union1 (SelectedRegions1, RegionUnion1)
*仿射变换后的模板区域与当前区域求差集,即缺陷部分
difference (RegionAffineTrans, RegionUnion1, RegionDifference)
*开运算去除很小点的噪声
opening_circle (RegionDifference, RegionOpening, 3)
*求缺陷的面积
area_center (RegionOpening, Area, Row1, Column1)
*缺陷面积大于阈值,说明有缺陷
if(Area>100)
*设置缺陷区域的颜色
dev_set_color ('blue')
*显示缺陷区域
dev_display (RegionOpening)
*显示NG
set_display_font (WindowHandle, 50, 'mono', 'true', 'false')
disp_message (WindowHandle,'NG', 'window', 15, 40, 'yellow', 'false')
*缺陷面积小于阈值,说明没缺陷
else
*显示OK
set_display_font (WindowHandle, 50, 'mono', 'true', 'false')
disp_message (WindowHandle,'OK', 'window', 15, 40, 'green', 'false')
endif
*没有匹配到模板,直接NG
else
set_display_font (WindowHandle, 50, 'mono', 'true', 'false')
disp_message (WindowHandle,'NG', 'window', 15, 40, 'yellow', 'false')
endif
endfor
四、Blob+差分法
Halcon官网自带挺多例子,这里以Blob分析的fin.hdev为例。 算子binary_threshold(Image : Region : Method, LightDark : UsedThreshold)使用二进制阈值分割图像,主要看LightDark参数。如果LightDark=‘light’,所有灰度值大于或等于的像素都被选中;如果LightDark=‘dark’,则选择灰度值小于的所有像素。比如fin.hdev目的提取背景区域,而背景偏向白色,故LightDark设为’light’即将背景区域提取出来。 完整区域的图像: ![在这里插入图片描述](https://img-blog.csdnimg.cn/20201223131718279.png?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl80NTYxNzMyMw==,size_16,color_FFFFFF,t_70)
* fin.hdev: Detection of a fin
*
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_color ('red')
dev_display (FinRegion)
area_center (FinRegion, FinArea, Row, Column)
if (I |