Halcon分类器之高斯混合模型分类器

Halcon分类器示例自我理解
看了很多网上的例子,总有一种纸上得来终觉浅,绝知此事要躬行的感觉。说干就干,将Halcon自带分类器例子classify_metal_parts.hdev按照自己的理解重新写一遍,示例中的分类器是MLP(多层感知机),我将它改变为GMM(高斯混合模型)。希望可以帮助刚入门的同学学习理解,大神请绕路吧,当然也喜欢各位看官帮我找出不足之处,共同进步。谢谢!
分类效果如图:
在这里插入图片描述
下面是HDevelop代码,图片请自己寻找,是Halcon自带图片,*代码所用图片路径:C:/Users/Public/Documents/MVTec/HALCON-12.0/examples/images/rings
#====================================================#

dev_close_window ()
dev_open_window (0, 0, 640, 480, 'black', WindowHandle)
*创建分类器(高斯混合模型)(依据6个特征分三类,每个分类的类的中心)
create_class_gmm (6, 3, 2, 'spherical', 'normalization', 10, 42, GMMHandle)
*定义要分类的类别
Class:=[0,1,2]
*添加样本1
list_files ('C:/Users/yanzhenwu/Desktop/TestPic/1', ['files','follow_links'], ImageFiles)
tuple_regexp_select (ImageFiles, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles)
for Index := 0 to |ImageFiles| - 1 by 1
    read_image (Image, ImageFiles[Index])
    threshold (Image, Regions, 0,100)
    connection (Regions, ConnectedRegions)
    count_obj (ConnectedRegions, Number)
    for I := 1 to Number by 1
        Features:=[]
        select_obj (ConnectedRegions, ObjectSelected, I)
        *Region紧凑度
        roundness (ObjectSelected, Distance, Sigma, Roundness, Sides)
        *Region圆度
        circularity (ObjectSelected, Circularity)
        *图像高阶矩(可表示出Region的方向和大小特征,具体不是很懂)
        moments_region_central_invar (ObjectSelected, PSI1, PSI2, PSI3, PSI4)
        *将这些特征添加到元祖中,作为一个特征向量给训练用
        Features:=real([Roundness,Circularity,PSI1, PSI2, PSI3, PSI4])
        *添加训练样本:这里的Features一定要和创建的分类器中规定的特征维度一样
        add_sample_class_gmm (GMMHandle,  Features, Class[0], 0)
    endfor
    disp_message (3600, '添加样本一第'+int(Index+1)+'张图像', 'window', 12, 12, 'black', 'true')
    stop ()
endfor

*添加样本2
list_files ('C:/Users/yanzhenwu/Desktop/TestPic/2', ['files','follow_links'], ImageFiles)
tuple_regexp_select (ImageFiles, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles)
for Index := 0 to |ImageFiles| - 1 by 1
    read_image (Image, ImageFiles[Index])
    threshold (Image, Regions, 0,100)
    connection (Regions, ConnectedRegions)
    count_obj (ConnectedRegions, Number)
    for I := 1 to Number by 1
        Features:=[]
        select_obj (ConnectedRegions, ObjectSelected, I)
        roundness (ObjectSelected, Distance, Sigma, Roundness, Sides)
        circularity (ObjectSelected, Circularity)
        moments_region_central_invar (ObjectSelected, PSI1, PSI2, PSI3, PSI4)
        Features:=[Roundness,Circularity,PSI1, PSI2, PSI3, PSI4]
        add_sample_class_gmm (GMMHandle, Features, Class[1], 0)
    endfor
    disp_message (3600, '添加样本二第'+int(Index+1)+'张图像', 'window', 12, 12, 'black', 'true')
    stop ()
endfor

*添加样本3
list_files ('C:/Users/yanzhenwu/Desktop/TestPic/3', ['files','follow_links'], ImageFiles)
tuple_regexp_select (ImageFiles, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles)
for Index := 0 to |ImageFiles| - 1 by 1
    read_image (Image, ImageFiles[Index])
    threshold (Image, Regions, 0,100)
    connection (Regions, ConnectedRegions)
    count_obj (ConnectedRegions, Number)
    for I := 1 to Number by 1
        Features:=[]
        select_obj (ConnectedRegions, ObjectSelected, I)
        roundness (ObjectSelected, Distance, Sigma, Roundness, Sides)
        circularity (ObjectSelected, Circularity)
        moments_region_central_invar (ObjectSelected, PSI1, PSI2, PSI3, PSI4)
        Features:=[Roundness,Circularity,PSI1, PSI2, PSI3, PSI4]
        add_sample_class_gmm (GMMHandle, Features, Class[2], 0)
    endfor
    disp_message (3600, '添加样本三第'+int(Index+1)+'张图像', 'window', 12, 12, 'black', 'true')
    stop ()
endfor
*训练
train_class_gmm (GMMHandle, 100, 0.001, 'training', 0.0001, Centers, Iter)
*这里注意:清理的是样本添加句柄,而不是分类句柄
clear_samples_class_gmm (GMMHandle)
*分类样本
list_files ('C:/Users/yanzhenwu/Desktop/TestPic/test', ['files','follow_links'], ImageFiles)
tuple_regexp_select (ImageFiles, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles)
for Index := 0 to |ImageFiles| - 1 by 1
    read_image (Image, ImageFiles[Index])
    threshold (Image, Regions, 0,100)
    connection (Regions, ConnectedRegions)
    count_obj (ConnectedRegions, Number)
    for I := 1 to Number by 1
        Features:=[]
        select_obj (ConnectedRegions, ObjectSelected, I)
        area_center (ObjectSelected, Area, Row, Column)
        roundness (ObjectSelected, Distance, Sigma, Roundness, Sides)
        circularity (ObjectSelected, Circularity)
        moments_region_central_invar (ObjectSelected, PSI1, PSI2, PSI3, PSI4)
        Features:=[Roundness,Circularity,PSI1, PSI2, PSI3, PSI4]
        *这里注意,当前选的的对象的类别设置为1,ClassID则给出预先定义的Class类别中的一个,否则,给出不唯一达不到分类效果
        classify_class_gmm (GMMHandle, Features, 1, ClassID, ClassProb, Density, KSigmaProb)
        if(ClassID==Class[0])
            dev_set_color ('red')
            dev_display (ObjectSelected)
            disp_message (3600, '圆环', 'window', Row, Column, 'black', 'false')
        elseif(ClassID==Class[1])
            dev_set_color ('blue')
            dev_display (ObjectSelected)
            disp_message (3600, '六边形', 'window', Row, Column, 'black', 'false')
        else
            dev_set_color ('yellow')
            dev_display (ObjectSelected)
            disp_message (3600, '齿轮', 'window', Row, Column, 'black', 'false')
        endif
    endfor
    disp_message (3600, '用高斯混合分类器分类图像', 'window', 5, 5, 'black', 'true')
    stop ()
endfor
*清理分类句柄
clear_class_gmm (GMMHandle)
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值