首先我们要明白码的识别思路
- 把窗口全部关闭
- 读取新的图片
- 图像预处理
- 创建条码模型
- 设置模型参数
- 搜索模型
- 获取条码结果
- 显示条码结果
图像预处理和条码增强
- 对比度太低: scale_image(或使用外部程序scale_image_range),增强图像的对比度
- 图像模糊:emphasize锐化图像,使条码看起来清晰
- 深背景上读取浅色条码:invert_image 反转图像
基本算子:
create_bar_code_model 创建条码模型
create_bar_code_model(GenParamNames,GenParamValues,BarCodeHandle)
- GenParamNames:参数名
- GenParamValues:参数值
- BarCodeHandle:条码句柄
set_bar_code_param 设置条码参数
set_bar_code_param(BarCodeHandle,GenParamName,GenParamValue)
- BarCodeHandle:条码句柄
- GenParamNames:参数名
- GenParamValue:条码参数
'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' | 分别指条码的方向和方向容差,设置准确可大大提高解码效率 |
'elemnet_height_min' | 条码的最小高度,默认设置-1白哦是子哦对那个推测条码高度,该参数对速度影响大 |
'stop_after_result_num' | 设置要解码的个数,0表示全部找出,设置为2表示找到2个就不找了 |
find_bar_code 查找条码
find_bar_code(Image,SymbolRegions,BarCodeHandle,CodeType,DecodedDataStrings)
- Image:输入图像
- SymbolRegions:检测到的条形码区域
- BarCodeHandle:条形码区域
- CodeType:条形码类型
- DecodedDataStrings:识别结果
get_bar_code_result 显示条码结果
get_bar_code_result(BarCodeHandle,CandidateHandle,ResultName,BarCodeResults)
- BarCodeHandle:条码模型处理(条码句柄)
- CandidateHandle:候选句柄
- ResultName:结果名
- BarCodeResults:条形码的结果
一维码示例
1.检测单个条形码
dev_close_window ()
dev_update_off ()
dev_set_draw ('margin')
dev_set_line_width (3)
read_image (Image, 'F:/Halcon/Image/1一维码barcor/barcode_1.bmp')
* 创建条码模型
create_bar_code_model ([], [], BarCodeHandle)
* 设置条码参数
set_bar_code_param (BarCodeHandle, 'barcode_width_min', 280)
set_bar_code_param (BarCodeHandle, 'barcode_height_min', 80)
* 检测读取一维码
find_bar_code (Image, SymbolRegions, BarCodeHandle, 'auto', DecodedDataStrings)
* 获取条码结果
get_bar_code_result (BarCodeHandle, 'all', 'decoded_types', BarCodeResults)
*显示
dev_get_window (WindowHandle)
msg:=BarCodeResults+'条码编号'+DecodedDataStrings
area_center (SymbolRegions, Area, Row, Column)
dev_display (Image)
dev_display (SymbolRegions)
disp_message (WindowHandle, msg, 'image', Row, Column, 'black', 'true')
2.检测多个条形码
dev_update_off ()
dev_close_window ()
dev_get_window (WindowHandle)
query_font (WindowHandle, Font)
tuple_find (Font, '楷体', Indices)
if(Indices!=-1)
set_display_font (WindowHandle, 16, '楷体', 'true', 'false')
else
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
endif
dev_set_draw ('margin')
dev_set_line_width (3)
dev_set_color ('green')
create_bar_code_model ([], [], BarCodeHandle)
minWidth:=280
minHeight:=60
set_bar_code_param (BarCodeHandle, 'barcode_height_min', minHeight)
set_bar_code_param (BarCodeHandle, 'barcode_width_min', minWidth)
list_files ('F:/Halcon/Image/1一维码barcor', 'files', Files)
tuple_regexp_select (Files, '.bmp', Selection)
num:=|Selection|-1
for i:=0 to num by 1
read_image (Image,Selection[i])
find_bar_code (Image, SymbolRegions, BarCodeHandle, 'auto', DecodedDataStrings)
get_bar_code_result (BarCodeHandle, 'all', 'decoded_types', BarCodeResults)
dev_display (Image)
dev_display (SymbolRegions)
msg:=BarCodeHandle+'\\='+DecodedDataStrings
disp_message (WindowHandle, msg, BarCodeResults, 12, 12, 'black', 'true')
stop ()
endfor
3.检测多个,和检测不到的
dev_update_off ()
dev_close_window ()
dev_get_window (WindowHandle)
query_font (WindowHandle, Font)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_set_draw ('margin')
dev_set_color ('green')
dev_set_line_width (3)
create_bar_code_model ([], [], BarCodeHandle)
gen_empty_obj (EmptyObject)
list_files ('F:/Halcon/Image/2一维码', 'files', Files)
tuple_regexp_select (Files, '.bmp', Selection)
num:=|Selection|-1
for i:=0 to num by 1
read_image (Image, Selection[i])
find_bar_code (Image, SymbolRegions, BarCodeHandle, 'auto', DecodedDataStrings)
test_equal_obj (EmptyObject, SymbolRegions, IsEqual)
if(IsEqual!=0)
disp_message (WindowHandle, '没有识别到一维码', 'image', 12, 12, 'black', 'true')
endif
get_bar_code_result (BarCodeHandle, 'all', 'decoded_types', BarCodeResults)
dev_display (Image)
dev_display (EmptyObject)
mesg:=BarCodeResults+'\\='+DecodedDataStrings
disp_message (WindowHandle, mesg, 'image', 12, 12, 'black', 'true')
stop ()
endfor
4. 查询多个不同种类
create_bar_code_model (['stop_after_result_num'], [1], BarCodeHandle)
dev_clear_window ()
dev_update_off ()
dev_get_window (WindowHandle)
query_font (WindowHandle, Font)
set_display_font (WindowHandle, 16, Font[0], 'true', 'false')
dev_set_draw ('margin')
dev_set_color ('green')
dev_set_line_width (3)
list_files ('F:/Halcon/Image/N维码/Ean13 一维码读取/ean13', 'files', Files)
tuple_regexp_select (Files, '(ean13)[0-9]{1,7}\\.(png)', Selection)
count:=|Selection|-1
for i:=0 to count by 1
read_image (Image, Selection[i])
find_bar_code (Image, SymbolRegions, BarCodeHandle, 'auto', DecodedDataStrings)
get_bar_code_result (BarCodeHandle, 'all', 'decoded_types', BarCodeResults)
dev_display (Image)
dev_display (SymbolRegions)
msg:=BarCodeResults+'\\'+DecodedDataStrings
area_center (SymbolRegions, Area, Row, Column)
disp_message (WindowHandle, msg, 'image', Row, Column, 'black', 'true')
if(i<count)
disp_continue_message (WindowHandle, 'black', 'true')
endif
stop ()
endfor
dev_disp_text ('已经是最后一张了', 'window', 'bottom', 'right', 'red', [], [])
clear_bar_code_model (BarCodeHandle)
5.旋转查询
方法1:
read_image (Image, 'barcode/ean13/ean1305')
get_image_size (Image, Width, Height)
dev_close_window ()
dev_open_window (0, 0, Width, Height, 'black', WindowHandle)
dev_display (Image)
dev_set_color ('green')
dev_set_draw ('margin')
dev_set_line_width (3)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
create_bar_code_model ('element_size_min', 1.5, BarCodeHandle)
for i:=0 to 360 by 30
* 旋转图像
rotate_image (Image, ImageRotate, i, 'constant')
dev_display (ImageRotate)
get_image_size (ImageRotate, Width, Height)
*改变当前激活窗口的大小和位置
dev_set_window_extents (0, 0, Width, Height)
find_bar_code (ImageRotate, SymbolRegions, BarCodeHandle, 'EAN-13', DecodedDataStrings)
get_bar_code_result (BarCodeHandle, 'all', 'orientation', Orientation)
area_center (SymbolRegions, Area, Row, Col)
* 创建一个十字箭头的轮廓
gen_arrow_contour_xld (Arrow, Row + sin(rad(Orientation)) * 70, Col - cos(rad(Orientation)) * 70, Row - sin(rad(Orientation)) * 70, Col + cos(rad(Orientation)) * 70, 25, 25)
dev_display (ImageRotate)
dev_display (SymbolRegions)
dev_set_color ('green')
dev_display (Arrow)
disp_message (WindowHandle, DecodedDataStrings, 'window', 12, 12, 'black', 'true')
stop ()
endfor
clear_bar_code_model (BarCodeHandle)
方法2:
**** 主要内容:如何获取图像与平行线的夹角
**** 基于夹角 生成带有方向的箭头
dev_update_off ()
read_image (Image, 'barcode/ean13/ean1305')
dev_set_draw ('margin')
dev_set_color ('green')
dev_set_line_width (3)
create_bar_code_model (['element_size_min'], [1.5], BarCodeHandle)
for rot :=0 to 360 by 30
find_bar_code (Image, SymbolRegions, BarCodeHandle, 'auto', DecodedDataStrings)
get_bar_code_result (BarCodeHandle, 'all', 'orientation', angle)
area_center (SymbolRegions, Area, cehnterRow, centerCol)
gen_arrow_contour_xld (Arrow1, cehnterRow, centerCol, cehnterRow, centerCol, 10, 1)
* 难题:怎么基于 区域的中线点,与 图像与水平面的弧度,求xld的起点和终点
* 以区域中心坐标为基准点,该点是,箭头上的点
* 以为该点位置,将箭头分为2个部分:假设第一部分,长度为100 第二部长度为80
* 已:匹配模板与水平面的夹角 rad(angle)
* 求:起点 行列坐标 与 终点行列坐标 、 下面就是计算公式。
startRow:= cehnterRow +sin(rad(angle)) * 100
startCol := centerCol - cos(rad(angle)) * 100
endRow := cehnterRow - sin(rad(angle)) * 80
endCol := centerCol + cos(rad(angle)) * 80
gen_arrow_contour_xld (Arrow, startRow, startCol, endRow, endCol, 20, 20)
* 显示结果
dev_display (Image)
dev_display (SymbolRegions)
dev_display (Arrow)
rotate_image (Image, Image, rot, 'constant')
stop()
endfor