halcon——缺陷检测常用方法总结(特征训练)
引言
机器视觉中缺陷检测分为一下几种:
- blob分析+特征
- 模板匹配(定位)+差分:halcon——缺陷检测常用方法总结(模板匹配(定位)+差分) - 唯有自己强大 - 博客园 (cnblogs.com)
- 光度立体:halcon——缺陷检测常用方法总结(光度立体) - 唯有自己强大 - 博客园 (cnblogs.com)
- 特征训练
- 测量拟合:halcon——缺陷检测常用方法总结(测量拟合) - 唯有自己强大 - 博客园 (cnblogs.com)
- 频域+空间域结合:halcon——缺陷检测常用方法总结(频域空间域结合) - 唯有自己强大 - 博客园 (cnblogs.com)
- 深度学习
本篇博文主要是对缺陷图像的纹理特征训练进行详细分析。
特征训练
在纹理中找瑕疵。基于高斯混合模型(GMM)分类器的纹理检查模型,适用于图像金字塔,可以分析纹理的多个频率范围。
要求:训练样本必须完美无瑕疵。
整体步骤:
- 创建模型create_texture_inspection_model或读取模型read_texture_inspection_model
- 添加训练样本add_texture_inspection_model_image
- 查看样本get_texture_inspection_model_image
- 训练模型train_texture_inspection_model
每层金字塔都会训练一个GMM模型,并确定该层的'novelty_threshold'(区分有无瑕疵的阈值)。
参数获取:get_texture_inspection_model_param
参数设定:set_texture_inspection_model_param
参数分析:'patch_normalization':'weber'对亮度鲁棒,‘none’需要亮度作为评判(默认)
'patch_rotational_robustness':'true'对旋转鲁棒,'false'需要旋转作为评判(默认)
'levels':设置具体的金字塔层参与训练,纹理越粗糙,则较低的金字塔层级越可省略。默认auto。
'sensitivity':灵敏度,影响'novelty_threshold'的计算结果。负值会导致更高的阈值,从而更少的发现缺陷。默认0。
'novelty_threshold',阈值,自动计算得到,若结果不理想,可以手动微调。
- 进行检测apply_texture_inspection_model
- 模型保存与释放write_texture_inspection_model
若模型不再需要,则释放clear_texture_inspection_model
halcon案例分析(apply_texture_inspection_model.hdev)
一,创建模型,添加训练样本(完好无损的图像)
TrainingImageIndices := [1,2]
TextureModelFilename := 'texture_model_carpet'
dev_open_window_fit_size (0, 0, Width, Height, -1, -1, WindowHandle1)
dev_display (Image)
*创建模型
create_texture_inspection_model ('basic', TextureInspectionModel)
for Index := 0 to |TrainingImageIndices| - 1 by 1
read_image (Image, 'carpet/carpet_' + TrainingImageIndices[Index]$'02')
dev_display (Image)
Message := '添加图片 ' + (Index + 1) + ' of ' + |TrainingImageIndices| + '训练准备'
dev_disp_text (Message, 'window', 12, 12, 'black', [], [])
*加载训练样本(两张)
add_texture_inspection_model_image (Image, TextureInspectionModel, Indices)
endfor


二,初步设置参数后,开始训练
*参数设定'patch_normalization':'weber'对亮度鲁棒,‘none’需要亮度作为评判(默认)
set_texture_inspection_model_param (TextureInspectionModel, 'patch_normalization', 'weber')
Levels := [2,3,4]
* 'levels':设置具体的金字塔层参与训练,纹理越粗糙,则较低的金字塔层级越可省略。默认auto。
set_texture_inspection_model_param (TextureInspectionModel, 'levels', Levels)
* 开始训练
train_texture_inspection_model (TextureInspectionModel)
*查看样本参数'novelty_threshold',阈值,自动计算得到,若结果不理想,可以手动微调。
get_texture_inspection_model_param (TextureInspectionModel, 'novelty_threshold', NoveltyThreshold)
* 查看各个金字塔等级的新颖性得分图像和新颖性区域,可以把'gen_result_handle'设置为'true',
*之后get_texture_inspection_result_object读取'novelty_score_image'和'novelty_region'。
set_texture_inspection_model_param (TextureInspectionModel, 'gen_result_handle', 'true')

三,对缺陷图像初测试,显示测试结果
*设置窗口,用于显示各个金字塔层图像
WindowWidth := 320
WindowHeight := 280
dev_open_window (0, 0, WindowWidth, WindowHeight, 'black', WindowHandle1)
set_display_font (WindowHandle1, 16, 'mono', 'true', 'false')
dev_open_window (0, WindowWidth + 8, WindowWidth, WindowHeight, 'black', WindowHandle2)
set_display_font (WindowHandle2, 16, 'mono', 'true', 'false')
dev_open_window (0, 2 * WindowWidth + 16, WindowWidth, WindowHeight, 'black', WindowHandle3)
set_display_font (WindowHandle3, 16, 'mono', 'true', 'false')
dev_open_window (WindowHeight + 50, WindowWidth / 2 + 8, 2 * WindowWidth, 2 * WindowHeight, 'black', WindowHandle4)
set_display_font (WindowHandle4, 16, 'mono', 'true', 'false')
WindowHandles := [WindowHandle1,WindowHandle2,WindowHandle3]
** 检测第一张训练图像上的纹理缺陷以微调参数。
for Index := 1 to 3 by 1
ImageIndex := 5
read_image (TestImage, 'carpet/carpet_' + ImageIndex$'02')
*测试当前图像
apply_texture_inspection_model (TestImage, NoveltyRegion, TextureInspectionModel, TextureInspectionResultID)
* 检查调试信息。
*查看各个金字塔等级的新颖性得分图像(NovScoreImage)和新颖性区域(NovRegionL)
* 新颖性评分图像可用于单独微调新颖性阈值。
get_texture_inspection_result_object (NovScoreImage, TextureInspectionResultID, 'novelty_score_image')
get_texture_inspection_result_object (NovRegion, TextureInspectionResultID, 'novelty_region')
* 显示每层(金字塔)的结果
count_obj (NovScoreImage, Number)
for Level := 1 to Number by 1
CurrentWindow := WindowHandles[Level - 1]
dev_set_window (CurrentWindow)
dev_clear_window ()
select_obj (NovScoreImage, NovScoreImageL, Level)
select_obj (NovRegion, NovRegionL, Level)
get_image_size (NovScoreImageL, Width, Height)
dev_set_part (0, 0, Height - 1, Width - 1)
dev_display (NovScoreImageL)
Legend := 'Novelty region (level ' + Levels[Level - 1] + ')'
dev_set_color ('red')
dev_set_line_width (2)
*
dev_display (NovRegionL)
dev_disp_text (['Novelty score image (level ' + Levels[Level - 1] + ')','Novelty threshold: ' + NoveltyThreshold[Level - 1]$'.1f'], 'window', 12, 12, 'black', [], [])
dev_disp_text (Legend, 'window', WindowHeight - 30, 12, 'white', ['box_color','shadow'], ['black','false'])
endfor
*显示结果
dev_set_window (WindowHandle4)
dev_display (TestImage)
dev_set_line_width (2)
dev_set_color ('red')
dev_display (NoveltyRegion)
area_center (NoveltyRegion, Area, Row, Column)
if (Index < 3)
dev_disp_text ('Result', 'window', 12, 12, 'black', [], [])
else
dev_disp_text ('Final result', 'window', 12, 12, 'black', [], [])
endif

四,根据测试结果进行微调参数
* 新奇阈值的微调。
if (Index == 1)
Message[0] := '图像中有很多小错误.'
Message[1] := '可以通过改变 novelty thresholds的值来调整灵敏度(sensitivity—)'
Message[2] := '例如减少灵敏度参数的值'
dev_disp_text (Message, 'window', 12, 12, 'black', [], []) * 设置阈值计算的灵敏度。 负值导致更高的阈值,因此检测到的缺陷更少。
* 'sensitivity':灵敏度,影响'novelty_threshold'的计算结果。负值会导致更高的阈值,从而更少的发现缺陷。默认0。
set_texture_inspection_model_param (TextureInspectionModel, 'sensitivity', -10)
get_texture_inspection_model_param (TextureInspectionModel, 'novelty_threshold', NoveltyThreshold)
endif
if (Index == 2)
Message := '也可以通过直接操纵新颖性边界来单独调整单个级别的敏感度'
dev_disp_text (Message, 'window', 12, 12, 'black', [], [])
* 新奇阈值的微调。
*
* 从纹理中获取(自动确定的)新奇阈值
* 检查模型并将适当修改的值设置为新的新颖性阈值。
*
*如果我们明确设置新颖性边界,则忽略敏感性。
* 我们在这里将其重新设置为 0 以避免混淆
set_texture_inspection_model_param (TextureInspectionModel, 'sensitivity', 0)
*
Offset := [25,10,30]
get_texture_inspection_model_param (TextureInspectionModel, 'novelty_threshold', NoveltyThreshold)
set_texture_inspection_model_param (TextureInspectionModel, 'novelty_threshold', Offset + NoveltyThreshold)
get_texture_inspection_model_param (TextureInspectionModel, 'novelty_threshold', NoveltyThreshold)
endif
endfor for Level := 1 to |WindowHandles| by 1
dev_set_window (WindowHandles[Level - 1])
dev_clear_window ()
endfor
dev_set_window (WindowHandle4)
dev_clear_window ()


五,至此,模型准备完毕,将全部图像进行缺陷检测并显示
*检测所有测试图像上的纹理缺陷。
NumImages := 7
for Index := 1 to NumImages by 1
read_image (TestImage, 'carpet/carpet_' + Index$'02')
*
*检测当前图像
apply_texture_inspection_model (TestImage, NoveltyRegion, TextureInspectionModel, TextureInspectionResultID)
*得到新颖性图像和区域
get_texture_inspection_result_object (NovScoreImage, TextureInspectionResultID, 'novelty_score_image')
get_texture_inspection_result_object (NovRegion, TextureInspectionResultID, 'novelty_region')
* 显示单个金字塔层数的结果
count_obj (NovScoreImage, Number)
for Level := 1 to Number by 1
CurrentWindow := WindowHandles[Level - 1]
dev_set_window (CurrentWindow)
dev_clear_window ()
select_obj (NovScoreImage, NovScoreImageL, Level)
select_obj (NovRegion, NovRegionL, Level)
get_image_size (NovScoreImageL, Width, Height)
dev_set_part (0, 0, Height - 1, Width - 1)
dev_display (NovScoreImageL)
Legend := 'Novelty region (level ' + Levels[Level - 1] + ')'
dev_set_color ('red')
dev_set_line_width (2)
*
dev_display (NovRegionL)
dev_disp_text (['Novelty score image (level ' + Levels[Level - 1] + ')','Novelty threshold: ' + NoveltyThreshold[Level - 1]$'.1f'], 'window', 12, 12, 'black', [], [])
dev_disp_text (Legend, 'window', WindowHeight - 50, 12, ['red','white'], ['box_color','shadow'], ['black','false'])
endfor
* 显示结果
dev_set_window (WindowHandle4)
dev_display (TestImage)
dev_set_line_width (2)
dev_set_color ('red')
dev_display (NoveltyRegion)
area_center (NoveltyRegion, Area, Row, Column)
if (Area > 100)
dev_disp_text ('Not OK', 'window', 12, 12, 'white', 'box_color', 'red')
else
dev_disp_text ('OK', 'window', 12, 12, 'white', 'box_color', 'forest green')
endif
if (Index < NumImages)
dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], [])
stop ()
endif
endfor


【术语解释】
- Patch:相邻像素的集合。
- Novelty Score:在测试过程中,将测试图像的纹理特征与纹理检查模型进行比较,并计算它们的'novelty score'。 该值越大,单个纹理特征越不适合纹理检查模型的可能性越大。
- Novelty Threshold:Novelty Score高于该阈值,则纹理有缺陷。
“ novelty_region”是通过组合不同金字塔等级的新颖性区域而生成的,即不同层级金字塔组成的交集区域。如果只有单层金字塔,那么该层的新颖性区域直接就是novelty_region。
若想查看各个金字塔等级的新颖性得分图像和新颖性区域,可以把'gen_result_handle'设置为'true',之后get_texture_inspection_result_object读取'novelty_score_image'和'novelty_region'。
参考博文:Halcon 纹理缺陷检测 apply_texture_inspection_model - 夕西行 - 博客园 (cnblogs.com)
halcon——缺陷检测常用方法总结(特征训练)的更多相关文章
- halcon——缺陷检测常用方法总结(光度立体)
引言 机器视觉中缺陷检测分为一下几种: blob+特征(官方示例surface_scratch.hdev) blob+差分+特征(官方示例pcb_inspection.hdev) 光度立体 特征训练 ...
- halcon——缺陷检测常用方法总结(模板匹配(定位)+差分)
引言 机器视觉中缺陷检测分为一下几种: blob分析+特征 模板匹配(定位)+差分 光度立体:halcon--缺陷检测常用方法总结(光度立体) - 唯有自己强大 - 博客园 (cnblogs.com) ...
- halcon——缺陷检测常用方法总结(测量拟合)
引言 机器视觉中缺陷检测分为一下几种: blob分析+特征 模板匹配(定位)+差分:halcon--缺陷检测常用方法总结(模板匹配(定位)+差分) - 唯有自己强大 - 博客园 (cnblogs.co ...
- halcon——缺陷检测常用方法总结(频域空间域结合)
摘要 缺陷检测是视觉需求中难度最大一类需求,主要是其稳定性和精度的保证.首先常见缺陷:凹凸.污点瑕疵.划痕.裂缝.探伤等. 缺陷检测算法不同于尺寸.二维码.OCR等算法.后者应用场景比较单一,基本都是 ...
- 图像处理笔记(二十):LAWS纹理滤波应用于缺陷检测
LAWS纹理滤波 texture_laws(Image, 原图像 ImageTexture, 输出值,滤波后图像 FilterType, 过滤器类型 Shift, 灰度值转换,滤波后的灰度值可能会比较 ...
- Opencv+Python实现缺陷检测
实验七.缺陷检测 一. 题目描述 对下面的图片进行缺陷检测操作,请详细地记录每一步操作的步骤. 第一站图片是标准样品,后面几张图中有几个样品有瑕疵,需要你通过计算在图片上显示出哪张是合格,哪张 ...
- NLP用CNN分类Mnist,提取出来的特征训练SVM及Keras的使用(demo)
用CNN分类Mnist http://www.bubuko.com/infodetail-777299.html /DeepLearning Tutorials/keras_usage 提取出来的特征 ...
- OpenCV特征点检测------Surf(特征点篇)
Surf(Speed Up Robust Feature) Surf算法的原理 ...
- 目标检测算法SSD之训练自己的数据集
目标检测算法SSD之训练自己的数据集 prerequesties 预备知识/前提条件 下载和配置了最新SSD代码 git clone https://github.com/weiliu89/caffe ...
随机推荐
- SpringBoot项目启动后自动打开浏览器
编写一个类,注册为Spring的Bean,然后实现CommandLineRunner接口,重写run()方法即可 @Component public class OpenBrowser impleme ...
- mongodb 在PHP中常见问题及解决方法
1.$in needs an array 解决:查询用到in操作的时候,说in操作对应的不是我一个数组,或者数组索引不是以0开始的 方法:array_values重新生成一个索引为0开始的数组即可 $ ...
- 制作一个简单的toast弹框
toast弹框的作用 toast弹框顾名思义,就是为了弹出一个提示框,效果如图: 使用toast弹框可以可用户带来更好的交互体验 toast弹框的使用 Toast组件 制做出toast的样式以及出现的 ...
- Java 进行时间处理
Java 进行时间处理 一.Calendar (1).Calender介绍 Calendar的中文翻译是日历,实际上,在历史上有着许多种计时的方法.所以为了计时的统一,必需指定一个日历的选择.那现在最 ...
- MSSQL·查询存储过程中的关键字
阅文时长 | 0.22分钟 字数统计 | 408字符 主要内容 | 1.引言&背景 2.声明与参考资料 『MSSQL·查询存储过程中的关键字』 编写人 | SCscHero 编写时间 | 20 ...
- 【Azure 云服务】Azure Cloud Service 创建 Alert 指南 [基于旧版 Alert(Classic)不可用情况下]
问题描述 在Azure云服务(Cloud Service)创建Alert(Classic)时候遇见失败消息:"Failed to update alert testclassicalertr ...
- ruby基础(二)
ruby语法基础 1.方法 方法时对象定义的与该对象相关的操作.在Ruby中,对象的所有的操作都被封装成 方法. 语法糖:语法糖是一种为了照顾一般人的习惯而产生的特殊语法. ruby中一切数据都是对象 ...
- 【Linux】Linux中在mate桌面和gnome桌面root自动登录设置
[Linux]Linux中在mate桌面和gnome桌面root自动登录设置 GLL_ 2020-03-05 11:41:40 762 收藏 1 分类专栏: Linux 文章标签: linux 版 ...
- VS 中的 lib 和 dll 的区别和使用
在 vs/c# 项目开发中,经常会遇到 lib 和 dll 文件,而且创建工程项目以及工程项目打包时也是必须要面对的,所以有必要掌握 lib 和 dll 的区别和使用. 静态库:在链接步骤中,连接器将 ...
- shell基础之编译安装nginx
本节新学知识:if 判断语句 1 #!/bin/bash 2 #检查环境 3 SESTATE=`getenforce` 4 if [ $SESTATE != "Disabled" ...