【Halcon】Halcon颜色识别之classify_fuses_gmm_based_lut.hdev

【Halcon】Halcon颜色识别之classify_fuses_gmm_based_lut.hdev_第1张图片


* In this example five different color fuses are segmented with
* a look-up table classifier (LUT) based on a Gaussian Mixture
* Model (GMM).
* 
**把程序窗口、变量窗口、显示窗体变为off状态
dev_update_off ()
*关闭窗口
dev_close_window ()
*打开窗口
dev_open_window (0, 0, 800, 600, 'white', WindowHandle)
**显示的对象只有边缘线
dev_set_draw ('margin')
*图片路径
ImageRootName := 'color/color_fuses_0'
*数据类型数组
FuseTypes := [5,10,15,20,30]
*颜色数组
FuseColors := ['Orange','Red','Blue','Yellow','Green']
*颜色数组
FuseHighlight := ['orange','red','blue','goldenrod','forest green']
*数据数组
DisplayTextShift := [85,65,75,85,85]
*设置窗口颜色
dev_set_color ('white')
*设置线宽度
dev_set_line_width (2)
*读取图像
read_image (Image, ImageRootName+'0')
*显示图像
dev_display (Image)
* 
* Define ROIs for the training data of the classifier
*设置窗体内文字格式
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
*获取矩形区域——产生一个平行坐标轴的矩形
gen_rectangle1 (FuseOrange, 195, 90, 230, 120)
*显示图像
dev_display (FuseOrange)
*显示文字提示
disp_message (WindowHandle, 'Orange Fuse', 'image', 160, 90-65, 'black', 'true')
*获取矩形区域——产生一个平行坐标轴的矩形
gen_rectangle1 (FuseRed, 191, 280, 226, 310)
dev_display (FuseRed)
disp_message (WindowHandle, 'Red Fuse', 'image', 160, 280-55, 'black', 'true')
gen_rectangle1 (FuseBlue, 190, 470, 225, 500)
dev_display (FuseBlue)
disp_message (WindowHandle, 'Blue Fuse', 'image', 160, 470-60, 'black', 'true')
gen_rectangle1 (FuseYellow, 192, 672, 227, 702)
dev_display (FuseYellow)
disp_message (WindowHandle, 'Yellow Fuse', 'image', 160, 672-70, 'black', 'true')
gen_rectangle1 (FuseGreen, 197, 880, 232, 910)
dev_display (FuseGreen)
disp_message (WindowHandle, 'Green Fuse', 'image', 160, 880-65, 'black', 'true')
*功能:创建一个空的目标元组
gen_empty_obj (Classes)
*功能:连接两个目标元组的图标
concat_obj (FuseOrange, FuseRed, Classes)
concat_obj (Classes, FuseBlue, Classes)
concat_obj (Classes, FuseYellow, Classes)
concat_obj (Classes, FuseGreen, Classes)
disp_message (WindowHandle, 'ROIs for the training data', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* 
* Create the GMM classifier, add the samples, and train it
*功能:为分类创建一个高斯混合模型
create_class_gmm (3, 5, 1, 'full', 'none', 3, 42, GMMHandle)
*从图像中得到高斯混合模型的样本
add_samples_image_class_gmm (Image, Classes, GMMHandle, 0)
disp_message (WindowHandle, 'Training GMM classifier...', 'window', 48, 12, 'black', 'true')
*功能:训练一个高斯混合模型
train_class_gmm (GMMHandle, 100, 0.001, 'training', 0.001, Centers, Iter)
* 
* Create the GMM-based LUT classifier
disp_message (WindowHandle, 'Creating LUT classifier...', 'window', 84, 12, 'black', 'true')
*功能:为分类创建一个高斯混合模型(根据LUT模式)
create_class_lut_gmm (GMMHandle, ['bit_depth','rejection_threshold'], [6,0.03], ClassLUTHandle)
*功能:清除一个高斯混合模型
clear_class_gmm (GMMHandle)
* 
* Segment images with LUT classifier
for Img := 0 to 3 by 1
    read_image (Image, ImageRootName + Img)
    *开始计时
    count_seconds (T1)
    *根据lut模式分类图像
    classify_image_class_lut (Image, ClassRegions, ClassLUTHandle)
    *结束计时
    count_seconds (T2)
    *求取执行时间
    TimeToClassify := (T2-T1)*1000
    *显示图像
    dev_display (Image)
    *设置线宽
    dev_set_line_width (3)
    for Fuse := 1 to 5 by 1
        * 
        * Perform post-processing on returned classes
        *功能:复制一个HALCON数据库中对象的图标
        copy_obj (ClassRegions, ObjectsSelected, Fuse, 1)
        *功能:关闭一个圆形结构基础的一个区域
        closing_circle (ObjectsSelected, RegionClosing, 3.5)
        *合并所有选定像素触摸相互连通区
        connection (RegionClosing, ConnectedRegions)
        *选择区域面积(属性:'面积')在指定区间内
        select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 2500, 99999)
        *填补选择区域中空洞的部分
        fill_up (SelectedRegions, RegionFillUp)
        *求取区域中心
        area_center (RegionFillUp, Area, Row, Column)
        *根据指定的参数对区域的形状进行转换, 如转换成正方形等
        shape_trans (RegionFillUp, RegionTrans, 'convex')
        *指定显示颜色
        dev_set_color (FuseHighlight[Fuse-1])
        *显示图像
        dev_display (RegionTrans)
        * 
        *用来计算被识别出来的区域的个数
        count_obj (RegionTrans, Number)
        for j := 1 to Number by 1
            disp_message (WindowHandle, FuseColors[Fuse-1]+' '+FuseTypes[Fuse-1]+' A', 'image', Row[j-1]-10, Column[j-1]-DisplayTextShift[Fuse-1], FuseHighlight[Fuse-1], 'true')
        endfor
    endfor
    disp_message (WindowHandle, TimeToClassify$'.1f'+' ms', 'window', 12, 12, 'black', 'true')
    if (Img < 3)
        disp_continue_message (WindowHandle, 'black', 'true')
        stop ()
    endif
endfor
* 清除lut
clear_class_lut (ClassLUTHandle)
disp_message (WindowHandle, 'No more lines to execute', 'window', 50, 12, 'black', 'true')

【Halcon】Halcon颜色识别之classify_fuses_gmm_based_lut.hdev_第2张图片 【Halcon】Halcon颜色识别之classify_fuses_gmm_based_lut.hdev_第3张图片 【Halcon】Halcon颜色识别之classify_fuses_gmm_based_lut.hdev_第4张图片 【Halcon】Halcon颜色识别之classify_fuses_gmm_based_lut.hdev_第5张图片 【Halcon】Halcon颜色识别之classify_fuses_gmm_based_lut.hdev_第6张图片

你可能感兴趣的:(halcon)