Halcon MLP多层神经网络的训练
程序员文章站
2023-08-24 17:52:15
Halcon MLP多层神经网络的训练...
上结果:
上训练原图:
贴代码:
dev_update_off ()
dev_close_window ()
dev_open_window (0, 0, 557, 416, 'black', WindowHandle)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
dev_set_draw ('margin')
*
* Initialization
ImageRootName := 'color/color_pieces_0'
Regions := ['yellow','pink','blue','background']
Highlight := ['goldenrod','magenta','cyan']
gen_empty_obj (Classes)
*
* Train and apply the MLP classifier
for Mode := 0 to 1 by 1
dev_set_color ('black')
* 读图片
read_image (Image, ImageRootName + '0')
*
* Simulate gray image
* 灰度图像处理
if (Mode == 1)
* rgb图像转灰度图像
rgb1_to_gray (Image, GrayImage)
* 将三张灰度图像转为三通道图像
compose3 (GrayImage, GrayImage, GrayImage, Image)
dev_display (Image)
disp_message (WindowHandle, 'Train and apply the classes again on gray images', 'window', 12, 12, 'black', 'false')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
endif
*
* Colored images
if (Mode == 0)
*
* Specify color classes
* 以下循环,得到不同颜色的矩形区域的列表
* 这些区域后续给MLP分类器训练用
for I := 1 to 4 by 1
dev_display (Image)
dev_display (Classes)
disp_message (WindowHandle, ['Drag rectangle inside ' + Regions[I - 1] + ' color','Click right mouse button to confirm'], 'window', 24, 12, 'black', 'false')
* 绘制一个平行于坐标轴的矩形
* 使用鼠标左键绘制矩形,绘制完毕后,单击右键确定绘制完毕
draw_rectangle1 (WindowHandle, Row1, Column1, Row2, Column2)
* 生成感兴趣矩形区域
gen_rectangle1 (Rectangle, Row1, Column1, Row2, Column2)
* 将上面生成的矩形区域加入列表中
concat_obj (Classes, Rectangle, Classes)
endfor
endif
*
* Train the specified color classes
* 下面的代码是:通过MLP分类器进行训练
* 但是不仅仅只是通过颜色这一个特征来进行分类
* 创建分类器
create_class_mlp (3, 7, 4, 'softmax', 'normalization', 3, 42, MLPHandle)
* 将样本从图像中添加到训练数据中
add_samples_image_class_mlp (Image, Classes, MLPHandle)
disp_message (WindowHandle, 'Training...', 'window', 100, 12, 'black', 'false')
* 训练
train_class_mlp (MLPHandle, 400, 0.5, 0.01, Error, ErrorLog)
*
* Use the trained MLP classifier to test if each image
* contains four game pieces of each color
* 下面的循环,使用训练的结果,对每一个图像的中的不同颜色对象进行分类,
* 并且计算各种不同颜色对象的个数
for J := 0 to 3 by 1
* 读图像
read_image (Image, ImageRootName + J)
* 模拟灰度图像处理
if (Mode == 1)
rgb1_to_gray (Image, GrayImage)
compose3 (GrayImage, GrayImage, GrayImage, Image)
endif
*
* Apply the trained classes
* 分类图像
classify_image_class_mlp (Image, ClassRegions, MLPHandle, 0.5)
dev_display (Image)
disp_message (WindowHandle, 'Looking for 4 game pieces of each color ...', 'window', 24, 12, 'black', 'false')
dev_set_line_width (2)
*
* Count the number of game pieces for each color class
* 计算每个颜色物件的个数
for Figure := 1 to 3 by 1
* 拷贝一个对象
copy_obj (ClassRegions, ObjectsSelected, Figure, 1)
* 计算连通域,得到联通域
connection (ObjectsSelected, ConnectedRegions)
* 特征直方图,使用面积特征选择区域
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 400, 99999)
* 计算区域个数
count_obj (SelectedRegions, Number)
dev_set_color (Highlight[Figure - 1])
dev_display (SelectedRegions)
OutString := Regions[Figure - 1] + ': ' + Number + ' '
dev_set_color ('green')
disp_message (WindowHandle, OutString, 'window', 24 + 30 * Figure, 12, 'black', 'false')
* 只有个数为4个,才会被标记显示为OK
if (Number != 4)
disp_message (WindowHandle, 'Not OK', 'window', 24 + 30 * Figure, 120, 'red', 'false')
else
disp_message (WindowHandle, 'OK', 'window', 24 + 30 * Figure, 120, 'green', 'false')
endif
endfor
if (J < 3)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
endif
endfor
endfor
dev_clear_window ()
dev_display (Image)
Message := 'The game pieces cannot be classified reliable on'
Message[1] := 'gray images because the gray values of the'
Message[2] := 'game pieces cannot always be distinguished from'
Message[3] := 'the gray values of the background.'
disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
本文地址:https://blog.csdn.net/ben_xiao_hai_123/article/details/107575406