设置首页收藏本站
开启左侧

[Halcon] 分享角度模板匹配

[复制链接]
WandRoc 发表于 2024-2-4 11:32:44 | 显示全部楼层 |阅读模式
*创建模板
Names := [0,1]
*模板路径用到的数组
Models := []
*创建模板
for i := 0 to 1 by 1
    read_image (Image, './Model/' + Names[i] + '.bmp')
    get_image_size (Image, OriginWidth, OriginHeight)
    zoom_image_factor (Image, Imagezoom, 0.4, 0.4, 'constant')   
    get_image_size (Imagezoom, ZoomWidth, ZoomHeight)
    dev_update_off ()
    dev_close_window ()
    dev_open_window_fit_image (Imagezoom, 0, 0, ZoomWidth, ZoomHeight, WindowHandle)
    dev_display (Imagezoom)
    set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
    dev_set_color ('red')
    dev_set_line_width (3)
    if (i = 0)
        disp_message (WindowHandle, '创建正面模板', 'window', 12, 12, 'lime green', 'false')
    elseif (i = 1)
        disp_message (WindowHandle, '创建反面模板', 'window', 12, 12, 'lime green', 'false')
    endif
    draw_circle (WindowHandle, Rows, Columns, Radiuss)
    gen_circle (ROI_0, Rows, Columns, Radiuss)
    reduce_domain (Imagezoom, ROI_0, ImageReduced)
    create_shape_model (ImageReduced, 'auto', 0, rad(360), 'auto', 'auto', 'use_polarity', 'auto', 'auto', ModelID)
    Models := [Models,ModelID]
    area_center (ROI_0, Area, Row, Column)
    dev_clear_window ()
    dev_display (Imagezoom)
    dev_display_shape_matching_results (ModelID, 'red', Row, Column, 0.0, 1.0, 1.0, 0)
    disp_message (WindowHandle, '模板已创建', 'window', 12, 12, 'lime green', 'false')
    stop ()
endfor
*匹配模板
Scores := []
count0 := 0
count1 := 0
error := 0
list_files ('E:/Test/project/Halcon18.11-MatchingDegree/Image', ['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
    dev_clear_window ()
    read_image (Image, ImageFiles[Index])
    count_seconds (Seconds1)
    zoom_image_factor (Image, Imagezooms, 0.4, 0.4, 'constant')
    find_shape_models (Imagezooms, Models, 0, rad(360), 0.2, 1, 0.0, 'least_squares', 9, 0.9, Row1, Column1, Angle, Score, Model)
    count_seconds (Seconds2)
    Scores := [Scores, Score]  
    if (Model = 0)
        dev_display (Imagezooms)
        dev_display_shape_matching_results (Models[0], 'red', Row1, Column1, Angle, 1.0, 1.0, 0)
        disp_message (WindowHandle, '正面' + ' Match found in ' + ((Seconds2 - Seconds1) * 1000.0)$'.1f' + ' ms', 'window', 12, 12, 'lime green', 'false')
        count0 :=  count0 + 1
    elseif(Model = 1)
        dev_display (Imagezooms)
        dev_display_shape_matching_results (Models[1], 'red', Row1, Column1, Angle, 1.0, 1.0, 0)
        disp_message (WindowHandle, '反面' + ' Match found in ' + ((Seconds2 - Seconds1) * 1000.0)$'.1f' + ' ms', 'window', 12, 12, 'lime green', 'false')
        count1 :=  count1 + 1
    else
        error := error + 1        
    endif
    stop()
endfor
*释放一个轮廓模型的内存
clear_shape_model(ModelID)








奖励计划已经开启,本站鼓励作者发布最擅长的技术内容和资源,流量变现就在现在,[点我]加入吧~~~Go
您需要登录后才可以回帖 登录 | 注册

本版积分规则

快速回复 返回顶部 返回列表