* This example program shows you how to use the novelty detection mode of
* the SVM classifier to perform a web inspection task. The novelty detection
* basically classifies feature vectors that are not contained in the training
* samples as a new class (hence the name). For the web inspection task,
* the SVM hence can be used to detect textures that do not correspond to the
* texture of the trained good objects.
* 这个示例程序向您展示了如何使用SVM分类器的新颖性检测模式来执行web检查任务。
新颖性检测基本上是将不包含在训练样本中的特征向量分类为一个新类(因此得名)。
因此,在web检测任务中,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')
* The texture filters used for the classification will return artifacts at the image
* borders because the images of the plastic mesh to be inspected do not
* contain an integer number of mesh cells. Because this would lead to wrongly
* detected errors at the image borders, we must exclude the area close to the
* image border from the training and classification. This is done with the following
* rectangle. Note that the image is later scaled down by a factor of two.
*用于分类的纹理滤波器会在图像边界处返回伪影,因为待检查的塑料网格图像不包含整数数量的网格单元。
这将导致在图像边界处被错误检测,所以我们必须从训练和分类中排除靠近图像边界的区域。
这是通过下面的矩形完成的。请注意,图像稍后将按两倍的比例缩小。
gen_rectangle1 (Rectangle, 10, 10, Height / 2 - 11, Width / 2 - 11)//获得矩形区域
* Create the SVM classifier with the novelty detection mode.创建svm模型
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.
*训练基于5张没有错误的图像。
for J := 1 to 5 by 1
read_image (Image, 'plastic_mesh/plastic_mesh_' + J$'02')
* The images are zoomed down because the resolution of the mesh is very
* high. This saves a large amount of processing time.
*图像被缩小了,因为网格的分辨率非常高。这节省了大量的处理时间。
zoom_image_factor (Image, ImageZoomed, 0.5, 0.5, 'constant')//缩放
dev_display (ImageZoomed)
disp_message (WindowHandle, 'Adding training samples...', 'window', 12, 12, 'black', 'true')
* Generate the texture image.产生纹理图像
gen_texture_image (ImageZoomed, ImageTexture)
* Add the samples to the classifier.添加训练样本
add_samples_image_class_svm (ImageTexture, Rectangle, SVMHandle)
endfor
dev_display (ImageZoomed)
disp_message (WindowHandle, 'Training SVM...', 'window', 12, 12, 'black', 'true')
* Train the SVM. This generates a fairly large amount of support vectors.
**训练SVM。这会生成相当多的支持向量。
train_class_svm (SVMHandle, 0.001, 'default')
* To increase the classification speed, reduce the number of support vectors.
*为了提高分类速度,可以减少支持向量的数量。
reduce_class_svm (SVMHandle, 'bottom_up', 2, 0.001, SVMHandleReduced)
* Now detect errors in the plastic meshes.检测错误
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)
*获得特定区域Region位置的图像,reduce_domain是缩小一个图像的定义域,不改变尺寸
reduce_domain (ImageTexture, Rectangle, ImageTextureReduced)
* Perform the novelty detection with the SVM.检测
classify_image_class_svm (ImageTextureReduced, Errors, SVMHandleReduced)
* Postprocess the returned raw errors to remove insignificant parts of the
* detected errors.对返回的原始错误进行后处理,以删除检测到的错误中无关紧要的部分。
*形态学处理
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 (ImageZoomed, ImageTexture):
* The texture image is a five-channel image that contains the result of applying
* five different Laws filters, which basically correspond to first and second
* derivatives, and smoothing them sufficiently.
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 ()
reduce_class_svm :
功能:用简化的支持向量机近似训练过的支持向量机,以便更快地分类。
reduce_class_svm( : : SVMHandle, Method, MinRemainingSV, MaxError : SVMHandleReduced)
Parameters:
1.SVMHandle (input_control) :
2.Method (input_control):
Default value: 'bottom_up'
List of values: 'bottom_up'
3.MinRemainingSV (input_control) :剩余SVs的最小数量
Default value: 2
Suggested values: 2, 3, 4, 5, 7, 10, 15, 20, 30, 50
Restriction: MinRemainingSV >= 2
4.MaxError (input_control) : 减少的最大允许误差
Default value: 0.001
Suggested values: 0.0001, 0.0002, 0.0005, 0.001, 0.002, 0.005, 0.01, 0.02, 0.05
Restriction: MaxError > 0.0
5.SVMHandleReduced (output_control) :
SVMHandle of reduced SVM.