Halcon 学习笔记九:mlp训练学习实战颜色识别

简介: Halcon 学习笔记九:mlp训练学习实战颜色识别

Halcon 学习笔记九:mlp训练学习实战颜色识别

* This example demonstrates a completeness check of colored game
* pieces using MLP classification. The training and application
* of the classifier is first shown on colored images and then on
* gray images.
* 
dev_update_off ()
dev_close_window ()
dev_open_window (0, 0, 557, 416, 'black', WindowHandle)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
dev_set_draw ('margin')
* 
* Initialization
ImageRootName := 'color/color_pieces_0'
Regions := ['yellow','pink','blue','background']
Highlight := ['goldenrod','magenta','cyan']
gen_empty_obj (Classes)
* 
* Train and apply the MLP classifier
for Mode := 0 to 1 by 1
    dev_set_color ('black')
    read_image (Image, ImageRootName + '0')
    * 
    * Simulate gray image
    if (Mode == 1)
        rgb1_to_gray (Image, GrayImage)
        compose3 (GrayImage, GrayImage, GrayImage, Image)
        dev_display (Image)
        disp_message (WindowHandle, 'Train and apply the classes again on gray images', 'window', 12, 12, 'black', 'false')
        disp_continue_message (WindowHandle, 'black', 'true')
        stop ()
    endif
    * 
    * Colored images
    if (Mode == 0)
        * 
        * Specify color classes
        for I := 1 to 4 by 1
            dev_display (Image)
            dev_display (Classes)
            disp_message (WindowHandle, ['Drag rectangle inside ' + Regions[I - 1] + ' color','Click right mouse button to confirm'], 'window', 24, 12, 'black', 'false')
            draw_rectangle1 (WindowHandle, Row1, Column1, Row2, Column2)
            gen_rectangle1 (Rectangle, Row1, Column1, Row2, Column2)
            concat_obj (Classes, Rectangle, Classes)
        endfor
    endif
    * 
    * Train the specified color classes
    create_class_mlp (3, 7, 4, 'softmax', 'normalization', 3, 42, MLPHandle)
    add_samples_image_class_mlp (Image, Classes, MLPHandle)
    disp_message (WindowHandle, 'Training...', 'window', 100, 12, 'black', 'false')
    train_class_mlp (MLPHandle, 400, 0.5, 0.01, Error, ErrorLog)
    * 
    * Use the trained MLP classifier to test if each image
    * contains four game pieces of each color
    for J := 0 to 3 by 1
        read_image (Image, ImageRootName + J)
        if (Mode == 1)
            rgb1_to_gray (Image, GrayImage)
            compose3 (GrayImage, GrayImage, GrayImage, Image)
        endif
        * 
        * Apply the trained classes
        classify_image_class_mlp (Image, ClassRegions, MLPHandle, 0.5)
        dev_display (Image)
        disp_message (WindowHandle, 'Looking for 4 game pieces of each color ...', 'window', 24, 12, 'black', 'false')
        dev_set_line_width (2)
        * 
        * Count the number of game pieces for each color class
        for Figure := 1 to 3 by 1
            copy_obj (ClassRegions, ObjectsSelected, Figure, 1)
            connection (ObjectsSelected, ConnectedRegions)
            select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 400, 99999)
            count_obj (SelectedRegions, Number)
            dev_set_color (Highlight[Figure - 1])
            dev_display (SelectedRegions)
            OutString := Regions[Figure - 1] + ': ' + Number + '   '
            dev_set_color ('green')
            disp_message (WindowHandle, OutString, 'window', 24 + 30 * Figure, 12, 'black', 'false')
            if (Number != 4)
                disp_message (WindowHandle, 'Not OK', 'window', 24 + 30 * Figure, 120, 'red', 'false')
            else
                disp_message (WindowHandle, 'OK', 'window', 24 + 30 * Figure, 120, 'green', 'false')
            endif
        endfor
        if (J < 3 or Mode == 0)
            disp_continue_message (WindowHandle, 'black', 'true')
            stop ()
        endif
    endfor
    clear_class_mlp (MLPHandle)
endfor
dev_clear_window ()
dev_display (Image)
Message := 'The game pieces cannot be classified reliable on'
Message[1] := 'gray images because the gray values of the'
Message[2] := 'game pieces cannot always be distinguished from'
Message[3] := 'the gray values of the background.'
disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')

后续可以尝试对下图进行训练和预测


相关文章
|
计算机视觉
Halcon 学习笔记八:颜色识别
Halcon 学习笔记八:颜色识别
1681 0
|
机器学习/深度学习 文字识别 算法
[Halcon&图像] 基于多层神经网络MLP分类器的思想提取颜色区域
[Halcon&图像] 基于多层神经网络MLP分类器的思想提取颜色区域
757 0
|
机器学习/深度学习 算法 数据处理
halcon视觉缺陷检测系列(1)常用的6种方法
halcon视觉缺陷检测系列(1)常用的6种方法
3595 0
halcon视觉缺陷检测系列(1)常用的6种方法
|
算法
[Halcon&拟合] 直线、矩形和圆的边缘提取
[Halcon&拟合] 直线、矩形和圆的边缘提取
1265 0
|
机器学习/深度学习 文字识别 算法
[Halcon&图像] 缺陷检测的一些思路、常规检测算法
[Halcon&图像] 缺陷检测的一些思路、常规检测算法
7942 2
Halcon 学习笔记十一:Halcon自定义算子封装
Halcon 学习笔记十一:Halcon自定义算子封装
1219 0
|
缓存 openCL 算法
关于实现Halcon算法加速的基础知识(2)(多核并行/GPU)
关于实现Halcon算法加速的基础知识(多核并行/GPU)
4539 0
关于实现Halcon算法加速的基础知识(2)(多核并行/GPU)
|
算法 数据可视化
Halcon边缘检测和线条检测(3),文章含BLOB检测常用方法和shape_trans内接和外接算子的说明
Halcon边缘检测和线条检测(3),文章含BLOB检测常用方法和shape_trans内接和外接算子的说明
3049 0
Halcon边缘检测和线条检测(3),文章含BLOB检测常用方法和shape_trans内接和外接算子的说明
|
算法 机器人
[3D&Halcon] 3D鞋点胶的点云边界提取
[3D&Halcon] 3D鞋点胶的点云边界提取
2251 0
|
C# C++
halcon联合c#、WPF学习笔记一(WPF配置halcon)
halcon联合c#、WPF学习笔记一(WPF配置halcon)
1771 1

热门文章

最新文章

下一篇
开通oss服务