HALCON示例程序measure_metal_part_id.hdev使用xld边缘拟合检测零件加工是否合格

HALCON示例程序measure_metal_part_id.hdev使用xld边缘拟合检测零件加工是否合格

示例程序源码(加注释)

  • 关于显示类函数解释
    dev_update_off ()
    Imagefiles := [‘metal-parts/metal-part-model-01’,‘metal-parts/metal-parts-01’,‘metal-parts/metal-part-distorted-01’,‘metal-parts/metal-part-distorted-02’,‘metal-parts/metal-part-distorted-03’]
    for k := 0 to |Imagefiles| - 1 by 1
    read_image (Image, Imagefiles[k])
    get_image_size (Image, Width, Height)
    dev_close_window ()
    dev_open_window (0, 0, Width, Height, ‘light gray’, WindowID)
    dev_set_part (0, 0, Height - 1, Width - 1)
    dev_set_line_width (2)
    dev_set_draw (‘margin’)
    dev_display (Image)
    set_display_font (WindowID, 16, ‘mono’, ‘true’, ‘false’)
    disp_message (WindowID, ‘Image’ + k, ‘window’, 10, 10, ‘white’, ‘false’)
    stop ()
    dev_set_draw (‘fill’)

    • 二值化、膨胀,减少定义域、求取边界
      threshold (Image, Region, 90, 255)
      dilation_rectangle1 (Region, RegionDilation, 10, 10)
      reduce_domain (Image, RegionDilation, ImageReduced)
    • 提取水平阈值的交叉点
      threshold_sub_pix (ImageReduced, Edges, 75)
    • 转正检测产品
      orientation_region (Region, OrientationRegion)
      area_center (Region, Area, RowCenter, ColumnCenter)
    • 创建一个以旋转中心与旋转角度为基础的旋转变换矩阵
      vector_angle_to_rigid (RowCenter, ColumnCenter, OrientationRegion, RowCenter, ColumnCenter, 0, HomMat2DRotate)
    • 对XLD轮廓应用任意仿射2D变换。
      affine_trans_contour_xld (Edges, ContoursAffinTrans, HomMat2DRotate)
    • 分割xld边缘轮廓
      segment_contours_xld (ContoursAffinTrans, ContoursSplit, ‘lines_circles’, 6, 4, 4)
    • 对XLD边缘轮廓进行排序
      sort_contours_xld (ContoursSplit, SortedContours, ‘upper_left’, ‘true’, ‘column’)
      dev_clear_window ()
      disp_message (WindowID, ‘Circles in a local coordinate system’, ‘window’, 10, 10, ‘white’, ‘false’)
      dev_set_color (‘black’)
      dev_display (SortedContours)
      dev_set_color (‘white’)
      count_obj (SortedContours, NumberSegments)
      RowsE := []
      ColsE := []
      RadiiE := []
      gen_empty_obj (Lines)
    for i := 1 to NumberSegments by 1
        select_obj (SortedContours, ObjectSelected, i)
        * 获取xld边缘轮廓属性
        get_contour_global_attrib_xld (ObjectSelected, 'cont_approx', Attrib)
        if (Attrib == 1)
        * 拟合圆形
            fit_circle_contour_xld (ObjectSelected, 'algebraic', -1, 0, 0, 3, 2, Row, Column, Radius, StartPhi, EndPhi, PointOrder)
            * 绘制圆形
            gen_circle_contour_xld (ContCircle, Row, Column, Radius, StartPhi, EndPhi, PointOrder, 1.5)
            RowsE := [RowsE,Row]
            ColsE := [ColsE,Column]
            RadiiE := [RadiiE,Radius]
            dev_display (ContCircle)
        else
        * 拟合直线
            fit_line_contour_xld (ObjectSelected, 'tukey', -1, 0, 5, 2, RowBegin, ColBegin, RowEnd, ColEnd, Nr, Nc, Dist)
            * 绘制边缘
            gen_contour_polygon_xld (Line, [RowBegin,RowEnd], [ColBegin,ColEnd])
            concat_obj (Lines, Line, Lines)
        endif
    endfor
    

    dev_set_color (‘white’)
    count_obj (Lines, NumberLines)

    • 根据线的方向筛选xld轮廓
      select_contours_xld (Lines, LinesVertical, ‘direction’, rad(88), rad(92), 0, 0)
      count_obj (LinesVertical, NumberLV)
    • 根据线的方向筛选xld轮廓
      select_contours_xld (Lines, LinesHorizontal, ‘direction’, rad(-2), rad(2), 0, 0)
      count_obj (LinesHorizontal, NumberLH)
      ColVmin := 0
      RowHmax := 0
      for i := 1 to NumberLV by 1
      select_obj (LinesVertical, SelectedV, i)
      get_contour_xld (SelectedV, RowV, ColV)
      if (i == 1)
      ColVmin := ColV[0]
      RowA1 := RowV[0]
      ColA1 := ColV[0]
      RowA2 := RowV[1]
      ColA2 := ColV[1]
      else
      if (ColV[0] < ColVmin)
      ColVmin := ColV[0]
      RowA1 := RowV[0]
      ColA1 := ColV[0]
      RowA2 := RowV[1]
      ColA2 := ColV[1]
      endif
      endif
      endfor
      for j := 1 to NumberLH by 1
      select_obj (LinesHorizontal, SelectedH, j)
      get_contour_xld (SelectedH, RowH, ColH)
      if (RowH[0] > RowHmax)
      RowHmax := RowH[0]
      RowB1 := RowH[0]
      ColB1 := ColH[0]
      RowB2 := RowH[1]
      ColB2 := ColH[1]
      endif
      endfor
    • 求两直线交点
      intersection_lines (RowA1, ColA1, RowA2, ColA2, RowB1, ColB1, RowB2, ColB2, RowO, ColO, IsOverlapping)
      gen_cross_contour_xld (Cross, RowO, ColO, 10, 0.785398)
      dev_display (Cross)
      disp_arrow (WindowID, RowO, ColO, RowO - 100, ColO, 2)
      disp_arrow (WindowID, RowO, ColO, RowO, ColO + 100, 2)
    • 仿射变换的齐次矩阵
      hom_mat2d_identity (HomMat2DIdentityResults)
    • 添加旋转
      hom_mat2d_slant (HomMat2DIdentityResults, rad(180), ‘x’, 0, 0, HomMat2DSlantResults)
    • 添加平移
      hom_mat2d_translate (HomMat2DSlantResults, RowO, -ColO, HomMat2DTranslateResults)
    • 进行像素的仿射变换
      affine_trans_pixel (HomMat2DTranslateResults, RowsE, ColsE, RowsELocal, ColsELocal)
      if (k == 0)
      RowsELocalRef := RowsELocal
      ColsELocalRef := ColsELocal
      RadiiERef := RadiiE
      NumberRowsERef := |RowsELocal|
      for i := 0 to |RadiiE| - 1 by 1
      disp_message (WindowID, i + 1, ‘window’, RowsE[i] + 10, ColsE[i] + RadiiE[i], ‘white’, ‘false’)
      endfor
      disp_message (WindowID, ‘Reference Object’, ‘window’, 50, 10, ‘white’, ‘false’)

    else
    ID_Deviation := []
    ID_Missing := []
    NumberRowsE := |RowsE|
    if (NumberRowsE == NumberRowsERef)
    distance_pp (RowsELocalRef, ColsELocalRef, RowsELocal, ColsELocal, DistanceEllipseCenters)
    DiffRadius := abs(RadiiE - RadiiERef)
    for i := 0 to |DistanceEllipseCenters| - 1 by 1
    if (DistanceEllipseCenters[i] > 2 or DiffRadius[i] > 2)
    ID_Deviation := [ID_Deviation,i + 1]
    endif
    endfor
    endif
    if (NumberRowsE < NumberRowsERef)
    j := 0
    for i := 0 to NumberRowsE - 1 by 1
    ok := 0
    while (ok == 0)
    distance_pp (RowsELocalRef[j], ColsELocalRef[j], RowsELocal[i], ColsELocal[i], Distance)
    DiffRadius := abs(RadiiE[i] - RadiiERef[j])
    if ((Distance < 10) and (DiffRadius < 10))
    if (Distance > 2 or DiffRadius > 2)
    ID_Deviation := [ID_Deviation,j + 1]
    endif
    ok := 1
    else
    ID_Missing := [ID_Missing,j + 1]
    endif
    if (j == NumberRowsERef - 1)
    ok := 1
    endif
    j := j + 1
    endwhile
    endfor
    endif
    if (NumberRowsE > NumberRowsERef)
    disp_message (WindowID, ‘There are more circles than in the reference model!’, ‘window’, 350, 150, ‘white’, ‘false’)
    endif
    dev_set_draw (‘margin’)
    NumCircleDeviation := |ID_Deviation|
    NumCircleMissing := |ID_Missing|
    counter := 0
    * 仿射变换的反变换
    hom_mat2d_invert (HomMat2DTranslateResults, HomMat2DInvert)
    for i := 0 to NumCircleDeviation - 1 by 1
    index := ID_Deviation[i]
    counter := counter + 1
    disp_message (WindowID, 'Deviation (>2 pixels) for circle with index ’ + index, ‘window’, 30 + counter * 20, 10, ‘red’, ‘false’)
    affine_trans_pixel (HomMat2DInvert, RowsELocalRef[index - 1], ColsELocalRef[index - 1], RowsEVis, ColsEVis)
    gen_circle (Circle, RowsEVis, ColsEVis, RadiiERef[index - 1])
    dev_display (Circle)
    endfor
    for i := 0 to NumCircleMissing - 1 by 1
    index := ID_Missing[i]
    counter := counter + 1
    disp_message (WindowID, 'Missing: circle with index ’ + index, ‘window’, 30 + counter * 20, 10, ‘red’, ‘false’)
    affine_trans_pixel (HomMat2DInvert, RowsELocalRef[index - 1], ColsELocalRef[index - 1], RowsEVis, ColsEVis)
    gen_circle (Circle, RowsEVis, ColsEVis, RadiiERef[index - 1])
    dev_display (Circle)
    endfor
    endif
    stop ()
    endfor
    dev_update_window (‘on’)

处理思路

这个例子是主要讲解了xld轮廓的拟合和xld轮廓的仿射变换。

后记

大家有什么问题可以向我提问哈,我看到了第一时间回复,希望在学习的路上多多结交良师益友。

  • 4
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值