Halcon之模板匹配

发布时间:2026/9/12 11:50:33

Halcon之模板匹配 特别注意事项常规模板匹配仅支持刚性变换平移、旋转、均匀缩放。无法处理透视形变若目标存在透视倾斜必须先通过标定和图像校正消除形变才能匹配。且仅返回目标的位置、旋转角、缩放系数无法描述透视形变。若是需要匹配发生倾斜形变的目标参考Halcon案例create_planar_uncalib_deformable_model_xld.hdev该案例展示直接从XLD亚像素轮廓生成模型无需原始模板图像支持平面透视投影形变Homography变换。可应对倾斜拍摄带来的轮廓透视拉伸完全无需相机标定直接在像素坐标系下完成匹配直接返回包含透视变换的2D投影映射参数。常见的模板匹配有三种基于灰度值由于该模板是基于灰度值进行匹配导致使用时要求图像的光照稳定有较高的要求在实际中使用的不多。基于互相关NCC可以解决光照不均不足光照变化的场景以及焦点变化的场景图像模糊的模板匹配。*特别需要注意使用create_ncc_model创建模板后模板区域角度将自动旋转到0度。场景1焦距变化图片模糊。*本例的目的是检查ncc检测的偏差 *由其参考模型在镜头焦点变化时得到,即在图片模糊时也可很好查找到目标 *迭代遍历序列。这里使用的匹配方案是 * NCC模型。被检测模型在x和y方向上的总体运动 *最终被描绘出来以像素为单位。 * dev_close_window () read_image (ImageRef, pcb_focus/pcb_focus_telecentric_061) get_image_size (ImageRef, Width, Height) dev_open_window_fit_image (ImageRef, 0, 0, -1, -1, WindowHandle) set_display_font (WindowHandle, 14, mono, true, false) dev_update_off () dev_set_draw (margin) dev_set_color (green) dev_set_line_width (1) * * create ncc model gen_rectangle1 (ModelRegion, 81.5, 148.5, 419.5, 633.5) reduce_domain (ImageRef, ModelRegion, TemplateImage) dev_clear_window () dev_display (TemplateImage) disp_message (WindowHandle, Creating the ncc model may take a few seconds ... , image, -1, -1, black, true) *特别需要注意使用create_ncc_model创建模板后模板区域角度将自动旋转到0度 *例如模板区域旋转角度是20度使用create_ncc_model创建模板后该区域旋转角度变为0度。 create_ncc_model (TemplateImage, auto, -rad(5), rad(10), auto, use_polarity, ModelID) * * find ncc model in image area_center (ModelRegion, Area, ModelRow, ModelColumn) Rows : [] Columns : [] for Index : 1 to 121 by 1 read_image (Image, pcb_focus/pcb_focus_telecentric_ Index$03 .png) find_ncc_model (Image, ModelID, -rad(5), rad(10), 0.5, 1, 0.5, true, 0, Row, Column, Angle, Score) Rows : [Rows,Row] Columns : [Columns,Column] *特别需要注意使用create_ncc_model创建模板后模板区域角度将自动旋转到0度所以这里是0 vector_angle_to_rigid (ModelRow, ModelColumn, 0, Row, Column, Angle, HomMat2D) affine_trans_region (ModelRegion, RegionAffineTrans, HomMat2D, nearest_neighbor) set_system (flush_graphic, false) dev_display (Image) dev_display (RegionAffineTrans) gen_cross_contour_xld (Cross, Row, Column, 12, rad(0)) dev_display (Cross) disp_message (WindowHandle, Finding ncc model in image: Index$03, image, -1, -1, white, false) set_system (flush_graphic, true) disp_line (WindowHandle, -101, -101, -99, -99) endfor * * display results dev_display (Image) wait_seconds (1) gen_contour_polygon_xld (Contour, Rows, Columns) fit_line_contour_xld (Contour, tukey, -1, 0, 5, 2, RowBegin, ColBegin, RowEnd, ColEnd, Nr, Nc, Dist) gen_contour_polygon_xld (Regression, [RowBegin,RowEnd], [ColBegin,ColEnd]) * resize window so that pixels appear square Ratio : (394 - 389 1) / real(252 - 250 1) dev_resize_window_fit_size (0, 0, Height * Ratio, Height, 640, 480) dev_set_part (250, 389, 252, 394) dev_display (ImageRef) dev_set_color (green) dev_display (Contour) dev_set_color (yellow) dev_display (Regression) disp_message (WindowHandle, [Deviation of ncc matches in (x,y)-direction,while defocusing lens], image, 249.75, 389, white, false) disp_message (WindowHandle, Deviation (green), Tukey\s robust regression (yellow) in pixel, image, 251.75, 389.25, white, false) * * close all handles clear_ncc_model (ModelID)模板图像模糊的图像也能完成模板查找场景2亮度变化* This example program shows how to use HALCONs correlation-based * matching. In particular it demonstrates the robustness of this method against * linear illumination changes. The training is performed in an image with good * illumination. The matching is applied in images where the exposure time varies * extremely from very short to very long. read_image (Image, cap_exposure/cap_exposure_03) get_image_size (Image, Width, Height) dev_close_window () dev_open_window (0, 0, Width, Height, black, WindowHandle) dev_update_window (off) gen_circle (Circle, 246, 336, 150) area_center (Circle, Area, RowRef, ColumnRef) reduce_domain (Image, Circle, ImageReduced) create_ncc_model (ImageReduced, auto, 0, 0, auto, use_polarity, ModelID) dev_set_draw (margin) dev_display (Image) dev_display (Circle) stop () Rows : [] Cols : [] for J : 1 to 10 by 1 read_image (Image, cap_exposure/cap_exposure_ J$02) find_ncc_model (Image, ModelID, 0, 0, 0.5, 1, 0.5, true, 0, Row, Column, Angle, Score) vector_angle_to_rigid (RowRef, ColumnRef, 0, Row, Column, 0, HomMat2D) affine_trans_region (Circle, RegionAffineTrans, HomMat2D, nearest_neighbor) Rows : [Rows,Row] Cols : [Cols,Column] dev_display (Image) dev_display (RegionAffineTrans) stop () endfor * Compute the standard deviation of the found positions. If the individual * positions in Rows and Cols are examined, it can be seen that the standard * deviation is caused mainly by the last four images, which are severely * overexposed. StdDevRows : deviation(Rows) StdDevCols : deviation(Cols) clear_ncc_model (ModelID)模板图片不同亮度下均可匹配成功。样例图片基于形状的匹配多模板匹配参考Halcon样例位于HDevelop示例-方法-模板匹配基于形状-pm_multiple_models.hdev*多模板匹配案例 dev_close_window () dev_open_window (0, 0, 512, 512, black, WindowHandle) * dev_update_pc (off) * dev_update_window (off) * dev_update_var (off) *模型句柄数组 ModelIdS:[] *模型对应轮廓的开始索引数组 IndexS:[] *模型对应轮廓结束索引数组 IndexE:[] *多模型轮廓集合 gen_empty_obj (Models) *定义显示轮廓的颜色 Colors:[red,green,blue] *加载模板图片 for Index : 1 to 3 by 1 read_image (Image, metal-parts/metal-part-model-Index$02) get_image_size (Image, Width, Height) dev_resize_window_fit_size (0, 0, Width, Height, -1, -1) binary_threshold (Image, Region, max_separability, light, UsedThreshold) smallest_rectangle1 (Region, Row1, Column1, Row2, Column2) gen_rectangle1 (Rectangle, Row1, Column1, Row2, Column2) *获取模板图像 dilation_rectangle1 (Rectangle, RegionDilation, 10, 10) reduce_domain (Image, RegionDilation, ImageReduced) *创建模板 create_shape_model (ImageReduced, auto, rad(0), rad(360), auto, auto, use_polarity, auto, auto, ModelID) *获取模板轮廓方法1 *connection (Region, ConnectedRegions) *获取其轮廓 * gen_contour_region_xld (ConnectedRegions, Contours, border_holes) *获取模板轮廓方法2,注意ModelRegions是边界而不是区域 * inspect_shape_model (ImageReduced, ModelImages, ModelRegions, 1, 30) * connection (ModelRegions, ConnectedRegions) * select_shape (ConnectedRegions, SelectedRegions, area, and, 27.98, 500) * union1 (SelectedRegions, RegionUnion) * gen_contour_region_xld (RegionUnion, Contours, border) *获取模板轮廓方法3 get_shape_model_contours (ModelContours, ModelID, 1) select_contours_xld (ModelContours, SelectedContours, contour_length, 20, 2000, -0.5, 0.5) count_obj (SelectedContours, ContoursNumber) count_obj (Models, ModelsNumber) *将模型轮廓添加到集合中 concat_obj (Models, SelectedContours,Models) *记录该模型轮廓的起始索引与结束索引 IndexS:[IndexS,ModelsNumber1] IndexE:[IndexE,ModelsNumberContoursNumber] *将轮廓添加到集合中 *concat_obj (Models,SelectedContours, Models) *将模板句柄添加到数组 ModelIdS:[ModelIdS,ModelID] disp_message (WindowHandle, 创建模板Index, window, 20, 20, blue, true) wait_seconds (0.5) endfor stop () dev_set_line_width (2) *进行模板匹配 for i : 1 to 15 by 1 read_image (Image, metal-parts/metal-parts-i$02) dev_display (Image) *进行多模板查找。贪婪系数可影响查找数量为0时精确查找尽量查找到所有速度慢为1时快速查找可能有遗漏 find_shape_models (Image, ModelIdS, rad(0), rad(360), 0.5, 0, 0.5, least_squares, 0, 0.6, Row, Column, Angle, Score, Model) *计算匹配到的数量 num:|Score| *进行匹配成功的模板轮廓的查找仿射变换 for j : 0 to num-1 by 1 *轮廓集合中获取匹配成功的模型轮廓. *IndexS[Model[j]]:该模板轮廓在轮廓集合中的起始索引 *IndexE[Model[j]]-IndexS[Model[j]]1构成该模板轮廓的轮廓数量 copy_obj (Models, ObjectsSelected,IndexS[Model[j]] , IndexE[Model[j]]-IndexS[Model[j]]1) *进行反射变换 vector_angle_to_rigid (0, 0, 0, Row[j], Column[j], Angle[j], HomMat2D) *设置显示的颜色 dev_set_color (Colors[Model[j]]) affine_trans_contour_xld (ObjectsSelected, ContoursAffinTrans, HomMat2D) dev_display (ContoursAffinTrans) endfor set_display_font (WindowHandle, 20, mono, true, false) disp_message (WindowHandle, 查找到num个, window, 20, 20, white, false) stop () endfor for t : 0 to 2 by 1 clear_shape_model (ModelIdS[t]) endfor如果在运行时出现如下显示在图形窗口右键-更新窗口-取消“在运行模式”选项左上角轮廓将不再显示。
延伸阅读

更多相关文章

2026/9/12 11:50:33

JDBC核心技术解析与高效数据库交互实践

/* MD / 富文本中的 .toc(含博客园搬家等嵌套结构);.toc-box 在侧栏,不受影响 */#content_views .toc,/* 编辑器常在目录前后插入空 p(:empty 仍占 20px),一并去掉避免顶空隙 */#content_views.markdown_views > p:empty:has(+ .toc),#content_views.markdown_views …

2026/9/12 11:50:33

使用string和string_view(三)——字符串格式化

目录1. std::format2. 格式说明符2.1 width2.2 [fill]align2.3 sign2.4 \#2.5 type2.6 precision2.7 0参考1. std::format 直到C20之前,字符串的格式化一般是通过printf()之类的C风格函数或是C的I/O流完成的:   1. C风格函数:不推荐&#x…

2026/9/12 11:50:33

基于3D物流图与改进奇里科夫图的图像混合加密方案

1. 项目概述在数字图像安全传输领域,图像加密技术一直是研究热点。这个项目提出了一种结合3D物流图和改进型奇里科夫图的混合加密方案,通过Matlab实现了一套完整的图像加密解密系统。我在实际测试中发现,这种混合算法在安全性、抗攻击性和执行…

2026/9/12 12:45:35

dcode 如何在会话中切换模型并持久化模型配置?

dcode 如何在会话中切换模型并持久化模型配置? 【免费下载链接】deepagents The batteries-included agent harness. 项目地址: https://gitcode.com/GitHub_Trending/de/deepagents 在 deepagents 仓库的终端编码产品 deepagents-code(命令名为 …

2026/9/12 12:45:35

Sunshine 游戏串流主机 30 分钟从安装到串出第一帧画面

Sunshine 游戏串流主机 30 分钟从安装到串出第一帧画面 【免费下载链接】Sunshine Self-hosted game stream host for Moonlight. 项目地址: https://gitcode.com/GitHub_Trending/su/Sunshine 场景切入 一句话定位 客厅电视很大,主机却塞在书房&#xff0…

2026/9/12 12:45:35

Retrospective: [Sprint N / Milestone Name]

Retrospective: [Sprint N / Milestone Name] 【免费下载链接】Claude-Code-Game-Studios Turn Claude Code into a full game dev studio — 49 AI agents, 72 workflow skills, and a complete coordination system mirroring real studio hierarchy. 项目地址: https://gi…

2026/9/12 12:40:34

从一张图片到高清3D模型:Hunyuan3D-2完整使用指南

从一张图片到高清3D模型:Hunyuan3D-2完整使用指南 【免费下载链接】Hunyuan3D-2 High-Resolution 3D Assets Generation with Large Scale Hunyuan3D Diffusion Models. 项目地址: https://gitcode.com/GitHub_Trending/hu/Hunyuan3D-2 做游戏资产、搭VR场景…

2026/9/12 2:05:33

超人会飞不算本事:系统稳定依赖清晰规则与边界设计

开头先不绕弯子。“#斯坦李吐槽dc 所以超人是无缘无故会飞的嘛哈哈哈哈哈哈哈锤哥真是技术人才啊!#雷神 #复联”这类调侃式短标题,第一波冲击力在于它把两个宇宙的角色塞进同一个吐槽箱里,但细想一下就能发现,它真正碰到的根本不是…

2026/9/12 3:55:12

超人VS蜘蛛侠:拆解超级IP的影响力与传播方法论

把“蜘蛛侠 vs 超人”放在 CSDN 上聊,可能很多人第一反应是走错片场了。但如果把这两个角色看成“两个持续运营了 80 多年的文化产品”,你会发现,这场比较本质上是两个不同 IP 策略的长期结果对比:超人赢在定义了整个超级英雄题材…

2026/9/12 10:09:03

基于CNN的调制信号识别:MATLAB实现时频图分类实战

简介:本资源是一套面向通信工程与信号处理方向学习者、研究者的深度学习实践方案,聚焦调制信号自动检测与识别这一典型无线通信任务,解决传统方法依赖人工特征、低信噪比下性能下降等痛点。压缩包共12个文件(10.73MB)&…

2026/9/12 0:04:17

MATLAB仿生优化框架:长鼻浣熊算法多策略融合实现

简介:本资源是一份面向智能优化算法研究者与MATLAB初学者的仿生智能算法实践代码包,聚焦于长鼻浣熊优化算法(COA)的多策略改进与性能验证。针对传统COA易陷局部最优、收敛精度不足等问题,作者融合Circle映射初始化提升…

2026/9/12 0:04:17

【JAVA毕设源码分享】基于 JavaWeb 的校园一卡通管理系统的设计与实现 基于 JavaWeb 的校园卡业务管理系统(程序+文档+代码讲解+一条龙定制)

博主介绍:✌️码农一枚 ,专注于大学生项目实战开发、讲解和毕业🚢文撰写修改等。全栈领域优质创作者,博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java、小程序技术领域和毕业项目实战 ✌️技术范围:&am…

2026/9/12 0:04:17

【JAVA毕设源码分享】基于 Java 的图书馆借阅管理平台的搭建与实现 基于 Java 的图书馆综合管理系统(程序+文档+代码讲解+一条龙定制)

博主介绍:✌️码农一枚 ,专注于大学生项目实战开发、讲解和毕业🚢文撰写修改等。全栈领域优质创作者,博客之星、掘金/华为云/阿里云/InfoQ等平台优质作者、专注于Java、小程序技术领域和毕业项目实战 ✌️技术范围:&am…

2026/9/12 6:29:36

USB Type-C PCB布局分区设计:电源、高速信号与PD协议全攻略

做硬件这行,Type-C接口算是典型的“看着简单,做起来全坑”的东西。光引脚就24个,高低速信号、电源、控制线全部塞在一个小小的连接器里,如果PCB布局不做规划,打样回来基本就是“插上没反应”、“高速掉线”、“静电一打…

2026/9/10 15:19:50

系统编程学习原型如何补齐稳定性边界

系统编程学习原型如何补齐稳定性边界预算有限时&#xff0c;我先优化明显多余的复制&#xff0c;而不是猜测性地换容器。用借用传递只读数据通常就能减少分配&#xff1a; fn parse(line: &str) -> Result<Item, Error> { /* ... */ }用基准确认热点确实在分配&am…

2026/9/12 6:37:43

雨花区哪家财务公司代理记账比较好?

在雨花区&#xff0c;企业处理财税事务常常面临诸多挑战&#xff0c;选择一家靠谱的财务公司至关重要。湖南巨勤财务管理咨询有限公司就是本地正规实体财税服务机构&#xff0c;深耕本地工商财税行业多年&#xff0c;熟悉当地工商局、税务局最新政策与申报流程。主营公司注册、…

还想了解更多?直接咨询顾问

免费诊断 + 免费方案 + 透明报价。

全国咨询热线400-8866-253
免费获取方案
咨询二维码