Halcon 检测物体定位点

这篇文章详细介绍了Halcon库中的几个关键功能,包括获取图像定义域、添加灰度通道、形状模型的搜索和匹配。通过实例展示了如何使用这些函数来检测和定位瓶子和标签在序列图像中的相对位置。
摘要由CSDN通过智能技术生成

get_domain 返回所有输入图像的定义域作为一个区域

Halcon 中的区域

get_domain(Image : Domain : : )
Image :  图像 (input_object): 类型可以是 (多通道-) 图像数组,对象类型为字节、方向、循环、int1、int2、uint2、int4、int8、实数、复数或矢量场。
    这是输入图像。
 Domain :(output_object): 类型是区域数组,对象类型。
    这是输入图像的定义域。

add_channels 给区域增加灰度值

add_channels(Regions, Image : GrayRegions : : )
region(-array) → object: 这个参数是指输入的区域,通常是一个数组,描述了图像中的某些区域,但不包含像素值。
Image (input_object) (multichannel-)image → object: 这个参数是指包含像素值的输入图像,可能是多通道图像。
GrayRegions (output_object) image(-array) → object: 这个参数表示输出的图像或图像数组,其中包含了具有像素值的区域,每个输入区域对应一个输出图像。

find_shape_model 发现匹配模板

find_shape_model(Image : :  //搜索图像
                ModelID, //模板句柄
                AngleStart,  // 搜索时的起始角度
                AngleExtent, //搜索时的角度范围,必须与创建模板时的有交集
                MinScore, //最小匹配值,输出的匹配的得分Score 大于该值
                NumMatches, //定义要输出的匹配的最大个数
                MaxOverlap, //当找到的目标存在重叠时,且重叠大于该值时选择一个好的输出
//如果MaxOverlap=0, 找到的目标区域不能存在重叠, 如果MaxOverla p=1,所有找到的目标区域都要返回。
                SubPixel, //计算精度的设置,五种模式,多选2,3
                NumLevels, //搜索时金字塔的层数
                Greediness : //贪婪度,搜索启发式,一般都设为0.9,越高速度快,容易出现找不到的情况
                           //0安全慢;1块不稳定;其他就是介于中间值 
                Row,Column, Angle, Score) //输出匹配位置的行和列坐标、角度、得分。

find_shape_models 发现最佳模板

find_shape_models(Image : : ModelIDs, AngleStart, AngleExtent, MinScore, NumMatches, MaxOverlap, SubPixel, NumLevels, Greediness : Row, Column, Angle, Score, Model)
    Image (input_object):这个参数表示输入图像数据,这可能是多通道图像数组,用于在其中找到模型。
    ModelIDs (input_control):这是模板或模型的ID数组,用于指定哪一个或多个模型将在图像中寻找。
    AngleStart (input_control):指定模型可能的最小旋转角度,单位是弧度。例如,-0.39弧度表示模型可能有一些初始旋转。
    AngleExtent (input_control):模型的旋转角度范围,单位为弧度。例如,0.79弧度表示可以允许模型在指定角度范围内旋转。
    MinScore (input_control):找到模型实例的最小评分标准。01之间的值,表示匹配得分的下限。
    NumMatches (input_control):要找到的模型实例的数量。如果为0,表示找出所有匹配的实例。
    MaxOverlap (input_control):指定模型实例之间的最大重叠程度。值在01之间,表示允许多大程度的重叠。
    SubPixel (input_control):选择是否使用子像素精度进行计算。如果不等于 'none',表示使用特定的子像素方法。
    NumLevels (input_control):用于匹配时的金字塔层级数量。更多的层级可能提供更快的匹配,但可能牺牲准确性。
    Greediness (input_control):搜索算法的“贪心度”。0表示安全但慢,1表示快速但可能错过匹配。值在01之间。
    Row (output_control)Column (output_control):找到的模型实例的行坐标和列坐标。
    Angle (output_control):找到的模型实例的旋转角度。
    Score (output_control):找到的模型实例的匹配得分。
    Model (output_control):找到的模型实例的索引。

示例

* This example program checks the right position of a label on
* a shampoo bottle.
* First, two shape models for the bottle and the label are created.
* Then, the models are searched in a sequence of images,
* and it is checked, if their relative position is within the
* specified tolerances.
* 
* init visualization
dev_update_off ()
dev_set_draw ('margin')
dev_set_line_width (2)
* 
* load reference image for model preparation
* 0.读取图片
set_system ('clip_region', 'false')
read_image (Image, 'packaging/shampoo_01')
dev_close_window ()
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
dev_display (Image)
* 
* prepare the shape model for matching
* 
* generate first model region and search ROI
* 1.产生第一个感兴趣的区域
BottleModelRow := 131
BottleModelColumn := 370
BottleModelLength1 := 350
BottleModelLength2 := 35
BottleModelPhi := -0.1093
* 绘制矩形
gen_rectangle2 (Rectangle1, BottleModelRow, BottleModelColumn, BottleModelPhi, BottleModelLength1, BottleModelLength2)
gen_rectangle2 (Rectangle2, BottleModelRow + 220, BottleModelColumn, -BottleModelPhi, BottleModelLength1, BottleModelLength2)
* 将矩形联合
union2 (Rectangle1, Rectangle2, TemplateBottleRegion)
* 获取整个图像的区域
get_domain (Image, Domain)
* 计算区域的交集
intersection (TemplateBottleRegion, Domain, TemplateBottleRegion)
* 获取相交的中心点区域和坐标
area_center (TemplateBottleRegion, Area, RowBottleRef, ColumnBottleRef)
* 产生一个圆
gen_circle (SearchROIBottle, RowBottleRef, ColumnBottleRef, 40)
* 将感兴趣的区域裁剪
reduce_domain (Image, TemplateBottleRegion, ImageReduced)
* 
* create shape model
* 2.创建形状匹配模型 ModelIDBottle
create_shape_model (ImageReduced, 5, -rad(3), rad(6), 0, 'auto', 'use_polarity', 25, 3, ModelIDBottle)
* 
* display shape model
dev_display (Image)
dev_set_color ('forest green')
dev_display (TemplateBottleRegion)
dev_set_color ('slate blue')
disp_message (WindowHandle, 'bottle shape model', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* 
* generate second model region and search ROI
* 3.产生第二个感兴趣的区域
LabelRow1 := 180
LabelRow2 := 310
LabelColumn1 := 50
LabelColumn2 := 470
* 产生一个矩形
gen_rectangle1 (TemplateLabelRegion, LabelRow1, LabelColumn1, LabelRow2, LabelColumn2)
* 获取区域的中心点以及坐标
area_center (TemplateLabelRegion, Area1, RowLabelRef, ColumnLabelRef)
* 产生一个圆
gen_circle (SearchROILabel, RowLabelRef, ColumnLabelRef, 60)
* 将感兴趣的区域裁剪
reduce_domain (Image, TemplateLabelRegion, ImageReduced)
* 4.检查已创建的形状模型并获得其属性信息
inspect_shape_model (ImageReduced, ModelImages, ModelRegions, 1, 25)
* 
* create shape model
* 创建模板 ModelIDLabel1
create_shape_model (ImageReduced, 5, rad(-3), rad(6), 0, 'auto', 'use_polarity', 25, 5, ModelIDLabel1)
* 将匹配模板选择180度创建模板 ModelIDLabel2
create_shape_model (ImageReduced, 5, rad(180 - 3), rad(6), 0, 'auto', 'use_polarity', 25, 5, ModelIDLabel2)
ModelIDsLabel := [ModelIDLabel1,ModelIDLabel2]
* 
* display label model
dev_display (Image)
dev_set_color ('forest green')
dev_display (TemplateLabelRegion)
dev_set_color ('slate blue')
dev_display (ModelRegions)
disp_message (WindowHandle, 'create label shape model', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* 
* MainLoop
* 
* check relative positions of label and bottle in all images
* 5.检测位置
for Index := 1 to 13 by 1
    read_image (Image, 'packaging/shampoo_' + Index$'.2')
    dev_display (Image)
    disp_message (WindowHandle, 'check label position', 'window', 12, 12, 'black', 'true')
    count_seconds (s1)
    * 裁剪第一次的搜索圆
    reduce_domain (Image, SearchROIBottle, ImageReduced)
    * 通过ModelIDBottle寻找搜索圆,获取ScoreBottle
    find_shape_model (ImageReduced, ModelIDBottle, -rad(3), rad(6), 0.7, 1, 0.5, 'least_squares', 0, 0.9, RowBottle, ColumnBottle, AngleBottle, ScoreBottle)
    * prepare search ROIs in search image
    * 将两个搜索标记合并
    concat_obj (SearchROILabel, SearchROILabel, SearchROIs)
    add_channels (SearchROIs, Image, GrayRegions)
    * 
    * search shape models
    * 通过ModelIDsLabel 查找匹配模板获取Score得分,注意角度范围,并且获取FoundModel
    find_shape_models (GrayRegions, ModelIDsLabel, [rad(-3),rad(180 - 3)], [rad(6),rad(6)], 0.6, 1, 1, 'interpolation', 0, 0.9, Row, Column, Angle, Score, FoundModel)
    count_seconds (s2)
    if (|Score| != 1 or |ScoreBottle| != 1)
        disp_message (WindowHandle, 'Model not found', 'window', 40, 12, 'red', 'true')
    else
        * 如果模板
        if (ModelIDsLabel[FoundModel] == ModelIDLabel2)
            * 显示图标选择 180°
            disp_message (WindowHandle, 'Label rotated by 180°', 'window', 40, 12, 'red', 'true')
        else
            * calculate y deviation
            * 计算y的偏移量
            Diffy := (RowBottle - Row) - (RowBottleRef - RowLabelRef)
            * calculate x deviation
            * 计算x的偏移量
            Diffx := (ColumnBottle - Column) - (ColumnBottleRef - ColumnLabelRef)
            * 
            *  check the rotation angle of the bottle label
            * 计算旋转角度
            Diffa := deg(AngleBottle - Angle)
            * 
            * calculate the time elapsed
            Time := s2 - s1
            * 
            * display results
            * 显示结果
            Color := 'black'
            ModelColor := 'forest green'
            if (abs(Diffx) > 3)
                Color := [Color,'red']
                ModelColor := 'red'
            else
                Color := [Color,'forest green']
            endif
            if (abs(Diffy) > 1)
                Color := [Color,'red']
                ModelColor := 'red'
            else
                Color := [Color,'forest green']
            endif
            if (abs(Diffa) > 1)
                Color := [Color,'red']
                ModelColor := 'red'
            else
                Color := [Color,'forest green']
            endif
            * 显示匹配结果
            dev_display_shape_matching_results (ModelIDsLabel, ['slate blue',ModelColor], Row, Column, Angle, 1, 1, FoundModel)
            * 提示消息
            disp_message (WindowHandle, ['Time       = ' + (Time * 1000)$' .2' + ' ms','Diff x     = ' + Diffx$' .2','Diff y     = ' + Diffy$' .2','Diff angle = ' + Diffa$' .2'], 'window', 35, 12, Color, 'true')
        endif
        * 
    endif
    if (Index < 13)
        disp_continue_message (WindowHandle, 'black', 'true')
        stop ()
    else
        disp_message (WindowHandle, 'Program finished', 'window', 450, 430, Color, 'true')
    endif
endfor
stop ()
clear_shape_model (ModelIDLabel1)
clear_shape_model (ModelIDLabel2)
clear_shape_model (ModelIDBottle)
set_system ('clip_region', 'true')

在这里插入图片描述

  • 7
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Halcon可以用于不同标签的检测。在印刷质量缺陷检测中,可以使用差异模型来对比一个或多个图像与理想图像,找出明显的不同,从而鉴定出有缺陷的物体。差异模型的优势在于可以直接通过灰度值比较,并且可以通过差异图像进行空间加权比较。\[1\] 缺陷检测是一项具有挑战性的任务,其稳定性和精度是关键。常见的缺陷包括凹凸、污点瑕疵、划痕、裂缝、探伤等。缺陷检测算法与尺寸、二维码、OCR等算法不同,因为不同行业的缺陷算法差异很大。随着缺陷检测要求的提高,机器学习和深度学习成为不可或缺的技术难点。传统算法检测缺陷的调试难度大,容易出现不稳定情况下的反复调参和复杂缺陷误测。机器学习检测缺陷一般使用类似MLP的单层神经网络,对缺陷特征进行训练分类。深度学习检测缺陷需要大量的缺陷样本和手动标注缺陷位置,训练周期长。深度学习检测缺陷的迁移学习法可能成为未来工业领域检测瑕疵的趋势,但需要收集各行业的缺陷类型图片和训练的网络模型,并进行共享。\[2\] 在日常工程应用中,Halcon通常使用形状匹配进行各种定位。当待匹配物体有轻微变形时,形状匹配可以得到准确的结果。然而,当待匹配物体有较大变形时,如塑料产品在成形时变形、纺织产品的花纹因褶皱而变形,要想得到精确的定位结果就变得困难。因为形状匹配本身只能得到一个点的匹配结果,对于较大变形的工件,定位结果可能不理想。\[3\] 综上所述,Halcon可以应用于不同标签的检测,包括印刷质量缺陷检测和形状匹配定位等。 #### 引用[.reference_title] - *1* [Halcon缺陷检测方法——模板匹配(定位)+差分](https://blog.csdn.net/Bovey66/article/details/120010977)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* *3* [基于halcon—缺陷检测常用方法与示例总结](https://blog.csdn.net/weixin_50016546/article/details/124981131)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^koosearch_v1,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值