前言:
==========================================================
分类器相对于深度学习来讲是一种古老传统的图片处理技术。halcon中常见的有四类分类器:
MLP(多层神经网络neural Nets)
SVM(支持向量机)
K-NN(K-最邻近)
GMM(高斯混合类型)
分类器的应用领域主要是下面这些:
image segmentation 图像分割
object recognition 对象识别
quality control 质量控制
novelty detection 缺陷检测
optical character recognition(OCR) 光学字符识别
勇哥第一次见到分类器的视觉项目是锂电池的极片缺陷检测,效果还不错。
这两年深度学习火起来后,发现深度学习完成上面所说的领域的应用更容易,效果也更好。
但深度学习对硬件要求太高,你把IPC加装个一百多W的显卡很多时候是不现实的。
如果你用cpu来跑,会发现速度乎快乎慢,cpu全部内核会100%被占用。
分类器相对于深度学习来讲不吃硬件,所以相对来讲算是轻量级的应用。
==========================================================
上篇例子虽然讲了“新奇检测”,但是用的2D数据做的演示,更偏重于可视化原理演示。
http://www.skcircle.com/?id=1645
本篇是实际应用,把新奇检测用于网格缺陷检测。
这个网格检测的例子,已经被MLP, GMM都应用过了,现在轮到SVM来试一把了。
MLP的例子:http://www.skcircle.com/?id=1488
GMM的例子:http://www.skcircle.com/?id=1630
不过在MLP中,没有所谓“新奇检测”,而是称为“拒绝采样策略”。
识别为OK的。
识别为ng的
无论是GMM还是SVM的新奇检测,因为这个例子的NG实在是太明显、太大、而且背景专一,所以才看上去效果不错。
现实中,缺陷背景复杂多变,缺陷形态完全随机、大小颜色各有不同,难度可比这个例子大多了。
*这个示例程序向您展示了如何使用SVM分类器的新奇检测模式来执行网络缺陷检查任务。 *新奇检测基本上将训练样本中不包含的特征向量分类为一个新类(因此得名)。 *对于网格检测任务,支持向量机可以用来检测与训练好的物体的纹理不一致的纹理。 * dev_update_off () read_image (Image, 'plastic_mesh/plastic_mesh_01') get_image_size (Image, Width, Height) dev_close_window () dev_open_window (0, 0, Width, Height, 'black', WindowHandle) dev_set_color ('red') set_display_font (WindowHandle, 16, 'mono', 'true', 'false') *用于分类的纹理过滤器将返回图像边界处的伪影,因为要检查的塑料网格的图像不包含整数个网格单元。 *由于这会导致图像边界错误检测,因此必须将图像边界附近的区域排除在训练和分类之外。 *这是通过以下矩形完成的。请注意,该图像稍后将按两倍的比例缩小 gen_rectangle1 (Rectangle, 10, 10, Height / 2 - 11, Width / 2 - 11) * Create the SVM classifier with the novelty detection mode. create_class_svm (5, 'rbf', 0.01, 0.0005, 1, 'novelty-detection', 'normalization', 5, SVMHandle) * The training is based on five images that contain no errors. for J := 1 to 5 by 1 read_image (Image, 'plastic_mesh/plastic_mesh_' + J$'02') * 由于网格的分辨率非常高,图像被缩小。这节省了大量的处理时间。 zoom_image_factor (Image, ImageZoomed, 0.5, 0.5, 'constant') dev_display (ImageZoomed) disp_message (WindowHandle, 'Adding training samples...', 'window', 12, 12, 'black', 'true') * 利用texture_laws算子处理后的图片. gen_texture_image (ImageZoomed, ImageTexture) * 添加采样到svm. add_samples_image_class_svm (ImageTexture, Rectangle, SVMHandle) endfor dev_display (ImageZoomed) disp_message (WindowHandle, 'Training SVM...', 'window', 12, 12, 'black', 'true') * 训练支持向量机。这会产生相当多的支持向量. train_class_svm (SVMHandle, 0.001, 'default') * 为了提高分类速度,减少支持向量的个数. reduce_class_svm (SVMHandle, 'bottom_up', 2, 0.001, SVMHandleReduced) * 现在检测塑料网格中的错误. dev_set_draw ('margin') dev_set_line_width (3) for J := 1 to 14 by 1 read_image (Image, 'plastic_mesh/plastic_mesh_' + J$'02') zoom_image_factor (Image, ImageZoomed, 0.5, 0.5, 'constant') dev_display (ImageZoomed) dev_set_color ('white') dev_display (Rectangle) gen_texture_image (ImageZoomed, ImageTexture) reduce_domain (ImageTexture, Rectangle, ImageTextureReduced) * 利用支持向量机进行新颖性检测. classify_image_class_svm (ImageTextureReduced, Errors, SVMHandleReduced) * 对返回的原始错误进行后处理,以删除检测到的错误中不重要的部分. opening_circle (Errors, ErrorsOpening, 3.5) closing_circle (ErrorsOpening, ErrorsClosing, 10.5) connection (ErrorsClosing, ErrorsConnected) select_shape (ErrorsConnected, FinalErrors, 'area', 'and', 300, 1000000) count_obj (FinalErrors, NumErrors) dev_set_color ('red') dev_display (FinalErrors) if (NumErrors > 0) disp_message (WindowHandle, 'Mesh not OK', 'window', 12, 12, 'red', 'true') else disp_message (WindowHandle, 'Mesh OK', 'window', 12, 12, 'forest green', 'true') endif if (J < 14) disp_continue_message (WindowHandle, 'black', 'true') endif stop () endfor
gen_texture_image函数的代码:
* 纹理图像是一个五通道的图像,它包含了应用五种不同的滤波法则 *(基本上对应于一阶导数和二阶导数)并对它们进行充分平滑的结果 texture_laws (Image, ImageEL, 'el', 5, 5) texture_laws (Image, ImageLE, 'le', 5, 5) texture_laws (Image, ImageES, 'es', 1, 5) texture_laws (Image, ImageSE, 'se', 1, 5) texture_laws (Image, ImageEE, 'ee', 2, 5) compose5 (ImageEL, ImageLE, ImageES, ImageSE, ImageEE, ImageLaws) smooth_image (ImageLaws, ImageTexture, 'gauss', 5) return ()
---------------------
作者:hackpig
来源:www.skcircle.com
版权声明:本文为博主原创文章,转载请附上博文链接!

