一、检测目的
对不同的金属零件进行MLP分类
二、代码示例
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_class_mlp (6, 5, 3, 'softmax', 'normalization', 3, 42, MLPHandle)
*nuts:螺母、washers:垫圈、retainers:夹持器
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)
binary_threshold (Image, Region, 'max_separability', 'dark', UsedThreshold)
connection (Region, ConnectedRegions)
fill_up (ConnectedRegions, Regions)
count_obj (Regions, Number)
for Index := 1 to Number by 1
select_obj (Regions, ObjectSelected, Index)
select_obj (ObjectSelected, SingleRegion, 1)
*区域的圆度(与圆相似)的形状因子
circularity (SingleRegion, Circularity)
*轮廓形状因子
roundness (SingleRegion, Distance, Sigma, Roundness, Sides)
*区域的几何矩
moments_region_central_invar (SingleRegion, PSI1, PSI2, PSI3, PSI4)
Features := [Circularity,Roundness,PSI1,PSI2,PSI3,PSI4]
add_sample_class_mlp (MLPHandle, Features, Classes[J])
disp_continue_message (WindowHandle, 'black', 'true')
endfor
endfor
train_class_mlp (MLPHandle, 200, 1, 0.01, Error, ErrorLog)
*清除多层感知器的训练数据
clear_samples_class_mlp (MLPHandle)
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')
binary_threshold (Image, Region, 'max_separability', 'dark', UsedThreshold)
connection (Region, ConnectedRegions)
fill_up (ConnectedRegions, Regions)
count_obj (Regions, Number1)
Classes := []
for Index1 := 1 to Number1 by 1
select_obj (Regions, ObjectSelected1, Index1)
select_obj (ObjectSelected1, SingleRegion, 1)
circularity (SingleRegion, Circularity)
roundness (SingleRegion, Distance, Sigma, Roundness, Sides)
moments_region_central_invar (SingleRegion, PSI1, PSI2, PSI3, PSI4)
Features := [Circularity,Roundness,PSI1,PSI2,PSI3,PSI4]
classify_class_mlp (MLPHandle, Features, 1, Class, Confidence)
Classes := [Classes,Class]
endfor
count_obj (Regions, Number2)
Colors := ['yellow','magenta','green']
for zf := 1 to Number2 by 1
select_obj (Regions, Region, zf)
dev_set_color (Colors[Classes[zf - 1]])
dev_display (Region)
endfor
if (J < 4)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
endif
endfor
stop ()
*
* Clear the classifier from memory
clear_class_mlp (MLPHandle)