3 锯片检测----halcon例程

系列文章目录

1 Halcon数据结构
2 一致性增强扩散
3 锯片检测
4 序列号读取
5 印刷检测


锯片检测

  • 系列文章目录
  • 1 应用背景
  • 2 检测算法逻辑
  • 3 Halcon例程代码
  • 总结
  • 参考资料


1 应用背景

  检测锯片的每一个锯齿的外形,保证锯齿符合预定形状,在锯片生产过程中,起着决定其合格与否的作用。
  本应用从锯片的轮廓中可以获取基本信息,且锯片很薄,可以使用漫反射背光照明。


2 检测算法逻辑

  a.从图像中提取锯片轮廓
  b.通过适当的轮廓分割、筛选和排序,得到每个锯齿的两条近直线边
  c.算出每个锯齿两侧的夹角角度

3 Halcon例程代码

* This program demonstrates how to inspect the teeth of a saw
* blade by measuring angles in an image.
* 
* Initialize some constants
PI := rad(180)
PI_2 := rad(90)
* 
* Display initialization
dev_update_off ()
read_image (Image, 'saw_blade/saw_01')
get_image_size (Image, Width, Height)
dev_close_window ()
dev_open_window_fit_image (Image, 0, 0, 640, 640, WindowHandleMain)
set_display_font (WindowHandleMain, 16, 'mono', 'true', 'false')
get_window_extents (WindowHandleMain, WindowRow, WindowColumn, WindowWidth, WindowHeight)
dev_open_window (400, WindowWidth / 2, 300, 300, 'black', WindowHandleZoom)
dev_set_line_width (2)
* 
* Main loop: process different images
for Index1 := 1 to 7 by 1
    read_image (Image, 'saw_blade/saw_' + Index1$'02')
    * 
    * By subpixel precise thresholding the contour of the saw is obtained.//提取锯片亚像素轮廓。
    threshold_sub_pix (Image, Border, 128)
    * 
    * Segment, select and sort linear contour parts//分段、筛选和排序线性轮廓。
    segment_contours_xld (Border, ContoursSplit, 'lines_circles', 3, 2, 1)
    select_shape_xld (ContoursSplit, SelectedContours, 'contlength', 'and', 30, 200)
    count_obj (SelectedContours, Number)
    gen_empty_obj (ToothSides)
    for Index2 := 1 to Number by 1
        select_obj (SelectedContours, SelectedContour, Index2)
        * Get the global contour attribute 'cont_approx'
        get_contour_global_attrib_xld (SelectedContour, 'cont_approx', Attrib)
        * The attribute 'cont_approx' has been set by the operator
        * segment_contours. For lines, the value is -1.
        if (Attrib == -1)
            concat_obj (ToothSides, SelectedContour, ToothSides)
        endif
    endfor
    sort_contours_xld (ToothSides, ToothSidesSorted, 'upper_left', 'true', 'column')
    * 
    * Calculate the orientation of the tooth sides//拟合直线,并计算直线方向。
    fit_line_contour_xld (ToothSidesSorted, 'tukey', -1, 0, 5, 2, Rows1, Columns1, Rows2, Columns2, Nr, Nc, Dist)
    line_orientation (Rows1, Columns1, Rows2, Columns2, Orientations)
    * 
    * Measure the angles between the sides that belong to the same tooth
    Angles := []
    * Check whether the first tooth is completely visible.
    * If not, start the measurement with the second tooth.
    if (Rows1[0] > Rows2[0])
        Start := 1
    else
        Start := 2
    endif
    for Index2 := Start to |Orientations| - 1 by 2
        Angle := abs(Orientations[Index2 - 1] - Orientations[Index2])
        * Ensure that the angle is within the interval [0,PI/2] degrees.
        if (Angle > PI_2)
            Angle := PI - Angle
        endif
        * 
        * Display the result
        dev_display_tooth (Image, ToothSides, Index2, Rows1, Columns1, Rows2, Columns2, WindowHandleMain, WindowHandleZoom)
        dev_set_window (WindowHandleMain)
        dev_disp_text ('Tooth ' + (Index2 + 1) / 2, 'window', 10, 10, 'black', [], [])
        dev_disp_text ('Included Angle: ' + deg(Angle)$'.2f' + ' deg', 'window', 35, 10, 'black', [], [])
        dev_disp_text ('Press Run (F5) to continue', 'window', 'bottom', 'right', 'black', [], [])
        Angles := [Angles,Angle]//得到所有锯齿夹角。
    endfor
endfor
* 
dev_set_window (WindowHandleMain)
dev_disp_text ('      End of program      ', 'window', 'bottom', 'right', 'black', [], [])
dev_set_window (WindowHandleZoom)
dev_close_window ()

总结

  本次锯片检测算法,集中在对XLD轮廓的处理上。要点是对轮廓的提取、分割、筛选、排序、拟合和计算夹角等,本次检测的本质特征为轮廓。


参考资料

  《机器视觉算法与应用》

你可能感兴趣的:(学习Halcon,视觉检测,图像处理,计算机视觉,人工智能)