模板变形后如何进行缺陷检测

引言 机器视觉中缺陷检测分为一下几种:
blob分析+特征
模板匹配(定位)+差分
光度立体:halcon——缺陷检测常用方法总结(光度立体)
特征训练
测量拟合
频域+空间域结合:halcon——缺陷检测常用方法总结(频域空间域结合)
深度学习
本篇主要总结一下缺陷检测中的定位+差分的方法。即用形状匹配,局部变形匹配去定位然后用差异模型去检测缺陷。
模板匹配(定位)+差分 整体思路(形状匹配):
先定位模板区域后,求得模板区域的坐标,创建物品的形状模板create_shape_model,注意把模板的旋转角度改为rad(0)和rad(360)。
匹配模板find_shape_model时,由于物品的缺陷使形状有局部的改变,所以要把minscore设置小一点,否则匹配不到模板。并求得匹配项的坐标。
关键的一步,将模板区域仿射变换到匹配成功的区域。由于差集运算是在相同的区域内作用的,所以必须把模板区域转换到匹配项的区域。
之后求差集,根据差集部分的面积判断该物品是否有缺陷。
模板匹配(定位)+差分的方法主要用来检测物品损坏,凸起,破洞,缺失,以及质量检测等。
halcon例程分析:
1.印刷质量缺陷检测(print_check.hdev)
该例程用到了差异模型,将一个或多个图像同一个理想图像做对比,去找到明显的不同。进而鉴定出有缺陷的物体。差异模型的优势是可以直接通过它们的灰度值做比较,并且通过差异图像,比较可以被空间地加权。
变化模型检测缺陷的整体思路:
create_variation_model —— 创建一个差异模型
get_variation_model —— 获得差异模型
train_variation_model —— 训练差异模型
prepare_variation_model —— 准备差异模型
compare_variation_model —— 比较模型与实例
clear_variation_model —— 清除差异模型
dev_update_off ()* 选择第1张图像创建形状模板read_image (image, 'pen/pen-01')get_image_size (image, width, height)dev_close_window ()dev_open_window (0, 0, width, height, 'black', windowhandle)set_display_font (windowhandle, 16, 'mono', 'true', 'false')dev_set_color ('red')dev_display (image)* 把我感兴趣的区域抠出来,原则上范围越小越好,因为这样创建模板时干扰会少很多threshold (image, region, 100, 255)fill_up (region, regionfillup)difference (regionfillup, region, regiondifference)shape_trans (regiondifference, regiontrans, 'convex')dilation_circle (regiontrans, regiondilation, 8.5)reduce_domain (image, regiondilation, imagereduced)inspect_shape_model (imagereduced, modelimages, modelregions, 1, 20)gen_contours_skeleton_xld (modelregions, model, 1, 'filter')* 获得抠图区域的中心,这是参考点area_center (regiondilation, area, rowref, columnref)* 创建形状模板create_shape_model (imagereduced, 5, rad(-10), rad(20), 'auto', 'none', 'use_polarity', 20, 10, shapemodelid)* 创建变化模型(用于和缺陷比较)create_variation_model (width, height, 'byte', 'standard', variationmodelid)* 文件夹中前15张图片是质量良好的,可以用来训练模板for i := 1 to 15 by 1read_image (image, 'pen/pen-' + i$'02d')* 先寻找模板的实例find_shape_model (image, shapemodelid, rad(-10), rad(20), 0.5, 1, 0.5, 'least_squares', 0, 0.9, row, column, angle, score)if (|score| == 1)* 使用仿射变换,将当前图像平移旋转到与模板图像重合,注意是当前图像转向模板图像vector_angle_to_rigid (row, column, angle, rowref, columnref, 0, hommat2d)affine_trans_image (image, imagetrans, hommat2d, 'constant', 'false')* 训练差异模型train_variation_model (imagetrans, variationmodelid)dev_display (imagetrans)dev_display (model)endifendfor* 获得差异模型get_variation_model (meanimage, varimage, variationmodelid)* 做检测之前可以先用下面这个算子对可变模型进行设参,这是一个经验值,需要调试者调整prepare_variation_model (variationmodelid, 20, 3)dev_set_draw ('margin')numimages := 30* 可变模板训练完成后,我们终于可以进入主题,马上对所有图像进行缺陷检测,思想就是差分for i := 1 to 30 by 1read_image (image, 'pen/pen-' + i$'02d')* 要注意做差分的两幅图像分辨率相同,当然也需要通过仿射变换把待检测的图像转到与模板图像重合* 先寻找模板的实例find_shape_model (image, shapemodelid, rad(-10), rad(20), 0.5, 1, 0.5, 'least_squares', 0, 0.9, row, column, angle, score)if (|score| == 1)* 使用仿射变换,将当前图像平移旋转到与模板图像重合,注意是当前图像转向模板图像vector_angle_to_rigid (row, column, angle, rowref, columnref, 0, hommat2d)affine_trans_image (image, imagetrans, hommat2d, 'constant', 'false')* 抠图reduce_domain (imagetrans, regiondilation, imagereduced)* 差分 (就是检查两幅图像相减,剩下的区域就是不同的地方了,与模板图像不同的地方就是缺陷)* 这里可不能用difference做差分啊,halcon为变形模板提供了专门的差分算子:compare_variation_modelcompare_variation_model (imagereduced, regiondiff, variationmodelid)connection (regiondiff, connectedregions)* 特征选择:用一些特征来判断这幅图像印刷是否有缺陷,这里使用面积* 其实可以考虑利用区域面积的大小来判断缺陷的严重程度,这里就不过多讨论了select_shape (connectedregions, regionserror, 'area', 'and', 20, 1000000)count_obj (regionserror, numerror)dev_clear_window ()dev_display (imagetrans)dev_set_color ('red')dev_display (regionserror)set_tposition (windowhandle, 20, 20)if (numerror == 0)dev_set_color ('green')write_string (windowhandle, 'clip ok')elsedev_set_color ('red')write_string (windowhandle, 'clip not ok')endifendifif (i 0)gen_warped_mesh_region (vectorfield, meshregion, 25)gen_region_contour_xld (deformedcontours, edgeregion, 'margin')dilation_circle (edgeregion, regiondilation, 2 * 25)intersection (regiondilation, meshregion, regionintersection)dev_set_line_width (1)dev_set_color ('yellow')dev_display (image)dev_display (regionintersection)found[index] := |score|dev_set_line_width (2)dev_set_color ('green')dev_display (deformedcontours)* 7.注意:这里显示的是修正过的图像 dev_display (imagerectified)endif  待检测图像:
局部变形匹配定位:
由于局部变形模板匹配在寻找到图像(find_local_deformable_model)后是自动矫正的,因此我们可以省掉仿射变换的步骤了。
通过差异模型差分得到缺陷工件
*6.差分compare_variation_model (imagerectified, region, variationmodelid)connection (region, connectedregions)*缺陷提取(特征选择,即面积大于60的定义为缺陷)select_shape (connectedregions, selectedregions, 'area', 'and', 60, 99999)count_obj (selectedregions, number)*显示if(number>0) disp_message (windowhandle, 'ng', 'image', 12, 12, 'red', 'false')else disp_message (windowhandle, 'ok', 'window', 12, 12, 'magenta', 'false')endif dev_set_color ('red')dev_display (selectedregions)stop()endfordev_update_on ()
相关算子分析:
create_local_deformable_model(创建局部变形匹配模板)
create_local_deformable_model(template , numlevels, anglestart, angleextent, anglestep, scalermin, scalermax, scalerstep, scalecmin,scalecmax, scalecstep, optimization, metric, contrast, mincontrast, paramname, paramvalue ,modelid)参数列表:template //输入多通道图像,用来创建modelnumlevels //金字塔层数:'auto', 0,1,2,3,。。。anglestart //输入起始角度(默认-0.39)angleextent//角度旋转的范围(默认0.79)anglestep //旋转的步长,即分辨率,默认’auto'scalermin//行方向的最小缩放比例,默认1.0,通常大于0小于1scalermax//行方向的最大缩放比例,默认1.0,通常大于1小于1.5scalerstep//行方向的缩放步长,可影响行方向的分辨率,默认'auto', 0.01,0.02,0.05,。。scalecmin//scalecmax// 列方向,同上scalecstep//optimization//生成模型时的优化方式,默认'none'可选,'auto','point_reduction_xxx'metric//比较时候的标准,默认'use_polarity'使用极坐标系进行比较contrast//在模板图片的滤波或者磁滞滤波中,控制结果的对比度,默认'auto', 10, 20....mincontrast//在搜寻对象过程中的最小对比度,默认'auto', 1, 2, 3, 5....paramname// 普通参数名字(不太清楚用途,后续研究)默认[], 'min_size','part_size'paramvalue//参数值, 默认[], 可选'small', 'medium', big'modelid// 输出的模型handle  
变形检测用来检测一个对象是否局部变形,这个检测模型在保持严格的基础上允许一些细小的变形,和find_shape_model(在图中找到相应图形的最佳匹配)不同,create_local_deformable_model更加智能化,它可以预估变形程度并且修正图像中物体的位置(物体相对于图像的相对位置),它可以处理更大的变形。
get_deformable_model_contours(得到局部变形模板的轮廓)
get_deformable_model_contours( modelcontours , modelid, level )//level决定了返回第几层金字塔图像的边缘  
find_local_deformable_model (在待匹配图像中寻找变形模板)
find_local_deformable_model(image ,imagerectified, vectorfield, deformedcontours : modelid, anglestart, angleextent, scalermin, scalermax, scalecmin, scalecmax, minscore, nummatches, maxoverlap, numlevels, greediness, resulttype, paramname, paramvalue : score, row, column)参数列表:image //输入待匹配图像imagerectified //输出匹配到的变形矫正后模板图像vectorfield//变形矢量区deformedcontours //匹配到的轮廓,不是矫正后的轮廓modelid//模型句柄anglestart//起始角度angleextent//角度范围scalermin//行缩放最小比例scalermax//行缩放最大比例scalecmin//scalecmax//列同上minscore//最小相似度nummatches//需要几个最佳匹配结果maxoverlap//最大重叠数numlevels//金字塔层数greediness//贪婪度(范围0-1,0表示精确缓慢,1表示迅速精度不高)resulttype//输出的结果类型([], 'deformed_contours', 'image_rectified', 'vector_field')paramname//参数名称score//输出匹配度row, column//输出行列坐标 这个函数的功能是在一张图片中找到变形模型中的最佳匹配结果(可以是多个,由nummatches输入),模型必须在之前的步骤中使用(create_local_deformable_model或者read_deformable_model)生成。这个函数会返回找到的结果的行,列坐标(row, column)作为输出。
另外函数在找到匹配的模型后,输出矫正后的图像(imagerectified),向量区域(vectorfield)和匹配到的轮廓(deformedcontours) 通过参数resulttype去选择需要返回哪个(默认[],都不返回)。
paramname可以调整参数的设置:
deformation_smoothness:平滑的度,对于变形越大参数越大
expand_border:扩大imagerecfified vectorfield 区域
gen_warped_mesh(生成变形网格,封装函数)

一图读懂英特尔云原生开源技术
华为在法国共申请 215 项专利
一套室内导航系统要多少钱?室内导航系统价格构成要素及参考报价
陶瓷电容和电解电容相比有哪些优势呢?
良品率仅为5%,小米MIX两个多月只产10万台,难怪抢不到
模板变形后如何进行缺陷检测
多家房产经纪机构遭约谈,政策高压之下,电销机器人或能提供解决方案
加密货币公司Dash宣布Dash核心v0.14.0软件升级已正式在主网上线
使用焊接机器人时会容易出现哪些问题
苹果耳机新专利:汽车驾驶与VR体验的结合
儒卓力在中国举办电源研讨会系列,重点介绍可增强电池性能的创新混合能源存储系统
电路设计之干扰问题的总结与详细分析
印度最大太阳能发电场建成并投产 装机容量达2245兆瓦
iPhone8、iPhone7S、iPhone7SPlus最新消息汇总:有理有据12大信息爆料,苹果iPhone8即将发布值得期待
湖北电信推出室内数字化分布系统Massive MIMO解决方案
电磁继电器的工作原理是电流的磁效应吗
Google Play Games Beta版带来进一步的更新
关于区块链的正确认识
如何正确的存放LED显示屏?
三星Note7一手评测:这颜值,没谁了