HALCON示例程序measure_pump.hdev螺纹孔位置与尺寸测量

HALCON示例程序measure_pump.hdev螺纹孔位置与尺寸测量

示例程序源码(加注释)

  • 关于显示类函数解释
    dev_update_var (‘off’)
    dev_update_off ()
    read_image (Image, ‘pumpe’)
    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 (1)
    dev_set_color (‘red’)
    dev_set_draw (‘margin’)
    dev_display (Image)
    set_display_font (WindowID, 16, ‘mono’, ‘true’, ‘false’)
    disp_continue_message (WindowID, ‘black’, ‘true’)
    stop ()
  • 快速二值化、筛选区域、轮廓变换、膨胀、缩小定义域、求边界
    fast_threshold (Image, Region, 0, 70, 150)
    dev_set_colored (3)
    connection (Region, ConnectedRegions)
    select_shape (ConnectedRegions, SelectedRegions, [‘outer_radius’,‘anisometry’,‘area’], ‘and’, [5,1,100], [50,1.8,99999])
    shape_trans (SelectedRegions, RegionTrans, ‘outer_circle’)
    dilation_circle (RegionTrans, RegionDilation, 5.5)
    union1 (RegionDilation, RegionUnion)
    reduce_domain (Image, RegionUnion, ImageReduced)
    dev_clear_window ()
    dev_display (ImageReduced)
    disp_continue_message (WindowID, ‘black’, ‘true’)
    stop ()
    threshold_sub_pix (ImageReduced, Border, 80)
  • 边界筛选
    select_shape_xld (Border, SelectedXLD, [‘contlength’,‘outer_radius’], ‘and’, [70,15], [99999,99999])
  • 分割
    segment_contours_xld (SelectedXLD, ContoursSplit, ‘lines_circles’, 4, 2, 2)
  • 在进行筛选
    select_shape_xld (ContoursSplit, SelectedXLD3, [‘outer_radius’,‘contlength’], ‘and’, [15,30], [45,99999])
  • 合并处于同一圆的xld轮廓
    union_cocircular_contours_xld (SelectedXLD3, UnionContours2, 0.5, 0.1, 0.2, 2, 10, 10, ‘true’, 1)
  • 对xld轮廓进行排序
    sort_contours_xld (UnionContours2, SortedContours, ‘upper_left’, ‘true’, ‘column’)
    dev_clear_window ()
    dev_set_color (‘white’)
    dev_display (ImageReduced)
    dev_display (SortedContours)
    disp_continue_message (WindowID, ‘black’, ‘true’)
    stop ()
    count_obj (SortedContours, NumSegments)
    dev_display (Image)
    dev_display (SortedContours)
    NumCircles := 0
    disp_message (WindowID, ‘Circle radius and average distance’, ‘window’, 10, 10, ‘white’, ‘false’)
    disp_message (WindowID, ‘between circle and contour:’, ‘window’, 30, 10, ‘white’, ‘false’)
for i := 1 to NumSegments by 1
    select_obj (SortedContours, SingleSegment, i)
    NumCircles := NumCircles + 1
    * 拟合圆形
    fit_circle_contour_xld (SingleSegment, 'atukey', -1, 2, 0, 5, 2, Row, Column, Radius, StartPhi, EndPhi, PointOrder)
    * 绘制圆形轮廓
    gen_circle_contour_xld (ContCircle, Row, Column, Radius, 0, rad(360), 'positive', 1)
    dev_display (ContCircle)
    * 计算轮廓到椭圆距离
    dist_ellipse_contour_xld (SingleSegment, 'algebraic', -1, 0, Row, Column, 0, Radius, Radius, MinDist, MaxDist, AvgDist, SigmaDist)
    disp_message (WindowID, NumCircles, 'window', Row - 10, Column - 5, 'white', 'false')
    disp_message (WindowID, 'R' + NumCircles + ': ' + Radius$'.3', 'window', (i - 1) * 50 + 30, 450, 'white', 'false')
    disp_message (WindowID, 'D_avg: ' + AvgDist$'.3', 'window', ((i - 1) * 50) + 50, 450, 'white', 'false')
endfor

dev_update_window (‘on’)

处理思路

这个例子是主要讲解了边缘提取,计算尺寸与位置。

后记

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

你可能感兴趣的:(#,Halcon示例程序解读与剖析)