提示:若没有查找的算子,可以评论区留言,会尽快更新
Halcon算子应用和技巧9
前言
提示:可以使用搜索小工具搜索对应算子名称:
本篇博文主要用于记录学习Halcon中算子的应用场景,及其使用代码和图像展示。只讲通俗易懂使用方法,不讲原理,不讲原理,不讲原理,重要的事情说三遍。
提示:以下是本篇文章正文内容,下面案例可供参考,注意参数坐标的使用,能帮助你理解算子
一、Halcon应用?
Halcon 是一个强大的图像处理工具,该工具是为了解决机器视觉项目任务而创建的。
二、算子汇总
每一博文仅展示10个算子,点击此链接进行查询所有算子,并点击对应算子跳转相应博文。'
【跳转链接】
三、应用算子
81. binary_threshold ()
先上代码:
read_image (Image, 'D:/HALCON_learn/理论/81-90/zhizhang.png')
rgb1_to_gray (Image, GrayImage)
binary_threshold (GrayImage, Region, 'max_separability', 'light', UsedThreshold)
stop ()
解析
自动阈值分割,适用于背景和目标有明显差异的,也就是灰度直方图是双峰的。
PS:根据设定参数选择亮的区域或者暗的区域
82. smooth_contours_xld ()
先上代码:
read_image (Image, 'D:/HALCON_learn/理论/81-90/zhizhang.png')
rgb1_to_gray (Image, GrayImage)
binary_threshold (GrayImage, Region, 'max_separability', 'light', UsedThreshold)
dilation_circle (Region, RegionDilation, 3.5)
reduce_domain (GrayImage, RegionDilation, ImageReduced)
edges_sub_pix (ImageReduced, Edges, 'canny', 1, 20, 40)
smooth_contours_xld (Edges, SmoothedContours, 9)
stop ()
解析
平滑亚像素边缘,左图原边缘,右图平滑后的边缘
PS:参数只能是奇数,参数越大,平滑越厉害,反之;一般用于产品磨边。
83. shape_trans_xld ()
先上代码:
read_image (Image, 'D:/HALCON_learn/理论/81-90/zhizhang2.png')
rgb1_to_gray (Image, GrayImage)
binary_threshold (GrayImage, Region, 'max_separability', 'light', UsedThreshold)
dilation_circle (Region, RegionDilation, 3.5)
reduce_domain (GrayImage, RegionDilation, ImageReduced)
edges_sub_pix (ImageReduced, Edges, 'canny', 1, 20, 40)
select_contours_xld (Edges, SelectedContours, 'contour_length', 500, 1000, -0.5, 0.5)
dev_set_color ('yellow')
shape_trans_xld (SelectedContours, XLDTrans, 'convex')
stop ()
解析
将xld形状改变,本次使用的凸包参数 ‘convex’,红色原始xld,黄色是改变后的xld
PS:你可以设定其他参数进行改变形状自行查看
84. threshold_sub_pix ()
先上代码:
read_image (Image, 'D:/HALCON_learn/理论/81-90/zhizhang2.png')
rgb1_to_gray (Image, GrayImage)
binary_threshold (GrayImage, Region, 'max_separability', 'light', UsedThreshold)
dilation_circle (Region, RegionDilation, 3.5)
reduce_domain (GrayImage, RegionDilation, ImageReduced)
threshold_sub_pix (ImageReduced, Border, 183)
解析:
根据设定的灰度阈值选择亚像素边缘
PS:该方法比较死板,以183为例, 该方法通过在172-190之间的灰度区域中分段,选择183灰度的区域,因为是亚像素,所以会这样分。
85. close_contours_xld ()
先上代码
gen_contour_polygon_xld (Contours, [50, 50, 150, 150], [50, 150, 150, 250])
gen_contour_polygon_xld (Contour2, [50, 50, 150, 150, 60], [300, 450, 450, 300, 300])
concat_obj (Contours, Contour2, Contours)
gen_contour_polygon_xld (Contour3, [200, 300, 300], [50, 50, 250])
concat_obj (Contours, Contour3, Contours)
gen_contour_polygon_xld (Contour4, [200, 200, 300, 300, 200], [320, 450, 450, 300, 300])
concat_obj (Contours, Contour4, Contours)
*
close_contours_xld (Contours, ClosedContours)
解析
将未闭合的xld进行闭合, 左边原图, 右边闭合结果图
PS:多边形构成也是点组成,该算子就是将首尾坐标一致, 添加了一个尾坐标和首坐标一样, 所以能将xld闭合.
86. sort_contours_xld ()
先上代码:
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)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_display (Image)
set_line_style (WindowHandle, [])
dev_set_color ('red')
threshold_sub_pix (Image, Edges, 128)
select_contours_xld (Edges, SelectedContours, 'contour_length', 10, 200, -0.5, 0.5)
* Sort the unsorted output:
* First sort the contours by row coordinates. The position of a
* contour is given by the upper left corner of its surrounding rectangle.
count_obj (SelectedContours, Number)
dev_set_line_width (3)
sort_contours_xld (SelectedContours, SortedContours1, 'character', 'true', 'column')
dev_display (Image)
disp_message (WindowHandle, 'XLD contours sorted by column', 'window', 12, 12, 'black', 'true')
for I := 1 to Number by 1
select_obj (SortedContours1, ObjectSelected, I)
dev_display (ObjectSelected)
wait_seconds (0.01)
endfor
解析
对混乱的xld边缘进行排序,排序方式以character为例, 以xld轮廓包围矩形左上顶点进行排序, 然后for循环方便选择xld
PS: 逐步从a选择到g, 还与’column’参数有关
87. clip_contours_xld ()
先上代码:
read_image (Angio, 'angio-part')
get_image_size (Angio, Width, Height)
dev_open_window (0, 0, 3 * Width / 2, 3 * Height / 2, 'black', WindowID)
lines_gauss (Angio, Lines, 2.3, 0.0, 0.7, 'dark', 'true', 'parabolic', 'true')
dev_display (Angio)
dev_set_color ('yellow')
dev_display (Lines)
stop ()
Top := 200
Left := 100
Bottom := 300
Right := 200
gen_rectangle1 (Rectangle, Top, Left, Bottom, Right)
reduce_domain (Angio, Rectangle, AngioReduced)
clip_contours_xld (Lines, LinesClipped, Top, Left, Bottom, Right)
解析
裁剪出对应矩形区域内的线(xld),右图为裁剪后的结果,中间是检测的线,左图为需要的矩形区域
88. clip_end_points_contours_xld ()
先上代码:
dev_close_window ()
dev_open_window (0, 0, 500, 500, 'black', WindowHandle)
dev_set_part (0, 0, 499, 499)
dev_set_line_width (3)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
* Create an ellipse with a contour point distance much larger than 1.
gen_ellipse_contour_xld (EllipseContour, 250, 250, 0, 200, 100, 0, rad(180), 'positive', 3)
* Clip off contour points for a total length of 20 pixels at both ends.
clip_end_points_contours_xld (EllipseContour, ClippedContoursLength, 'length', 20)
* Clip off 20 contour points at both ends.
clip_end_points_contours_xld (EllipseContour, ClippedContoursNumPoints, 'num_points', 20)
解析
从xld两端按照指定模式将两端的xld裁剪掉,左图原图,中间裁剪length,右图裁剪point
PS: point 比 length 长一点,这是因为点与点之间有长度在
89. clip_region_rel ()
先上代码:
read_image (Image, 'double_circle')
dev_close_window ()
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
* Segment a region containing the edges
fast_threshold (Image, Region, 0, 120, 7)
boundary (Region, RegionBorder, 'inner')
clip_region_rel (RegionBorder, RegionClipped, 5, 5, 5, 5)
解析
将图像整体看作一个矩形,从该矩形的上下左右裁剪掉指定参数量的像素数,左图原region,中图为区域边界,右图为裁剪后结果
PS:适用于裁剪图像四周不需要的xld
90. segment_contours_xld ()
read_image (Image, 'double_circle')
dev_close_window ()
get_image_size (Image, Width, Height)
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
fast_threshold (Image, Region, 0, 120, 7)
boundary (Region, RegionBorder, 'inner')
clip_region_rel (RegionBorder, RegionClipped, 5, 5, 5, 5)
dilation_circle (RegionClipped, RegionDilation, 2.5)
reduce_domain (Image, RegionDilation, ImageReduced)
edges_sub_pix (ImageReduced, Edges, 'canny', 2, 20, 60)
segment_contours_xld (Edges, ContoursSplit, 'lines_circles', 5, 4, 3)
count_obj (ContoursSplit, Number)
解析
将一根xld分割成多段,比如**‘lines_circles’**,将分割出可能的线和圆弧,左图为reduce_domain,从右图可见变为多段xld,obj从数量1变为6
PS:有时测量工件(齿轮的齿)的圆形度如何,需要该方法
以上内容陆续更新。。。
内容如有错误之处,望不吝指出,谢谢
以上内容陆续更新。。。