第五章-拟合
拟合
所有fit开头的算子都是和拟合相关的算子
一般测量时往往是通过blob分析获得区域然后设置测量矩形,或者通过助手直接设置测量矩形,这里提供了一种特殊的方法就是直接把目标进行拟合成几何图形,然后根据几何图形的参数来计算测量距离
拟合流程:
- 采集图像,
- 预处理,
- 边缘提取,
- 轮廓联合分割
- 拟合,获得几何参数
- 根据拟合结果求去目标距离
Halcon的拟合示例在ctrl+E中的方法:边缘提取(亚像素)&几何测量中
通过拟合,你可以获得相应图形的几何表示,更方便求距离,面积,等参数,进而根据标定板或者像素尺寸转换来获得实际测量距离
轮廓联合与分割
示例1:拟合圆
拟合3个圆圈⭕️,提取圆的一段轮廓
* circles.hdev
* The edges in the image are segmented into lines and circles.
* For the edges that are part of a circle, the circle parameters
* are estimated and the resulting circle is displayed.
read_image (Image, 'double_circle')
*
* Init window
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)
dilation_circle (RegionClipped, RegionDilation, 2.5)
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)
segment_contours_xld (Edges, ContoursSplit, 'lines_circles', 5, 4, 3)
count_obj (ContoursSplit, Number)
dev_display (Image)
dev_set_draw ('margin')
dev_set_color ('white')
dev_update_window ('off')
for I := 1 to Number by 1
select_obj (ContoursSplit, ObjectSelected, I)
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)
fit_circle_contour_xld (ObjectSelected, 'ahuber', -1, 2, 0, 3, 2, Row, Column, Radius, StartPhi, EndPhi, PointOrder)
gen_circle_contour_xld (ContCircle, Row, Column, Radius, 0, rad(360), 'positive', 1.0)
dev_display (ContCircle)
endif
endfor
dev_set_colored (12)
dev_set_line_width (3)
dev_display (ContoursSplit)
步骤分析
1. blob分析,获得目标区域

read_image (Image, 'double_circle')
*
* Init window
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)
dilation_circle (RegionClipped, RegionDilation, 2.5)
reduce_domain (Image, RegionDilation, ImageReduced)
算子简介
clip_region_rel(Region输入区域,
RegionClipped输入裁剪结果,
Top,
Bottom,
Left,
Right)

2. 亚像素边缘提取
edges_sub_pix (ImageReduced目标区域图片,
Edges输出亚像素边缘轮廓,
'canny'边缘检测算法,
2, 平滑系数(canny算子平滑系数越大越平滑,其他相反)
20, 边缘差分阈值
60)

3. 轮廓分割
上述轮廓需要拟合成三个圆,
/segment_contours_xld(Contours输入轮廓,
ContoursSplit输出分割的轮廓,
Mode分割模式, 即你想要分割成直线lines还是几个圆lines_circles或者椭圆lines_ellipses
SmoothCont, 用来平滑轮廓的点数
MaxLineDist1, 第一次迭代轮廓到多边形的距离阈值
MaxLineDist2第二次迭代轮廓到多边形的距离阈值)
/
segment_contours_xld (Edges, ContoursSplit, 'lines_circles', 5, 4, 3)

本文介绍了Halcon中的拟合技术,包括拟合圆、直线的过程及应用案例。通过边缘提取、轮廓分割与拟合,实现了距离、半径等参数的精确测量。

5118

被折叠的 条评论
为什么被折叠?



