一、图像预处理和条码增强

 

对比度太低:scale_image(或使用外部程序scale_image_range),增强图像的对比度。

 

图像模糊:emphasize锐化图像,使条码看起来更清晰。

 

深色背景上读取浅色条码:invert_image反转图像。

 

 

二、解码涉及的主要算子

 

read_image :读图

create_bar_code_model :创建条码模型

find_bar_code :查找条码

clear_bar_code_model :清除条码模型

 

 

如果条码非常简单,那么顺次执行上面4个算子就可以完成解码了。另外还有几个算子也很重要:

 

set_bar_code_param :设置解码时的参数

decode_bar_code_rectangle2 :在指定的矩形区域内解码

get_bar_code_param :获取解码时的参数(如果没有设置过,则获得的是默认值)

get_bar_code_result :获得解码后的结果,例如可以获得条码的类型(Code 128、Code 39等等)

get_bar_code_object :获得解码时的一些对象,例如可以获得解码后的条码区域

 

 

三、提高解码能力的其他措施

 

如果条码图像预处理以后,仍旧解码困难或者解码率不高,那么可以通过以下措施进一步提高解码能力:

 

1、如果整张图信息太多,则可以先把条码区域挖出来,使用reduce_domain和crop_domain算子,这样不仅可以降低解码难度,还可以减少解码时间。也可使用decode_bar_code_rectangle2在指定的矩形区域内解码。

 

2、可以尝试把条码图像转正再解码。(这种操作未经严格验证,不知道是否可以有效提高解码率)

 

3、当条码很密或者很小的时候,可以尝试用zoom_image_factor放大了条码图像。

 

4、find_bar_code中将“CodeType”设置为“auto”可以读取多种类型的条码,但是会增加运行时间,且可能会降低解码的可靠性。最好只扫描预知的条形码类型。

 

5、如果对于质量很差的条码,可以模拟日常手机扫码时的操作,即多次改变曝光,多次解码的方式,参考文章:

https://www.cnblogs.com/xh6300/p/9809692.html

 

6、通过set_bar_code_param算子设置解码时的参数,可以有效提高解码能力。(见下文)

 

 

四、set_bar_code_param算子的参数解析

 

'element_size_min'

条码的最小尺寸,指条码宽度和间距,大码应设大一点,减少处理时间

'element_size_max'

条码的最大尺寸,不能过小也不能过大

'check_char'

是否验证校验位,'absent'不检查校验和,'present'检查校验和

'persistence'

设置为1,则会保留中间结果,评估条码印刷质量时会用到

'num_scanlines'

解码时所用扫描线的最大数目,设置为0表示自动确定,一般设置为2-30

'start_stop_tolerance'

容许误差值,可设置为'low'或者'high',设置为'high'可能造成误判

'orientation'、'orientation_tol'

分别指条码的方向和方向容差,设置准确可大大提高解码效率

'element_height_min'

条码的最小高度,默认值-1表示自动推测条码高度,该参数对速度影响大

'stop_after_result_num'

设置要解码的个数,0表示全部找出,设置为2表示找到2个就不找了

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

 

有多个参数需要设置时,也可以把多个参数放在一句话里面,例如:

set_bar_code_param (BarCodeHandle,['check_char','element_size_min','element_size_max'], ['present',5,30])

 

 

 下面用一个完整的条码解码程序来演示一下:

 

 

 1 dev_set_draw ('margin')
 2 dev_set_line_width (2)
 3 set_font (3600, '-Courier New-18-*-*-*-*-1-')
 4 
 5 list_files ('pic', ['files','follow_links'], ImageFiles)
 6 tuple_regexp_select (ImageFiles, ['\\.(tif|tiff|gif|bmp|jpg|jpeg|jp2|png|pcx|pgm|ppm|pbm|xwd|ima|hobj)$','ignore_case'], ImageFiles)
 7 for Index := 0 to |ImageFiles| - 1 by 1
 8     read_image (Image, ImageFiles[Index])    
 9     gen_rectangle1 (Rectangle, 500, 10, 765.5, 2050)
10     reduce_domain (Image, Rectangle, Image)
11 *     crop_domain (ImageReduced, Image)
12     
13     *优化条码图像
14     emphasize (Image, Image, 3, 3, 1)
15     scale_image_range (Image, ImageScaled, 30, 220)
16   
17     *创建条码模型
18     create_bar_code_model ([], [], BarCodeHandle)
19     
20     *设置解码参数
21     set_bar_code_param (BarCodeHandle, 'element_size_min', 4)
22     set_bar_code_param (BarCodeHandle, 'element_size_max',32)
23     set_bar_code_param (BarCodeHandle,'check_char','present')
24     set_bar_code_param (BarCodeHandle, 'persistence', 1)
25     set_bar_code_param (BarCodeHandle, 'num_scanlines', 10)
26     set_bar_code_param (BarCodeHandle, 'start_stop_tolerance', 'high')
27     set_bar_code_param (BarCodeHandle, 'orientation', 0)
28     set_bar_code_param (BarCodeHandle, 'orientation_tol', 20)
29     set_bar_code_param (BarCodeHandle, 'element_height_min', 100)
30     set_bar_code_param (BarCodeHandle, 'stop_after_result_num', 0)
31       
32     
33     *解码
34     **decode_bar_code_rectangle2的解码能力似乎不如find_bar_code,漏掉了一个码
35 *     smallest_rectangle2 (Rectangle, Row1, Column1, Phi, Length1, Length2)
36 *     decode_bar_code_rectangle2 (Image, BarCodeHandle, ['Code 128','Code 39'], Row1, Column1, Phi, Length1, Length2, DecodedDataStrings)
37     
38     * ['Code 128','Code 39']这么写表示既可以解128码,也可以解39码
39     find_bar_code (Image, SymbolRegions1, BarCodeHandle, ['Code 128','Code 39'], BarCodeStrings)
40     
41     get_bar_code_param (BarCodeHandle, 'element_size_min', GenParamValues)
42     get_bar_code_object (BarCodeObjects, BarCodeHandle, 'all', 'symbol_regions')
43     get_bar_code_result (BarCodeHandle, 'all', 'decoded_types', BarCodeResults)
44     get_bar_code_result (BarCodeHandle, 0, 'quality_isoiec15416', Quality)
45     disp_message (3600, BarCodeResults + '码:' + BarCodeStrings, 'image', 20, 20, 'black', 'true')
46 
47     *清除条码模型
48     clear_bar_code_model (BarCodeHandle)
49     stop ()   
50 endfor
51     

 

 

 

参考资料:

1、Halcon解决方案指南(16)一维码识别

2、基于Halcon的一维条码识别技巧

posted on 2019-03-06 18:08  xh6300  阅读(12541)  评论(0编辑  收藏  举报