Halcon学习之测量拟合案例

案例代码:

dev_update_off ()
dev_get_window (WindowHandle)
dev_set_line_width (3)

read_image (Image, './punched_holes.png')

*边缘提取
binary_threshold (Image, Region, 'max_separability', 'light', UsedThreshold)
boundary (Region, RegionBorder, 'inner')
dilation_circle (RegionBorder, RegionDilation, 5)
reduce_domain (Image, RegionDilation, ImageReduced)
edges_sub_pix (ImageReduced, Edges, 'canny', 2, 20, 40)
sort_contours_xld (Edges, SortedContours, 'character', 'true', 'row')
*拟合
fit_rectangle2_contour_xld (SortedContours, 'tukey', -1, 0, 0, 3, 2, Row, Column, Phi, Length1, Length2, PointOrder)
gen_rectangle2_contour_xld (Rectangle, Row, Column, Phi, Length1, Length2)

dev_display (Image)

count_obj (Rectangle, Number)
for Index := 1 to Number by 1
    *获取轮廓上的点
    select_obj (SortedContours, ObjectSelected, Index)
    get_contour_xld (ObjectSelected, CheckRow, CheckCol)
    select_obj (Rectangle, ObjectSelected1, Index)
    get_contour_xld (ObjectSelected1, RectRow, RectCol)
   *计算检测轮廓上的点到拟合矩形轮廓上的角点的距离,从而判断是否是拐点,拐点的距离差会比较大
     D1:=[sqrt(pow(CheckRow-RectRow[0],2))+sqrt(pow(CheckCol-RectCol[0],2))]
     D2:=[sqrt(pow(CheckRow-RectRow[1],2))+sqrt(pow(CheckCol-RectCol[1],2))]
     D3:=[sqrt(pow(CheckRow-RectRow[2],2))+sqrt(pow(CheckCol-RectCol[2],2))]
     D4:=[sqrt(pow(CheckRow-RectRow[3],2))+sqrt(pow(CheckCol-RectCol[3],2))]
     *取检测轮廓上的点到拟合矩形轮廓上的角点的最小距离,归为了该拐点区域
     MinD:=min2(min2(D1,D2),min2(D3,D4))
      *计算两轮廓上对应点的距离
     dist_rectangle2_contour_points_xld (ObjectSelected, 0, Row[Index-1], Column[Index-1], Phi[Index-1], Length1[Index-1], Length2[Index-1], Distances)
    *判断点是否满足:sgn(x)为符号函数;x>0,值为1;x=0,值为0,x<0,值为-1.
      *判断点是否满足:sgn(x)为符号函数;x>0,值为1;x=0,值为0,x<0,值为-1.
     mark:=max2(0, sgn(MinD-8))
     MaxD:=max(mark*Distances)
     if (MaxD > 1.5)
         
         disp_message (WindowHandle, 'NG', 'image', Row[Index-1], Column[Index-1], 'red', 'true')
     else
         disp_message (WindowHandle, 'OK', 'image', Row[Index-1], Column[Index-1], 'green', 'true')
     endif
     stop ()
     
    
endfor

运行效果图:
在这里插入图片描述

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值