HALCON示例程序measure_metal_part_first_example.hdev通过拟合边缘进行尺寸测量

HALCON示例程序measure_metal_part_first_example.hdev通过拟合边缘进行尺寸测量

示例程序源码(加注释)

  • 关于显示类函数解释
    dev_update_off ()
    read_image (Image, ‘metal-parts/metal-parts-01’)
    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 (3)
    dev_set_color (‘white’)
    dev_set_draw (‘margin’)
    dev_display (Image)
    set_display_font (WindowID, 16, ‘mono’, ‘true’, ‘false’)
    stop ()
    dev_set_draw (‘fill’)
  • 二值化
    threshold (Image, Region, 100, 255)
  • 求取区域中心
    area_center (Region, AreaRegion, RowCenterRegion, ColumnCenterRegion)
  • 使用椭圆拟合求取区域角度
    orientation_region (Region, OrientationRegion)
    dev_display (Region)
    disp_message (WindowID, ‘Center Row: ’ + RowCenterRegion ′ . 5 ′ , ′ w i n d o w ′ , 20 , 10 , ′ w h i t e ′ , ′ f a l s e ′ ) d i s p m e s s a g e ( W i n d o w I D , ′ A r e a : ′ + A r e a R e g i o n + ′ p i x e l ′ , ′ w i n d o w ′ , 20 , 300 , ′ w h i t e ′ , ′ f a l s e ′ ) d i s p m e s s a g e ( W i n d o w I D , ′ C e n t e r C o l u m n : ′ + C o l u m n C e n t e r R e g i o n '.5', 'window', 20, 10, 'white', 'false') disp_message (WindowID, 'Area: ' + AreaRegion + ' pixel', 'window', 20, 300, 'white', 'false') disp_message (WindowID, 'Center Column: ' + ColumnCenterRegion .5,window,20,10,white,false)dispmessage(WindowID,Area:+AreaRegion+pixel,window,20,300,white,false)dispmessage(WindowID,CenterColumn:+ColumnCenterRegion’.5’, ‘window’, 60, 10, ‘white’, ‘false’)
    disp_message (WindowID, ‘Orientation: ’ + OrientationRegion$’.3’ + ’ rad’, ‘window’, 60, 300, ‘white’, ‘false’)
    dev_set_color (‘gray’)
    disp_cross (WindowID, RowCenterRegion, ColumnCenterRegion, 15, 0)
    disp_arrow (WindowID, RowCenterRegion, ColumnCenterRegion, RowCenterRegion - 60 * sin(OrientationRegion), ColumnCenterRegion + 60 * cos(OrientationRegion), 2)
    stop ()
  • 提取边缘
    edges_sub_pix (Image, Edges, ‘canny’, 0.6, 30, 70)
  • 分割边缘
    segment_contours_xld (Edges, ContoursSplit, ‘lines_circles’, 6, 4, 4)
    dev_clear_window ()
    dev_set_colored (12)
    dev_display (ContoursSplit)
    stop ()
    dev_open_window (0, round(Width / 2), (535 - 225) * 2, (395 - 115) * 2, ‘black’, WindowHandleZoom)
    dev_set_part (round(115), round(225), round(395), round(535))
    set_display_font (WindowHandleZoom, 18, ‘mono’, ‘true’, ‘false’)
    count_obj (ContoursSplit, NumSegments)
    dev_display (Image)
    NumCircles := 0
    RowsCenterCircle := []
    ColumnsCenterCircle := []
    disp_message (WindowHandleZoom, 'Circle radii: ', ‘window’, 120, 230, ‘white’, ‘false’)
for i := 1 to NumSegments by 1
    select_obj (ContoursSplit, SingleSegment, i)
    * 获取xld轮廓属性,是圆形椭圆还是直线
    get_contour_global_attrib_xld (SingleSegment, 'cont_approx', Attrib)
    if (Attrib == 1)
        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)
        RowsCenterCircle := [RowsCenterCircle,Row]
        ColumnsCenterCircle := [ColumnsCenterCircle,Column]
        dev_display (ContCircle)
        disp_message (WindowHandleZoom, 'C' + NumCircles, 'window', Row - Radius - 10, Column, 'white', 'false')
        disp_message (WindowHandleZoom, 'C' + NumCircles + ': Radius = ' + Radius$'.4', 'window', 275 + NumCircles * 15, 230, 'white', 'false')
    endif
endfor
distance_pp (RowsCenterCircle[1], ColumnsCenterCircle[1], RowsCenterCircle[2], ColumnsCenterCircle[2], Distance_2_3)
disp_line (WindowHandleZoom, RowsCenterCircle[1], ColumnsCenterCircle[1], RowsCenterCircle[2], ColumnsCenterCircle[2])
disp_message (WindowHandleZoom, 'Distance C2-C3 = ' + Distance_2_3$'.4', 'window', 275 + (NumCircles + 3) * 15, 230, 'magenta', 'false')
distance_pp (RowsCenterCircle[0], ColumnsCenterCircle[0], RowsCenterCircle[2], ColumnsCenterCircle[2], Distance_1_3)
disp_line (WindowHandleZoom, RowsCenterCircle[0], ColumnsCenterCircle[0], RowsCenterCircle[2], ColumnsCenterCircle[2])
disp_message (WindowHandleZoom, 'Distance C1-C3 = ' + Distance_1_3$'.4', 'window', 275 + (NumCircles + 2) * 15, 230, 'yellow', 'false')
distance_pp (RowsCenterCircle[3], ColumnsCenterCircle[3], RowsCenterCircle[4], ColumnsCenterCircle[4], Distance_4_5)
disp_line (WindowHandleZoom, RowsCenterCircle[3], ColumnsCenterCircle[3], RowsCenterCircle[4], ColumnsCenterCircle[4])
disp_message (WindowHandleZoom, 'Distance C4-C5 = ' + Distance_4_5$'.4', 'window', 275 + (NumCircles + 4) * 15, 230, 'cyan', 'false')
stop ()

dev_set_window (WindowHandleZoom)
dev_close_window ()
dev_set_part (0, 0, Height - 1, Width - 1)
dev_update_window (‘on’)

处理思路

这个例子是主要讲解了使用xld边缘拟合测量圆的大小与相对位置的例子。

后记

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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值