HALCON示例程序classify_image_class_mlp.hdev如何使用MLP分类器分割RGB图像

HALCON示例程序classify_image_class_mlp.hdev如何使用MLP分类器分割RGB图像

示例程序源码(加注释)

关于显示相关显示在其他帖子有介绍。
dev_update_off ()
dev_close_window ()
dev_open_window (0, 0, 735, 485, ‘black’, WindowHandle)
set_display_font (WindowHandle, 14, ‘mono’, ‘true’, ‘false’)
dev_set_draw (‘margin’)
dev_set_colored (6)
dev_set_line_width (3)
read_image (Image, ‘patras’)
dev_display (Image)
Color := [‘indian red’,‘cornflower blue’,‘white’,‘black’,‘yellow’]

  • 创建分割不同类别的区域
    gen_rectangle1 (Sea, 10, 10, 120, 270)
    gen_rectangle2 (Deck, [170,400], [350,375], [-0.56,-0.75], [64,104], [26,11])
    union1 (Deck, Deck)
    gen_rectangle1 (Walls, 355, 623, 420, 702)
    gen_rectangle2 (Chimney, 286, 623, -0.56, 64, 33)
  • concat_obj介绍
    concat_obj (Sea, Deck, Classes)
    concat_obj (Classes, Walls, Classes)
    concat_obj (Classes, Chimney, Classes)
  • 显示
    dev_set_color (Color[0])
    dev_display (Deck)
    dev_set_color (Color[1])
    dev_display (Sea)
    dev_set_color (Color[2])
    dev_display (Walls)
    dev_set_color (Color[3])
    dev_display (Chimney)
    Message := ‘Training regions for the color classifier’
    disp_message (WindowHandle, Message, ‘window’, 12, 12, ‘black’, ‘true’)
    disp_continue_message (WindowHandle, ‘black’, ‘true’)
  • 程序停止运行,便于观察
    stop ()
  • 创建MLP分类器,并添加分类样本
  • create_class_mlp( : : 输入维度数量, 隐藏单位数, 输出类别数, 输出函数类型, 预处理类型, 预处理参数, 迭代次数 : MLP句柄)
    create_class_mlp (3, 3, 4, ‘softmax’, ‘principal_components’, 3, 42, MLPHandle)
  • 为MLP添加训练样本
  • add_samples_image_class_mlp(图像, 不同类的区域: : MLP句柄 : )
    add_samples_image_class_mlp (Image, Classes, MLPHandle)
    dev_display (Image)
    Message := ‘Training …’
    disp_message (WindowHandle, Message, ‘window’, 12, 12, ‘black’, ‘true’)
  • 训练分类器
  • train_class_mlp( : : MLP句柄, 最大迭代次数, 权重差异阈值, 误差差异阈值: 平均误差, 平均误差Log)
    train_class_mlp (MLPHandle, 200, 1, 0.01, Error, ErrorLog)
    Message := Message + ’ ready.’
    Message[1] := ‘Segment image using the classifier …’
    disp_message (WindowHandle, Message, ‘window’, 12, 12, ‘black’, ‘true’)
  • 分割图像
  • classify_image_class_mlp(图像 : 分割结果: MLP句柄, 拒绝分类区域阈值: )
    classify_image_class_mlp (Image, ClassRegions, MLPHandle, 0.5)
  • 使用区域的平均灰度值填充图像
    region_to_mean (ClassRegions, Image, ImageClass)
    dev_display (ImageClass)
    Message[1] := Message[1] + ’ ready.’
    disp_message (WindowHandle, Message, ‘window’, 12, 12, ‘black’, ‘true’)
  • 清除句柄
    clear_class_mlp (MLPHandle)
    disp_continue_message (WindowHandle, ‘black’, ‘true’)
    stop ()
  • 下边分割了另几种颜色和上文一样
    gen_rectangle2 (Rejection, [193,66,261], [235,332,328], [-0.32,-1.45,-1.51], [33,34,60], [4,3,3])
    union1 (Rejection, Rejection)
    concat_obj (Classes, Rejection, Classes)
    dev_display (Image)
    dev_set_color (Color[0])
    dev_display (Deck)
    dev_set_color (Color[1])
    dev_display (Sea)
    dev_set_color (Color[2])
    dev_display (Walls)
    dev_set_color (Color[3])
    dev_display (Chimney)
    dev_set_color (Color[4])
    dev_display (Rejection)
    disp_message (WindowHandle, ‘Add a rejection class to improve the robustness of the classifier’, ‘window’, 12, 12, ‘black’, ‘true’)
    disp_continue_message (WindowHandle, ‘black’, ‘true’)
    stop ()
    dev_display (Image)
  • Create the classifier and add the samples
    create_class_mlp (3, 4, 5, ‘softmax’, ‘principal_components’, 3, 42, MLPHandle)
    add_samples_image_class_mlp (Image, Classes, MLPHandle)
  • Train the classifier
    Message := ‘Training the classifier with rejection class…’
    disp_message (WindowHandle, Message, ‘window’, 12, 12, ‘black’, ‘true’)
    train_class_mlp (MLPHandle, 200, 1, 0.01, Error, ErrorLog)
    Message := Message + ’ ready.’
    Message[1] := ‘Segment image using the classifier …’
    disp_message (WindowHandle, Message, ‘window’, 12, 12, ‘black’, ‘true’)
  • Segment (classify) the image
    classify_image_class_mlp (Image, ClassRegionsNotRejected, MLPHandle, 0.5)
  • Select every class except the rejection class.
    copy_obj (ClassRegionsNotRejected, ClassRegionsNotRejected, 1, 4)
  • Note that the black areas in the mean image correspond to the rejected pixels
    region_to_mean (ClassRegionsNotRejected, Image, ImageClassNotRejected)
    dev_display (ImageClassNotRejected)
    Message[1] := Message[1] + ’ ready.’
    disp_message (WindowHandle, Message, ‘window’, 12, 12, ‘black’, ‘true’)
    clear_class_mlp (MLPHandle)

处理思路

这个例子介绍了使用MLP分类器对彩色图片进行分类的例子,选取多通道图片的感兴趣区域与背景可以对多通道图片进行快速分类。

后记

大家有什么问题可以向我提问哈,我看到了第一时间回复,希望在学习的路上多多结交良师益友。

你可能感兴趣的:(#,Halcon示例程序解读与剖析)