HALCON示例程序color_pieces.hdev通过MLP训练器对彩色棋子进行分类识别

HALCON示例程序color_pieces.hdev通过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’)
  • 初始化
    ImageRootName := ‘color/color_pieces_0’
    Regions := [‘yellow’,‘pink’,‘blue’,‘background’]
    Highlight := [‘goldenrod’,‘magenta’,‘cyan’]
    gen_empty_obj (Classes)
    for Mode := 0 to 1 by 1
    dev_set_color (‘black’)
    read_image (Image, ImageRootName + ‘0’)
    • 生成灰度三通道图像
      if (Mode == 1)
      • rgb1_to_gray - 将RGB图像转换为灰度图像。
      • rgb1_to_gray(RGB图:灰度图 ::);转换公式:灰色= 0.299 *R+ 0.587 *G+ 0.114 *B。
        rgb1_to_gray (Image, GrayImage)
      • compose3 - 将3个图像转换为三通道图像。
      • compose3(图1,图2,图3:多通道图像 ::)
        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
        if (Mode == 0)
      • 指定颜色类
        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解释的贴子注意和union1的区别
        concat_obj (Classes, Rectangle, Classes)
        endfor
        endif
    • 创建MLP分类器并添加训练样本;关于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)
      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
      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)
      for Figure := 1 to 3 by 1
      * copy_obj - 复制HALCON数据库中的图标对象。
      * copy_obj(要复制对象:复制出的对象,开始索引号,对象数量:)
      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’)

处理思路

这个例子是将三通道的RGB图像使用MLP分类器进行分类,分别对灰度图像与彩色图像进行了训练与识别,对比发现还是彩色图像分类较为准确,因为灰度图像不能很好地分割出棋子与背景。

后记

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

你可能感兴趣的:(#,Halcon示例程序解读与剖析,halcon,MLP,分类,彩色,分割,灰度,分割)