select_shape()
是 HALCON 中用于基于形状特征筛选区域的关键算子,广泛应用于图像分割、目标检测和工业质检等领域。它允许用户根据指定的几何特征从输入区域集合中选择符合条件的区域。
至于为什么单独介绍这个算子呢,因为他筛选特征的方式有太多种了,如果可以熟练的掌握这些特征,那在后面的例程学习以及实际应用中,可谓是得心应手了。
select_shape(Regions : SelectedRegions : Features, Operation, Min, Max : )
参数 | 类型 | 说明 |
---|---|---|
Regions |
输入 | 待筛选的区域集合(单个或多个区域) |
SelectedRegions |
输出 | 筛选后符合条件的区域集合 |
Features |
输入 | 筛选依据的形状特征(字符串或字符串元组) |
Operation |
输入 | 逻辑操作符:'and' 或 'or' |
Min |
输入 | 特征最小值(数值或数值元组) |
Max |
输入 | 特征最大值(数值或数值元组) |
这个算子给出的特征描述太多了,下面就详细介绍一下这些,方便大家了解一下里面都有一些什么方法,可以提取哪些目标。
select_shape()
支持 50+ 种形状特征,主要分为以下几类:
特征名 | 描述 | 计算公式/说明 |
---|---|---|
'area' |
区域面积 | 像素数量 |
'width' |
区域宽度 | 边界框宽度 |
'height' |
区域高度 | 边界框高度 |
'row' |
中心行坐标 | 区域重心行坐标 |
'column' |
中心列坐标 | 区域重心列坐标 |
row1 | Row index of upper left corner | 左上角行坐标 | |
column1 | Column index of upper left corner | 左上角列坐标 | |
row2 | Row index of lower right corner | 右下角行坐标 | |
column2 | Column index of lower right corner | 右下角列坐标 |
这几个特征应该不难理解,个人感觉应用较多的还是这个area特征,通过设定筛选的area的高低阈值范围,可以过滤掉一些不符合面积要求的目标。
特征名 | 描述 | 计算公式/说明 |
---|---|---|
'circularity' |
圆度 | 【OpenCV 例程 300篇】226. 区域特征之紧致度/圆度/偏心率_opencv300-CSDN博客 |
'compactness' |
紧凑度 | 【OpenCV 例程 300篇】226. 区域特征之紧致度/圆度/偏心率_opencv300-CSDN博客 |
'rectangularity' |
矩形度 | 区域面积 / 最小外接矩形面积 |
'convexity' |
凸度 | 凸包面积 / 原始区域面积 |
'anisometry' |
各向异性 | 长轴 / 短轴 |
'bulkiness' |
椭圆参数,蓬松度 | π*Ra*Rb/A |
struct_factor | Structur Factor | 椭圆参数,Anisometry*Bulkiness-1 | |
outer_radius | Radius of smallest surrounding circle | 最小外接圆半径 | |
inner_radius | Radius of largest inner circle | 最大内接圆半径 | |
inner_width | Width of the largest axis-parallel rectangle that fits into the region | 最大内接矩形宽度 | |
inner_height | Height of the largest axis-parallel rectangle that fits into the region | 最大内接矩形高度 | |
dist_mean | Mean distance from the region border to the center | 区域边界到中心的平均距离 | |
dist_deviation | Deviation of the distance from the region border from the center | 区域边界到中心距离的偏差 | |
roundness | Roundness | 圆度,与circularity计算方法不同 | |
num_sides | Number of polygon sides | 多边形边数 | |
connect_num | Number of connection components | 连通数 | |
holes_num | Number of holes | 区域内洞数 | |
area_holes | Area of the holes of the object | 所有洞的面积 | |
max_diameter | Maximum diameter of the region | 最大直径 | |
orientation | Orientation of the region | 区域方向 | |
euler_number | Euler number | 欧拉数,即连通数和洞数的差 | |
rect2_phi | Orientation of the smallest surrounding rectangle | 最小外接矩形的方向 | |
rect2_len1 | Half the length of the smallest surrounding rectangle | 最小外接矩形长度的一半?? | smallest_rectangle2 |
rect2_len2 | Half the width of the smallest surrounding rectangle | 最小外接矩形宽度的一半 |
特征名 | 描述 |
---|---|
'contlength' |
轮廓长度 |
'roundness' |
圆整度 |
'num_sides' |
多边形边数 |
'phi' |
主轴方向 |
特征名 | 描述 |
---|---|
'moments_m11' |
二阶中心矩 |
'moments_ia' |
惯性矩 |
'moments_ib' |
|
'moments_m02' |
有关矩特征的,还有很多,我也没有搞懂这么多,每一个怎么用,各位如果感兴趣,可以深入了解一下。
* 读取图像并分割 read_image(Image, 'particles.png') threshold(Image, Regions, 128, 255) connection(Regions, ConnectedRegions) * 筛选面积在100-500像素之间的区域 select_shape(ConnectedRegions, SelectedRegions, 'area', 'and', 100, 500) * 显示结果 dev_display(Image) dev_set_color('red') dev_display(SelectedRegions)
* 筛选圆形且紧凑的区域
select_shape(ConnectedRegions, RoundRegions,
['circularity', 'compactness'], // 特征
'and', // 逻辑操作
[0.85, 1.0], // 最小值
[1.0, 1.5]) // 最大值
* 筛选宽高比>2且方向在±15°内的区域
select_shape(ConnectedRegions, LongRectangles,
['anisometry', 'phi'],
'and',
[2.0, rad(-15)], // 最小值 (rad将角度转为弧度)
[9999, rad(15)]) // 最大值
* 筛选面积>1000 OR 圆度>0.9的区域
select_shape(Regions, LargeOrRound,
['area', 'circularity'],
'or',
[1000, 0.9],
[99999, 1.0])
* 基于平均面积筛选
area_center(Regions, Area, Row, Column)
mean := mean(Area)
select_shape(Regions, AboveAverage, 'area', 'and', mean, 99999)
* 分类圆形和方形区域
select_shape(Regions, Circles, 'circularity', 'and', 0.9, 1.0)
select_shape(Regions, Squares, 'rectangularity', 'and', 0.9, 1.0)
不得不承认,halcon是真牛。