第一步 创建支持向量机分类器
create_class_svm (7, 'rbf', KernelParam, Nu, |ClassNames|, 'one-versus-one', 'principal_components', 5, SVMHandle)
第二步 添加样本到分类器里
for ClassNumber := 0 to |ClassNames| - 1 by 1
*列出目录下的所有文件
list_files (ReadPath + ClassNames[ClassNumber], 'files', Files)
*获取后缀为PNG的所有文件
Selection := regexp_select(Files,'.*[.]png')
for Index := 0 to |Selection| - 1 by 1
*读取图像
read_image (Image, Selection[Index])
dev_display (Image)
* 'Add Samples...', -1
*图像阈值,获取黑色区域
threshold (Image, Region, 0, 40)
* calculate_features_define (Region, Features)
calculate_features_define (Region, Features)
add_sample_class_svm (SVMHandle, Features, ClassNumber)
endfor
endfor
* add_samples_to_svm_define(ClassNames, SVMHandle, WindowHandle, ReadPath)
* 关闭窗口
dev_clear_window ()
*第三步 训练支持向量机分类器
disp_message (WindowHandle, 'Training...', 'window', -1, -1, 'black', 'true')
train_class_svm (SVMHandle, 0.001, 'default')
disp_message (WindowHandle, 'Training completed', 'window', -1, -1, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
*第四步 用SVM分类器进行分类
*读取图像
read_image (Image, Selection[Index])
*对图像阈值
threshold (Image, Region, 0, 40)
*计算区域的特征值
calculate_features_define (Region, Features)
*用SVM分类器进行分类
classify_class_svm (SVMHandle, Features, 1, Class)
第五步 显示和输出结果
dev_display (Region)
disp_message (WindowHandle, 'Classified as:' + ClassNames[Class], 'window', -1, -1, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')