1500字范文,内容丰富有趣,写作好帮手!
1500字范文 > Halcon拟合系列(6):轮廓平滑算子smooth_contours_xld

Halcon拟合系列(6):轮廓平滑算子smooth_contours_xld

时间:2021-10-09 10:45:05

相关推荐

Halcon拟合系列(6):轮廓平滑算子smooth_contours_xld

----inspect_frame_width.hdev(经典)

计算两个轮廓之间的距离

1、length_xld

获取轮廓的长度

2、tuple_sort_index( : : Tuple : Indices)

按升序对元组的所有元素进行排序,Indices中存储元素的索引,索引的对应元素值从小到大,元素值最小的索引为0。

length_xld (SelectedContours, Length)

Length =[2783.54, 2720.84, 2854.62, 2926.87]//Length元组中的索引为[0,1,2,3]

tuple_sort_index (Length, Indices)//Length中的值按从小到大排列,索引1的值<索引0<索引2<索引3

Indices = [1, 0, 2, 3]//输出的索引,对应的值从小到大

3、distance_contours_xld(ContourFrom, ContourTo : ContourOut : Mode : )

计算ContourFrom轮廓上的点到ContourTo轮廓上的点或线段最小距离,距离作为属性存储在输出轮廓中

ContourFrom:输入轮廓1

ContourTo:输入轮廓2

ContourOut :输出轮廓,由ContourFrom组成,包含计算所得到的两个轮廓之间距离信息,距离可以用 get_contour_attrib_xld(Contour::Name:Attrib) 算子提取,设置Attrib= distance获得

Mode: 'point_to_point'、'point_to_segment',速度更快

'point_to_point':点到点 ,如下图d1,速度更快

point_to_segment : 点到段,如下图d2,精度更准

4、segment_contour_attrib_xld(Contour : ContourPart : Attribute, Operation, Min, Max : )

根据Attribute属性对轮廓Contour 进行分割,返回符合要求的轮廓ContourPart

分割的时候Attribute属性可以根据需求同时设置两个,如果Operation=‘and’,则分割的轮廓必须同时满足两个属性的特征,都必须处于Min, Max限制范围内。如果Operation=‘or’,则至少有一个Attribute属性必须在Min, Max限制范围内。属性的处理顺序与在属性中给出的顺序相同。如果只使用一个Attribute属性,则忽略操作的值。

Contour :输入轮廓

ContourPart :输出轮廓

Attribute:属性

Default value: 'distance'

List of values: 'angle', 'asymmetry', 'contrast', 'distance', 'edge_direction', 'response', 'width_left', 'width_right'

Operation:运算符

5、smooth_contours_xld(Contours : SmoothedContours : NumRegrPoints : )

平滑轮廓

NumRegrPoints : 值越大平滑程度越厉害

平滑之前:

平滑之后:

6、get_contour_attrib_xld(Contour : : Name : Attrib)

根据不同的Attrib属性,返回亚像素轮廓Attrib属性特征的值

Attrib:

Suggested values: 'angle', 'edge_direction', 'width_right', 'width_left', 'response', 'contrast', 'asymmetry', 'distance'

经常和distance_contours_xld(ContourFrom, ContourTo : ContourOut : Mode : )算子一起用,当Mode =distance时, 可用get_contour_attrib_xld(Contour : : Name : Attrib)Attrib=distance,将distance_contours_xld计算所得的轮廓之间 的距离提取出来

* This program shows how to calculate the pointwise* distance of two contours to inspect the camera frame* of a phone housing.* * It uses the operator distance_contours_xld to calculate* the distance between the inner and outer contour of the* frame and the operator segment_contour_attrib_xld* to extract the defects for visualization.* * Set the thresholds for the allowed distances in pixelsMinWidth := 30MaxWidth := 35SmoothnessTolerance := .2* * Initialize visualizationdev_update_off ()dev_close_window ()read_image (Image, 'plastic_parts/phone_camera_frame_01')dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)set_display_font (WindowHandle, 14, 'mono', 'true', 'false')Colors := ['white','green','yellow','orange','red']Legend[0] := 'Legend:'Legend[1] := 'Extracted contours'Legend[2] := 'Frame too narrow'Legend[3] := 'Frame too wide'Legend[4] := 'Contour defect'* * Main loop:* Inspect the camera frame* NumImages := 2for I := 1 to NumImages by 1read_image (Image, 'plastic_parts/phone_camera_frame_' + I$'02')* * Extract frame edges* * Select the frame regionthreshold (Image, Region, 100, 255)dilation_rectangle1 (Region, RegionDilation, 15, 15)reduce_domain (Image, RegionDilation, ImageReduced)* Extract edges in the reduced image domainedges_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)count_obj (SelectedContours, Number)* Select the inner and the outer contour*获取轮廓的长度length_xld (SelectedContours, Length)*tuple_sort_index按升序对元组的所有元素进行排序,并按元素值从小到大的顺序返回相应元素在原来的元祖中的索引,元素值最小的索引为0,tuple_sort_index (Length, Indices)*选择最里面的轮廓,selected_obj的Index是从1开始的,素组的索引是从0开始的,所以需要加1*Indices[1] = 0,轮廓长度最小,Index是从1开始的,所以Indices[1]得加1select_obj (SelectedContours, InnerContour, Indices[1]+1 )*选择最外面的轮廓select_obj (SelectedContours, OuterContour, Indices[3]+1 )* 先计算两个轮廓之间的距离* Calculate the distances between the inner and outer contour.* The distances are stored as an attribute to the output contour.distance_contours_xld (InnerContour, OuterContour, OuterContourWithWidth, 'point_to_segment')* Get the contour parts that lie outside of the tolerances*分割提取出距离小于指定最小距离的轮廓部分segment_contour_attrib_xld (OuterContourWithWidth, OuterContourPartToNarrow, 'distance', 'or', 0, MinWidth)length_xld (OuterContourPartToNarrow, Length1)*分割提取出距离大于指定距离的轮廓部分segment_contour_attrib_xld (OuterContourWithWidth, OuterContourPartToWide, 'distance', 'or', MaxWidth, 10000)length_xld (OuterContourPartToWide, Length2)* 检查轮廓本身的平滑度* Check, if the contours are sufficiently smooth by* comparing each contour with a smoothed version of itselfsmooth_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')* Get the contour parts that lie outside of the tolerancessegment_contour_attrib_xld (OuterContourWithDistance, OuterContourDefect, 'distance', 'or', SmoothnessTolerance, 100)segment_contour_attrib_xld (InnerContourWithDistance, InnerContourDefect, 'distance', 'or', SmoothnessTolerance, 100)* * Get pointwise distances for debuggingget_contour_attrib_xld (OuterContourWithWidth, 'distance', FrameWidth)get_contour_attrib_xld (OuterContourWithDistance, 'distance', InnerDistances)get_contour_attrib_xld (InnerContourWithDistance, 'distance', OuterDistances)** Display resultsdev_display (Image)disp_message (WindowHandle, 'Inspect frame of phone camera (image ' + I + '/' + NumImages + ')', 'window', 12, 12, 'black', 'true')count_obj (OuterContourPartToNarrow, NumTooNarrow)count_obj (OuterContourPartToWide, NumTooWide)count_obj (OuterContourDefect, NumInnerDefects)count_obj (InnerContourDefect, NumOuterDefects)if (NumTooNarrow + NumTooWide + NumInnerDefects + NumOuterDefects == 0)OK := 1disp_message (WindowHandle, 'Frame OK', 'window', 50, 12, 'forest green', 'true')elseOK := 0disp_message (WindowHandle, 'Frame not OK', 'window', 50, 12, 'red', 'true')endif* dev_set_line_width (1)dev_set_color (Colors[1])dev_display (OuterContour)dev_display (InnerContour)dev_set_line_width (5)dev_set_color (Colors[2])dev_display (OuterContourPartToNarrow)dev_set_color (Colors[3])dev_display (OuterContourPartToWide)dev_set_color (Colors[4])dev_display (OuterContourDefect)dev_set_color (Colors[4])dev_display (InnerContourDefect)disp_message (WindowHandle, Legend, 'window', 500, 12, Colors, 'false')if (I < NumImages)disp_continue_message (WindowHandle, 'black', 'true')stop ()endifendfor

————————————————

版权声明:本文为CSDN博主「lfw」的原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接及本声明。

原文链接:/ABC13222880223/article/details/100017771

本内容不代表本网观点和政治立场,如有侵犯你的权益请联系我们处理。
网友评论
网友评论仅供其表达个人看法,并不表明网站立场。