halcon MLP分类器

halcon代码

*关闭当前窗口
dev_close_window()
*创建新窗口
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
*设置显示颜色
dev_set_colored(6)
*设置绘制形状的方式
dev_set_draw ('margin')
dev_set_line_width (3)
*创建MLP分类器,输出方法选择softmax,用于分类
create_class_mlp (1, 1, 2, 'softmax', 'normalization', 3, 42, MLPHandle)
*创建训练样本图像与其分类的对应关系
*图像和分类名称意义对应
FileNames:=['m1','m2','m3','m4']
Classes :=[0,0,1,1]
for J := 0 to |FileNames| - 1 by 1
    *读取训练图像
    read_image (Image, 'F:/Halcon_workstation/data/'+FileNames[J])
    dev_display (Image)
    *对图像进行分割
    rgb1_to_gray (Image, GrayImage)
    *全局阈值
    threshold (GrayImage, darkRegion, 0, 105)
    *给予直方图的自动阈值分割方法
    *auto_threshold (GrayImage, darkRegion, 8.0)
    *自动全局阈值分割方法
    *binary_threshold (GrayImage, darkRegion, 'max_separability', 'dark', UsedThreshold)
    connection (darkRegion, ConnectedRegions)
    select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 2000, 99999)
    fill_up (SelectedRegions, Objects)
    dev_display (Objects)
    disp_message (WindowHandle, 'Add Sample'+J+', Class Index '+ Classes[J], 'window', 10, 10, 'black', 'true')
    *将分割后的对象Objects添加进分类器对应Classes[J]中
    count_obj (Objects, Number)
    *提取特征(圆度)
    for N := 1 to Number by 1
        select_obj (Objects, Region, N)
        circularity (Region, Circularity)
        add_sample_class_mlp (MLPHandle, Circularity, Classes[J])
    endfor
    stop()
    disp_continue_message (WindowHandle, 'black', 'true')
endfor
dev_clear_window()
disp_message (WindowHandle, 'Training...', 'window', 10, 10, 'black', 'true')
*训练MLP分类器
train_class_mlp (MLPHandle, 200, 1, 0.01, Error, ErrorLog)
clear_samples_class_mlp (MLPHandle)
*读取输入的待检测图像
read_image (testImage, 'F:/Halcon_workstation/data/m5.jpg')
rgb1_to_gray (testImage, GrayTestImage)
*将图像进行分割
threshold (GrayTestImage, darkTestRegion, 0, 105)
connection (darkTestRegion, ConnectedTestRegions)
select_shape (ConnectedTestRegions, SelectedTestRegions, 'area', 'and', 1500, 99999)
fill_up (SelectedTestRegions, testObjects)
*将分割后的图像Objects进行分类
count_obj (testObjects, Number)
Classes := []
Colors := ['yellow','magenta']
dev_set_colored (6)
dev_display (testImage)
*提取特征(圆度)
for J := 1 to Number by 1
    select_obj (testObjects, singleRegion, J)
    circularity (singleRegion, Circularity)
    classify_class_mlp (MLPHandle, Circularity, 1, Class, Confidence)
    
    Classes := [Classes,Class]
    dev_set_color (Colors[Classes[J-1]])
    dev_display (singleRegion)
    *area_center (singleRegion, Area, Row, Column)
    *disp_message (WindowHandle, '面积' + Area + '坐标:'+ Column + ' ' + Row, 'window', 0,0, 'white', 'false')

dfor
*清空MLP分类器,释放内存
clear_class_mlp (MLPHandle)

halcon多层感知器MLP的使用 - 简书

基于halcon的MLP分类识别_星辰_2020的博客-CSDN博客

https://blog.csdn.net/qq_42318305/article/details/87449356

halcon案例classify_wood.hdev

* The object of this example is to classify different
* kinds of wood according to their surface texture.
file_exists ('classify_wood.gmc', FileExists)
if (FileExists)
    USE_STORED_CLASSIFIER := 1
else
    USE_STORED_CLASSIFIER := 0
endif
* First, the path to the images is set, the initial image
* is read and the settings are specified.
get_system ('image_dir', HalconImages)
get_system ('operating_system', OS)
if (OS{0:2} == 'Win')
    tuple_split (HalconImages, ';', HalconImages)
else
    tuple_split (HalconImages, ':', HalconImages)
endif
ImagePath := '/wood/'
ReadOK := false
for k := 0 to |HalconImages| - 1 by 1
    try
        read_image (Image, HalconImages[k] + ImagePath + 'apple/apple_01')
        ReadPath := HalconImages[k] + ImagePath
        ReadOK := true
        break
    catch (Exception)
    endtry
endfor
if (not ReadOK)
    disp_message (WindowID, 'Could not find the images in $HALCONIMAGES', 'window', 12, 12, 'black', 'true')
    stop ()
endif
read_image (Image, ImagePath + 'apple/apple_01')
get_image_size (Image, Width, Height)
dev_close_window ()
dev_open_window_fit_image (Image, 0, 0, Width, Height, WindowID)
set_display_font (WindowID, 16, 'mono', 'true', 'false')
dev_display (Image)
dev_update_off ()
* Now the different wood classes are specified.
Classes := ['apple','beech','cherry','maple','oak','walnut']
* The program uses by default a stored classifier. If you, however,
* want to perform the training, set USE_STORED_CLASSIFIER to 0.
* If the classifier can not be found, USE_STORED_CLASSIFIER
* is set to 0 automatically.
if (USE_STORED_CLASSIFIER == 1)
    read_class_mlp ('classify_wood.gmc', MLPHandle)
    NumClasses := |Classes|
else
    gen_features (Image, FeatureVector)
    NumFeatures := |FeatureVector|
    NumClasses := |Classes|
    NumHidden := 15
    create_class_mlp (NumFeatures, NumHidden, NumClasses, 'softmax', 'normalization', 10, 42, MLPHandle)
    for CorrectClassID := 0 to NumClasses - 1 by 1
        list_files (ReadPath + Classes[CorrectClassID], 'files', Files)
        for k := 0 to |Files| - 1 by 2
            read_image (Image, Files[k])
            dev_display (Image)
            gen_features (Image, FeatureVector)
            add_sample_class_mlp (MLPHandle, FeatureVector, CorrectClassID)
        endfor
    endfor
    train_class_mlp (MLPHandle, 200, 1, 0.0001, Error, ErrorLog)
    write_class_mlp (MLPHandle, 'classify_wood.gmc')
    disp_message (WindowID, 'Training of wood textures completed\nPress \'Run\' to continue', 'window', 12, 12, 'black', 'true')
    stop ()
endif
Errors := 0
Count := 0
for CorrectClassID := 0 to NumClasses - 1 by 1
    list_files (ReadPath + Classes[CorrectClassID], 'files', Files)
    for k := 0 to |Files| - 1 by 1
        Count := Count + 1
        read_image (Image, Files[k])
        gen_features (Image, FeatureVector)
        classify_class_mlp (MLPHandle, FeatureVector, 2, FoundClassIDs, Confidence)
        dev_display (Image)
        dev_set_color ('blue')
        disp_message (WindowID, 'correct class: ' + Classes[CorrectClassID], 'window', 12, 12, 'black', 'true')
        if (CorrectClassID == FoundClassIDs[0])
            disp_message (WindowID, 'found class:   ' + Classes[FoundClassIDs[0]], 'window', 42, 12, 'forest green', 'true')
        else
            Errors := Errors + 1
            disp_message (WindowID, 'found class:   ' + Classes[FoundClassIDs[0]], 'window', 42, 12, 'red', 'true')
            disp_continue_message (WindowID, 'black', 'true')
            stop ()
        endif
        wait_seconds (0.1)
    endfor
endfor
ErrorRate := real(Errors) / Count * 100.0
disp_message (WindowID, 'ErrorRate = ' + ErrorRate + '%', 'window', 72, 12, 'black', 'true')
disp_end_of_program_message (WindowID, 'black', 'true')
clear_class_mlp (MLPHandle)

 gen_features (Image : : : FeatureVector)函数

FeatureVector := []
* Compute features.
gen_sobel_features (Image, FeatureVector, FeatureVector)
* Downscale the image (image pyramid) and compute features.
zoom_image_factor (Image, Zoomed1, 0.5, 0.5, 'constant')
gen_sobel_features (Zoomed1, FeatureVector, FeatureVector)
* Uncomment lines to use further pyramid levels:
* zoom_image_factor (Zoomed1, Zoomed2, 0.5, 0.5, 'constant')
* gen_sobel_features (Zoomed2, FeatureVector, FeatureVector)
* zoom_image_factor (Zoomed2, Zoomed3, 0.5, 0.5, 'constant')
* gen_sobel_features (Zoomed3, FeatureVector, FeatureVector)
* zoom_image_factor (Zoomed3, Zoomed4, 0.5, 0.5, 'constant')
* gen_sobel_features (Zoomed4, FeatureVector, FeatureVector)
FeatureVector := real(FeatureVector)
return ()

 gen_sobel_features(Image :  : Features : FeaturesExtended)

* Coocurrence matrix for 90 deg:
cooc_feature_image (Image, Image, 6, 90, Energy, Correlation, Homogeneity, Contrast)
* Absolute histogram of edge amplitudes:
sobel_amp (Image, EdgeAmplitude, 'sum_abs', 3)
gray_histo_abs (EdgeAmplitude, EdgeAmplitude, 8, AbsoluteHistoEdgeAmplitude)
* 
* You could of course compute more features:
* Entropy and anisotropy:
* entropy_gray (Image, Image, Entropy, Anisotropy)
* Absolute histogram of gray values:
* gray_histo_abs (Image, Image, 8, AbsoluteHistoImage)
* Add features to feature vector:
FeaturesExtended := [Features,Energy,Correlation,Homogeneity,Contrast]
FeaturesExtended := [FeaturesExtended,AbsoluteHistoEdgeAmplitude]
* Activate the following lines to add the additional features you activated:
* FeaturesExtended := [FeaturesExtended,Entropy,Anisotropy]
* FeaturesExtended := [FeaturesExtended,AbsoluteHistoImage]
return ()

你可能感兴趣的:(Halcon,深度学习)