Halcon 作业六 找出圆
- 作业描述
- 1. 代码如下:
- 2. 一些函数的用法
- 相关知识
作业描述
找出圆和被遮挡的圆
1. 代码如下:
* 此处是由halcon设置配置读取文件夹自动生成插入的代码
* Image Acquisition 01: Code generated by Image Acquisition 01
list_files('D:/MC_ws/第6次作业图像', ['files','follow_links'], ImageFiles)
tuple_regexp_select(ImageFiles, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles)
for Index := 0 to |ImageFiles| - 1 by 1
read_image (Image, ImageFiles[Index])
* Image Acquisition 01: Do something
endfor
get_image_size(Image, Width, Height)
read_image(Image, ImageFiles[2])
*高斯滤波
gauss_filter(Image, ImageGauss, 3)
*二值化,可用灰度直方图进行可视化
threshold(ImageGauss, Region, 180, 255)
*对外边缘进行平滑处理,闭运算,此处开闭运算有疑惑,用过开运算感觉效果不好
closing_circle(Region,Regionclosing, 3.5)
*将不相连的区域都分割成单独的区域
connection(Regionclosing, ConnectedRegions)
*根据面积进行特征选择
select_shape(ConnectedRegions, SelectedRegions, 'area', 'and', 1000, 1000000)
*根据圆度进行选择
select_shape(SelectedRegions, SelectedRegions1, 'circularity', 'and', 0.2, 1)
*补全所有的圆
gen_contour_region_xld(SelectedRegions1, Contours, 'border')
shape_trans(SelectedRegions1, RegionTrans, 'outer_circle')
*找到圆心位置和面积
area_center(RegionTrans, Area, Row, Column)
*计算半径
d := 2*sqrt(Area/3.1415926)
dev_display(Image)
dev_display(SelectedRegions1)
2. 一些函数的用法
- select_shape(Regions : SelectedRegions : Features, Operation, Min, Max : )1000, 1000000)
根据特征值选择区域,其中
第三个参数表示所需要筛选的具体特征。
具体有:‘area’----面积;‘circularity’----圆度
第四个参数:‘and’ ‘or’ 似乎是用来表示筛选的特征种类个数,只筛选一个可直接用’and’.
第五个和第六个参数则分别是上限下限。
参考资料:
halcon 中 select_shape 算子 相关特征参数Halcon 算子 select_shape - gen_contour_region_xld(Regions : Contours : Mode : )
如果区域是从分割操作中获得的,那么这个算子是有用的,但是要在它们的边界上执行更高层次的算子,例如多边形近似和平行线的提取。对于输入区域的每个连通分量,生成边界的闭合轮廓。
参数Mode 可以取以下值:
center边界像素的中心用作轮廓点
border边界像素的外边界作为轮廓点
border_holes除了输入区域的外边界,还可以得到所有孔的轮廓。
通过以下区域外表,可以看出“border”和“center”两种模式的区别:
上图单个框表示单个像素。
然后用“border”和“center”计算轮廓,分别得到如下两条轮廓:
halcon gen_contour_region_xld函数介绍
- shape_trans(Region : RegionTrans : Type : )
函数作用:
变换区域的形状
参数Type的可选项解释如下:
convex:凸包性
ellipse:与输入区域有相同的矩和区域的椭圆
outer_circle:最小外接圆
inner_circle:最大内接圆
rectangle1:平行于坐标轴的最小外接矩形
rectangle2:最小外接矩形
inner_rectangle1:平行于坐标轴的最大内接矩形
inner_rectangle2:输入区域的骨架点拥有同输入区域的重心点最小的距离
shape_trans(Region : RegionTrans : Type : )
相关知识
1.xld,Region、XLD、Polygon之间的互相转换(HALCON)2.halcon学习笔记——Image,region,xld初步