Halcon 图像分类-MLP分类器使用

Halcon 图像分类

学习内容:

(1)基于神经网络的多层感知器MLP分类器
(2)基于支持向量机的SVM分类器
(3)基于高斯混合模型的GMM分类器
(4)基于K-NN分类器

本章学习目标:

1.了解图像分类的一般流程:
(1)建立分类器
(2)根据样本特征向量训练分类器
(3)对目标进行检测获取特征向量
(4)根据创建好的分类器模型进行分类
(5)清除分类器
2.学习根据提取区域的形状、灰度,纹理等特征建立特征向量集合,用于制作分类器
3.MLP分类器是一种基于神经网络的动态分类器。如果分类的类别只有2个类别,或称作2分类。目标识别中称为是或不是,缺陷检测中称为瑕疵或者背景等等。如果分类的类别超过2个,那分类器的模型可能就是超平面的,其维度2维以上。

本章学习工具:

(1)halcon 18.05
(2)黑洞NG样本100张
(3)背景干扰样本100张
(4)混合测试样本100张

测试图片

Halcon 图像分类-MLP分类器使用_第1张图片 图中有黑色圆形空心黑洞,标记为瑕疵NG

Halcon 图像分类-MLP分类器使用_第2张图片纯背景无瑕疵

Halcon中MLP分类器使用代码:

dev_close_window ()
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
read_image (inputimage, 'C:/Users/yangguowei/Desktop/MLP样本/黑洞NG样本/0009')
Get_Feature (inputimage, FeatureVector, FeatureDimension)
Classes:=['黑洞NG','背景OK']
dev_set_draw ('margin')
dev_set_line_width (2)
*创建MLP分类器,输出函数使用softmax
create_class_mlp (FeatureDimension, 15, |Classes|, 'softmax', 'normalization', 10, 42, MLPHandle)

*黑洞NG图像训练分类
* 黑洞图像路径
list_files ('C:/Users/yangguowei/Desktop/MLP样本/黑洞NG样本', ['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])
    Get_Feature (Image, FeatureVector_current, FeatureDimension)
    if(|FeatureVector_current|==0)
       continue
    endif
     *将每张图得到的特征向量加入到多层感知器的训练库中 
    add_sample_class_mlp (MLPHandle, FeatureVector_current, 0) 
endfor
   
*训练MLP分类器
train_class_mlp (MLPHandle, 200, 1, 0.01, Error, ErrorLog)
clear_samples_class_mlp (MLPHandle)
stop ()
*读入需要测试的图像,根据分类器提供的特征检测

* Image Acquisition 01: Code generated by Image Acquisition 01
confidence:=[]
list_files ('C:/Users/yangguowei/Desktop/测试样本', ['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 (ImageTest, ImageFiles[Index])
    Get_Feature (ImageTest, FeatureVector2, FeatureDimension2)
    if(|FeatureVector2|==0)
       continue
    endif
    classify_class_mlp (MLPHandle, FeatureVector2, 2, FoundClassifyIDs, Confidence)
    confidence:=[confidence,Confidence]
    *如果对于每一个区域找到和训练样本类似的特征向量就归属到哪一类中,并将分类结果输出、
    if (FoundClassifyIDs[0]==0)
        dev_display(ImageTest)
        disp_message (WindowHandle, ImageFiles[Index], 'window', 0, 0, 'black', 'true')
        disp_message (WindowHandle, '图片中有黑洞:', 'window', 20, 0, 'black', 'true')
        stop()
    else
        dev_display (ImageTest)
        disp_message (WindowHandle, ImageFiles[Index], 'window', 0, 0, 'black', 'true')
        disp_message (WindowHandle, '图片背景很纯:', 'window', 20, 0, 'black', 'true')
    endif 
endfor
stop ()

*清除MLP分类器,清楚内存
clear_class_mlp (MLPHandle)

Get_Feature()函数内联代码

mean_image (inputimage, ImageMean, 3, 3)
threshold (ImageMean, Region, 0, 50)
connection (Region, ConnectedRegions)
dev_set_colored (3)
select_shape (ConnectedRegions, SelectedRegions1, 'circularity', 'and', 0.5, 1)
select_shape (SelectedRegions1, SelectedRegions2, 'roundness', 'and', 0.5, 1)
select_gray (SelectedRegions2, ImageMean, SelectedRegions3, 'deviation', 'and', 12, 25)
select_gray (SelectedRegions3, ImageMean, SelectedRegion4, 'mean', 'and', 0, 35)
select_shape (SelectedRegion4, FinallSelectedRegion, 'area', 'and', 140, 99999)
dev_display (FinallSelectedRegion)
FeatureVector:=[]
circularity (FinallSelectedRegion, Circularity)
intensity (FinallSelectedRegion, ImageMean, Mean, Deviation)
roundness (FinallSelectedRegion, Distance, Sigma, Roundness, Sides)
FeatureVector:=[Circularity,Mean,Deviation,Roundness]
FeatureDimension:=|FeatureVector|

分类结果

Halcon 图像分类-MLP分类器使用_第3张图片

你可能感兴趣的:(计算机视觉,深度学习)