Hello Mat

 找回密码
 立即注册
查看: 9054|回复: 7

基于多模板平均值的匹配方法

[复制链接]

1323

主题

1551

帖子

0

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22647
发表于 2020-2-12 20:36:31 | 显示全部楼层 |阅读模式
基于多模板平均值的匹配方法:多个模板,取个平均模板,增强鲁棒性
第一步:采用程序或者手工,绘制一个轮廓模型,标准的轮廓模型:create_reference_image (ReducedReferenceImage)
  1. Row1 := 20
  2. Row2 := 64
  3. Column1 := 20
  4. Column2 := 106
  5. Column11 := 38
  6. Column12 := 88
  7. gen_contour_polygon_rounded_xld (TemplateShape, [Row1,Row1,Row2,Row2,Row1], [Column1,Column2,Column2,Column1,Column1], [9,9,9,9,9], 1)
  8. gen_contour_polygon_rounded_xld (TemplateLeft, [Row1,Row1,Row2,Row2,Row1], [Column1,Column11,Column11,Column1,Column1], [9,7,7,9,9], 1)
  9. gen_contour_polygon_rounded_xld (TemplateRight, [Row1,Row1,Row2,Row2,Row1], [Column12,Column2,Column2,Column12,Column12], [7,9,9,7,7], 1)
  10. gen_image_const (BlacKBacKground, 'byte', Column2 + 20, Row2 + 20)
  11. paint_xld (TemplateShape, BlacKBacKground, ReferenceImage1, 90)
  12. paint_xld (TemplateLeft, ReferenceImage1, ReferenceImageTmp, 150)
  13. paint_xld (TemplateRight, ReferenceImageTmp, ReferenceImage, 150)
  14. concat_obj (TemplateShape, TemplateLeft, Tmp)
  15. concat_obj (Tmp, TemplateRight, DispTemplate)
  16. gen_rectangle1 (TemplateROI, Row1 - 10, Column1 - 10, Row2 + 10, Column2 + 10)
  17. reduce_domain (ReferenceImage, TemplateROI, ReducedReferenceImage)
复制代码

第二步:采用绘制好的模板,初定位实物图像,得到很多定位出的图像:
  1. dev_update_off ()
  2. dev_close_window ()
  3. read_image (Image, 'smd/smd_samples')
  4. get_image_size (Image, Width, Height)
  5. dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
  6. dev_display (Image)
  7. dev_set_line_width (2)
  8. set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
  9. * 创建人工程序绘制的模板
  10. create_reference_image (ReducedReferenceImage)
  11. * 模板匹配定位(带scale)
  12. create_scaled_shape_model (ReducedReferenceImage, 0, rad(-1), rad(2), 0, 0.77, 0.78, 0, 'point_reduction_low', 'use_polarity', 30, 3, ModelID)
  13. * get_shape_model_params (ModelID, NumLevels, AngleStart, AngleExtent, AngleStep, ScaleMin, ScaleMax, ScaleStep, Metric, MinContrast)
  14. MinScore1 := 0.65
  15. find_scaled_shape_model (Image, ModelID, rad(0), rad(180), 0.77, 0.78, MinScore1, 0, 0, 'interpolation', 2, 0.7, Row, Column, Angle, Scale, Score)
  16. * 显示匹配定位的结果
  17. dev_set_window (WindowHandle)
  18. dev_display (Image)
  19. dev_set_color ('green')
  20. dev_display_shape_matching_results (ModelID, 'green', Row, Column, Angle, Scale, Scale, 0)
  21. disp_message (WindowHandle, 'MinScore: ' + MinScore1, 'window', -1, -1, 'black', 'true')
复制代码

第三步:获取平均模板图像,通过扣取定位的结果,求解多个结果的平均值,即平均模板,增强鲁棒性;
  1. * 获取全部的匹配定位结果
  2. area_center (ReducedReferenceImage, Area, RowRef, ColumnRef)
  3. get_image_size (ReducedReferenceImage, RefImgWidth, RefImgHeight)
  4. dev_open_window (0, Width + 12, 3 * RefImgWidth, 3 * RefImgHeight, 'black', WindowHandleAvg)
  5. dev_set_window (WindowHandleAvg)
  6. dev_set_line_width (2)
  7. get_shape_model_contours (ModelContours, ModelID, 1)
  8. gen_empty_obj (Templates)
  9. for K := 0 to |Row| - 1 by 1
  10.     * 仿射变换--平移
  11.     vector_angle_to_rigid (RowRef, ColumnRef, 0, Row[K], Column[K], Angle[K], HomMat2DTranslate)
  12.     * 仿射变换--缩放
  13.     hom_mat2d_scale (HomMat2DTranslate, Scale[K], Scale[K], Row[K], Column[K], HomMat2DScale)
  14.     * 仿射变换--逆变换
  15.     hom_mat2d_invert (HomMat2DScale, HomMat2DInvert)
  16.     affine_trans_image (Image, ImageAffinTrans, HomMat2DInvert, 'constant', 'false')
  17.     crop_rectangle1 (ImageAffinTrans, ImagePart, 0, 0, 80, 130)
  18.     concat_obj (Templates, ImagePart, Templates)
  19.     * 显示结果
  20.     dev_display_shape_matching_results (ModelID, 'yellow', Row[K], Column[K], Angle[K], Scale[K], Scale[K], 0)
  21.     dev_set_window (WindowHandleAvg)
  22.     dev_display (ImagePart)
  23. endfor
  24. * 将多通道的Templates,求解其平均值 Average template image
  25. channels_to_image (Templates, MultiChannelImage)
  26. mean_n (MultiChannelImage, ImageMean)
  27. dev_set_window (WindowHandleAvg)
  28. dev_display (ImageMean)
复制代码

第四步:采用平均模板图像构建find_shape_model进行模板定位匹配,得到结果如上图所示。
  1. * 采用平均模板图像进行模板定位匹配
  2. create_scaled_shape_model (ImageMean, 0, rad(-1), rad(2), 0, 0.77, 0.78, 0, 'point_reduction_low', 'use_polarity', 45, 30, ModelIDAvg)
  3. MinScore2 := 0.8
  4. find_scaled_shape_model (Image, ModelIDAvg, rad(-1), rad(2), 0.77, 0.78, MinScore2, 0, 0, 'interpolation', 2, 0.5, Row, Column, Angle, Scale, Score)
  5. * 显示匹配定位的结果
  6. dev_set_window (WindowHandleAvg)
  7. dev_display (Image)
  8. dev_set_color ('green')
  9. dev_display_shape_matching_results (ModelIDAvg, 'green', Row, Column, Angle, Scale, Scale, 0)
  10. disp_message (WindowHandleAvg, 'MinScore: ' + MinScore2, 'window', -1, -1, 'black', 'true')
  11. * 清理内存
  12. clear_shape_model (ModelIDAvg)
  13. clear_shape_model (ModelID)
复制代码


















本帖子中包含更多资源

您需要 登录 才可以下载或查看,没有帐号?立即注册

x
算法QQ  3283892722
群智能算法链接http://halcom.cn/forum.php?mod=forumdisplay&fid=73
回复

使用道具 举报

1323

主题

1551

帖子

0

金钱

管理员

Rank: 9Rank: 9Rank: 9

积分
22647
 楼主| 发表于 2023-3-7 19:34:05 | 显示全部楼层
算法QQ  3283892722
群智能算法链接http://halcom.cn/forum.php?mod=forumdisplay&fid=73
回复

使用道具 举报

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

Python|Opencv|MATLAB|Halcom.cn ( 蜀ICP备16027072号 )

GMT+8, 2024-11-22 16:13 , Processed in 0.226497 second(s), 25 queries .

Powered by Discuz! X3.4

Copyright © 2001-2021, Tencent Cloud.

快速回复 返回顶部 返回列表