当前位置: 首页 > 工具软件 > Citrus > 使用案例 >

Halcon水果分类的案例学习classify_citrus_fruits.hdev

谭兴学
2023-12-01

放了些个人理解的注释

* This example program shows how to apply a general GMM
* classification to distinguish citrus fruits using the
* features 'area' and 'circularity'. Additionally, the
* 2D feature space for the extracted fruits is visualized.
* 
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']
* 
* Create a GMM classifier
create_class_gmm (2, 2, 1, 'spherical', 'normalization', 10, 42, GMMHandle) 
             // 前面这俩2是    2个特征,分类数为2

* 
* Add training samples
for I := 1 to 4 by 1    //嵌套的FOR,每一张图片里FOR多个水果
    read_image (Image, 'color/citrus_fruits_' + I$'.2d')
    dev_display (Image)
    * 'Add Samples'
    get_regions (Image, SelectedRegions)//自定义函数,提取出水果
    
*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 ()
    
    dev_display (SelectedRegions)
    count_obj (SelectedRegions, NumberObjects) //水果数量
    for J := 1 to NumberObjects by 1
        select_obj (SelectedRegions, ObjectSelected, J)//选择第J个水果
        get_features (ObjectSelected, WindowHandle, Circularity, Area, RowRegionCenter, ColumnRegionCenter)
        
*!!!!!!!!!circularity (ObjectSelected, Circularity)//计算圆度
*!!!!!!!!!area_center (ObjectSelected, Area, Row, Column)//位置、面积
*dev_set_color ('white')
* 'Area: '+Area, Row-80, Column-100
* 'Circularity: '+Circularity, Row-50, Column-100
*return ()
        
        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 ()
* 
* Visualize the feature space
visualize_2D_feature_space (Cross, Height, Width, WindowHandle, FeaturesArea[0:5], FeaturesCircularity[0:5], 'dim gray', 18)    //能体现特征的图
* 'oranges', 40, 440
visualize_2D_feature_space (Cross, Height, Width, WindowHandle, FeaturesArea[6:11], FeaturesCircularity[6:11], 'light gray', 18)
* 'lemons', 70, 440
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* 
* Train the classifier
train_class_gmm (GMMHandle, 100, 0.001, 'training', 0.0001, Centers, Iter)//训练
* 
* Classify
for I := 1 to 15 by 1
    read_image (Image, 'color/citrus_fruits_' + I$'.2d')
    dev_display (Image)
    * 'Classify Image', 10, 10

    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 (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 the classifier from memory
clear_class_gmm (GMMHandle)//清除高斯混合模型

 

 类似资料: