1、Halcon圆形分割
圆形分割的一般步骤:(1) 读取图像;(2) 图像灰度化;(3) 二值化;(4) 做闭运算封闭图像;(5) 将不相连的的部分区分开;(6) 按条件挑选出需要分割的区域;(7) 找到最小圆,将圆画出来;
代码如下:
read_image (Image, 'E:/HALCON_TEXT/Image/131770.bmp') //读取图像
rgb1_to_gray (Image, GrayImage) //灰度化
* get_image_size (Image, Width, Height)
* dev_open_window (0, 0, Width/3, Height/3, 'black', Window1)
threshold (GrayImage, Region, 122, 255) //二值化
closing_circle (Region, RegionClosing,10) //闭运算
connection (RegionClosing, ConnectedRegions) //将相连接的部分区分开
select_region_point (ConnectedRegions, DestRegions, 1000, 1000) //按条件挑选区域
smallest_circle (DestRegions, Row, Column, Radius) //最小外接圆
* dev_display (Image)
gen_circle (Circle, Row, Column, Radius) //生成圆
*设置只能看到线条
dev_set_draw ('margin')
2、图像上文本显示
文本显示算子经常在Halcon图像处理中用到,所以这里将其封装为外部函数,后续使用直接调用即可,封装的函数中参数有:字体类型、大小、窗体、颜色、位置、文本内容。代码如下:
*----------------------------------*
*设置窗口字体
*----------------------------------*
*字体类型、大小、窗体、颜色、位置、Message
Fonts := 'Times New Roman' //字体类型
Size := 32 //大小
Windows := 200000 //窗体
colors :='green' //颜色
positions := [100, 20] //位置
string :='显示内容' //Message
*设置字体
set_display_font (Windows, Size, Fonts, 'true', 'false')
*设置输出message的位置
disp_message (Windows, string, 'window', positions[0], positions[1], colors, 'false')
*外部函数调用
SetFont (Windows, Size, Fonts, string, positions, colors)
stop()
3、批量读图
在Halcon应用中经常会遇到批量读图的操作,在这里做一个记录,后续使用可以直接复制,代码如下(示例):
*--------------------------------------*
*批量读图
*--------------------------------------*
*读取文件夹
list_files ('E:/HALCON_TEXT/Image', 'files', Files)
*筛选相应格式的图像
tuple_regexp_select (Files, '\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima)$', ImageFiles)
*循环读图
for imgIndex := 0 to |ImageFiles| - 1 by 1
read_image (Image, ImageFiles[imgIndex])
endfor