【HALCON常用的一些操作】

该代码示例展示了如何使用HALCON库遍历指定文件夹,筛选bmp和jpg格式的图片,显示并保存图片。此外,还提供了批量转换图片格式(如从tiff转为png)的功能,并可选择性删除原图。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

HALCON文件夹遍历和文件筛选

* 遍历文件夹  
list_image_files ('C:/Users/Public/Documents/MVTec/HALCON-17.12-Progress/examples/images', 'default', 'recursive', ImageFiles) 
* 筛选bmp及jmp格式的图片  
tuple_regexp_select(ImageFiles, ['\\.(bmp|jpg|tif)$','ignore_case'], ImageFiles)  
* 打开窗口    
dev_open_window (0, 0, 512, 512, 'black', WindowHandle1)  
* 依次读取所选图片并显示 在窗口中  
for Index := 0 to |ImageFiles| - 1 by 1  
    read_image (Image, ImageFiles[Index])  
    dev_display(Image)  
    * 分割文件名  
    parse_filename(ImageFiles[Index], BaseName, Extension, Directory)  
    * 保存图片,给出路径及名称,新文件名为在原文件名后添加_adj  
    dev_disp_text (BaseName, 'window', 'top', 'left', 'black', [], [])
    dev_disp_text (Extension, 'window', 'top', 'right', 'black', [], [])
    dev_disp_text (Directory , 'window', 'bottom', 'left', 'black', [], [])
    *dump_window(WindowHandle,'bmp',Directory + BaseName + '_adj')  
    stop ()  
endfor  

注意使用绝对路径时Windows是右斜杠“\\”而halcon里是用的是左斜杠“//”

格式转换

*用Halcon批量修改图片格式,例如将bmp改为png(删除原图)
*原始格式
SourceFormat:='tiff'
*转换后格式
DestFormat:='png'
*是否删除原图
IsDeltaImage:=0

*可以同时选择多个文件
dev_open_file_dialog ('read_image', 'read_multi', 'default', SelectFiles)
*'ignore_case':忽略大小写
tuple_regexp_select (SelectFiles, ['\\.'+SourceFormat,'ignore_case'], ImageFiles)
hadTransform := 0
for Index :=0 to |ImageFiles| - 1 by 1
    read_image (Image, ImageFiles[Index])
    if (hadTransform = 1)
        delete_file (ImageFiles[Index -1])
    endiif
    parse_filename (ImageFiles[Index], BaseName, Extension, Directory)
    Substring:=Directory+BaseName
    write_image (Image, DestFormat, 0, Substring)
    hadTransform := IsDeltaImage
endfor
if (hadTransform = 1)
    delete_file (ImageFiles[Index -1])
endif

*若此处报错说明没有进行转换
dev_disp_text (Substring, 'window', 'center', 'left', 'black', [], [])
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值