相关链接
(1)【2021 亚太杯数学建模】赛题A-Image Edge Analysis and application图像边缘分析与应用 赛题思路解析及实现
(2)代码看这儿:betterbench.top/#/46/detail
1 Halcon软件使用简要指导
halcon的函数调用,返回值变量在()参数中去定义,比如我要初始化一个用Test(A,B)函数实现某个功能,A 是传入的参数,B是返回值参数。
3 实现
边缘检测,轮廓拟合,并对轮廓进行彩色绘图
【数据和代码下载】
*读取图片
read_image (Image, 'Pic1_1.bmp')
* 关闭窗口
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')
*用圆形结构元素扩张一个区域。
dilation_circle (RegionBorder, RegionDilation, 3.5)
*获得特定区域Region位置的图像
reduce_domain (Image, RegionDilation, ImageReduced)
*
* In the subdomain of the image containing the edges,
* extract subpixel precise edges.
*提取亚像素精密边缘轮廓
edges_sub_pix (ImageReduced, Edges, 'canny', 2, 20, 60)
*将一个XLD轮廓分割为直线段、圆(圆弧)、椭圆弧
segment_contours_xld (Edges, ContoursSplit, 'lines_circles', 5, 4, 3)
* dev_display (Edges)
*计算个数
count_obj (ContoursSplit, Number)
dev_display (Image)
*定义region填充模式
dev_set_draw ('margin')
dev_set_color ('white')
dev_update_window ('off')
for I := 1 to Number by 1
*选择一个元素
select_obj (ContoursSplit, ObjectSelected, I)
*返回XLD轮廓的全局属性值
get_contour_global_attrib_xld (ObjectSelected, 'cont_approx', Attrib)
* Fit a circle to the line segment that are arcs of a circle
if (Attrib > 0)
*用圆近似XLD轮廓
fit_circle_contour_xld (ObjectSelected, 'ahuber', -1, 2, 0, 3, 2, Row, Column, Radius, StartPhi, EndPhi, PointOrder)
*创建对应于圆或圆弧的XLD轮廓
gen_circle_contour_xld (ContCircle, Row, Column, Radius, 0, rad(360), 'positive', 1.0)
endif
endfor
dev_set_colored (12)
dev_set_line_width (3)
dev_display (ContoursSplit)
检测效果如下