当前位置: 首页 > news >正文

【机械视觉】Halcon—【十五、一维码(条形码)和二维码识别】

一维码(条形码)检测

基本原理

一维码是由黑白条纹组成的图形标识,通过条纹的宽度和间隔来表示数据。Halcon支持多种一维码标准,如EAN-13、UPC-A、Code 128等。

检测步骤

  1. 创建条码模型

    create_bar_code_model([], [], BarCodeHandle)
  2. 设置参数(可选)

    set_bar_code_param(BarCodeHandle, 'element_size_min', 1.5)
    set_bar_code_param(BarCodeHandle, 'persistence', 1)
  3. 检测条

    find_bar_code(Image, SymbolRegions, BarCodeHandle, 'auto', DecodedDataStrings)
  4. 获取结果

    get_bar_code_result(BarCodeHandle, 'all', 'decoded_types', DecodedTypes)
  5. 清除模型

    clear_bar_code_model(BarCodeHandle)

关键参数

  • element_size_min: 最小元素大小(控制检测灵敏度)

  • persistence: 持久性参数(控制检测稳定性)

  • orientation: 条码方向(可指定或自动检测)


一维码实例一

read_image (Image, './一维码图/barcode_5.bmp')
dev_set_draw ('margin')
dev_set_color ('green')* 1 核心算子 创建一个条形码的模版
* 参数1 预留元组 写条形码的参数名字
* 参数2 预留元组 写条形码的参数值
* 参数3 BarCodeHandle 条形码实例对象
create_bar_code_model ([], [], BarCodeHandle)* 2 核心算子设置模版参数
set_bar_code_param (BarCodeHandle, 'barcode_width_min', 300) // 设置条形码宽度
set_bar_code_param (BarCodeHandle, 'barcode_height_min', 80) // 设置条形码高度* 3 根据条形码模版找合适区域
* 第四个参数  auto 指定要搜索条形码类型
* DecodedDataStrings 解码之后的结果
find_bar_code (Image, SymbolRegions, BarCodeHandle, 'auto', DecodedDataStrings)* 4 获取条形码的具体信息
* all 获取所有已经解码的结果
* 'decoded_types' 指定要搜索结果的类型的可选项 获取条形码的类型 code39类型
get_bar_code_result (BarCodeHandle, 'all', 'decoded_types', BarCodeResults)dev_get_window (WindowHandle)
msg:=BarCodeHandle+':'+DecodedDataStrings

一维码实例二(使用循环扫描多张图片)

dev_close_window ()
dev_update_off ()
dev_get_window (WindowHandle)* query 查询
* font 字体
* 查询当前窗口的字体类型
query_font (WindowHandle, Font)
* 查找元组里面是否有哪个字体 返回值查到索引值
tuple_find (Font, '新宋体', Indices)
if (Indices!=-1)set_display_font (WindowHandle, 16, '新宋体', 'true', 'false')
elseset_display_font (WindowHandle, 16, '楷体', 'true', 'false')
endifdev_set_draw ('margin')
dev_set_line_width (3)
dev_set_color ('green')* 1 创建条形码模型
create_bar_code_model ([], [], BarCodeHandle)
* 2 设置参数
set_bar_code_param (BarCodeHandle, 'barcode_width_min', 280)
set_bar_code_param (BarCodeHandle, 'barcode_height_min', 60)* 3 加载多张图片
list_files ('./一维码图', 'files', Files)
tuple_regexp_select (Files, '.(jpg|bmp|png)', Selection)* 4 遍历图片进行查询条形码
for i := 1 to |Selection| by 1* 获取图片read_image (Image, Selection[i])* 搜索条形码find_bar_code (Image, SymbolRegions, BarCodeHandle, 'auto', DecodedDataStrings)tuple_find (DecodedDataStrings, DecodedDataStrings, Indices1)text:=''if (Indices1!=0)text:='未找到一维码'else* 获取条形码信息get_bar_code_result (BarCodeHandle, 'all', 'decoded_types', BarCodeResults)text:=BarCodeResults+':'+DecodedDataStringsendifdev_display (Image)dev_display (SymbolRegions)area_center (SymbolRegions, Area, Row, Column)disp_message (WindowHandle,text , 'image', 0, 0, 'black', 'true')stop()
endfor

一维码实例三(环形条形码检测)

*关闭实时更新
dev_update_off ()
*获取系统参数clip_region值从  默认情况超出边界下自动裁剪
get_system ('clip_region', Information)
set_system ('clip_region', 'true')
*读取图像
read_image (Image, 'circular_barcode')
*获取图像大小
get_image_size (Image, Width, Height)
*关闭现有的窗口
dev_close_window ()
*打开窗口
dev_open_window (0, 0, Width/2, Height/2, 'black', WindowHandle)*设置颜色
dev_set_colored (12)
*显示图像
dev_display (Image)
dev_set_draw ('margin')***************提取条形码的环型区域*************************进行阈值范围提取
threshold (Image, Region, 0, 100)
*进行闭运算
closing_circle (Region, Region, 3.5)
*做连通处理
connection (Region, ConnectedRegions)*晒算特征
*width和height 外接矩形的宽度和高度
select_shape (ConnectedRegions, Ring, ['width','height'], 'and', [550,550], [750,750])*形状变换 找出圆环的外圆
shape_trans (Ring, OutCircle, 'outer_circle')*对圆环进行图像翻转
complement (Ring, RegionComplement)*对提取背景进行连通处理
connection (RegionComplement, ConnectedRegions1)
*再次进行筛选
select_shape (ConnectedRegions1, InnerCircle, ['width','height'], 'and', [550,550], [750,750])*获取最小的外接圆
smallest_circle (Ring, Row, Column, OuterRadius)
gen_circle (Circle, Row, Column, OuterRadius)*获取物体内圆的外接圆
smallest_circle (InnerCircle, Row1, Column1, Radius)
gen_circle (Circle1, Row1, Column1, Radius)*
dev_set_color ('green')
dev_set_draw ('margin')
dev_display (Image)
dev_display (OutCircle)
dev_display (InnerCircle)
stop ()***********获取环型区域条码************************
Width1:=1440 //矩形的宽度
Height1:=round(OuterRadius-Radius-10) //矩形的默认的高度*将圆弧转换为极坐标
*1 输入图像
*2 输出图像 展开之后的矩形图像
*参数3、4 极坐标的行坐标 列坐标,设置为圆心坐标
*参数5、6起始和结束角度范围  0-360 逆时针。360-0顺时针
*参数7/8 最小半径和最大半径 :最小半径设置标准外圆半径-安全量  最大半径:内圆半径+安全量
*参数9 、10 输出图像宽度和高度
polar_trans_image_ext (Image, PolarTransImage,  Row, Column,  rad(360), 0, OuterRadius-5 , Radius+5, Width1, Height1, 'nearest_neighbor')*取反操作
invert_image (PolarTransImage, ImageInvert)create_bar_code_model ([], [], BarCodeHandle)
find_bar_code (ImageInvert, SymbolRegions, BarCodeHandle, 'auto', DecodedDataStrings)
clear_bar_code_model (BarCodeHandle)*设置窗口大小
dev_set_window_extents (0, 0, Width1/2, Height1)
*展示到展开之后的图像
dev_display (PolarTransImage)*展示条形码区域
dev_display (SymbolRegions)
disp_message (WindowHandle, DecodedDataStrings, Information, 0, 0, 'black', [])
stop ()*展示在原图上 并且在原图绘制条形码区域
*把极坐标转换成圆弧的坐标
polar_trans_region_inv (SymbolRegions, XYTransImage,Row, Column, rad(360),0, OuterRadius-5 , Radius+5 ,Width1, Height1,Width,Height, 'nearest_neighbor')*重新设置窗口宽度和高度
dev_set_window_extents (0, 0, Width/2, Height/2)
dev_display (Image)
dev_display (XYTransImage)
disp_message (WindowHandle, DecodedDataStrings, Information, 0, 0, 'black', [])


二维码检测

基本原理

二维码是二维矩阵式条码,比一维码存储更多信息。Halcon支持QR Code、Data Matrix、PDF417等多种二维码。

检测步骤

  1. 创建二维码模型

    create_data_code_2d_model('Data Matrix ECC 200', [], [], DataCodeHandle)
  2. 设置参数(可选)

    set_data_code_2d_param(DataCodeHandle, 'polarity', 'light_on_dark')
    set_data_code_2d_param(DataCodeHandle, 'contrast_tolerance', 'high')
  3. 检测二维码

    find_data_code_2d(Image, SymbolRegions, DataCodeHandle, 'auto', ResultHandles, DecodedDataStrings)
  4. 获取结果

    get_data_code_2d_results(DataCodeHandle, 'all', 'all', ResultNames, ResultValues)
  5. 清除模型

    clear_data_code_2d_model(DataCodeHandle)

关键参数

  • polarity: 明暗极性(light_on_dark或dark_on_light)

  • contrast_tolerance: 对比度容差

  • symbol_size: 符号尺寸范围

  • module_size: 模块大小范围


二维码实例一(识别多种二维码)

dev_get_window (WindowHandle)*1创建Data Matrix ECC 200 或者称为D码模版
*default_parameters 默认参数
*maximum_recognition 最大识别度
create_data_code_2d_model ('Data Matrix ECC 200', ['default_parameters'], ['maximum_recognition'], DataCodeHandle)*创建QRCode的模版
create_data_code_2d_model ('QR Code', ['default_parameters'], ['maximum_recognition'], DataCodeHandle1)*创建PDF417的模版
create_data_code_2d_model ('PDF417', ['default_parameters'], ['maximum_recognition'], DataCodeHandle2)*2设置模版参数
*set_data_code_2d_param (DataCodeHandle1, 'polarity', 'light_on_dark')*3  加载多张图片
list_files ('D:/机器视觉/Halcon/10_条码_二维码识别/二维码/兼容多种二维码读取案例/二维码', 'files', Files)
*ignore_case 无论大小写
tuple_regexp_select (Files, ['.(png|jpg)','ignore_case'], Selection)*4 循环遍历进行查找2微码
for Index := 0 to |Selection|-1 by 1read_image (Image, Selection[Index])*匹配D码*ResultHandles 结果句柄  DecodedDataStrings 结果字符串find_data_code_2d (Image, SymbolXLDs, DataCodeHandle, [], [], ResultHandles, DecodedDataStrings)*二维码的轮廓的数量count_obj (SymbolXLDs, Number)if (Number>0)*证明是D码disp_message (WindowHandle,'发现的是D码:'+ DecodedDataStrings, 'window', 12, 12, 'black', 'true')disp_continue_message (WindowHandle, 'black', 'true') //按f5继续提示stop ()else*查找pdf码find_data_code_2d (Image, SymbolXLDs1, DataCodeHandle2, [], [], ResultHandles1, DecodedDataStrings1)count_obj (SymbolXLDs1, Number1)if (Number1>0)*证明是PDF码disp_message (WindowHandle,'发现的是PDF码:'+ DecodedDataStrings1, 'window', 12, 12, 'black', 'true')disp_continue_message (WindowHandle, 'black', 'true') //按f5继续提示stop ()else*查找QRCodefind_data_code_2d (Image, SymbolXLDs2, DataCodeHandle1, [], [], ResultHandles2, DecodedDataStrings2)count_obj (SymbolXLDs2, Number2)if (Number2>0)disp_message (WindowHandle,'发现的是QR码:'+ DecodedDataStrings2, 'window', 12, 12, 'black', 'true')disp_continue_message (WindowHandle, 'black', 'true') //按f5继续提示 stop ()elsedisp_message (WindowHandle,'未发现任何码', 'window', 12, 12, 'black', 'true')disp_continue_message (WindowHandle, 'black', 'true') //按f5继续提示 stop ()endifendifendif
endfor*释放模版实例
clear_data_code_2d_model (DataCodeHandle2)
clear_data_code_2d_model (DataCodeHandle1)
clear_data_code_2d_model (DataCodeHandle)

二维码实例二(识别二维码中文码)

二维码中文码就是检测结果是汉字(一般正常的检测结果都是数字)

read_image (Image, '1.png')
*set_system ('filename_encoding', 'utf8') //设置当前窗口的编码方式create_data_code_2d_model ('QR Code', [], [], DataCodeHandle) //创建QR模版set_data_code_2d_param (DataCodeHandle, 'string_encoding', 'utf8') // 设置解析字符串类型
find_data_code_2d (Image, SymbolXLDs, DataCodeHandle, [], [], ResultHandles, DecodedDataStrings)
dev_display (Image)
dev_display (SymbolXLDs)
dev_disp_text (DecodedDataStrings, 'window', 'top', 'left', 'black', [], [])
clear_data_code_2d_model (DataCodeHandle)


检测技巧与优化

  1. 图像预处理

    • 增强对比度

    • 去噪处理

    • 光照均匀化

  2. 参数调整

    • 对于低质量图像,增加容差参数

    • 对于小条码,调整元素大小参数

  3. ROI限制

    • 指定搜索区域提高检测速度

  4. 多码检测

    • 两种条码类型可以同时检测


示例代码(同时检测一维码和二维码)

* 读取图像
read_image(Image, 'barcodes.jpg')* 创建一维码模型
create_bar_code_model([], [], BarCodeHandle)
* 创建二维码模型
create_data_code_2d_model('QR Code', [], [], QRCodeHandle)* 检测一维码
find_bar_code(Image, BarCodeRegions, BarCodeHandle, 'auto', BarCodeStrings)
* 检测二维码
find_data_code_2d(Image, QRCodeRegions, QRCodeHandle, 'auto', [], QRCodeStrings)* 显示结果
dev_display(Image)
dev_display(BarCodeRegions)
dev_display(QRCodeRegions)* 清除模型
clear_bar_code_model(BarCodeHandle)
clear_data_code_2d_model(QRCodeHandle)


常见问题解决

  1. 检测不到条码

    • 检查图像质量

    • 调整元素大小参数

    • 尝试不同的极性设置

  2. 误识别

    • 增加对比度容差

    • 限制条码类型范围

    • 使用ROI缩小搜索区域

  3. 解码错误

    • 检查图像是否模糊

    • 尝试不同的条码标准

    • 增加图像分辨率

Halcon的条码检测功能非常强大,通过合理设置参数可以应对各种复杂场景下的条码识别需求。

http://www.lryc.cn/news/572450.html

相关文章:

  • SpringBoot扩展——发送邮件!
  • Java求职者面试指南:Spring, Spring Boot, Spring MVC, MyBatis技术点深度解析
  • Windows 10开始菜单优化方案,如何实现Win7风格开始菜单的还原
  • 火山引擎TTS使用体验
  • 类与对象(中)(详解)
  • 多卡解决报错torch.distributed.elastic.multiprocessing.errors.ChildFailedError的问题
  • API 接口:程序世界的通用语言与交互基因
  • 【音视频】PJSIP库——示例简介、C++类说明
  • 深度学习——激活函数
  • # python正则表达式——实战学习+理论
  • 跟踪大型语言模型的思想:对语言之间共享;提前规划;cot
  • RK3588调试之旅:adbd服务配置全攻略
  • stm32之使用中断控制led灯
  • 新生活的开启:从 Trae AI 离开后的三个月
  • linux操作命令(最常用)
  • 打破物理桎梏:CAN-ETH网关如何用UDP封装重构工业网络边界
  • 大模型更重要关注工艺
  • 目标检测之YOLOV11自定义数据使用OBB训练与验证
  • Neo4j常用语法-path
  • JS红宝书笔记 8.3 继承
  • 煤矿井下Modbus转Profibus网关的传感器与PLC互联解决方案
  • 机器学习×第十二卷:回归树与剪枝策略——她剪去多余的分支,只保留想靠近你的那一层
  • 运维人员常用网站列表
  • 【unitrix】 3.2 位取反运算(not.rs)
  • 【数字人开发】Unity+百度智能云平台实现长短文本个性化语音生成功能
  • 吃透 Golang 基础:Goroutine
  • golang excel导出时需要显示刷新
  • Set_path_margin 命令介绍
  • C++中所有数据类型
  • Jenkins通过Pipeline流水线方式编译Java项目