threshold_sub_pix (GrayImage, Border, 148) //获得亚像素轮廓
dev_set_color ('red')
* edges_sub_pix (GrayImage, Edges, 'sobel', 1, 20, 40) //获得亚像素轮廓的另一种方式
* area_center_xld (Border, Area, Row, Column, PointOrder)
* gen_cross_contour_xld (Cross, Row, Column, 6, 0.785398)
union_cocircular_contours_xld (Border, UnionContours, 0.5, 0.1, 0.2, 30, 10, 10, 'true', 1) //连接同一圆上的轮廓
union_adjacent_contours_xld (UnionContours, UnionContours1, 10, 1, 'attr_keep') //连接邻近的轮廓
select_contours_xld (UnionContours1, SelectedContours, 'contour_length', 100, 200000, -0.5, 0.5) //筛选轮廓,此为轮廓长度
select_contours_xld (SelectedContours, SelectedContours1, 'closed', 0.5, 200, -0.5, 0.5) //筛选轮廓,此为闭合的类型
fit_circle_contour_xld (SelectedContours1, 'algebraic', -1, 0, 0, 3, 2, Row, Column, Radius, StartPhi, EndPhi, PointOrder) //拟合成圆
* gen_circle_contour_xld (ContCircle, Row, Column, Radius, StartPhi, StartPhi, 'positive', 1)
gen_circle (Circle, Row, Column, Radius) //生成圆
dev_display (Circle)
shape_trans_xld (SelectedContours1, XLDTrans, 'rectangle1') //生成外接矩形
count_obj (XLDTrans, Number)
dev_set_color ('orange')
select_obj (XLDTrans, ObjectSelected, 1) //选择某一对象
dev_display (ObjectSelected)
* segment_contours_xld (SelectedContours1, ContoursSplit, 'lines_circles', 5, 4, 2)
* get_contour_xld (SelectedContours, Row, Col)
---------------------------------------------------------------------------------------------------------------
segment_contours_xld (SelectedContours, ContoursSplit, 'lines_circles', 5, 4, 3) //以线和圆作为分类切割
count_obj (ContoursSplit, Number)
for Index := 1 to Number by 1
select_obj (ContoursSplit, ObjectSelected, Index) //逐一选择
*返回亚像素轮廓的全局属性
*如果Attrib =-1,这一部分轮廓最适合被拟合为直线段。
*如果Attrib =0,这一部分轮廓最适合被拟合为椭圆弧。
*如果Attrib =1,这一部分轮廓最适合被拟合为圆弧。
get_contour_global_attrib_xld (ObjectSelected, 'cont_approx', Attrib) //判断是圆还是线
dev_set_color ('green')
if(Attrib > 0) // 若是圆弧
fit_circle_contour_xld (ObjectSelected, 'ahuber', -1, 0, 0, 3, 2, Row, Column, Radius, StartPhi, EndPhi, PointOrder)
gen_circle (Circle, Row, Column, Radius)
dev_display (Circle)
endif
if(Attrib = -1) // 若是直线
fit_line_contour_xld (ObjectSelected, 'tukey', -1, 0, 5, 2, RowBegin, ColBegin, RowEnd, ColEnd, Nr, Nc, Dist)
gen_line (Line, RowBegin, ColBegin, RowEnd, ColEnd, 'xld')
dev_display (Line)
endif
endfor
----------------------------------------------------
union_collinear_contours_xld:合并在同一直线的XLD
union_cocircular_contours_xld:合并在同圆的XLD
union_adjacent_contours_xld:合并邻近的XLD
===================
算子:select_counters_XLD
功能:选择“轮廓”-根据几个特征选择XLD轮廓。
格式:select_contours_xld(Contour,SelectedContour, Featours,Min1,Max1, Min2,Max2)
根据以下特征(参数特征)从输入轮廓轮廓轮廓中选择XLD轮廓:
‘contour_length’:
“轮廓”长度:
所有长度小于min1或大于max1的轮廓都不会返回(min2和max2在这里没有影响)。
‘maximum_extent’:
‘最大范围’:
所有最大范围(根据Haralick和Shapiro:Computer and Robot Vision,Addison-Wesley 1992,第3.2章)小于min1或大于max1的轮廓(根据其行和列方向上的八个极端点测量)都不会返回(min2和max2在这里没有影响)。
‘direction’:
“方向”:
仅返回回归线方向在min1和max1之间(以弧度表示,逆时针)的轮廓。min1和max1映射到[0,2*pi]的范围。(min2和max2在这里没有影响)。
‘curvature’:
“曲率”:
仅返回距离回归线平均距离在min1和max1之间,距离的标准偏差在min2和max2之间的轮廓。
‘closed’:
“关闭”:
仅返回起点和终点之间的距离小于或等于max1像素的轮廓。(min1、min2和max2在这里没有影响。)
‘open’:
“打开”:
仅返回其起点和终点之间的距离大于min1像素的轮廓。(max1、min2和max2在这里没有影响)。
如果根据曲率选择min1=max1=0或min2=max2=0,则各自的特征对选择没有影响。
————————————————
halcon中什么是xld?
xld(eXtended Line Descriptions) 扩展的线性描述,它不是基于像素的,人们称它是亚像素,只不过比像素更精确罢了,可以精确到像素内部的一种描述。
而image和region这些数据结构都是像素级别的。
亚像素精度数据可以通过亚像素阈值分割或者亚像素边缘提取来获得,这些处理得到的结果就是用亚像素轮廓来表示。
上面提到的亚像素轮廓其实在halcon中指的就是xld,它可以表示直线或多边形,是一组有序的控制点集合,控制点顺序用来说明彼此相连的关系。
当然亚像素轮廓可以分为两种:
1.闭轮廓:首尾相交
2.开轮廓:首尾不相交
--------------------------------------------------------------------------------------------------------------
我们可以通过如下算子得到xld:
threshold_sub_pix(Image:Boreder:Threshold:):提取xld并不是沿着像素与像素边界交界的地方,而是halcon经过某种计算得出的位置(一种描述)。其中Threshold为提取xld的阈值,灰度值以阈值为界提取xld。
xld的特征部分和region有很多相同的部分:
1.基础特征:xld面积,中心,宽高,左上角及右下角坐标。
2.形状特征:圆度,紧密度,长度,矩形度,凸性,偏心率,蓬松度(bulkiness),外接圆半径,外接矩形的方向及两边长度等。
3.云点特征:云点面积,中心,等效椭圆半轴及角度,云点方向等。
4.几何特征:二阶矩等。
--------------------------------------------------------------------------------------------------------------
选取特定xld:
使用算子:
select_shape_xld(XLD:SelectedXLD:Features,Operation,Min,Max:)
作用:选择特定形状特征要求的xld轮廓或多边形
XLD:要提取的XLD
SelectedXLD:提取到的XLD
Features:提取XLD的特征依据
Operation:特征之间的逻辑关系(and or)
Min,Max:特征值的要求范围
--------------------------------------------------------------------------------------------------------------
Select_Contours_xld(Contours:SelectedContours:Features,Min,Max,Min2,Max2:)
作用:选择多种特征要求的XLD轮廓 (如长度 开闭 方向 等特征,不支持多边形)
Contours:要提取的XLD轮廓
SelectedContours:提取到的XLD轮廓
Features:提取XLD轮廓的依据(筛选)
Min,Max,Min2,Max2:特征值的范围
--------------------------------------------------------------------------------------------------------------
area_center_xld(XLD:::Area,Row,Column,PointOrder)
作用:计算xld的面积和中心位置
下面来看看XLD的合并操作:
union_collinear_contours_xld:合并在同一直线的XLD
union_cocircular_contours_xld:合并在同圆的XLD
union_adjacent_contours_xld:合并邻近的XLD
这些算子以后会详细讲解。
--------------------------------------------------------------------------------------------------------------
再来看看XLD的合并操作:
把XLD转换成其他规则形状的XLD,相应说明及算子和region的很类似:
1.convex hull
2.outer_circle
3.retangle1
4.retangle2
这四种类型转换使用同一个算子:
shape_trans_xld(XLD:XLDTrans:Type:)
作用:转换XLD的形状,其中Type为转换的类型如上面的四种类型
--------------------------------------------------------------------------------------------------------------
接下来看看XLD的分割操作:
算子:
segment_contours_xld(Contours:ContoursSplit:Mode,SmoothCont,Max,LineDist1,MaxLineDist2:) //将轮廓用圆与线进行分类
get_contour_global_attrib_xld (ObjectSelected, 'cont_approx', Attrib) //求线段为圆还是线
*如果Attrib =-1,这一部分轮廓最适合被拟合为直线段。
*如果Attrib =0,这一部分轮廓最适合被拟合为椭圆弧。
*如果Attrib =1,这一部分轮廓最适合被拟合为圆弧。
--------------------------------------------------------------------------------------------------------------
然后就是XLD的拟合操作:(fit_***_contour_xld)
具体算子如下:
fit_line_contour_xld:拟合直线
fit_circle_contour_xld:拟合圆
fit_ellipse_contour_xld:拟合椭圆
fit_retangle2_contour_xld:拟合矩形
---------------------------------------------------------------------------------------------------------------------
length_xld (Contour, Length) 求轮廓的长度