halcon快速模板匹配

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

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

猜你喜欢

转载自blog.csdn.net/m0_51559565/article/details/128368765