Halcon模板匹配代码分析(可直接复制粘贴使用)

这段代码是使用 Halcon 机器视觉库编写的,主要实现了形状模型的初始化、训练以及应用的过程。以下是对这段代码的详细分析、原理说明以及延申应用的讨论。

效果图

 

1. 代码总体结构

代码分为两个主要部分:模型初始化部分和模型应用部分。

  • 模型初始化部分:负责读取图像、构建感兴趣区域(ROI)、训练形状模型、获取模型轮廓以及显示模型相关信息。
  • 模型应用部分:设置搜索参数,在图像中查找模型,并获取和显示匹配结果的相关参数。

2. 模型初始化部分

2.1 读取图像
read_image (Image, 'C:/Users/1/Desktop/10.jpg')

这行代码使用 Halcon 的read_image函数从指定路径C:/Users/1/Desktop/10.jpg读取一张图像,并将其存储在Image变量中。这是进行后续处理的基础。

2.2 构建感兴趣区域(ROI)
gen_rectangle1 (ModelRegion, 93.3845, 29.6218, 237.303, 159.938)

gen_rectangle1函数用于生成一个矩形区域,参数分别为矩形的左上角和右下角坐标。这里生成的矩形区域ModelRegion将作为后续处理的感兴趣区域,用于提取模型的特征。

2.3 减少模型模板区域
reduce_domain (Image, ModelRegion, TemplateImage)
boundary (ModelRegion, RegionBorder, 'inner')

reduce_domain函数将图像Image根据感兴趣区域ModelRegion进行裁剪,得到模板图像TemplateImageboundary函数用于获取感兴趣区域ModelRegion的内边界,存储在RegionBorder中。

2.4 计算边界区域的最小外接矩形
smallest_rectangle1(RegionBorder, Row1, Column1, Row2, Column2)

smallest_rectangle1函数计算区域RegionBorder的最小外接矩形,并返回矩形的左上角和右下角坐标Row1Column1Row2Column2

2.5 输出四角坐标
RowTopLeft := Row1
ColumnTopLeft := Column1
RowTopRight := Row1
ColumnTopRight := Column2
RowBottomLeft := Row2
ColumnBottomLeft := Column1
RowBottomRight := Row2
ColumnBottomRight := Column2

这里将最小外接矩形的四角坐标分别存储在相应的变量中,方便后续处理和显示。

2.6 创建和训练形状模型
create_generic_shape_model (ModelID)
set_generic_shape_model_param (ModelID, 'metric', 'use_polarity')
train_generic_shape_model (TemplateImage, ModelID)

  • create_generic_shape_model函数创建一个通用形状模型,并返回模型的 ID(ModelID)。
  • set_generic_shape_model_param函数设置形状模型的参数,这里设置了匹配度量为use_polarity,表示在匹配时考虑图像的极性信息。
  • train_generic_shape_model函数使用模板图像TemplateImage对形状模型进行训练,使得模型能够识别与模板相似的形状。
2.7 获取模型轮廓
get_shape_model_contours (ModelContours, ModelID, 1)

get_shape_model_contours函数获取形状模型的轮廓,存储在ModelContours中。参数1表示获取模型的第 1 个轮廓(对于复杂模型可能有多个轮廓)。

2.8 支持显示模型
area_center (ModelRegion, ModelRegionArea, RefRow, RefColumn)
vector_angle_to_rigid (0, 0, 0, RefRow, RefColumn, 0, HomMat2D)
affine_trans_contour_xld (ModelContours, TransContours, HomMat2D)

  • area_center函数计算感兴趣区域ModelRegion的面积和中心坐标,存储在ModelRegionAreaRefRowRefColumn中。
  • vector_angle_to_rigid函数创建一个刚性变换矩阵HomMat2D,将模型轮廓从世界坐标系变换到图像坐标系。
  • affine_trans_contour_xld函数使用变换矩阵HomMat2D对模型轮廓ModelContours进行仿射变换,得到变换后的轮廓TransContours
2.9 显示模型轮廓
dev_display (Image)
dev_set_color ('green')
dev_set_draw ('margin')
dev_display (ModelRegion)
dev_display (TransContours)
stop ()

  • dev_display函数用于在图形窗口中显示图像和区域。这里先显示原始图像Image,然后设置显示颜色为绿色,显示模式为边界。
  • 接着显示感兴趣区域ModelRegion和变换后的模型轮廓TransContours
  • stop函数暂停程序执行,等待用户操作(如按下键盘上的键)。

3. 模型应用部分

3.1 设置搜索参数
set_generic_shape_model_param (ModelID, 'border_shape_models', 'false')

set_generic_shape_model_param函数设置形状模型的搜索参数,这里将border_shape_models设置为false,表示在搜索时不考虑模型的边界。

3.2 查找模型
find_generic_shape_model (Image, ModelID, MatchResultID, NumMatchResult)

find_generic_shape_model函数在图像Image中查找与形状模型ModelID匹配的区域。匹配结果存储在MatchResultID中,匹配的数量存储在NumMatchResult中。

3.3 检索结果
for I := 0 to NumMatchResult-1 by 1
    dev_display (Image)
    get_generic_shape_model_result_object (MatchContour, MatchResultID, I, 'contours')
    dev_set_color ('green')
    dev_display (MatchContour)
    get_generic_shape_model_result (MatchResultID, I, 'row', Row)
    get_generic_shape_model_result (MatchResultID, I, 'column', Column)
    get_generic_shape_model_result (MatchResultID, I, 'angle', Angle)
    get_generic_shape_model_result (MatchResultID, I, 'scale_row', ScaleRow)
    get_generic_shape_model_result (MatchResultID, I, 'scale_column', ScaleColumn)
    get_generic_shape_model_result (MatchResultID, I, 'hom_mat_2d', HomMat2D)
    get_generic_shape_model_result (MatchResultID, I, 'score', Score)
    stop ()
endfor

这部分代码通过循环遍历所有匹配结果:

  • 显示原始图像Image
  • 使用get_generic_shape_model_result_object函数获取匹配区域的轮廓,存储在MatchContour中,并显示该轮廓。
  • 使用get_generic_shape_model_result函数获取每个匹配结果的相关参数,如匹配点的行坐标Row、列坐标Column、旋转角度Angle、缩放比例ScaleRowScaleColumn、变换矩阵HomMat2D以及匹配分数Score
  • stop函数暂停程序执行,等待用户操作。

4. 原理说明

4.1 形状模型的原理

形状模型是一种用于图像匹配和目标识别的技术。它通过对目标的形状特征进行建模,然后在图像中搜索与模型匹配的区域。形状模型通常基于图像的轮廓信息,通过计算轮廓的几何特征(如角度、长度、曲率等)来描述目标的形状。

在 Halcon 中,create_generic_shape_model函数创建一个通用形状模型,train_generic_shape_model函数使用模板图像对模型进行训练。训练过程中,Halcon 会提取模板图像的形状特征,并将其存储在模型中。

4.2 匹配度量的原理

匹配度量是用于衡量模型与图像中区域匹配程度的指标。在代码中,使用set_generic_shape_model_param函数设置匹配度量为use_polarity,表示在匹配时考虑图像的极性信息。极性信息通常指图像的灰度值分布,通过考虑极性信息可以提高匹配的准确性。

4.3 仿射变换的原理

仿射变换是一种几何变换,用于将模型轮廓从世界坐标系变换到图像坐标系。在代码中,使用vector_angle_to_rigid函数创建一个刚性变换矩阵HomMat2D,然后使用affine_trans_contour_xld函数对模型轮廓进行仿射变换。仿射变换包括平移、旋转和缩放操作,可以将模型轮廓准确地对齐到图像中的目标位置。

整体代码

* 
* Matching 01: ************************************************
* Matching 01: BEGIN of generated code for model initialization
* Matching 01: ************************************************
* 
* Matching 01: Obtain the model image
read_image (Image, 'C:/Users/1/Desktop/10.jpg')
* 
* Matching 01: Build the ROI from basic regions
gen_rectangle1 (ModelRegion, 93.3845, 29.6218, 237.303, 159.938)
* 
* Matching 01: Reduce the model template
reduce_domain (Image, ModelRegion, TemplateImage)
boundary (ModelRegion, RegionBorder, 'inner')
* 计算边界区域的最小外接矩形
smallest_rectangle1(RegionBorder, Row1, Column1, Row2, Column2)

* 输出四角坐标
* 左上角坐标
RowTopLeft := Row1
ColumnTopLeft := Column1
* 右上角坐标
RowTopRight := Row1
ColumnTopRight := Column2
* 左下角坐标
RowBottomLeft := Row2
ColumnBottomLeft := Column1
* 右下角坐标
RowBottomRight := Row2
ColumnBottomRight := Column2
* 
* Matching 01: Create and train the shape model
create_generic_shape_model (ModelID)
* Matching 01: set the model parameters
set_generic_shape_model_param (ModelID, 'metric', 'use_polarity')
train_generic_shape_model (TemplateImage, ModelID)
* 
* Matching 01: Get the model contour for transforming it later into the image
get_shape_model_contours (ModelContours, ModelID, 1)
* 
* Matching 01: Support for displaying the model
* Matching 01: Get the reference position
area_center (ModelRegion, ModelRegionArea, RefRow, RefColumn)
vector_angle_to_rigid (0, 0, 0, RefRow, RefColumn, 0, HomMat2D)
affine_trans_contour_xld (ModelContours, TransContours, HomMat2D)
* 
* Matching 01: Display the model contours
dev_display (Image)
dev_set_color ('green')
dev_set_draw ('margin')
dev_display (ModelRegion)
dev_display (TransContours)
stop ()
* 
* Matching 01: END of generated code for model initialization
* Matching 01:  * * * * * * * * * * * * * * * * * * * * * * *
* Matching 01: BEGIN of generated code for model application
* Matching 01: Set the search paramaters
set_generic_shape_model_param (ModelID, 'border_shape_models', 'false')
* Matching 01: The following operations are usually moved into
* Matching 01: that loop where the acquired images are processed
* 
* Matching 01: Find the model
find_generic_shape_model (Image, ModelID, MatchResultID, NumMatchResult)
* 
* Matching 01: Retrieve results
for I := 0 to NumMatchResult-1 by 1
    * 
    * Matching 01: Display the detected match
    dev_display (Image)
    get_generic_shape_model_result_object (MatchContour, MatchResultID, I, 'contours')
    dev_set_color ('green')
    dev_display (MatchContour)
    * 
    * Matching 01: Retrieve parameters of the detected match
    get_generic_shape_model_result (MatchResultID, I, 'row', Row)
    get_generic_shape_model_result (MatchResultID, I, 'column', Column)
    get_generic_shape_model_result (MatchResultID, I, 'angle', Angle)
    get_generic_shape_model_result (MatchResultID, I, 'scale_row', ScaleRow)
    get_generic_shape_model_result (MatchResultID, I, 'scale_column', ScaleColumn)
    get_generic_shape_model_result (MatchResultID, I, 'hom_mat_2d', HomMat2D)
    get_generic_shape_model_result (MatchResultID, I, 'score', Score)
    stop ()
endfor
* 
* Matching 01: *******************************************
* Matching 01: END of generated code for model application
* Matching 01: *******************************************
* 

5. 延申应用

5.1 工业检测

在工业生产中,形状模型可以用于检测产品的外观缺陷。例如,检测电子元件的形状是否符合标准,检测机械零件的尺寸是否正确等。通过训练形状模型来描述标准产品的形状,然后在生产线上的图像中查找与模型匹配的产品,从而实现自动化检测。

5.2 机器人视觉

形状模型可以应用于机器人视觉系统,帮助机器人识别和抓取目标物体。机器人可以通过摄像头获取图像,然后使用形状模型在图像中查找目标物体的位置和姿态,从而控制机器人的手臂进行准确的抓取操作。

5.3 医学图像分析

在医学图像分析中,形状模型可以用于分割和识别医学图像中的器官和病变区域。例如,通过训练形状模型来描述心脏、肝脏等器官的形状,然后在医学图像中查找与模型匹配的区域,从而实现器官的自动分割和分析。

5.4 安防监控

形状模型可以应用于安防监控系统,用于检测和识别异常行为。例如,通过训练形状模型来描述人体的正常行走姿势,然后在监控视频中查找与模型不匹配的行为,从而实现异常行为的检测和报警。

5.5 交通监控

在交通监控中,形状模型可以用于识别车辆的类型和车牌号码。通过训练形状模型来描述不同类型车辆的形状和车牌的特征,然后在交通监控图像中查找与模型匹配的区域,从而实现车辆的自动识别和管理。

综上所述,这段 Halcon 代码实现了形状模型的初始化和应用,其原理基于形状特征的提取和匹配,具有广泛的延申应用领域。通过对代码的理解和应用,可以在不同的领域中实现自动化的目标识别和检测任务。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值