HALCON示例程序measure_screw.hdev螺纹尺寸测量

HALCON示例程序measure_screw.hdev螺纹尺寸测量

示例程序源码(加注释)

  • 关于显示类函数解释
    dev_update_off ()
    read_image (Image, ‘screw_thread’)
    get_image_pointer1 (Image, Pointer, Type, Width, Height)
    dev_close_window ()
    dev_open_window (0, 0, Width / 2, Height / 2, ‘light gray’, WindowID)
    dev_set_part (0, 0, Height - 1, Width - 1)
    dev_set_line_width (2)
    dev_set_color (‘blue’)
    dev_set_draw (‘margin’)
    dev_display (Image)
    set_display_font (WindowID, 14, ‘mono’, ‘true’, ‘false’)
    disp_continue_message (WindowID, ‘black’, ‘true’)
    stop ()
    dev_set_draw (‘fill’)
    dev_set_line_width (2)
    dev_set_color (‘black’)
  • 二值化
    threshold (Image, Region, 0, 100)
  • 求区域角度
    orientation_region (Region, OrientationRegion)
  • 求取区域中心
    area_center (Region, Area, RowCenter, ColumnCenter)
  • 由中心点角度生成仿射变换矩阵
    vector_angle_to_rigid (RowCenter, ColumnCenter, OrientationRegion, RowCenter, ColumnCenter, rad(90), HomMat2DRotate)
  • 进行仿射变换
    affine_trans_region (Region, RegionAffineTrans, HomMat2DRotate, ‘nearest_neighbor’)
    dev_clear_window ()
    disp_message (WindowID, ‘Region (transformed)’, ‘window’, -1, -1, ‘black’, ‘false’)
    dev_display (RegionAffineTrans)
    disp_continue_message (WindowID, ‘black’, ‘true’)
    stop ()
  • 使用圆形进行闭运算
    closing_circle (RegionAffineTrans, RegionToProcess, 4.5)
    dev_clear_window ()
    disp_message (WindowID, ‘Region (transformed)’, ‘window’, -1, -1, ‘black’, ‘false’)
    dev_display (RegionToProcess)
  • get_region_runs - 访问区域的游程编码。
    get_region_runs (RegionToProcess, RowRegionRuns, ColumnBegin, ColumnEnd)
    NumberLines := |RowRegionRuns|
    ColumnBeginSelected := ColumnBegin[90:NumberLines - 90]
    ColumnEndSelected := ColumnEnd[90:NumberLines - 90]
    Diameter := ColumnEndSelected - ColumnBeginSelected + 1
    meanDiameter := mean(Diameter)
    minDiameter := min(Diameter)
    maxDiameter := max(Diameter)
    dev_set_color (‘white’)
  • 生成一个线区域
    gen_region_line (ScanLine1, RowRegionRuns[90], ColumnBegin[90], RowRegionRuns[90], ColumnEnd[90])
    gen_region_line (ScanLine2, RowRegionRuns[NumberLines - 90], ColumnBegin[NumberLines - 90], RowRegionRuns[NumberLines - 90], ColumnEnd[NumberLines - 90])
    dev_display (ScanLine1)
    dev_display (ScanLine2)
  • 显示十字线
    disp_cross (WindowID, RowRegionRuns[90], ColumnBegin[90], 20, 0)
    disp_cross (WindowID, RowRegionRuns[90], ColumnEnd[90], 20, 0)
    disp_cross (WindowID, RowRegionRuns[NumberLines - 90], ColumnBegin[NumberLines - 90], 20, 0)
    disp_cross (WindowID, RowRegionRuns[NumberLines - 90], ColumnEnd[NumberLines - 90], 20, 0)
    disp_message (WindowID, ‘Width of object’, ‘image’, 10, 720, ‘black’, ‘false’)
    disp_message (WindowID, 'per row (between ', ‘image’, 60, 720, ‘black’, ‘false’)
    disp_message (WindowID, ‘the white lines):’, ‘image’, 110, 720, ‘black’, ‘false’)
    disp_message (WindowID, ‘mean width: ’ + meanDiameter + ’ pixels’, ‘image’, 210, 720, ‘black’, ‘false’)
    disp_message (WindowID, ‘minimum width: ’ + minDiameter + ’ pixels’, ‘image’, 260, 720, ‘black’, ‘false’)
    disp_message (WindowID, ‘maximum width: ’ + maxDiameter + ’ pixels’, ‘image’, 310, 720, ‘black’, ‘false’)
    disp_continue_message (WindowID, ‘black’, ‘true’)
    stop ()
  • 将区域缩小到边缘
    boundary (Region, RegionBorder, ‘inner_filled’)
  • 使用圆形元素膨胀
    dilation_circle (RegionBorder, RegionDilation, 7.5)
  • 缩减定义域
    reduce_domain (Image, RegionDilation, ImageReduced)
    dev_clear_window ()
    dev_display (ImageReduced)
    disp_message (WindowID, ‘ROI for contour extraction’, ‘window’, -1, -1, ‘white’, ‘false’)
    disp_continue_message (WindowID, ‘black’, ‘true’)
    stop ()
    sigma := 3
  • 用高斯卷积图像。
    derivate_gauss (ImageReduced, DerivGauss, sigma, ‘laplace’)
  • 从亚像素精度的图像中提取值为零的边界。
    zero_crossing_sub_pix (DerivGauss, Edges)
  • 使用连续长度筛选轮廓
    select_contours_xld (Edges, SelectedEdges, ‘contour_length’, 3000, 99999, -0.5, 0.5)
  • 仿射变换
    affine_trans_contour_xld (SelectedEdges, ContoursAffinTrans, HomMat2DRotate)
  • 最小外接矩形
    smallest_rectangle1_xld (ContoursAffinTrans, Row11, Column11, Row21, Column21)
  • 剪切XLD轮廓
    clip_contours_xld (ContoursAffinTrans, ClippedContours, Row11 + 90, 0, Row21 - 90, Width)
  • 拟合直线
    fit_line_contour_xld (ClippedContours, ‘regression’, -1, 0, 5, 2, RowBegin, ColBegin, RowEnd, ColEnd, Nr, Nc, Dist1)
    dev_clear_window ()
    dev_display (ClippedContours)
    disp_message (WindowID, ‘Contours (transformed and clipped)’, ‘window’, -1, -1, ‘black’, ‘false’)
    disp_continue_message (WindowID, ‘black’, ‘true’)
    stop ()
  • 生成一个多边形边界
    gen_contour_polygon_xld (RegressContour0, [RowBegin[0],RowEnd[0]], [ColBegin[0],ColEnd[0]])
    select_obj (ClippedContours, EdgeContour0, 1)
  • 返回xld轮廓的位置
    get_contour_xld (EdgeContour0, RowEdge0, ColEdge0)
  • 计算一个电与轮廓之间的距离
    distance_pc (RegressContour0, RowEdge0, ColEdge0, DistanceMin0, DistanceMax0)
    minDistance0 := min(DistanceMin0)
    maxDistance0 := max(DistanceMin0)
    meanDistance0 := mean(DistanceMin0)
    gen_contour_polygon_xld (RegressContour1, [RowBegin[1],RowEnd[1]], [ColBegin[1],ColEnd[1]])
    select_obj (ClippedContours, EdgeContour1, 2)
    get_contour_xld (EdgeContour1, RowEdge1, ColEdge1)
    distance_pc (RegressContour1, RowEdge1, ColEdge1, DistanceMin1, DistanceMax1)
    minDistance1 := min(DistanceMin1)
    maxDistance1 := max(DistanceMin1)
    meanDistance1 := mean(DistanceMin1)
    dev_clear_window ()
    dev_set_color (‘gray’)
    dev_display (EdgeContour0)
    dev_display (EdgeContour1)
    dev_set_color (‘black’)
disp_message (WindowID, 'Contours (transformed and clipped)', 'image', 10, 10, 'black', 'false')
dev_display (RegressContour0)
dev_display (RegressContour1)
disp_message (WindowID, 'Maximum distances', 'image', 10, 720, 'black', 'false')
disp_message (WindowID, 'between contours', 'image', 60, 720, 'black', 'false')
disp_message (WindowID, 'and regression lines:', 'image', 110, 720, 'black', 'false')
* 
disp_message (WindowID, 'max (contour/edge 0): ', 'image', 210, 720, 'black', 'false')
disp_message (WindowID, maxDistance0$'.4' + ' pixels', 'image', 260, 720, 'black', 'false')
* 
disp_message (WindowID, 'max (contour/edge 1): ', 'image', 360, 720, 'black', 'false')
disp_message (WindowID, maxDistance1$'.4' + ' pixels', 'image', 410, 720, 'black', 'false')
* 
disp_message (WindowID, '0', 'image', max([RowBegin[0],RowEnd[0]]) + 20, ColEnd[0] - 9, 'black', 'false')
disp_message (WindowID, '1', 'image', max([RowBegin[1],RowEnd[1]]) + 20, ColEnd[1] - 9, 'black', 'false')

dev_set_color (‘white’)
dev_set_draw (‘margin’)
gen_rectangle1 (Rectangle1, 500, 620, 550, 680)
dev_display (Rectangle1)
dev_open_window (0, Width / 2 + 2, 300, 300, ‘light gray’, WindowHandleZoom)
dev_set_part (500, 620, 550, 680)
dev_set_color (‘black’)
dev_display (EdgeContour0)
dev_display (RegressContour0)
get_contour_xld (RegressContour0, RowContour0, ColContour0)
for i := 800 to 950 by 1

  • projection_pl - 计算点到线的投影。
    projection_pl (RowEdge0[i], ColEdge0[i], RowContour0[0], ColContour0[0], RowContour0[1], ColContour0[1], RowProj0, ColProj0)
    • 从多边形生成xld轮廓
      gen_contour_polygon_xld (Contour, [RowEdge0[i],RowProj0], [ColEdge0[i],ColProj0])
      dev_display (Contour)

endfor
disp_continue_message (WindowID, ‘black’, ‘true’)
stop ()
dev_close_window ()
dev_set_window (WindowID)
distance_pp (RowBegin[1], ColBegin[1], RowEnd[0], ColEnd[0], Distance1)
distance_pp (RowBegin[0], ColBegin[0], RowEnd[1], ColEnd[1], Distance2)
if (abs(Distance1 - Distance2) < 1)
dev_clear_window ()
dev_set_color (‘gray’)
dev_display (EdgeContour0)
dev_display (EdgeContour1)
dev_set_color (‘black’)
disp_message (WindowID, ‘Contours (transformed and clipped)’, ‘image’, -1, -1, ‘black’, ‘false’)
dev_display (RegressContour0)
dev_display (RegressContour1)
disp_message (WindowID, ‘Mean distance’, ‘image’, 10, 720, ‘black’, ‘false’)
disp_message (WindowID, ‘between regression’, ‘image’, 60, 720, ‘black’, ‘false’)
disp_message (WindowID, ‘lines:’, ‘image’, 110, 720, ‘black’, ‘false’)
disp_message (WindowID, ((Distance1 + Distance2) / 2)$’.4’ + ’ pixels’, ‘image’, 210, 720, ‘black’, ‘false’)
dev_set_draw (‘fill’)
dev_set_color (‘white’)
disp_arrow (WindowID, RowEnd[1] + 100, ColEnd[1], RowBegin[0] + 100, ColBegin[0], 2)
disp_arrow (WindowID, RowBegin[0] + 100, ColBegin[0], RowEnd[1] + 100, ColEnd[1], 2)
else
dev_set_color (‘black’)
disp_message (WindowID, ‘Contours (transformed and clipped)’, ‘image’, -1, -1, ‘black’, ‘false’)
dev_display (RegressContour0)
dev_display (RegressContour1)
disp_message (WindowID, ‘RegressContours’, ‘image’, 10, 720, ‘black’, ‘false’)
disp_message (WindowID, ‘are not parallel’, ‘image’, 60, 720, ‘black’, ‘false’)

endif

处理思路

这个例子是主要讲解了distance_transform算子的应用。

后记

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值