工控编程吧

标题: halcon smooth_contours_xld函数介绍 [打印本页]

作者: qq263946146    时间: 2019-5-22 17:04
标题: halcon smooth_contours_xld函数介绍
smooth_contours_xld(Contours : SmoothedContours : NumRegrPoints : )

smooth_contours_xld对输入的XLD轮廓进行平滑,
结果保存于SmoothedContours
平滑是通过将轮廓点投影到局部回归线(即,为最小二乘近似直线),
由当前轮廓点每边的 NumRegrPoints计算得到。
例如,在轮廓缩放之前,应该调用这个函数。


例程
MinWidth := 30
MaxWidth := 35
SmoothnessTolerance := .2
read_image (Image, 'plastic_parts/phone_camera_frame_02')
threshold (Image, Region, 100, 255)
dilation_rectangle1 (Region, RegionDilation, 15, 15)
reduce_domain (Image, RegionDilation, ImageReduced)
*提取图像XLD轮廓
edges_sub_pix (ImageReduced, Edges, 'canny', 0.7, 10, 60)
union_adjacent_contours_xld (Edges, UnionContours, 7, 7, 'attr_keep')
select_shape_xld (UnionContours, SelectedContours, 'contlength', 'and', 700, 99999)
*提取XLD轮廓最内最外轮廓
length_xld (SelectedContours, Length)
tuple_sort_index (Length, Indices)
select_obj (SelectedContours, InnerContour, Indices[0] + 1)
select_obj (SelectedContours, OuterContour, Indices[3] + 1)
*计算最内,最外轮廓距离,距离信息保存于轮廓OuterContourWithWidth ,此轮廓等于InnerContour   
distance_contours_xld (InnerContour, OuterContour, OuterContourWithWidth, 'point_to_segment')
*提取轮廓InnerContour, OuterContour间的距离超出上公差,下公差范围的轮廓   
segment_contour_attrib_xld (OuterContourWithWidth, OuterContourPartToNarrow, 'distance', 'or', 0, MinWidth)
segment_contour_attrib_xld (OuterContourWithWidth, OuterContourPartToWide, 'distance', 'or', MaxWidth, 10000)
*平滑最内,最个轮廓,再计算将平滑前后轮廓距离,提取距离超出公差的轮廓   
    smooth_contours_xld (OuterContour, OuterContourSmooth, 11)
    smooth_contours_xld (InnerContour, InnerContourSmooth, 11)
    distance_contours_xld (OuterContour, OuterContourSmooth, OuterContourWithDistance, 'point_to_segment')
    distance_contours_xld (InnerContour, InnerContourSmooth, InnerContourWithDistance, 'point_to_segment')
    segment_contour_attrib_xld (OuterContourWithDistance, OuterContourDefect, 'distance', 'or', SmoothnessTolerance, 100)
    segment_contour_attrib_xld (InnerContourWithDistance, InnerContourDefect, 'distance', 'or', SmoothnessTolerance, 100)
    *显示结果
    dev_close_window ()
    dev_open_window_fit_image(Image,0, 0, 512, 512, WindowHandle)
    count_obj (OuterContourPartToNarrow, NumTooNarrow)
    count_obj (OuterContourPartToWide, NumTooWide)
    count_obj (OuterContourDefect, NumInnerDefects)
    count_obj (InnerContourDefect, NumOuterDefects)
    if (NumTooNarrow + NumTooWide + NumInnerDefects + NumOuterDefects == 0)
        OK := 1
        disp_message (WindowHandle, 'Frame OK', 'window', 50, 12, 'forest green', 'true')
    else
        OK := 0
        disp_message (WindowHandle, 'Frame not OK', 'window', 50, 12, 'red', 'true')
    endif
    dev_set_line_width (5)
    dev_display (Image)
    dev_set_color ('yellow')
    dev_display (OuterContourPartToNarrow)
    dev_set_color ('red')
    dev_display (OuterContourPartToWide)
    dev_set_color ('green')
    dev_display (OuterContourDefect)
    dev_set_color ('blue')
    dev_display (InnerContourDefect)
[halcon]1[/halcon]






欢迎光临 工控编程吧 (https://www.gkbc8.com/) Powered by Discuz! X3.4