Halcon的一維條碼解碼步驟和解碼技巧


 

一、圖像預處理和條碼增強

 

對比度太低: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的一維條碼識別技巧


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM