halcon快速模板匹配

31 篇文章 26 订阅
5 篇文章 5 订阅

快速的找到我们需要的模板,然后对区域进行膨胀,可以直接快速的采集到模板的轮廓,然后对模板进行创建轮廓即可。
当我们进行匹配时出现匹配到的图像与我们预先设定的图像有差别时,这时候我们可以对单张图进行多模板匹配

read_image (Image, 'E:/762halcon/101模组入箱/临时抓料位C1/00_42_01_262.jpg')
threshold (Image, Regions, 1, 202)
connection (Regions, ConnectedRegions)
select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 80798.5, 822243)
dilation_circle (SelectedRegions, RegionDilation, 8)
reduce_domain (Image, RegionDilation, ImageReduced)
create_shape_model (ImageReduced, 5, rad(0), rad(360), 'auto', 'pregeneration', 'use_polarity', 30, 7, ModelID_PGQ02_C2)
get_shape_model_contours (ModelCont, ModelID_PGQ02_C2, 1)

find_shape_model (Image, ModelID_PGQ02_C2, rad(0), rad(360), 0.1,1, 0.01, 'least_squares', 0, 0.9, Row, Column, Angle, Score)
gen_circle (Circle4, Row, Column, 100.5)
dev_display (Image)
dev_display (Circle4)

* Image Acquisition 02: Code generated by Image Acquisition 02
list_files ('E:/762halcon/101模组入箱/临时抓料位C1', ['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      
    count_seconds (S1)
    read_image (Image1, ImageFiles[Index])
    find_shape_model (Image1, ModelID_PGQ02_C2, rad(0), rad(360), 0.1,5, 0.01, 'least_squares', 0, 0.9, Row3, Column3, Angle, Score1)
for A := 0 to |Score1| - 1 by 1

endfor
endfor

当我们单模板不能达到预先效果时,我们已经使用多模板匹配来实现效果

Row1 := [1327.85,1120.96]
Column1 := [ 1837.64,2106.25]
Row2 := [1756.94,1906.39]
Column2 := [2274.41,2811.29]
gen_empty_obj (Models)
list_files ('E:/762halcon/101模组入箱/PGQ06_C2/模型', ['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
    read_image (Image, ImageFiles[Index])
    * Image Acquisition 01: Do something
    *打印语句,当前创建的模板数
     write_string (WindowHandle, 'Generating shape model ' + Index$'d') 
    gen_rectangle1 (ROI_0, Row1[Index], Column1[Index], Row2[Index], Column2[Index])
    reduce_domain (Image, ROI_0, ImageReduced)
    *模板0
     if (Index==0)
        threshold (ImageReduced, Regions, 151, 255)
        connection (Regions, ConnectedRegions)
        select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 33487.9, 73933.2)
     endif
     *模板1
     if (Index==1)
        threshold (ImageReduced, Regions1, 220, 255)
        connection (Regions1, ConnectedRegions1)
        select_shape (ConnectedRegions1, SelectedRegions, 'area', 'and', 88533.8, 159211)   
     endif
     *膨胀选取痕迹
    dilation_circle (SelectedRegions, RegionDilation, 8)
    reduce_domain (ImageReduced, RegionDilation, ImageReduced1)
    *创建模板
    create_shape_model (ImageReduced1, 5, rad(0), rad(360), 'auto', 'pregeneration', 'use_polarity', 30, 7, ModelID_PGQ02_C2)
    get_shape_model_contours (ModelCont, ModelID_PGQ02_C2, 1)
    *2个模板放置在模板数组中
    count_obj (ModelCont, NumModel)
    count_obj (Models, NumModels)
    concat_obj (Models, ModelCont, Models)  
    *模板验证
    find_shape_model (Image, ModelID_PGQ02_C2, rad(0), rad(360), 0.1,1, 0.01, 'least_squares', 0, 0.9, Row, Column, Angle, Score)
    gen_circle (Circle4, Row, Column, 100.5)
dev_display (Image)
dev_display (Circle4)
endfor
  • 0
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值