天天看点

HALCON示例程序measure_grid.hdev使用XLD分割键盘轮廓HALCON示例程序measure_grid.hdev使用XLD分割键盘轮廓

HALCON示例程序measure_grid.hdev使用XLD分割键盘轮廓

示例程序源码(加注释)

  • 关于显示类函数解释

    dev_update_off ()

    read_image (Image, ‘keypad’)

    get_image_pointer1 (Image, Pointer, Type, Width, Height)

    dev_close_window ()

    dev_open_window (0, 0, Width, Height, ‘white’, WindowHandle)

    set_display_font (WindowHandle, 16, ‘mono’, ‘true’, ‘false’)

    dev_display (Image)

    dev_set_colored (3)

    dev_set_draw (‘fill’)

  • 动态阈值分割,提取连接区域

    mean_image (Image, ImageMean, 7, 7)

    dyn_threshold (Image, ImageMean, RegionDynThresh, 4, ‘dark’)

  • 分割连通域

    connection (RegionDynThresh, ConnectedRegions)

  • 使用最大长度和最大直径筛选区域

    select_shape (ConnectedRegions, SelectedRegions, [‘max_diameter’,‘contlength’], ‘and’, [200,800], [99999,99999])

  • 使用圆形元素进行闭运算

    closing_circle (SelectedRegions, RegionClosing, 1.5)

    dev_display (Image)

    dev_display (RegionClosing)

    disp_continue_message (WindowHandle, ‘black’, ‘true’)

    stop ()

  • 提取骨架

    skeleton (RegionClosing, Skeleton)

  • 将骨架转化为XLD轮廓

    gen_contours_skeleton_xld (Skeleton, ContoursSkeleton, 1, ‘filter’)

  • 分割XLD轮廓

    segment_contours_xld (ContoursSkeleton, ContoursSplitSkeleton, ‘lines’, 5, 2, 1)

  • 使用连续长度筛选XLD轮廓

    select_contours_xld (ContoursSplitSkeleton, SelectedContours, ‘contour_length’, 30, 1000, -0.5, 0.5)

  • union_collinear_contours_xld - 联合近似共线的轮廓。

    union_collinear_contours_xld (SelectedContours, UnionCollinearContours, 100, 10, 20, rad(10), ‘attr_keep’)

    dev_display (Image)

    dev_display (UnionCollinearContours)

    disp_continue_message (WindowHandle, ‘black’, ‘true’)

    stop ()

  • 计数

    count_obj (UnionCollinearContours, NumberContours)

    gen_empty_obj (LinesHorizontal)

    gen_empty_obj (LinesVertical)

for i := 1 to NumberContours by 1
    select_obj (UnionCollinearContours, ObjectSelected, i)
    * 拟合直线XLD轮廓
    fit_line_contour_xld (ObjectSelected, 'tukey', -1, 0, 5, 2, RowBegin, ColBegin, RowEnd, ColEnd, Nr, Nc, Dist)
    * 由点生成多边形XLD轮廓
    gen_contour_polygon_xld (Contour, [RowBegin,RowEnd], [ColBegin,ColEnd])
    Phi := atan2(-Nr,Nc)
    if (abs(Phi) < rad(5))
        concat_obj (LinesVertical, Contour, LinesVertical)
    endif
    if (rad(85) < abs(Phi) and abs(Phi) < rad(95))
        concat_obj (LinesHorizontal, Contour, LinesHorizontal)
    endif
endfor
           

dev_display (Image)

dev_set_color (‘red’)

dev_display (LinesVertical)

dev_set_color (‘yellow’)

dev_display (LinesHorizontal)

disp_continue_message (WindowHandle, ‘black’, ‘true’)

stop ()

RowJunction := []

ColJunction := []

RowRealJunction := []

ColRealJunction := []

count_obj (LinesHorizontal, NumberLH)

count_obj (LinesVertical, NumberLV)

for i := 1 to NumberLH by 1
    select_obj (LinesHorizontal, HorizontalLine, i)
    * get_contour_xld  - 返回XLD轮廓的坐标。
    get_contour_xld (HorizontalLine, RowHorizontal, ColHorizontal)
    for j := 1 to NumberLV by 1
        select_obj (LinesVertical, VerticalLine, j)
        get_contour_xld (VerticalLine, RowVertical, ColVertical)
        * intersection_lines  - 计算两条线的交点
        intersection_lines (RowHorizontal[0], ColHorizontal[0], RowHorizontal[1], ColHorizontal[1], RowVertical[0], ColVertical[0], RowVertical[1], ColVertical[1], Row, Column, IsOverlapping)
        * distance_ps  - 计算点和线段之间的距离
        distance_ps (Row, Column, RowHorizontal[0], ColHorizontal[0], RowHorizontal[1], ColHorizontal[1], DistanceH, DistanceHMax)
        distance_ps (Row, Column, RowVertical[0], ColVertical[0], RowVertical[1], ColVertical[1], DistanceV, DistanceVMax)
        RowJunction := [RowJunction,Row]
        ColJunction := [ColJunction,Column]
        if ((DistanceH <= 30) and (DistanceV <= 30))
            RowRealJunction := [RowRealJunction,Row]
            ColRealJunction := [ColRealJunction,Column]
        endif
    endfor
endfor
           

dev_set_color (‘white’)

gen_cross_contour_xld (Cross, RowJunction, ColJunction, 12, 0.785398)

dev_display (Cross)

disp_continue_message (WindowHandle, ‘black’, ‘true’)

stop ()

dev_display (Image)

dev_set_color (‘gray’)

dev_display (LinesHorizontal)

dev_display (LinesVertical)

dev_set_color (‘white’)

gen_cross_contour_xld (Cross, RowRealJunction, ColRealJunction, 12, 0.785398)

dev_display (Cross)

disp_continue_message (WindowHandle, ‘black’, ‘true’)

stop ()

set_display_font (WindowHandle, 14, ‘mono’, ‘true’, ‘false’)

dev_display (Image)

junctions_skeleton (Skeleton, EndPoints, JuncPoints)

get_region_points (JuncPoints, RowJunctionRegionProcessing, ColumnJunctionRegionProcessing)

gen_cross_contour_xld (CrossCenter, RowJunctionRegionProcessing, ColumnJunctionRegionProcessing, 12, 0.785398)

dev_set_color (‘gray’)

dev_display (Skeleton)

dev_set_color (‘white’)

dev_display (CrossCenter)

disp_message (WindowHandle, ‘Result of corresponding’, ‘window’, 40, 10, ‘white’, ‘false’)

disp_message (WindowHandle, ‘region processing’, ‘window’, 90, 10, ‘white’, ‘false’)

处理思路

这个例子是主要讲解了XLD轮廓拟合直线,进而进行测量的例子。之前我们进行便于提取是提取最外边的轮廓,之后进行sub_edge求取边缘进行拟合,这次不同,这次使用的是求取区域,提取区域骨架,对骨架进行XLD拟合分析。

后记

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

继续阅读