0
点赞
收藏
分享

微信扫一扫

Halcon局部可变形模板匹配

M4Y 03-09 17:30 阅读 3

文章目录

算子

prepare_direct_variation_model(RefImage, VarImage : : ModelID, AbsThreshold, VarThreshold : )
以下是参数的解释:
    RefImage (输入对象):对象的参考图像。这是一个单通道图像对象,用于表示对象的参考图像。
    VarImage (输入对象):对象的变异图像。这是一个单通道图像对象,用于表示对象的变异情况。
    ModelID (输入控制,状态被修改):变异模型的ID。用于指定要使用的变异模型的ID。
    AbsThreshold (输入控制):图像与变异模型之间差异的绝对最小阈值。默认值为10,建议的取值范围为05101520304050。限制条件为AbsThreshold >= 0VarThreshold (输入控制):基于变异模型变化的差异阈值。默认值为2,建议的取值范围为11.522.533.544.55。限制条件为VarThreshold >= 0
get_deformable_model_contours( : ModelContours : ModelID, Level : )
以下是参数的解释:
    ModelContours (输出对象):变形模型的轮廓表示。这是一个XLD(可扩展链表数据)数组对象,用于表示变形模型的轮廓。
    ModelID (输入控制):模型的句柄。用于指定要使用的变形模型的句柄。
    Level (输入控制):应返回轮廓表示的金字塔级别。默认值为1,建议的取值范围为12345678910。限制条件为Level >= 1
find_local_deformable_model(Image : ImageRectified, VectorField, DeformedContours : ModelID, AngleStart, AngleExtent, ScaleRMin, ScaleRMax, ScaleCMin, ScaleCMax, MinScore, NumMatches, MaxOverlap, NumLevels, Greediness, ResultType, GenParamName, GenParamValue : Score, Row, Column)
这是一个用于模型匹配的参数列表。以下是各个参数的说明:
    Image (input_object): 输入图像,在其中进行模型匹配。
    ImageRectified (output_object): 找到的模型的矫正后图像。
    VectorField (output_object): 矫正变换的向量场。
    DeformedContours (output_object): 找到的模型实例的轮廓。
    ModelID (input_control): 模型的句柄。
    AngleStart (input_control): 模型的最小旋转角度。
    AngleExtent (input_control): 旋转角度的范围。
    ScaleRMin (input_control): 模型在行方向上的最小尺度。
    ScaleRMax (input_control): 模型在行方向上的最大尺度。
    ScaleCMin (input_control): 模型在列方向上的最小尺度。
    ScaleCMax (input_control): 模型在列方向上的最大尺度。
    MinScore (input_control): 要找到的模型实例的最小得分。
    NumMatches (input_control): 要找到的模型实例的数量(或者设置为0以找到所有匹配)。
    MaxOverlap (input_control): 要找到的模型实例的最大重叠度。
    NumLevels (input_control): 匹配中使用的金字塔级别数。
    Greediness (input_control): 搜索启发式算法的“贪婪程度”。
    ResultType (input_control): 请求的图标结果类型。
    GenParamName, GenParamValue (input_control): 通用参数名称和值。
    Score, Row, Column (output_control): 找到的模型实例的得分、行坐标和列坐标。
vector_field_to_real(VectorField : Row, Col : : )
这些参数用于处理矢量场的相关操作。以下是各个参数的说明:
    VectorField (input_object): 输入的矢量场。
    Row (output_object): 行方向上的矢量分量图像。
    Col (output_object): 列方向上的矢量分量图像。
tuple_gen_const( : : Length, Const : Newtuple)
这些参数用于生成一个新的元组(tuple)。以下是各个参数的说明:
    Length (input_control): 要生成的元组的长度。
    Const (input_control): 用于初始化元组元素的常量值。
    Newtuple (output_control): 生成的新元组。
get_grayval_interpolated(Image : : Row, Column, Interpolation : Grayval)
这些参数用于获取图像特定位置的灰度值。以下是各个参数的说明:
    Image (input_object): 要访问灰度值的图像。
    Row (input_control): 指定位置的行坐标。
    Column (input_control): 指定位置的列坐标。
    Interpolation (input_control): 插值方法,用于确定非整数坐标位置的灰度值。
    Grayval (output_control): 选择的图像坐标的灰度值。
gen_region_points( : Region : Rows, Columns : )
这些参数用于创建一个特定区域(Region)。以下是各个参数的说明:
    Region (output_object): 创建的区域对象。
    Rows (input_control): 区域中像素所在的行坐标。
    Columns (input_control): 区域中像素所在的列坐标。
elliptic_axis(Regions : : : Ra, Rb, Phi)
这些参数用于描述输入的区域属性,并计算出与每个区域相关的主半径、次要半径和角度。以下是各个参数的说明:
    Regions (input_object): 输入的区域对象数组。
    Ra (output_control): 主半径,以区域面积为标准化单位。
    Rb (output_control): 次要半径,以区域面积为标准化单位。需满足Rb大于等于0且小于等于Ra。
    Phi (output_control): 主半径与x轴之间的夹角(弧度表示),需满足-π/2小于Phi并且Phi小于等于π/2
gen_region_contour_xld(Contour : Region : Mode : )
这些参数用于处理轮廓(contour)并创建相应的区域(region)。以下是各个参数的说明:

    Contour (input_object): 输入的轮廓对象数组。
    Region (output_object): 创建的区域对象数组。
    Mode (input_control): 区域的填充模式。
        'filled': 完全填充区域。
        'margin': 填充边缘。
gen_ellipse_contour_xld( : ContEllipse : Row, Column, Phi, Radius1, Radius2, StartPhi, EndPhi, PointOrder, Resolution : )
这些参数用于创建椭圆轮廓(ellipse contour)。以下是各个参数的说明:
    ContEllipse (output_object): 创建的椭圆轮廓对象。
    Row (input_control): 椭圆中心的行坐标。
    Column (input_control): 椭圆中心的列坐标。
    Phi (input_control): 椭圆主轴的方向(弧度)。
    Radius1 (input_control): 椭圆长轴的长度。
        约束条件: Radius1大于0Radius2 (input_control): 椭圆短轴的长度。
        约束条件: Radius2大于等于0且小于等于Radius1。
    StartPhi (input_control): 沿椭圆边界的起始角度(弧度)。
    EndPhi (input_control): 沿椭圆边界的结束角度(弧度)。
    PointOrder (input_control): 沿边界的点顺序。
        可选值: 'negative'(逆时针), 'positive'(顺时针)。
    Resolution (input_control): 分辨率,相邻轮廓点之间的最大距离。
        约束条件: Resolution大于等于1.192e-7
clear_deformable_model (ModelID)
clear_variation_model (VariationModelID)

Halcon 使用局部变形算法案例

在这里插入图片描述

* This example program shows how to use local deformable
* matching to find and inspect objects that are deformed.
* 
* 1.读取图片
dev_update_off ()
Smoothness := 25
* 读取模板图片
read_image (ModelImage, 'gasket/gasket_model')
* 读取测试图片
read_image (Image, 'gasket/gasket_01')
dev_close_window ()
dev_open_window_fit_image (ModelImage, 0, 0, 500, -1, WindowHandle1)
set_display_font (WindowHandle1, 16, 'mono', 'true', 'false')
get_window_extents (WindowHandle1, Row, Column, Width, Height)
dev_open_window_fit_image (Image, 0, Width + 12, 1024 - Width - 36, -1, WindowHandle2)
set_display_font (WindowHandle2, 16, 'mono', 'true', 'false')
* 
* Create variation model
* 2.创建图像比较的变化模型
* 使用索贝尔算子检测边缘(振幅)sobel_amp (ModelImage, EdgeAmplitude, 'thin_max_abs', 5)
* 创建图像比较变化模型
create_variation_model (425, 410, 'byte', 'direct', VariationModelID)
* 准备一个变化模型,以便与图像进行比较
prepare_direct_variation_model (ModelImage, EdgeAmplitude, VariationModelID, 30, 1.5)
* 
* Create locally deformable model
* 3.创建局部可变形模板
create_local_deformable_model (ModelImage, 'auto', [], [], 'auto', 0.9, [], 'auto', 0.9, [], 'auto', 'none', 'use_polarity', 'auto', 'auto', [], [], ModelID)
* 返回可变形模型的轮廓表示形式
get_deformable_model_contours (ModelContours, ModelID, 1)
* 获取模型的行列坐标
area_center (ModelImage, Area, Row, Column)
* 仿射运算
hom_mat2d_identity (HomMat2DIdentity)
hom_mat2d_translate (HomMat2DIdentity, Row, Column, HomMat2DTranslate)
affine_trans_contour_xld (ModelContours, ContoursAffineTrans, HomMat2DTranslate)
* 显示
dev_set_window (WindowHandle1)
dev_set_line_width (2)
dev_set_color ('yellow')
dev_display (ModelImage)
dev_display (ContoursAffineTrans)
disp_message (WindowHandle1, 'Model image and contours', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle1, 'black', 'true')
stop ()
* 
* Process images iteratively 
* 4.找到图像中局部可变形模型的最佳匹配
NumImages := 9
for Index := 1 to NumImages by 1
    read_image (Image, 'gasket/gasket_' + Index$'02')
    dev_set_window (WindowHandle2)
    dev_display (Image)
    disp_message (WindowHandle2, 'Search ...', 'window', 12, 12, 'black', 'true')
    * Find the model in the search image.
    * As result, the rectified image, the respective
    * vector field, and the found contours are queried.
    count_seconds (S1)
    * 找到图像中局部可变形模型的最佳匹配
    find_local_deformable_model (Image, ImageRectified, VectorField, DeformedContours, ModelID, rad(-10), rad(20), 1, 1, 1, 1, 0.93, 1, 0.7, 0, 0.4, ['image_rectified','vector_field','deformed_contours'], ['deformation_smoothness','expand_border','subpixel'], [Smoothness,0,1], Score, Row, Column)
    count_seconds (S2)
    Time := S2 - S1
    if (|Score| > 0)
        * 产生网格
        gen_warped_mesh_region (VectorField, MeshRegion, Smoothness)
        *从XLD轮廓创建一个区域
        gen_region_contour_xld (DeformedContours, EdgeRegion, 'margin')
        * 轮廓进行膨胀
        dilation_circle (EdgeRegion, RegionDilation, 2 * Smoothness)
        * 计算区域的交集,和网格的交集
        intersection (RegionDilation, MeshRegion, RegionIntersection)
        dev_set_line_width (1)
        dev_set_color ('yellow')
        dev_display (RegionIntersection)
        Found[Index] := |Score|
        dev_set_line_width (2)
        dev_set_color ('green')
        dev_display (DeformedContours)
        disp_message (WindowHandle2, ['Match found in ' + Time$'1.2f' + ' s','Score: ' + Score$'.2f'], 'window', 12, 12, 'black', 'true')
        dev_set_window (WindowHandle1)
        dev_display (ImageRectified)
        * 比较两个区域的不同
        compare_variation_model (ImageRectified, Region, VariationModelID)
        * 形成单个连通域
        connection (Region, ConnectedRegions)
        * 筛选区域面积
        select_shape (ConnectedRegions, SelectedRegions, 'area', 'and', 40, 99999)
        count_obj (SelectedRegions, Number)
        if (Number > 0)
            area_center (SelectedRegions, Area, Row1, Column1)
            * 计算等效的椭圆参数
            elliptic_axis (SelectedRegions, Ra, Rb, Phi)
            tuple_gen_const (Number, 1, Ones)
            PointOrder := []
            for Idx := 0 to Number - 1 by 1
                PointOrder := [PointOrder,'positive']
            endfor
            * 产生椭圆亚像素轮廓
            gen_ellipse_contour_xld (ContEllipse, Row1, Column1, Phi, Ra + 10, Rb + 10, 0 * Ones, 6.28318 * Ones, PointOrder, 1.5)
            dev_set_color ('red')
            dev_display (ContEllipse)
            disp_message (WindowHandle1, 'Part not OK!', 'window', 12, 12, 'red', 'true')
        else
            disp_message (WindowHandle1, 'Part OK', 'window', 12, 12, 'forest green', 'true')
        endif
    else
        disp_message (WindowHandle2, 'Nothing found', 'window', 12, 12, 'black', 'true')
    endif
    if (Index < NumImages)
        disp_continue_message (WindowHandle2, 'black', 'true')
        stop ()
    endif
endfor
* 
* Clean up
clear_deformable_model (ModelID)
clear_variation_model (VariationModelID)

在这里插入图片描述

举报

相关推荐

0 条评论