本文基于halcon实现条形码的识别,并且对识别器的一些参数进行设置
这里上脚本,备注和逻辑在halcon脚本上做了注释
dev_get_window (WindowHandle)
dev_update_off ()
count_seconds (Start)
read_image (Image1, '1.bmp')
dev_set_draw ('margin')
dev_set_color ('red')
create_bar_code_model ([], [], BarCodeHandle)
set_bar_code_param (BarCodeHandle, 'slanted', 'auto')
*识别条码只找一条,我们期望对每一个图像解码单个条形码
set_bar_code_param (BarCodeHandle, 'stop_after_result_num', 2)
*最小间隙
set_bar_code_param (BarCodeHandle, 'element_size_min', 1)
*条形码阅读器将使用不同的方法,在条码的'element_size_min'为1是很有用
set_bar_code_param (BarCodeHandle, 'meas_param_estimation', 'true')
*'present': 检查校验和,验证条码的正确性 'absent':不检查校验
set_bar_code_param (BarCodeHandle, 'check_char', 'present')
*设置条码候选区域的最小对比度
set_bar_code_param (BarCodeHandle, 'contrast_min', 0)
*表面的变形,为true条形码阅读器会尝试补偿这种扭曲,如贴在玻璃瓶
set_bar_code_param (BarCodeHandle, 'element_size_variable', 'true')
*当值为1的时候会保存读码的中间结果
set_bar_code_param (BarCodeHandle, 'persistence', 1)
*读取条码是否开启多数投票
set_bar_code_param (BarCodeHandle, 'majority_voting', 'true')
*使用值为大于0.0的'meas_thresh_abs',强制条形码阅读器仅使用扫描线的部分,具有至少该值的绝对对比度因此减少误报的数量。
set_bar_code_param (BarCodeHandle, 'meas_thresh_abs', 10.0)
*使用默认的扫描线数运行检测
set_bar_code_param (BarCodeHandle, 'num_scanlines', 0)
*用于测量扫描线内边缘位置的相对阈值
set_bar_code_param (BarCodeHandle, 'meas_thresh', 0.1)
* 有效的错误检测,因为错误来自单个假扫描线的检测结果
set_bar_code_param (BarCodeHandle, 'min_identical_scanlines', 0 + 1)
*容许误差值,可设置为'low'或者'high',设置为'high'可能造成误判
set_bar_code_param (BarCodeHandle, 'start_stop_tolerance', 'low')
*设置最小条形码高度或宽度并再次搜索条形码
*set_bar_code_param (BarCodeHandle, 'barcode_height_min', 600)
*set_bar_code_param (BarCodeHandle, 'barcode_width_min', 1600)
*在矩形区域中读取条码
*draw_rectangle2 (3600, Row1, Column1, Phi1, Length1, Length2)
*decode_bar_code_rectangle2 (Image1, BarCodeHandle, 'Code 128', Row1, Column1, Phi1, Length1, Length2, DecodedDataStrings1)
*在图片中读取条码
find_bar_code (Image1, SymbolRegions, BarCodeHandle, 'auto', DecodedDataStrings)
*获得所有提取条码的扫描线
*get_bar_code_object (BarCodeObjects, BarCodeHandle, 'all', 'candidate_regions')
*获得条码region区域,跟SymbolRegions一样
get_bar_code_object (SymbolRegion, BarCodeHandle, 'all', 'symbol_regions')
*获得条码字符串,跟DecodedDataStrings一样
get_bar_code_result (BarCodeHandle, 'all', 'decoded_strings', DecodedData)
*获得可识别条码的类型
get_param_info ('find_bar_code', 'CodeType', 'value_list', AllCodeTypes)
*获得条码XLD的轮廓角度
get_bar_code_result (BarCodeHandle, 'all', 'orientation', BarCodeResults)
*获得条码的类型
get_bar_code_result (BarCodeHandle, 'all', 'decoded_types', DecodedDataTypes)
*获得条码切割线的分割
get_bar_code_object (ValidScanlines, BarCodeHandle, 'all', 'scanlines_valid')
*检查扫描线OK NG -----> ScanlinesAll 代表NG ;ScanlinesValid代表OK
get_bar_code_object (ScanlinesAll, BarCodeHandle, 0, 'scanlines_all')
get_bar_code_object (ScanlinesValid, BarCodeHandle, 0, 'scanlines_valid')
*关于条码的完整参考数据
get_bar_code_result (BarCodeHandle, 0, 'decoded_reference', Reference)
*显示条码的角度方向
area_center (SymbolRegions, Area, Row, Col)
gen_arrow_contour_xld (Arrow, Row + sin(rad(BarCodeResults)) * 70, Col - cos(rad(BarCodeResults)) * 70, Row - sin(rad(BarCodeResults)) * 70, Col + cos(rad(BarCodeResults)) * 70, 25, 25)
dev_display (Arrow)
clear_bar_code_model (BarCodeHandle)
*等待0.5s
wait_seconds(0.5)
disp_obj (Image1, WindowHandle)
if(|Area| > 0)
dilation_circle (SymbolRegions, RegionDilation, 150)
count_obj (RegionDilation, Number)
for Index := 1 to 2 by 1
select_obj (RegionDilation, RegionDilation_each, Index)
reduce_domain (Image1, RegionDilation_each, ImageReduced)
median_image (ImageReduced, ImageMedian1, 'circle', 1, 'mirrored')
threshold (ImageMedian1, Region, 71, 223)
closing_rectangle1 (Region, RegionClosing, 20, 20)
threshold (ImageMedian1, Regions, 137, 147)
opening_rectangle1 (RegionClosing, RegionOpening1, 20, 20)
connection (RegionOpening1, ConnectedRegions1)
select_shape (ConnectedRegions1, SelectedRegions, 'area', 'and', 20000, 100000)
shape_trans (SelectedRegions, RegionTrans, 'rectangle2')
set_color (WindowHandle, 'green')
disp_region (RegionTrans, WindowHandle)
area_center (RegionTrans, Area, Row, Col)
orientation_region (RegionTrans, Phi)
gen_arrow_contour_xld (Arrow, Row + sin(Phi) * 70, Col - cos(Phi) * 70, Row - sin(Phi) * 70, Col + cos(Phi) * 70, 25, 25)
set_color (WindowHandle, 'red')
dev_display (Arrow)
endfor
endif
dev_update_on ()