工控编程吧

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

作者: qq263946146    时间: 2019-5-22 17:35
标题: halcon sort_contours_xld函数介绍
sort_contours_xld(Contours : SortedContours : SortMode, Order, RowOrCol : )
ort_contours_xld根据轮廓的相对位置对它们进行排序。
RowOrCol指定了排序标准:
“row”首先根据其位置的行坐标对轮廓进行排序,
如果行坐标相同,则根据列坐标对轮廓进行排序。
相反,“column”首先根据列坐标对轮廓进行排序。
SortMode通过单独的参考点定义轮廓的位置。


参数值如下:
upper_left
upper_right
lower_left
lower_right
外接平行矩形的左上角,右上角,左下角,右下角。

character
位置由外接矩形的左上角决定。
与'upper_left'相反,如果轮廓线与参数RowOrCol指定的坐标方向重叠,
轮廓线也会根据剩余坐标进行排序。


参数Order决定排序顺序是递增还是递减:使用“true”表示排序顺序递增,
使用“false”表示排序顺序递减。


例程
dev_close_window ()
read_image (Image, 'letters')
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width / 2, Height / 2 + 100, 'black', WindowHandle)
dev_set_part (-100, 0, Height - 1, Width - 1)
dev_display (Image)
*图像XLD边缘提取
threshold_sub_pix (Image, Edges, 128)
select_contours_xld (Edges, SelectedContours, 'contour_length', 10, 200, -0.5, 0.5)
*轮廓排序
sort_contours_xld (SelectedContours, SortedContours1, 'character', 'true', 'column')
dev_set_line_width (3)
count_obj (SelectedContours, Number)
for I := 1 to Number by 1
    select_obj (SortedContours1, ObjectSelected, I)
    dev_display (ObjectSelected)
    wait_seconds (0.01)
endfor
dev_clear_window ()
sort_contours_xld (SortedContours1, SortedContours2, 'character', 'true', 'row')
for I := 1 to Number by 1
    select_obj (SortedContours2, ObjectSelected, I)
    dev_display (ObjectSelected)
    wait_seconds (0.01)
endfor

也可参考另一例程
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)
sort_contours_xld (SelectedContours, SortedContours1, 'upper_left', 'true', 'row')
select_obj (SortedContours1, OuterContour, 1)
select_obj (SortedContours1, InnerContour, 4)
*计算最内,最外轮廓距离,距离信息保存于轮廓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)
例程中屏蔽代码可替代sort_contours_xld函数


[halcon]1[/halcon]









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