前面介绍过,Blob分析方法很多,很灵活,这里使用模板匹配的方法,
来区分不同国家的硬币。
模板匹配主要用于在对比 度较好的环境,其匹配精度 比较高。
模板匹配方法又有很多细分,
工业 视觉中采用形状类型的模板 匹配居多。
模板匹配一般得事先通过算子create_shape_model创建模板,
然后再通过find_shape_models来查找事先创建的模板。
共两步。
当前例子事先创建四个国家硬币的模板,
然后在多张图片中查找事先创建的四个模板,将找到的模板显示出来。
可以运行下面代码查看 效果与结果。
dev_close_window ()
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
names := ['german','italian','greek','spanish']
models :=[]
for i:= 0 to 3 by 1
file := 'coins/20cent_'+ names
read_image (Image, file)
dev_display (Image)
threshold (Image, Regions, 70, 255)
connection (Regions, ConnectedRegions)
select_shape_std (ConnectedRegions, SelectedRegions, 'max_area', 0)
shape_trans (SelectedRegions, RegionTrans, 'convex')
area_center (RegionTrans, Area, Row, Column)
gen_circle (Circle, Row, Column, 120)
reduce_domain (Image, Circle, ImageReduced)
*inspect_shape_model (Image, ModelImages, ModelRegions, 2, 30)
create_shape_model (ImageReduced, 'auto', 0, rad(360), 'auto', 'none', 'ignore_local_polarity', 'auto', 'auto', ModelID)
models := [models,ModelID]
endfor
stop()
for i:=1 to 13 by 1
file := 'coins/20cent_'+i$'.2'+'.png'
read_image (Image, file)
threshold (Image, Regions, 70, 255)
connection (Regions, ConnectedRegions)
select_shape_std (ConnectedRegions, SelectedRegions, 'max_area', 0)
shape_trans (SelectedRegions, RegionTrans, 'convex')
area_center (RegionTrans, Area, Row, Column)
gen_circle (Circle, Row, Column, 35)
*find_shape_models (ImageReduced, Models, 0, rad(360), 0.6, 1, 0, 'least_squares', 0, 0.9, Row, Column, Angle, Score, Model)
reduce_domain(Image, Circle, ImageReduced1)
find_shape_models (ImageReduced1, models, 0, rad(360), 0.6, 1, 0.5, 'least_squares', 0, 0.9, Row1, Column1, Angle, Score,model)
get_shape_model_contours (ModelContours, ModelID, 1)
vector_angle_to_rigid (0, 0, 0, Row1, Column1, Angle, HomMat2D)
affine_trans_contour_xld (ModelContours, ContoursAffineTrans, HomMat2D)
dev_display (Image)
dev_set_line_width (3)
dev_set_color('blue')
dev_display (ContoursAffineTrans)
dev_disp_text (names[model], 'window', 10, 10, 'black', [], [])
stop()
endfor
for i := 0 to 3 by 1
clear_shape_model (models)
endfor
帖子为下面halcon视频教程的部分内容,
可以下载视频教程,观看与学习。
上位机VC MFC程序开发精典实例大全源码与视频讲解配套下载408例 经历1年的编程与录制点击进入查看
如果您认可,可联系功能定制! 如果您着急,充值会员可直接联系发您资料!
|