一,基本的图像通道知识
在图像处理中有一个很重要的概念叫图像通道,在RGB色彩模式下就是指那单独的红色、绿色、蓝色部分。也就是说,一幅完整的图像,是由红色绿色蓝色三个通道组成的。他们共同作用产生了完整的图像。
一幅完整的图像,红色绿色蓝色三个通道缺一不可。即使图像中看起来没有蓝色,只能说蓝色光的亮度均为0,但不能说没有蓝色通道存在。
“存在、亮度为零”和“不存在”是两个不同的概念。
一幅图像,如果关闭了红色通道,那么图像就偏青色。如果关闭了绿色通道,那么图像就偏洋红色。如果关闭了蓝色通道,那么图像就偏黄色。
这个现象再次印证了反转色模型:红色对青色、绿色对洋红色、蓝色对黄色。
如果察看单个通道,发现每个通道都显示为一幅灰度图像(不能说是黑白图像)。某个通道的灰度图像中的明暗对应该通道色的明暗,从而表达出该色 光在整体图像上的分布情况。由于通道共有3个,所以也就有了3幅灰度图像。
通道中的纯白,代表了该色光在此处为最高亮度,亮度级别是255。
通道中的纯黑,代表了该色光在此处完全不发光,亮度级别是0。
介于纯黑纯白之间的灰度,代表了不同的发光程度,亮度级别介于1至254之间。
灰度中越偏白的部分,表示色光亮度值越高,越偏黑的部分则表示亮度值越低。
现在可以明白为何通道用灰度表示了吧?因为通道中色光亮度从最低到最高的特性,正符合灰度模式那种从黑到白过渡的表示。正是因为灰度的这种特性,使得它在以后还被应用到其它地方。通道中的灰度,与颜色调板的灰度滑块是对应的。
二,单通道图像分割
1阈值分割
全局阈值,局部阈值法,直方图自动阈值(自适应阈值)
指定全局阈值
threshold( Image: Region: MinGray, MaxGray) //人工设置阈值
局部阈值法,后面再加以描述
直方图自动阈值(自适应阈值)
auto_threshold( Image: Region:Sigma:)
//自动阈值输入必须是是单通道图像,会有多阈值分割,Sigma用于对灰度直方图进行高斯平滑,决定了平滑的程度(分割细致程度),当sigma很大时,灰度直方图基本会被平滑为只剩下一个波峰,而分割是根据平滑后直方图的波谷来进行的,Sigma小,分割的越细致。
三,彩色图像的分割
decompose3
trans_from_rgb(red,green,blue,result1,result2,result3,colortypespace)
二 相关的算子详解
选择区域(分割图像)
原图如下,需要选择其中的3个焊点。
代码:
threshold (ImageReduced, Regions1, 0, 201) connection(Regions1, ConnectedRegions) count_obj(ConnectedRegions, N1) select_obj(ConnectedRegions, ObjectSelected, 3) select_shape(ConnectedRegions, SelectedRegions, 'area', 'and', 150, 99999) count_obj(SelectedRegions, N2)
代码解释:
threshold阈值算子
threshold (ImageReduced, Regions1, 0, 201)
这个是取阈值的算子,阈值是分割图像的一种常见办法。它选择满足输入控制参数的灰度值之内的灰度值,当所有的像素点都满足这个区域的话,就形成一个区域。如下图所示是选择灰度值在0和201之间的像素构成一个区域。
可以如下图这样调出向导(灰度直方图),很方便的插入代码。
connection(Regions1, ConnectedRegions)
count_obj(ConnectedRegions, N1)
取连接区域,然后计数,这个例子中连通区域数量N1=4
select_obj(ConnectedRegions, ObjectSelected, 3)
这句可以选中4个连通区域中的一个,索引号为3的区域。
select_shape(ConnectedRegions, SelectedRegions, 'area', 'and', 150, 99999)
count_obj(SelectedRegions, N2)
通过按area大小范围来过滤shape。这里选中了3个区域,N2=3
select_shape算子, 跟据特征值选择区域。
其相关功能的算子可以求Region指定特征值:region_features(Regions : : Features : Value)
select_shape算子可以由向导(特征直方图)来进行可视化操作,可以很方便的生成代码。如下图所示:
如上图所示的“特征”种类及其意义见下表:
Region特征一览:
特征 | 英 | 译 | 备注 |
area | Area of the object | 对象的面积 | |
row | Row index of the center | 中心点的行坐标 | |
column | Column index of the center | 中心点的列坐标 | |
width | Width of the region | 区域的宽度 | |
height | Height of the region | 区域的高度 | |
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 | 右下角列坐标 | |
circularity | Circularity | 圆度 | 0~1 |
compactness | Compactness | 紧密度 | 0~1 |
contlength | Total length of contour | 轮廓线总长 | |
convexity | Convexity | 凸性 | |
rectangularity | Rectangularity | 矩形度 | 0~1 |
ra | Main radius of the equivalent ellipse | 等效椭圆长轴半径长度 | |
rb | Secondary radius of the equivalent ellipse | 等效椭圆短轴半径长度 | |
phi | Orientation of the equivalent ellipse | 等效椭圆方向 | |
anisometry | Anisometry | 椭圆参数,Ra/Rb长轴与短轴的比值 | |
bulkiness | 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 | 最小外接矩形宽度的一半 | |
moments_m11 | Geometric moments of the region | 几何矩 | |
moments_m20 | Geometric moments of the region | 几何矩 | |
moments_m02 | Geometric moments of the region | 几何矩 | |
moments_ia | Geometric moments of the region | 几何矩 | |
moments_ib | Geometric moments of the region | 几何矩 | |
moments_m11_invar | Geometric moments of the region | 几何矩 | |
moments_m20_invar | Geometric moments of the region | 几何矩 | |
moments_m02_invar | Geometric moments of the region | 几何矩 | |
moments_phi1 | Geometric moments of the region | 几何矩 | |
moments_phi2 | Geometric moments of the region | 几何矩 | |
moments_m21 | Geometric moments of the region | 几何矩 | |
moments_m12 | Geometric moments of the region | 几何矩 | |
moments_m03 | Geometric moments of the region | 几何矩 | |
moments_m30 | Geometric moments of the region | 几何矩 | |
moments_m21_invar | Geometric moments of the region | 几何矩 | |
moments_m12_invar | Geometric moments of the region | 几何矩 | |
moments_m03_invar | Geometric moments of the region | 几何矩 | |
moments_m30_invar | Geometric moments of the region | 几何矩 | |
moments_i1 | Geometric moments of the region | 几何矩 | |
moments_i2 | Geometric moments of the region | 几何矩 | |
moments_i3 | Geometric moments of the region | 几何矩 | |
moments_i4 | Geometric moments of the region | 几何矩 | |
moments_psi1 | Geometric moments of the region | 几何矩 | |
moments_psi2 | Geometric moments of the region | 几何矩 | |
moments_psi3 | Geometric moments of the region | 几何矩 | |
moments_psi4 | Geometric moments of the region | 几何矩 |
---------------------
作者:hackpig
来源:www.skcircle.com
版权声明:本文为博主原创文章,转载请附上博文链接!
halocn基础系列教程列表:
Halcon学习(27-1)halcon测量技术:亚像素,亚像素精度
Halcon学习(26)dev_get_preferences的用法
Halcon学习(4-6)Region处理:动态阈值,分水岭
Halcon学习(4-5)Region处理:合并区域,扩大和缩小区域,取区域特征值
Halcon学习(4-4)Region处理: 区域相减,区域求补集
Halcon学习(4-2)Region处理:选择区域,分割图像
Halcon学习(4-1)ROI感兴趣区域,reduce_domain和crop_domain,鼠标绘制区域
视觉检测之产品瑕疵检测
视觉检测之焊点检测
模板匹配/仿射变换
halcon模板匹配 scaled_shape_model模板的另一个例子
Halcon中两种实现旋转的方法rotate_image和affine_trans_image
halcon的 shape_model模板匹配的算子使用流程的总结

