halcon脚本-条形码识别【附源码】


前言

本文基于halcon实现条形码的识别,并且对识别器的一些参数进行设置


一、实验原图

在这里插入图片描述


二、实验思路

  • 首先获得halcon的条码识别器
  • 根据识别器里边获得条码的扫描线
  • 根据扫描线对附近区域扩大
  • 阈值获取条码位置

三、halcon脚本实现

这里上脚本,备注和逻辑在halcon脚本上做了注释

dev_get_window (WindowHandle)
dev_update_off ()
count_seconds (Start)
read_image (Image1, '1.bmp')
dev_set_draw ('margin')
dev_set_color ('red')
create_bar_code_model ([], [], BarCodeHandle)

set_bar_code_param (BarCodeHandle, 'slanted', 'auto')
*识别条码只找一条,我们期望对每一个图像解码单个条形码
set_bar_code_param (BarCodeHandle, 'stop_after_result_num', 2)
*最小间隙
set_bar_code_param (BarCodeHandle, 'element_size_min', 1)
*条形码阅读器将使用不同的方法,在条码的'element_size_min'1是很有用
set_bar_code_param (BarCodeHandle, 'meas_param_estimation', 'true')
*'present': 检查校验和,验证条码的正确性   'absent':不检查校验
set_bar_code_param (BarCodeHandle, 'check_char', 'present')
*设置条码候选区域的最小对比度
set_bar_code_param (BarCodeHandle, 'contrast_min', 0)
*表面的变形,为true条形码阅读器会尝试补偿这种扭曲,如贴在玻璃瓶
set_bar_code_param (BarCodeHandle, 'element_size_variable', 'true')
*当值为1的时候会保存读码的中间结果
set_bar_code_param (BarCodeHandle, 'persistence', 1)
*读取条码是否开启多数投票
set_bar_code_param (BarCodeHandle, 'majority_voting', 'true')
*使用值为大于0.0'meas_thresh_abs',强制条形码阅读器仅使用扫描线的部分,具有至少该值的绝对对比度因此减少误报的数量。
set_bar_code_param (BarCodeHandle, 'meas_thresh_abs', 10.0)
*使用默认的扫描线数运行检测
set_bar_code_param (BarCodeHandle, 'num_scanlines', 0)
*用于测量扫描线内边缘位置的相对阈值
set_bar_code_param (BarCodeHandle, 'meas_thresh', 0.1)
* 有效的错误检测,因为错误来自单个假扫描线的检测结果
set_bar_code_param (BarCodeHandle, 'min_identical_scanlines', 0 + 1)
*容许误差值,可设置为'low'或者'high',设置为'high'可能造成误判
set_bar_code_param (BarCodeHandle, 'start_stop_tolerance', 'low')
*设置最小条形码高度或宽度并再次搜索条形码
*set_bar_code_param (BarCodeHandle, 'barcode_height_min', 600)
*set_bar_code_param (BarCodeHandle, 'barcode_width_min', 1600)

*在矩形区域中读取条码
*draw_rectangle2 (3600, Row1, Column1, Phi1, Length1, Length2)
*decode_bar_code_rectangle2 (Image1, BarCodeHandle, 'Code 128', Row1, Column1, Phi1, Length1, Length2, DecodedDataStrings1)
*在图片中读取条码
find_bar_code (Image1, SymbolRegions, BarCodeHandle, 'auto', DecodedDataStrings)


*获得所有提取条码的扫描线
*get_bar_code_object (BarCodeObjects, BarCodeHandle, 'all', 'candidate_regions')
*获得条码region区域,跟SymbolRegions一样
get_bar_code_object (SymbolRegion, BarCodeHandle, 'all', 'symbol_regions')
*获得条码字符串,跟DecodedDataStrings一样
get_bar_code_result (BarCodeHandle, 'all', 'decoded_strings', DecodedData)
*获得可识别条码的类型
get_param_info ('find_bar_code', 'CodeType', 'value_list', AllCodeTypes)
*获得条码XLD的轮廓角度
get_bar_code_result (BarCodeHandle, 'all', 'orientation', BarCodeResults)
*获得条码的类型
get_bar_code_result (BarCodeHandle, 'all', 'decoded_types', DecodedDataTypes)
*获得条码切割线的分割
get_bar_code_object (ValidScanlines, BarCodeHandle, 'all', 'scanlines_valid')
*检查扫描线OK  NG   ----->  ScanlinesAll 代表NG  ;ScanlinesValid代表OK
get_bar_code_object (ScanlinesAll, BarCodeHandle, 0, 'scanlines_all')
get_bar_code_object (ScanlinesValid, BarCodeHandle, 0, 'scanlines_valid')
*关于条码的完整参考数据
get_bar_code_result (BarCodeHandle, 0, 'decoded_reference', Reference)

*显示条码的角度方向
area_center (SymbolRegions, Area, Row, Col)
gen_arrow_contour_xld (Arrow, Row + sin(rad(BarCodeResults)) * 70, Col - cos(rad(BarCodeResults)) * 70, Row - sin(rad(BarCodeResults)) * 70, Col + cos(rad(BarCodeResults)) * 70, 25, 25)
dev_display (Arrow)

clear_bar_code_model (BarCodeHandle)

*等待0.5s
wait_seconds(0.5)

disp_obj (Image1, WindowHandle)
if(|Area| > 0)
    dilation_circle (SymbolRegions, RegionDilation, 150)
    count_obj (RegionDilation, Number)
    for Index := 1 to 2 by 1
        select_obj (RegionDilation, RegionDilation_each, Index)
        reduce_domain (Image1, RegionDilation_each, ImageReduced)
        median_image (ImageReduced, ImageMedian1, 'circle', 1, 'mirrored')
        threshold (ImageMedian1, Region, 71, 223)
        closing_rectangle1 (Region, RegionClosing, 20, 20)
        threshold (ImageMedian1, Regions, 137, 147)
        opening_rectangle1 (RegionClosing, RegionOpening1, 20, 20)
        connection (RegionOpening1, ConnectedRegions1)
        select_shape (ConnectedRegions1, SelectedRegions, 'area', 'and', 20000, 100000)
        shape_trans (SelectedRegions, RegionTrans, 'rectangle2')
        
        set_color (WindowHandle, 'green')
        disp_region (RegionTrans, WindowHandle)
        area_center (RegionTrans, Area, Row, Col)
        orientation_region (RegionTrans, Phi)
        gen_arrow_contour_xld (Arrow, Row + sin(Phi) * 70, Col - cos(Phi) * 70, Row - sin(Phi) * 70, Col + cos(Phi) * 70, 25, 25)
        
        set_color (WindowHandle, 'red')
        dev_display (Arrow)
    endfor
endif
dev_update_on ()

四、效果图

在这里插入图片描述
在这里插入图片描述

  • 2
    点赞
  • 16
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

爱学习的广东仔

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值