Halcon视觉检测——2D Metrology测量小结(一)

Halcon中的2D Metrology模块提供了亚像素级别的卡尺测量功能。主要针对圆/圆弧、椭圆/椭圆弧、直线、rectangle2矩形创建测量对象。

步骤

一般首次使用2D Metrology测量工具有如下步骤:

  1. 创建测量模型;使用create_metrology_model();
  2. 设置测量对象图像的大小;使用set_metrology_model_image_size();
  3. 提供近似值;将测量对象添加到模型中,同时将测量对象的近似参数和控制测量的参数添加到模型中。
    主要通过以下算子实现对应对象的添加:
  • add_metrology_object_circle_measure() 将圆或圆弧添加到度量模型中
  • add_metrology_object_ellipse_measure() 将椭圆或椭圆弧添加到度量模型中
  • add_metrology_object_line_measure() 将直线添加到度量模型中
  • add_metrology_object_rectangle2_measure() 将一个矩形添加到度量模型中
  • add_metrology_object_generic()创建几个不同几何形状的测量
    其中,模型参数的设置也可以通过set_metrology_model_param()完成,如果模型中有多个测量形状需要设置则可以通过 set_metrology_object_param()进行设置。
    4.应用测量;使用apply_metrology_model()将测量应用到指定图像中。
    5.获取结果;
    6.清除内存;使用clear_metrology_model()销毁模型。

测量示例

如图,计算图中元件的中心与缺口角度问题:

read_image (Image, 'uranium_targeting_256.bmp')
try
    get_image_size (Image, Width, Height)
    auto_threshold (Image, Regions, 5)
    fill_up (Regions, RegionFillUp)
    select_shape_std (RegionFillUp, SelectedRegions1, 'max_area', 70)
    reduce_domain (Image, SelectedRegions1, ImageReduced1)
    threshold (ImageReduced1, Region2, 0, 150)
    connection (Region2, ConnectedRegions5)
    fill_up (ConnectedRegions5, RegionFillUp2)
    select_shape_std (RegionFillUp2, SelectedRegions4, 'max_area', 70)
    opening_circle (SelectedRegions4, RegionOpening, 10)
    gen_contour_region_xld (RegionOpening, Contours, 'border')
    fit_circle_contour_xld (Contours, 'atukey', -1, 0, 0, 3, 2, CircleCenterRow, CircleCenterColumn, CircleCenterRadius, StartPhi1, EndPhi1, PointOrder1)
    gen_circle_contour_xld (ContCircle1, CircleCenterRow, CircleCenterColumn, CircleCenterRadius, 0, 6.28318, 'positive', 1)
    gen_region_contour_xld (Contours, Region3, 'margin')
    gen_region_contour_xld (ContCircle1, Region, 'margin')
    
    shape_trans (RegionOpening, RegionTrans, 'rectangle2')
    erosion_circle (RegionTrans, RegionErosion, 30)
    difference (Region3, RegionErosion, RegionDifference)
    connection (RegionDifference, ConnectedRegions4)
    select_shape_std (ConnectedRegions4, SelectedRegions3, 'max_area', 70)
    dilation_circle (SelectedRegions3, RegionDilation, 5)
    skeleton (RegionDilation, Skeleton1)
    gen_contours_skeleton_xld (Skeleton1, Contours2, 10, 'filter')
    fit_line_contour_xld (Contours2, 'tukey', -1, 0, 5, 2.0, RowBegin, ColBegin, RowEnd, ColEnd, Nr, Nc, Dist)
    gen_region_line (RegionLines, RowBegin, ColBegin, RowEnd, ColEnd)
    intersection (RegionLines, Region, RegionIntersection2)
    connection (RegionIntersection2, ConnectedRegions1)
    * 直线检测的起始点与终止点
    area_center (ConnectedRegions1, Area4, LineRegionRow, LineRegionColumn)
    **卡尺精定位部分
    create_metrology_model (MetrologyHandle)
    set_metrology_model_image_size (MetrologyHandle, Width, Height)
    add_metrology_object_circle_measure (MetrologyHandle, CircleCenterRow, CircleCenterColumn, CircleCenterRadius, 10, 5, 1.5, 20, ['measure_transition','min_score'], ['positive',0.5], MetrologyCircleIndex)
    apply_metrology_model (Image, MetrologyHandle)
    get_metrology_object_result (MetrologyHandle, MetrologyCircleIndex, 'all', 'result_type', 'all_param', CircleParameter)   
    add_metrology_object_line_measure (MetrologyHandle, LineRegionRow[0], LineRegionColumn[0], LineRegionRow[1], LineRegionColumn[1], 10, 5, 1, 20, ['measure_transition','min_score'], ['uniform',0.5], MetrologyLineIndex)   
    apply_metrology_model (Image, MetrologyHandle)
    get_metrology_object_result (MetrologyHandle, MetrologyLineIndex, 'all', 'result_type', 'all_param', LineParameter)    
    ** 显示信息
    get_metrology_object_result_contour (MetrologyCircleContour, MetrologyHandle, MetrologyCircleIndex, 'all', 1.5)
    get_metrology_object_result_contour (MetrologyLineContour, MetrologyHandle, MetrologyLineIndex, 'all', 1.5)
    gen_region_line (RegionLinesH, LineParameter[0], LineParameter[1], LineParameter[2], LineParameter[3])
    * *** Output ****
    if (LineParameter[1]CircleParameter[0])
        ReturnDeg := Deg - 180
    elseif(RowCircleParameter[1])
        ReturnDeg := 0
    elseif (Row==CircleParameter[0] and Column

输出结果:
在这里插入图片描述

你可能感兴趣的:(Halcon,视觉检测,计算机视觉,人工智能)