此例程主要是通过训练几幅图像,用来归类,然后通过输入图片,检测图片中目标对象的类别。
红色部分为笔记
* This example program shows how to classify different
* metal parts using a general MLP classification
*
dev_update_off ()
dev_close_window ()
dev_open_window (0, 0, 640, 480, 'black', WindowHandle)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_set_colored (6)
dev_set_draw ('margin')
dev_set_line_width (3)
*
* Create an MLP classifier
create_class_mlp (6, 5, 3, 'softmax', 'normalization', 3, 42, MLPHandle)
*
* Add training samples to the classifier
FileNames := ['nuts_01','nuts_02','nuts_03','washers_01','washers_02','washers_03','retainers_01','retainers_02','retainers_03']
Classes := [0,0,0,1,1,1,2,2,2]
for J := 0 to |FileNames| - 1 by 1
read_image (Image, 'rings/' + FileNames[J])
dev_display (Image)
dev_set_colored (6)
segment (Image, Objects)
dev_display (Objects)
dev_set_color ('black')
disp_message (WindowHandle, 'Add Sample ' + J + ', Class Index ' + Classes[J], 'window', 10, 10, 'black', 'true')
add_samples (Objects, MLPHandle, Classes[J])
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
endfor
*
* Train the classifier with the training samples
dev_clear_window ()
dev_set_color ('black')
disp_message (WindowHandle, 'Training...', 'window', 10, 10, 'black', 'true')
train_class_mlp (MLPHandle, 200, 1, 0.01, Error, ErrorLog)
clear_samples_class_mlp (MLPHandle)
disp_message (WindowHandle, 'Training... completed', 'window', 10, 10, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
*
* Classify objects
dev_set_draw ('fill')
for J := 1 to 4 by 1
read_image (Image, 'rings/mixed_' + J$'02d')
dev_display (Image)
dev_set_color ('black')
dev_set_draw ('margin')
disp_message (WindowHandle, 'Classifiy Image' + J, 'window', 10, 10, 'black', 'true')
segment (Image, Objects)
classify (Objects, MLPHandle, Classes)
disp_obj_class (Objects, Classes)
if (J < 4)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
endif
endfor
stop ()
*
* Clear the classifier from memory
clear_class_mlp (MLPHandle)
* Create an MLP classifier 创建用于分类或回归的多层感知器。参数如下 create_class_mlp (6, 5, 3, 'softmax', 'normalization', 3, 42, MLPHandle)
*NumInput:MLP输入变量的数量
*NumHidden:MLP隐藏单位数
*NumOutput:MLP输出变量数
*OutputFunction:输出层中的激活函数的类型
*PreProcessing:用于转换特征向量的预处理类型
*NumComponents:转换特征的数量
*RandSeed:随机数生成器的种子值,用于使用随机值初始化MLP
*segment()是一个自编程序,代码如下
*binary_threshold (Image, Region, 'max_separability', 'dark', UsedThreshold)二进制阈值分割
*'max_separability'最大分离性,还有一个参数可选'smooth_histo'
*connection (Region, ConnectedRegions)
*fill_up (ConnectedRegions, Regions)
*return ()
*add_samples()是一个自编程序,代码如下
*count_obj (Regions, Number)
*for J := 1 to Number by 1
*select_obj (Regions, Region, J)
*get_features (Region, Features)其中此函数中有一个roundness()来自轮廓的形状因素(输出:1距中心的平均距离2距离的标准偏差3圆度4多边形边数)
*另一个函数moments_region_central_invar()计算在平移和一般线性变换下不变的矩
*add_sample_class_mlp (MLPHandle, Features, Class)将训练样本添加到多层感知器的训练数据中
*endfor
*return ()
*MaxIterations:优化算法的最大迭代次数
*WeightTolerance:优化算法的两次迭代之间MLP的权重差异的阈值
*ErrorTolerance:在优化算法的两次迭代之间MLP对训练数据的平均误差的差异的阈值
classify (Objects, MLPHandle, Classes)
*在训练器中选出目标对象的分类结果