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.
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'.