HALCON示例程序classify_citrus_fruits.hdev应用常规gmm分类器进行水果分类

HALCON示例程序classify_citrus_fruits.hdev应用常规gmm分类器进行水果分类

示例程序源码(加注释)

  • 读入图片、显示与显示相关设置
    read_image (Image, ‘color/citrus_fruits_01’)
    get_image_pointer1 (Image, Pointer, Type, Width, Height)
    dev_close_window ()
    dev_open_window (0, 0, Width, Height, ‘white’, WindowHandle)
    set_display_font (WindowHandle, 12, ‘mono’, ‘true’, ‘false’)
    dev_set_draw (‘margin’)
    dev_set_line_width (2)
    dev_display (Image)
    dev_update_window (‘off’)
    dev_update_pc (‘off’)
    dev_update_var (‘off’)

  • 定义变量数组
    FeaturesArea := []
    FeaturesCircularity := []
    ClassName := [‘orange’,‘lemon’]

  • 创建一个GMM分类器,这个函数之前的帖子介绍过
    create_class_gmm (2, 2, 1, ‘spherical’, ‘normalization’, 10, 42, GMMHandle)

  • 为分类器增加例子
    for i := 1 to 4 by 1
    read_image (Image, ‘color/citrus_fruits_’ + i$’.2d’)
    dev_display (Image)

    • 自定义函数
      get_regions (Image, SelectedRegions)
      将彩色图像分割成RGB三个通道,并使用简单bolo方法进行分析
      decompose3 (Image, ImageRed, ImageGreen, ImageBlue)
      dev_set_color (‘white’)
      threshold (ImageRed, Region, 50, 255)
      fill_up (Region, RegionFillUp)
      connection (RegionFillUp, ConnectedRegions)
      select_shape (ConnectedRegions, SelectedRegions, ‘area’, ‘and’, 50, 999999)
      return ()
    • 显示SelectedRegions区域
      dev_display (SelectedRegions)
    • 对区域SelectedRegions进行计数
      count_obj (SelectedRegions, NumberObjects)
      for j := 1 to NumberObjects by 1
      • 选取数组当中的指定元素
        select_obj (SelectedRegions, ObjectSelected, j)
      • 自定义函数
        get_features (ObjectSelected, WindowHandle, Circularity, Area, RowRegionCenter, ColumnRegionCenter)
        得到ObjectSelected区域的圆度值
        circularity (ObjectSelected, Circularity)
        求取ObjectSelected的面积与中心坐标
        area_center (ObjectSelected, Area, Row, Column)
        dev_set_color (‘white’)
        return ()

        FeaturesArea := [FeaturesArea,Area]
        FeaturesCircularity := [FeaturesCircularity,Circularity]
      • real将输入数组元素转换为浮点数形式
        FeatureVector := real([Circularity,Area])
        if (i <= 2)
        • add_sample_class_gmm - 将训练样本添加到高斯混合模型的训练数据中
        • add_sample_class_gmm(:: GMM句柄,样本要素向量,类别ID,随机噪声:)
          add_sample_class_gmm (GMMHandle, FeatureVector, 0, 0)
        • 显示信息
          disp_message (WindowHandle, ‘Add to Class:’ + ClassName[0], ‘window’, RowRegionCenter, ColumnRegionCenter - 100, ‘black’, ‘true’)
          else
          add_sample_class_gmm (GMMHandle, FeatureVector, 1, 0)
          disp_message (WindowHandle, ‘Add to Class:’ + ClassName[1], ‘window’, RowRegionCenter, ColumnRegionCenter - 100, ‘black’, ‘true’)
          endif
          endfor
    • 显示按F5继续
      disp_continue_message (WindowHandle, ‘black’, ‘true’)
      stop ()
      endfor
      dev_clear_window ()
  • 构建可视化空间,即二维坐标系
    visualize_2D_feature_space (Cross, Height, Width, WindowHandle, FeaturesArea[0:5], FeaturesCircularity[0:5], ‘dim gray’, 18)
    dev_set_color (‘black’)
    OriginOfGraph := [Height - 0.1 * Height,0.1 * Width]
    画箭头
    disp_arrow (WindowID, OriginOfGraph[0], OriginOfGraph[1], OriginOfGraph[0], Width - 0.2 * Width, 2)
    disp_arrow (WindowID, OriginOfGraph[0], OriginOfGraph[1], 0.1 * Height, OriginOfGraph[1], 2)
    显示横纵坐标的名称
    set_tposition (WindowID, OriginOfGraph[0], Width - 0.2 * Width)
    write_string (WindowID, ‘Area’)
    set_tposition (WindowID, 0.07 * Height, OriginOfGraph[1])
    write_string (WindowID, ‘Circularity’)
    dev_set_color (ColorFeatureVector)
    ExtentOfGraph := Height - 0.3 * Height
    RangeC := 0.5
    RangeA := 24000
    ScaleC := ExtentOfGraph / RangeC
    ScaleA := ExtentOfGraph / RangeA
    MinC := 0.5
    MinA := 20000
    NumberFeatureVectors := |FeaturesA|
    for i := 0 to NumberFeatureVectors - 1 by 1
    DiffC := ScaleC * (FeaturesC[i] - MinC)
    DiffA := ScaleA * (FeaturesA[i] - MinA)
    RowFeature := OriginOfGraph[0] - DiffC
    ColumnFeature := OriginOfGraph[1] + DiffA
    画X号
    gen_cross_contour_xld (Cross, RowFeature, ColumnFeature, CrossSize, 0.785398)
    dev_display (Cross)
    endfor
    return ()

  • 柠檬的自定义函数
    visualize_2D_feature_space (Cross, Height, Width, WindowHandle, FeaturesArea[6:11], FeaturesCircularity[6:11], ‘light gray’, 18)
    disp_continue_message (WindowHandle, ‘black’, ‘true’)
    stop ()

  • 训练分类器train_class_gmm(:: 分类器句柄,最大迭代次数,阈值,计算方法,规范单一协方差矩阵:类中心数量,每个类的迭代次数)
    train_class_gmm (GMMHandle, 100, 0.001, ‘training’, 0.0001, Centers, Iter)

  • 分类
    for i := 1 to 15 by 1
    read_image (Image, ‘color/citrus_fruits_’ + i$’.2d’)
    dev_display (Image)

    • 和上文get_regions含义一致
      get_regions (Image, SelectedRegions)
      dev_display (SelectedRegions)
      count_obj (SelectedRegions, NumberObjects)
      for j := 1 to NumberObjects by 1
      select_obj (SelectedRegions, ObjectSelected, j)
      get_features (ObjectSelected, WindowHandle, Circularity, Area, RowRegionCenter, ColumnRegionCenter)
      FeaturesArea := [FeaturesArea,Area]
      FeaturesCircularity := [FeaturesCircularity,Circularity]
      FeatureVector := real([Circularity,Area])
      • classify_class_gmm - 通过高斯混合模型计算特征向量的类。
      • 函数原型classify_class_gmm(:: 分类器句柄,特征向量,确定的最佳类数:分类结果,类的后验概率,概率密度,归一化概率)
        classify_class_gmm (GMMHandle, FeatureVector, 1, ClassID, ClassProb, Density, KSigmaProb)
      • 显示
        disp_message (WindowHandle, 'Class: ’ + ClassName[ClassID], ‘window’, RowRegionCenter, ColumnRegionCenter - 100, ‘black’, ‘true’)
        disp_message (WindowHandle, 'KSigmaProb: ’ + KSigmaProb, ‘window’, RowRegionCenter + 30, ColumnRegionCenter - 100, ‘black’, ‘true’)
        endfor
        if (i != 15)
        disp_continue_message (WindowHandle, ‘black’, ‘true’)
        endif
        stop ()
        endfor
  • 清除训练句柄
    clear_class_gmm (GMMHandle)

处理思路

这个例子介绍了GMM分类器的使用方法,步骤是create_class_gmm 、add_sample_class_gmm、train_class_gmm、classify_class_gmm 先创建分类器,为分类器添加样本,训练分类器,使用分类器对进行分类。一个全面介绍GMM分类器使用的一个例子。通过GMM分类器实现了橙子和柠檬 的分类。

后记

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

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