Halcon基于深度模型的模板匹配建模和实现

熟悉halcon或者想熟悉halcon'的同学可以了解下,在这里我仅记录个人理解,如有异常或者不足之处,欢迎指正共同学习!

基于深度模式的模板匹配可以用来计数(比如烧烤签计数等),也可以确定位置(定位抓取等)。接下来我们进行第一步:创建模板

  read_image (TemplateImage, './image/test')       //读取一张模板图像
    
    *选择几个区域进行扣模板
    gen_rectangle2 (ROI_0, 530.858, 342.976, rad(-20.5599), 67.496, 63.2034)
    gen_rectangle2 (ROI_1, 562.463, 1046.04, rad(-22.3843), 72.6184, 81.0093)
    concat_obj (ROI_0, ROI_1, TemplateRegions)
   
    *gen_rectangle2 (ROI_2, 2985.49, 1545.77, rad(5.10233), 520.543, 518.913)
    
    *concat_obj (ROI, ROI_2,ROI)
    *gen_rectangle2 (ROI_3, 2501.08, 4601.12, rad(32.0496), 554.606, 527.54)
    *concat_obj (ROI, ROI_3,TemplateRegions)
    **concat_obj (ROI, ROI_2, TemplateRegions)
    
   ** read_object (TemplateRegions, 'template_regions_' + ImageSet)     //创建三个ROI区域用来制作模板
    * 
    * Open a window with an appropriate size.打开相同尺寸的窗口
    get_image_size (TemplateImage, Width, Height)
    dev_open_window_fit_image (TemplateImage, 0, 0, 800, 600, WindowHandle)
    dev_disp_text ('Preparing Deep Counting model...', 'window', 'top', 'left', 'black', [], [])
    * 
    * Reduce the template image by the template regions to
    * create the templates.
    count_obj (TemplateRegions, NumTemplates)
    gen_empty_obj (Templates)
        for TemplateIdx := 1 to NumTemplates by 1
            select_obj (TemplateRegions, TemplateRegion, TemplateIdx)
            reduce_domain (TemplateImage, TemplateRegion, Template)
            concat_obj (Templates, Template, Templates)   //获取模板
        endfor
    
    **保存制作的模板,可以自行指定路径和名字
    write_object (Templates, 'models1')

建模完毕后就可以加载图像进行这个模板应用匹配了

dev_update_off ()
pathmodel:='models.hobj'
read_image (SearchImage, './image/test')
**numcount:=0
score:=0.5
Counting_num (SearchImage, EmptyRegion, pathmodel, score, numcount) //这个函数是我自己封装起来的,内部如下:

try
     read_object (Object, pathmodel)
    * Create a Deep Counting model. It will be re-used for
    * the different objects by changing the templates.
    create_deep_counting_model ([], [], DeepCountingHandle)
    * Set inference device. 检测是否有GPU,如果有的话选择GPU否则用cpu
    query_available_dl_devices (['runtime', 'runtime'], ['gpu', 'cpu'], DLDeviceHandles)
    set_deep_counting_model_param (DeepCountingHandle, 'device', DLDeviceHandles[0])   
    **设置起始角度和终止角度以及最小匹配分数,还有很多参数可以设置可以自行了解
    set_deep_counting_model_param (DeepCountingHandle, 'angle_start', 0)
    set_deep_counting_model_param (DeepCountingHandle, 'angle_end', rad(0))
    set_deep_counting_model_param (DeepCountingHandle, 'min_score', score)     
    prepare_deep_counting_model (Object, DeepCountingHandle)  //把创建的模板加载到模型中
     * Apply the Deep Counting model on the search image.开始搜索,获得搜索到的总体数量和对应的结果
    apply_deep_counting_model (SearchImage, DeepCountingHandle, Count, DeepCountingResult)     
    gen_empty_region (EmptyRegion)
    for Index := 0 to |DeepCountingResult.row|-1 by 1
                gen_circle (Circle, DeepCountingResult.row[Index], DeepCountingResult.column[Index], 10.5)
                union2 (Circle, EmptyRegion, EmptyRegion)
    endfor
    
    numcount:=Count
    
    
catch (Exception)
    numcount:=0
endtry
return ()

结果如下:其中的绿色是简历的模板,其他的是匹配的结果

  • 0
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值