QQ登录

只需一步,快速开始

GMM分类器识别水果种类实例

[ 复制链接 ]

GMM分类器识别水果种类实例

GMM分类器识别水果种类实例

* 例程序展示如何应用一个通用的GMM分类器,再配合属性area,circularity来区分柑橘类水果
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')
    decompose3 (Image, ImageRed, ImageGreen, ImageBlue)
threshold (ImageRed, Region, 50, 255)
fill_up (Region, RegionFillUp)
connection (RegionFillUp, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 50, 999999)  
    count_obj (SelectedRegions, NumberObjects)
    for J := 1 to NumberObjects by 1
        select_obj (SelectedRegions, ObjectSelected, J)
        circularity (ObjectSelected, Circularity)
        area_center (ObjectSelected, Area, RowRegionCenter, ColumnRegionCenter)
        FeaturesArea := [FeaturesArea,Area]
        FeaturesCircularity := [FeaturesCircularity,Circularity]
        FeatureVector := real([Circularity,Area])
        if (I <= 2)
            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
    disp_continue_message (WindowHandle, 'black', 'true')
    stop ()
endfor
dev_clear_window ()
train_class_gmm (GMMHandle, 100, 0.001, 'training', 0.0001, Centers, Iter)
stop ()
*执行分类
for I := 1 to 15 by 1
    read_image (Image, 'color/citrus_fruits_' + I$'.2d')
  dev_display (Image)
    decompose3 (Image, ImageRed, ImageGreen, ImageBlue)
threshold (ImageRed, Region, 50, 255)
fill_up (Region, RegionFillUp)
connection (RegionFillUp, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 50, 999999)  
    count_obj (SelectedRegions, NumberObjects)
    for J := 1 to NumberObjects by 1
        select_obj (SelectedRegions, ObjectSelected, J)
        circularity (ObjectSelected, Circularity)
        area_center (ObjectSelected, Area, RowRegionCenter, ColumnRegionCenter)
        FeaturesArea := [FeaturesArea,Area]
        FeaturesCircularity := [FeaturesCircularity,Circularity]
        FeatureVector := real([Circularity,Area])
        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)

  

halcon从自学到接项目视频教程,另外再赠送全网最全资源  

  

欢迎围观我录制的一套halcon自学视频教程(进入)



回复

使用道具 举报

快速回复 返回列表 客服中心 搜索