halcon 拟合

*打开一张图像
read_image (Image, 'die_pads')

*关闭已经打开的窗口
dev_close_window ()

*获取图像大小
get_image_size (Image, Width, Height)

*打开新窗口
dev_open_window (0, 0, Width * 2, Height * 2, 'black', WindowHandle)

*在新窗口里显示图像
dev_display (Image)

*修改窗口里显示图像的大小
dev_set_part (0, 0, Height - 1, Width - 1)

*快速阈值
fast_threshold (Image, Region, 180, 255, 20)

*对区域进行连通处理,获取区域里连通的组件
connection (Region, ConnectedRegions)


*过滤面积大小在【200, 1200】范围里和区域的长轴和短轴的比值在【1, 2】的区域
select_shape (ConnectedRegions, SelectedRegions, ['area','anisometry'], 'and', [200,1], [1200,2])

*填充过滤后区域里的内部间隙
fill_up (SelectedRegions, RegionFillUp)

*将填充区域形状转化为凸形
shape_trans (RegionFillUp, RegionTrans, 'convex')

*获取凸形区域的边界
boundary (RegionTrans, RegionBorder, 'inner')

*对RegionBorder区域进行形态学的膨胀操作,结构元素为2.5
dilation_circle (RegionBorder, RegionDilation, 2.5)

*将膨胀后的区域合并为一个区域
union1 (RegionDilation, RegionUnion)

*剪切出来RegionUnion里的图像
reduce_domain (Image, RegionUnion, ImageReduced)

*对ImageReduced图像进行边缘提取
edges_sub_pix (ImageReduced, Edges, 'sobel_fast', 0.5, 20, 40)

*对Edges里的轮廓进行过滤,保留长度范围在【10, 200】的轮廓
select_shape_xld (Edges, SelectedContours, 'contlength', 'and', 10, 200)

*将相邻的轮廓合并为一个轮廓
union_adjacent_contours_xld (SelectedContours, UnionContours, 2, 1, 'attr_keep')

*将UnionContours轮廓拟合成为一个仿射矩形轮廓
fit_rectangle2_contour_xld (UnionContours, 'tukey', -1, 0, 0, 3, 2, Row, Column, Phi, Length1, Length2, PointOrder)

*生成仿射轮廓
gen_rectangle2_contour_xld (Rectangle, Row, Column, Phi, Length1, Length2)

*显示图像
dev_display (Image)

*设置输出显示的颜色数目
dev_set_colored (12)

*显示PADs区域
dev_display (Rectangle)

### Halcon 拟合线段的功能及用法 Halcon 提供了多种算子来实现线段拟合,这些算子可以根据输入的轮廓数据进行直线、圆弧或更复杂的曲线拟合。以下是关于 Halcon拟合线段功能的详细介绍和示例代码。 #### 1. 使用 `fit_line_contour_xld` 进行线段拟合 `fit_line_contour_xld` 是 Halcon 中用于对 XLD 轮廓进行直线拟合的核心算子。该算子可以计算出拟合直线的起点和终点坐标,并返回相关的统计信息。 ```python # 示例代码:使用 fit_line_contour_xld 进行线段拟合 read_image(Image, 'blobs') edges := edges_sub_pix(Image, 'canny', 1, 5, 20, 40) select_contours_shape_xld(Edges, SelectedContours, 'length', 'and', 100, 99999) # 对选定的轮廓进行直线拟合 fit_line_contour_xld(SelectedContours, 'tukey', 0, 0, 3, 0.2, RowBegin, ColBegin, RowEnd, ColEnd, Nr, Nc, Dist) # 绘制拟合结果 gen_contour_polygon_xld(Line, [RowBegin, RowEnd], [ColBegin, ColEnd]) dev_display(Line) ``` 此代码中,`fit_line_contour_xld` 的参数说明如下: - `'tukey'`:指定鲁棒算法。 - `0` 和 `0`:限制最大点数。 - `3`:迭代次数。 - `0.2`:剪切因子[^1]。 #### 2. 使用 `segment_contours_xld` 分割并拟合多个线段 如果需要将复杂的轮廓分割为多个线段并分别进行拟合,可以使用 `segment_contours_xld` 算子。 ```python # 示例代码:使用 segment_contours_xld 分割并拟合多个线段 read_image(Image, 'shapes') edges := edges_sub_pix(Image, 'canny', 1, 5, 20, 40) # 将轮廓分割为直线和圆弧 segment_contours_xld(Edges, ContoursSplit, 'lines_circles', 5, 4, 3) # 遍历每个分割后的轮廓并进行拟合 for i := 0 to |ContoursSplit| - 1 by 1 fit_line_contour_xld(ContoursSplit[i], 'tukey', 0, 0, 3, 0.2, RowBegin, ColBegin, RowEnd, ColEnd, Nr, Nc, Dist) gen_contour_polygon_xld(Line, [RowBegin, RowEnd], [ColBegin, ColEnd]) dev_display(Line) endfor ``` 在此代码中,`segment_contours_xld` 的参数说明如下: - `'lines_circles'`:指定分割模式为直线和圆弧。 - `5`:平滑轮廓的点数。 - `4` 和 `3`:第一次和第二次迭代的距离阈值[^2]。 #### 3. 使用 `gen_contour_nurbs_xld` 进行样条曲线拟合 对于更复杂的曲线拟合需求,可以使用 `gen_contour_nurbs_xld` 算子生成非均匀有理 B 样条(NURBS)曲线。 ```python # 示例代码:使用 gen_contour_nurbs_xld 进行样条曲线拟合 read_image(Image, 'nurbs_example') edges := edges_sub_pix(Image, 'canny', 1, 5, 20, 40) # 生成 NURBS 曲线 gen_contour_nurbs_xld(NurbsContour, ControlPointsRow, ControlPointsCol, Weights, KnotVector) # 显示拟合结果 dev_display(NurbsContour) ``` 此代码中,`gen_contour_nurbs_xld` 的参数包括控制点坐标、权重和节点向量[^4]。 --- ###
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值