Halcon模板匹配中模板的保存和读取

该博客介绍了在Halcon中如何保存和读取形状模型用于模板匹配。首先,通过创建和减少模型区域,建立模板并保存为'Shaping01.matching.shm'。然后,读取模型并应用于多个测试图像,找到匹配位置,并显示匹配后的模型轮廓。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Halcon模板匹配中模板的保存和读取Halcon模板匹配中模板的保存和读取Halcon

一 保存:write_shape_model (ModelID, 'C:/Users/123/Desktop/ocr/matching.shm')

write_shape_model (ModelID, 'C:/Users/123/Desktop/ocr/matching.shm')

二 读取read_shape_model ('C:/Users/123/Desktop/ocr/matching.shm', ModelID)

read_shape_model ('C:/Users/123/Desktop/ocr/matching.shm', ModelID)
get_shape_model_contours (ModelContours, ModelID, 1)

实例:保存

* 
* Matching 01: ************************************************
* Matching 01: BEGIN of generated code for model initialization
* Matching 01: ************************************************
set_system ('border_shape_models', 'false')
* 
* Matching 01: Obtain the model image
read_image (Image, 'C:/Users/29939/Desktop/ocr/QQ图片20210927213156.jpg')
* 
* Matching 01: Build the ROI from basic regions
gen_rectangle1 (ModelRegion, 340.467, 409.747, 402.461, 485.535)
gen_rectangle1 (_TmpRegion, 255.633, 1151.16, 337.204, 1348.87)
union2 (ModelRegion, _TmpRegion, ModelRegion)
gen_rectangle1 (_TmpRegion, 249.107, 1467.49, 343.73, 1645.43)
union2 (ModelRegion, _TmpRegion, ModelRegion)
* 
* Matching 01: Reduce the model template
reduce_domain (Image, ModelRegion, TemplateImage)
* 
* Matching 01: Create the shape model
create_scaled_shape_model (TemplateImage, 5, rad(0), rad(360), rad(0.1324), 0.92, 1.09, 0.0023, ['point_reduction_medium','no_pregeneration'], 'use_polarity', [5,13,16], 4, ModelID)
* 
* Matching 01: Get the model contour for transforming it later into the image
get_shape_model_contours (ModelContours, ModelID, 1)

write_shape_model (ModelID, 'C:/Users/123/Desktop/ocr/matching.shm')

* 2
* Matching 01: Get the reference position
area_center (ModelRegion, ModelRegionArea, RefRow, RefColumn)
vector_angle_to_rigid (0, 0, 0, RefRow, RefColumn, 0, HomMat2D)
affine_trans_contour_xld (ModelContours, TransContours, HomMat2D)
* 
* Matching 01: Display the model contours
dev_display (Image)
dev_set_color ('green')
dev_set_draw ('margin')
dev_display (ModelRegion)
dev_display (TransContours)
stop ()
* 
* Matching 01: END of generated code for model initialization
* Matching 01:  * * * * * * * * * * * * * * * * * * * * * * *
* Matching 01: BEGIN of generated code for model application
* 
* Matching 01: Loop over all specified test images
TestImages := ['C:/Users/29939/Desktop/ocr/QQ图片20210927213203.jpg','C:/Users/29939/Desktop/ocr/QQ图片20210927213136.jpg','C:/Users/29939/Desktop/ocr/QQ图片20210927213156.jpg']
for T := 0 to 2 by 12
    * 
    * Matching 01: Obtain the test image
    read_image (Image, TestImages[T])
    * 
    * Matching 01: Find the model
    find_scaled_shape_model (Image, ModelID, rad(0), rad(360), 0.92, 1.09, 0.36, 5, 0.5, 'least_squares', [5,1], 1, Row, Column, Angle, Scale, Score)
    * 
    * Matching 01: Transform the model contours into the detected positions
    dev_display (Image)
    for I := 0 to |Score| - 1 by 1
        hom_mat2d_identity (HomMat2D)
        hom_mat2d_scale (HomMat2D, Scale[I], Scale[I], 0, 0, HomMat2D)
        hom_mat2d_rotate (HomMat2D, Angle[I], 0, 0, HomMat2D)
        hom_mat2d_translate (HomMat2D, Row[I], Column[I], HomMat2D)
        affine_trans_contour_xld (ModelContours, TransContours, HomMat2D)
        dev_set_color ('green')
        dev_display (TransContours)
        stop ()
    endfor
endfor
* 
* Matching 01: Clear model when done
stop ()
clear_shape_model (ModelID)
* Matching 01: *******************************************
* Matching 01: END of generated code for model application
* Matching 01: *******************************************
* 

实例:读取

read_shape_model ('C:/Users/29939/Desktop/ocr/matching.shm', ModelID)
get_shape_model_contours (ModelContours, ModelID, 1)


* 
* Matching 01: END of generated code for model initialization
* Matching 01:  * * * * * * * * * * * * * * * * * * * * * * *
* Matching 01: BEGIN of generated code for model application
* 
* Matching 01: Loop over all specified test images
TestImages := ['C:/Users/39/Desktop/ocr/QQ图片20210927213203.jpg','C:/Users/39/Desktop/ocr/QQ图片20210927213136.jpg','C:/Users/39/Desktop/ocr/QQ图片20210927213156.jpg']
for T := 0 to 2 by 12
    * 
    * Matching 01: Obtain the test image
    read_image (Image, TestImages[T])
    * 
    * Matching 01: Find the model
    find_scaled_shape_model (Image, ModelID, rad(0), rad(360), 0.92, 1.09, 0.36, 5, 0.5, 'least_squares', [5,1], 1, Row, Column, Angle, Scale, Score)
    * 
    * Matching 01: Transform the model contours into the detected positions
    dev_display (Image)
    for I := 0 to |Score| - 1 by 1
        hom_mat2d_identity (HomMat2D)
        hom_mat2d_scale (HomMat2D, Scale[I], Scale[I], 0, 0, HomMat2D)
        hom_mat2d_rotate (HomMat2D, Angle[I], 0, 0, HomMat2D)
        hom_mat2d_translate (HomMat2D, Row[I], Column[I], HomMat2D)
        affine_trans_contour_xld (ModelContours, TransContours, HomMat2D)
        dev_set_color ('green')
        dev_display (TransContours)
        stop ()
    endfor
endfor
* 
* Matching 01: Clear model when done
stop ()
clear_shape_model (ModelID)
* Matching 01: *******************************************
* Matching 01: END of generated code for model application
* Matching 01: *******************************************
* 
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值