首先我們要明白碼的識別思路
- 把窗口全部關閉
- 讀取新的圖片
- 圖像預處理
- 創建條碼模型
- 設置模型參數
- 搜索模型
- 獲取條碼結果
- 顯示條碼結果
圖像預處理和條碼增強
- 對比度太低: 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')
elseset_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 1read_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+'\\='+DecodedDataStringsdisp_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 1read_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')endifget_bar_code_result (BarCodeHandle, 'all', 'decoded_types', BarCodeResults)dev_display (Image)dev_display (EmptyObject)mesg:=BarCodeResults+'\\='+DecodedDataStringsdisp_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 1read_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+'\\'+DecodedDataStringsarea_center (SymbolRegions, Area, Row, Column)disp_message (WindowHandle, msg, 'image', Row, Column, 'black', 'true')if(i<count)disp_continue_message (WindowHandle, 'black', 'true')endifstop ()
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 30find_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)) * 80endCol := centerCol + cos(rad(angle)) * 80gen_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