Halcon 例程——Atom
这个例程的目的是使用算子来找出结构不规则的原则,其中涉及到一些算子:
crop_rectangle1:从图像中截取矩形图像
gauss_filter:高斯滤波
watersheds:图像分水岭算法
smallest_rectangle1:平行于坐标轴的最小外接矩形
Part Ⅰ 初始化程序
dev_update_off ()
dev_close_window ()
dev_set_draw ('margin')
dev_set_line_width (2)
都是基本的语句,其中例程中有一句:dev_update_window ('off')
意为不自动更新窗口,如果需要显示则必须调用dev_display
,但是若将其设置为’ on ’ ,无明显变化。但是设置为’ off '可以显著降低运算时间。
Part Ⅱ 图像预处理
首先是读入图像,然后经过观察其图像下方有不规则区域,于是为了减少计算量,我们使用 crop_rectangle1
算子来提取下半部分进行处理。
read_image (Image, 'atoms') *读取图像
get_image_size (Image, Width, Height) *获取图像大小
crop_rectangle1 (Image, Image, Height / 2, 0, Height - 1, Width - 1) *裁剪区域
crop_rectangle1(Image : ImagePart : Row1, Column1, Row2, Column2 : )
上一步的操作为获取图像大小,这个算子的参数即Image为输入图像,ImagePart 为输出图像,Row1,Column1为起始的左上角行列坐标,Row2,Column2为起始的右下角行列坐标。
其中在自己实现的时候,我将crop_rectangle1
输入为crop_part
,于是变成了截取相应的图像中相应的区域,而不是截取图像生成新的图像。crop_rectangle1
后产生的图像为416X356,而crop_part
后为416X710。由于误输入,产生的图像下半部分为黑色,怀疑为dev_update_window ('off')
有问题,经过多次排错,才发现这样的低级问题。
get_image_size (Image, Width, Height) *获取图片的大小
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowID) *打开一个适合图片的窗口
set_display_font (WindowID, 14, 'mono', 'true', 'false') *设置字体
dev_set_draw ('margin') *描绘边缘
dev_set_line_width (2) *设置字宽
dev_display (Image) *显示图片
disp_message (WindowID, 'Original image', 'window', 12, 12, 'black', 'true') *显示信息
disp_continue_message (WindowID, 'black', 'true') *显示继续
stop ()
Part Ⅲ 图像分割
* ****
* Segment image
* ****
* -> Using watershed
gauss_filter (Image, ImageGauss, 5) *高斯滤波
watersheds (ImageGauss, Basins, Watersheds) *分水岭算法
dev_display (Image) *显示图片
dev_set_colored (12) * 使用多种颜色输出
dev_display (Watersheds) *显示图片
disp_message (WindowID, 'Watersheds', 'window', 12, 12, 'black', 'true') *显示信息
disp_continue_message (WindowID, 'black', 'true') *显示信息
stop ()
在使用watersheds
时最好预先使用平滑算子(例如 binomial_filter
or gauss_filter
算子进行平滑)以减少输出区域的数目。
Part Ⅳ 处理区域操作
smallest_rectangle1 (Basins, Row1, Column1, Row2, Column2) *水平方向求出最小外接矩形
select_shape (Basins, SelectedRegions1, 'column1', 'and', 2, Width - 1)
select_shape (SelectedRegions1, SelectedRegions2, 'row1', 'and', 2, Height - 1)
select_shape (SelectedRegions2, SelectedRegions3, 'column2', 'and', 1, Width - 3)
select_shape (SelectedRegions3, Inner, 'row2', 'and', 1, Height - 3) *以上选择形状操作为去除边缘部分(因为受图片边缘影响为不规则矩形)
* -> Select irregularly shaped atoms
select_shape (Inner, Irregular, ['moments_i1','moments_i1'], 'or', [0,9.5e8], [1.5e8,1e10])*选出不规则矩形的形状 (moments_i1 几何矩)
dev_display (Image)
dev_set_line_width (1)
dev_set_color ('white')
dev_display (Inner)
dev_set_line_width (3)
dev_set_color ('red')
dev_display (Irregular)
disp_message (WindowID, 'Defects', 'window', 12, 12, 'black', 'true')
以下转自:http://www.cnblogs.com/iluzhiyong/p/4857877.html
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 | 椭圆参数,蓬松度πRaRb/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 | 几何矩 |