基于halcon的MLP分类识别

分类识别

  • 原图
    • 训练图
    • 识别图
  • 代码
  • 结果

原图

训练图

识别图



代码

dev_close_window ()

read_image (Image, 'train.bmp')
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
 
Regions := ['yellow','red','blue','background']
 
Highlight := ['goldenrod','magenta','cyan']

gen_empty_obj (ClassesObject)

dev_set_draw ('margin')

for i:= 1 to |Regions| by 1
    dev_display (Image)
    dev_display (ClassesObject)
    disp_message (WindowHandle, '请框选  '+Regions[i-1]+'  颜色区域', 'window', 12, 12, 'black', 'true')
    draw_rectangle1 (WindowHandle, Row1, Column1, Row2, Column2)
    gen_rectangle1 (Rectangle, Row1, Column1, Row2, Column2)
    concat_obj (ClassesObject, Rectangle, ClassesObject)
endfor
stop ()

*1、创建mlp分类器
create_class_mlp (3, 7, 4, 'softmax', 'normalization', 3, 42, MLPHandle)
*2、添加训练模型分类器
add_samples_image_class_mlp (Image, ClassesObject, MLPHandle)

*3、训练
train_class_mlp (MLPHandle, 200, 1, 0.01, Error, ErrorLog)
*4、保存训练文件
write_class_mlp (MLPHandle, 'classify_color.gmc')

stop ()

ImagePath := './1/'
list_files (ImagePath, ['files','follow_links'], ImageFiles)
tuple_regexp_select (ImageFiles, ['\\.(bmp|jpg|jpeg)$','ignore_case'], ImageFiles)
for i := 0 to |ImageFiles|-1 by 1
    read_image (Image1, ImageFiles[i])
    get_image_size (Image1, Width, Height)
    dev_display(Image1)
    *5、进行分类
    classify_image_class_mlp (Image1, ClassRegions, MLPHandle, 0.5)
    disp_message (WindowHandle, 'MLP分类结果_'+(i+1), 'window', 12, 12, 'red', 'false')
    for j := 1 to |Highlight| by 1
        copy_obj (ClassRegions, ObjectsSelected, j, 1)
        connection (ObjectsSelected, ConnectedRegions)        
        select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 100, 20000)
        count_obj (SelectedRegions, Number)
        smallest_circle (SelectedRegions, Row, Column, Radius)
        dev_set_color (Highlight[j - 1])
        set_display_font (WindowHandle, 12, 'mono', 'true', 'false')
        gen_circle (Circle, Row, Column, Radius)
        for k := 0 to |Row|-1 by 1
            if(Row[k] + Radius[k] + 10 + 15 > Height)
                set_tposition (WindowHandle, Row[k] - Radius[k] - 20 , Column[k] -40)
            else
                set_tposition (WindowHandle, Row[k] + Radius[k] + 10 , Column[k] -40)
            endif
            
            write_string (WindowHandle, (k+1)+'、颜色:'+Regions[j-1])
        endfor
        
    endfor
    dump_window (WindowHandle, 'png', 'MLP分类结果_'+(i+1))
    stop ()
endfor

结果

基于halcon的MLP分类识别_第1张图片
基于halcon的MLP分类识别_第2张图片
基于halcon的MLP分类识别_第3张图片

你可能感兴趣的:(HALCON,图像识别,分类算法,计算机视觉)