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')

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


相关文章
|
机器学习/深度学习 搜索推荐 算法
计算机视觉教程6-1:图解双目视觉系统与立体校正原理
计算机视觉教程6-1:图解双目视觉系统与立体校正原理
1729 0
计算机视觉教程6-1:图解双目视觉系统与立体校正原理
halcon算子模板匹配(一)基于形状的模板匹配
halcon算子模板匹配(一)基于形状的模板匹配
6467 0
|
JSON 人工智能 数据格式
AI计算机视觉笔记二十六:YOLOV8自训练关键点检测
本文档详细记录了使用YOLOv8训练关键点检测模型的过程。首先通过清华源安装YOLOv8,并验证安装。接着通过示例权重文件与测试图片`bus.jpg`演示预测流程。为准备训练数据,文档介绍了如何使用`labelme`标注工具进行关键点标注,并提供了一个Python脚本`labelme2yolo.py`将标注结果从JSON格式转换为YOLO所需的TXT格式。随后,通过Jupyter Notebook可视化标注结果确保准确性。最后,文档展示了如何组织数据集目录结构,并提供了训练与测试代码示例,包括配置文件`smoke.yaml`及训练脚本`train.py`,帮助读者完成自定义模型的训练与评估。
7397 3
|
监控 算法 C#
C#与Halcon联合编程实现鼠标控制图像缩放、拖动及ROI绘制
C#与Halcon联合编程实现鼠标控制图像缩放、拖动及ROI绘制
2813 0
|
机器学习/深度学习 文字识别 算法
[Halcon&图像] 缺陷检测的一些思路、常规检测算法
[Halcon&图像] 缺陷检测的一些思路、常规检测算法
9217 2
|
数据采集 机器学习/深度学习 编解码
基于OFDM技术的无线局域网系统设计
基于OFDM技术的无线局域网系统设计
|
API 开发工具 计算机视觉
YOLO11 语句整理
本内容介绍基于YOLOv11模型的开发流程,涵盖模型下载、安装依赖库、训练与推理、模型转换为OpenVINO格式及部署。通过Ultralytics工具包实现模型加载、训练和预测,并使用OpenVINO优化推理性能。此外,提供数据集划分方法,按指定比例生成训练集、验证集和测试集,确保数据准备规范化,提升模型训练效果与实用性。
|
计算机视觉
Halcon 学习笔记八:颜色识别
Halcon 学习笔记八:颜色识别
2311 0
|
机器学习/深度学习 文字识别 算法
[Halcon&图像] 基于多层神经网络MLP分类器的思想提取颜色区域
[Halcon&图像] 基于多层神经网络MLP分类器的思想提取颜色区域
1105 0