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)