Halcon 一維條碼識別

快速上手:就三步

create_bar_code_model ([], [], BarCodeHandle)
read_image (Image,'D:/Today/Data/Data/11.jpg')
find_bar_code (Image, SymbolRegions, BarCodeHandle, 'auto', DecodedDataStrings)

 如上效果圖,進一步深入,打開Halcon示例程序:找到一維碼:barcode_typical_cases.hdev

* This program is intended to help you if the barcode reader
* fails to find and/or decode a barcode. It shows how to debug
* the crucial steps of the decoding.(它展示瞭解碼過程中如何去調試關鍵步驟) First, you have to find
* out at which stage of the bar code reading an error occurs.

(首先:明確在哪一步讀碼發生錯誤,這可以依靠查找中間結果來做到)
* This is done by looking at intermediate results.
* You can obtain intermediate results at two stages:(通過下面2個環節來獲取中間結果)
* - check the candidate regions(1:檢測候選區域)
* - check the scanlines(2:檢查掃描線)

* Based on typical image defects, the program shows how to
* solve the decoding task - even with bad image data.

dev_set_draw ('margin')
dev_update_window ('off')
dev_set_line_width (2)
dev_set_color ('#08f133')

CodeType := 'EAN-13'
read_bar_code_image_and_initialize (Image, 'Case 0: The bar code can be read', 'D:/Today/Data/Data/11.jpg', BarCodeHandle, WindowHandle)
rgb1_to_gray (Image, GrayImage)
try_to_find_bar_code (GrayImage, SymbolRegions, BarCodeHandle, WindowHandle, CodeType, DecodedDataStrings, CodeIsReadable)
if(CodeIsReadable==1)
    set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
    set_tposition (WindowHandle, 200,80)
    write_string (WindowHandle, DecodedDataStrings)
endif
disp_bar_code_candidates (BarCodeHandle, WindowHandle, NumberCandidates)
disp_bar_code_scanlines (BarCodeHandle, WindowHandle)
clear_bar_code_model (BarCodeHandle)

上面disp_bar_code_candidates爲本地函數(顯示條碼候選區域):其關鍵邏輯爲:

get_bar_code_object (Candidates, BarCodeHandle, 'all', 'candidate_regions')
count_obj (Candidates, NumberCandidates)
dev_display (Candidates)

接着:下面顯示掃描線函數邏輯爲:

get_bar_code_result (BarCodeHandle, 'all', 'decoded_types', DecodedTypes)
if (|DecodedTypes| == 0)
    get_bar_code_object (AllScanlines, BarCodeHandle, 'all', 'scanlines_all')
    dev_set_color ('red')
    dev_display (AllScanlines)
    disp_message (WindowHandle, 'No valid scanlines found', 'window', 72, 12, 'red', 'true')
else
    get_bar_code_object (ValidScanlines, BarCodeHandle, 0, 'scanlines_valid')
    dev_set_color ('green')
    dev_display (ValidScanlines)
    disp_message (WindowHandle, 'Valid scanlines detected', 'window', 72, 12, 'forest green', 'true')
endif

 

識別失敗圖中可以看出線條雜亂無章,而識別成功可以看到scanlines非常清晰且暗含一定的規律。 

如果在顯示中間結果時報錯:

處理辦法是: set_bar_code_param (BarCodeHandle, 'persistence', 1)

找出掃描線中有效的部分: 

get_bar_code_object (ValidScanlines, BarCodeHandle, 0, 'scanlines_valid')

ObjectName (input_control)  string → (string)
Name of the iconic object to return.
Default value: 'candidate_regions'
可選擇的參數爲: 'candidate_regions', 'scanlines_all', 'scanlines_all_plain', 'scanlines_merged_edges', 'scanlines_valid', 'scanlines_valid_plain', 'symbol_regions'

案例2:給出了一張對比度低的原圖:處理辦法是提高對比度

scale_image_range (GrayImage, ScaledImage, 0,100)

其他和上例一致。

案例3:光照不連續情形下的識別處理


* Solution: Decrease meas_thresh 降低meas_thresh 

meas_thresh :Relative threshold for measuring the edge position within a scanline.

個人理解:降低掃描線邊緣開始處的灰白閾值,這樣就可以把灰暗處的線條也包含進來。
* Note that in this example decreasing meas_thresh to a very low value can only
* be effective by deactivating measure_thresh_abs (i.e. setting it to 0.0).
* Otherwise, measure_thresh_abs sets a default value of 5.0.

* We expect to decode a single bar code per image
set_bar_code_param (BarCodeHandle, 'stop_after_result_num', 1)

set_bar_code_param (BarCodeHandle, 'meas_thresh_abs', 0.0)
set_bar_code_param (BarCodeHandle, 'meas_thresh', 0.01)
try_to_find_bar_code (Image, SymbolRegions, BarCodeHandle, WindowHandle, CodeType, DecodedDataStrings, CodeIsReadable)
disp_bar_code_candidates (BarCodeHandle, WindowHandle, NumberCandidates11)
disp_bar_code_scanlines (BarCodeHandle, WindowHandle)
clear_bar_code_model (BarCodeHandle)

'meas_thresh':
Relative threshold for measuring the edge position within a scanline. This setting activates the training mode for the parameter 'meas_thresh'.

'meas_thresh_abs':
Absolute threshold for measuring the edge position within a scanline. This setting activates the training mode for the parameter 'meas_thresh_abs'.

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章