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

 

1、圖像預處理和條碼加強html

 

對比度過低:scale_image(或使用外部程序scale_image_range),加強圖像的對比度。dom

 

圖像模糊:emphasize銳化圖像,使條碼看起來更清晰。優化

 

深色背景上讀取淺色條碼:invert_image反轉圖像。spa

 

 

2、解碼涉及的主要算子.net

 

read_image :讀圖code

create_bar_code_model :建立條碼模型regexp

find_bar_code :查找條碼htm

clear_bar_code_model :清除條碼模型對象

 

 

若是條碼很是簡單,那麼順次執行上面4個算子就能夠完成解碼了。另外還有幾個算子也很重要:blog

 

set_bar_code_param :設置解碼時的參數

decode_bar_code_rectangle2 :在指定的矩形區域內解碼

get_bar_code_param :獲取解碼時的參數(若是沒有設置過,則得到的是默認值)

get_bar_code_result :得到解碼後的結果,例如能夠得到條碼的類型(Code 12八、Code 39等等)

get_bar_code_object :得到解碼時的一些對象,例如能夠得到解碼後的條碼區域

 

 

3、提升解碼能力的其餘措施

 

若是條碼圖像預處理之後,仍舊解碼困難或者解碼率不高,那麼能夠經過如下措施進一步提升解碼能力:

 

一、若是整張圖信息太多,則能夠先把條碼區域挖出來,使用reduce_domain和crop_domain算子,這樣不只能夠下降解碼難度,還能夠減小解碼時間。也可以使用decode_bar_code_rectangle2在指定的矩形區域內解碼。

 

二、能夠嘗試把條碼圖像轉正再解碼。(這種操做未經嚴格驗證,不知道是否能夠有效提升解碼率)

 

三、當條碼很密或者很小的時候,能夠嘗試用zoom_image_factor放大了條碼圖像。

 

四、find_bar_code中將「CodeType」設置爲「auto」能夠讀取多種類型的條碼,可是會增長運行時間,且可能會下降解碼的可靠性。最好只掃描預知的條形碼類型。

 

五、若是對於質量不好的條碼,能夠模擬平常手機掃碼時的操做,即屢次改變曝光,屢次解碼的方式,參考文章:

http://www.javashuo.com/article/p-oeiqsevl-gk.html

 

六、經過set_bar_code_param算子設置解碼時的參數,能夠有效提升解碼能力。(見下文)

 

 

4、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     

 

 

 

參考資料:

一、Halcon解決方案指南(16)一維碼識別

二、基於Halcon的一維條碼識別技巧

相關文章
相關標籤/搜索