一、介绍
在进行颜色识别时,可以利用HSV不同分量具有不同特点,在H或者S通道进行颜色选择,使用过Blob分析:二值化,形态学,形状选择,但是对光照敏感,不稳定。这里另一种方法:MLP分类器的方式来识别颜色。
二、步骤
1、特征提取
*创建空对象,用以保存训练样本
gen_empty_obj(Class)
*绘制ROI选择要训练的颜色
draw_rectangle2 (WindowHandle, Row, Column, Phi, Length1, Length2)
gen_rectangle2 (Rectangle, Row, Column, Phi, Length1, Length2)
concat_obj (Class, Rectangle, Class)
2、创建分类器
create_class_mlp (3, 10, 5, 'softmax', 'normalization', 10, 42, MLPHandle)
3、添加样本
add_samples_image_class_mlp (Image, Class, MLPHandle)
4、训练样本
train_class_mlp (MLPHandle, 200, 0.01, 0.01, Error, ErrorLog)
5、识别
classify_image_class_mlp (Image, ClassRegions, MLPHandle, 0.5)
三、实例
打开用例
识别颜色,原始图像,进行学习
代码.
* This example demonstrates a completeness check of colored game
* pieces using MLP classification. The training and application
* of the classifier is first shown on colored images and then on
* gray images.
*
*此过程将dev_update_pc、dev_update_var和dev_update_window设置为“关闭”
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']
*创建一个空元组Classes
gen_empty_obj (Classes)
*
* 训练 并使用 MLP 分类器
for Mode := 0 to 1 by 1
dev_set_color ('black')
read_image (Image, ImageRootName + '0')
*
* Simulate gray image
if (Mode == 1)
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
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
* 创建多层感知器,3维特征向量维度,7层神经元的隐藏层,输出4类,softmax分类器(多项式分布),预处理设置为'normalization',使用主成分分析,特征向量所在的组件数3,随机数初始化种子是42*
create_class_mlp (3, 7, 4, 'softmax', 'normalization', 3, 42, MLPHandle)
*将图像中的训练样本添加到mlp模型的训练数据中,其中Classes是被训练的区域
add_samples_image_class_mlp (Image, Classes, MLPHandle)
disp_message (WindowHandle, 'Training...', 'window', 100, 12, 'black', 'false')
*训练mlp模型
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')
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')
识别效果
参考:
(机器视觉)Halcon下颜色识别与联合C#撸代码!1 | 码农家园
网格缺陷检测(MLP(多层神经网络))_create_class_mlp参数_w3071206219的博客-CSDN博客