Halcon union_adjacent_contours_xld详解_智信仁勇严道的博客-CSDN博客
1、合并端点在一起的轮廓
union_adjacent_contours_xld(Contours : UnionContours : MaxDistAbs, MaxDistRel, Mode : )
功能
合并轮廓。
参数列表
Contours (input_object) :输入轮廓
UnionContours (output_object) :合并后输出轮廓
MaxDistAbs (input_control) :两个轮廓端点间的最小距离控制参数
MaxDistRel (input_control) :两个轮廓端点间的最小距离与最长轮廓长度的比值控制参数
Mode (input_control) :定义轮廓属性处理的模式,即,是否保留或丢弃轮廓属性
详解
写到这里你可能被halcon官方文档搞晕了,文档里明明对MaxDistAbs、MaxDistRel的论述和算子union_collinear_contours_xld中的论述是一字不差的雷同,但根据union_collinear_contours_xld的理解在这个算子上却完全对不上。是不是很疑惑,是不是开始怀疑人生, 我也困扰了很久。但细心的童鞋会发现halcon文档虽然对这两个参数详解描述相同,但对变量定义的描述却不同。
union_adjacent_contours_xld算子的描述:
union_collinear_contours_xld算子的描述:
哎,始终想不通Halcon为啥这样设计文档内容。
举例
MaxDistAbs、MaxDistRel两个参数有一个不满足就不合并。如下图,只要两个参数中的其中一个比设置的临界值小,那么两个轮廓将不会合并。
如果想对比算子union_collinear_contours_xld的参数设置,可以访问我的另一篇博客。
https://blog.csdn.net/qq_18620653/article/details/105518295
代码
gen_contour_polygon_xld (FirstContour, [500,1000], [1,1])
gen_contour_polygon_xld (SecondContour, [1001,1500], [2,501])
length_xld (FirstContour, FirstLength)
length_xld (SecondContour, SecondLength)
concat_obj (FirstContour, SecondContour, ConcatContour)
union_adjacent_contours_xld (ConcatContour, UnionContours, sqrt(2), 0.0020041, ‘attr_keep’)
在类似如下图中查找物体的数量:目标物体如果是连接在一起的,可以进行腐蚀处理后,再打散查询物体的数量。
1、先开运算除去毛刺,然后打散处理,打散处理后连接在一起的物体依然连接在一起
2、进行腐蚀再打散,然后利用count_obj求出物体的数量
3、求出数量后,再膨胀
* This programs demonstrates the use of basic morphology
* operators.
* The aim of the program is to detect each single pellet
* (bright particle on a darker background).
*
dev_update_off ()
read_image (Image, 'pellets')
dev_close_window ()
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowID)
dev_set_part (0, 0, Height - 1, Width - 1)
set_display_font (WindowID, 16, 'mono', 'true', 'false')
dev_set_colored (6)
dev_set_draw ('margin')
dev_set_line_width (3)
dev_display (Image)
disp_message (WindowID, 'Detect each single pellet', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowID, 'black', 'true')
stop ()
*
* Segment the regions of the pellets from the background
binary_threshold (Image, LightRegion, 'max_separability', 'light', UsedThreshold)
opening_circle (LightRegion, Region, 3.5)
dev_display (Region)
disp_message (WindowID, 'First, segment the pellets', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowID, 'black', 'true')
stop ()
*
* Compute the connected pellet regions
* Note, that this approach fails, because some of
* the pellets are still connected.
connection (Region, ConnectedRegionsWrong)
dev_display (Image)
dev_display (ConnectedRegionsWrong)
disp_message (WindowID, 'Simple connection fails', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowID, 'black', 'true')
stop ()
*
* Separate each pellet from the others by erosion
erosion_circle (Region, RegionErosion, 7.5)
dev_display (Image)
dev_display (RegionErosion)
disp_message (WindowID, 'Erosion of the pellet regions', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowID, 'black', 'true')
stop ()
*
* Now, compute the connected pellet regions
connection (RegionErosion, ConnectedRegions)
dev_display (Image)
dev_display (ConnectedRegions)
disp_message (WindowID, 'Perform connection now', 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowID, 'black', 'true')
stop ()
*
* Turn back to the original pellet size by applying a dilation
dilation_circle (ConnectedRegions, RegionDilation, 7.5)
count_obj (RegionDilation, Number)
dev_display (Image)
dev_display (RegionDilation)
disp_message (WindowID, Number + ' pellets detected', 'window', 12, 12, 'black', 'true')