Halcon一维码识别

文章目录

  • 参数连接
  • halcon 自带案例1(设置校验位识别条码)
  • Halcon 自带案例2(设置对比度识别条码)
  • Halcon 自带案例3(存在曲面变形)
  • Halcon 自带案例4(设置条码扫描线)
  • Halcon 自带案例5(一维码阈值)
  • Halcon Codabar编码案例
  • Halcon Code128编码案例
  • Halcon 符合编码案例
  • Halcon 当条码被遮挡的情况下
  • Halcon 检测条码绘制条码矩形 decode_bar_code_rectangle2
  • halcon 设置条码的高度和宽度案例

参数连接

一维码识别算子

halcon 自带案例1(设置校验位识别条码)

ctrl +e 选择 一维码的第一个案例, set_bar_code_param (BarCodeHandle, ‘check_char’, ‘present\absent’)设置条码是否检查校验位

在这里插入图片描述

*check_char    是否验证校验位(present是 absent 否)
*composite_code   附加一个二维条码构成"组合码"
*element_height_min 最小条码的高度
*element_size_max   条码最大尺寸(宽度和间距)
*element_size_min   条码最小尺寸(宽度和间距)
*max_diff_orient    相邻边缘方向的最大角度容差
*meas_thresh        用来识别条码边沿,一般取值[0.05,0.2]
*meas_thresh_abs
*min_identical_scanlines   认定成功解码所需的最少扫描线数,默认为1
*num_scanlines        条码扫描的最大数目
*orientation          条码方向()
*orientation_tol      条码方向容差()
*persistence          保存解码的中间结果
*start_stop_tolerance 当检测扫描线的起点和终点图案时,用该语句设置"容许误差"
*stop_after_result_num 设置要解码的条码条数
*upce_encodation       用以不同的输出格式(UPC-E码)
*timeout               检测超时
*train                 训练
*quiet_zone    
*slanted               对倾斜条码额外处理* Read bar codes of type 2/5 Industrial
*这是创建一个空的条码模型,并将其存储在变量BarCodeHandle中。该模型可以用于设置和调整条码参数。
create_bar_code_model ([], [], BarCodeHandle)
* We expect to decode a single bar code per image
*此代码行设置读取图像时停止的结果数量。在本例中,我们只需要读取一个条码,因此此参数设置为1set_bar_code_param (BarCodeHandle, 'stop_after_result_num', 1)* Some codes show a minimal code length of 1 digit. Hence, we need to decrease the
* default setting for this application.
*此代码行设置具体条码类型的参数。在本例中,‘2/5 Industrial’条码类型的最小长度被设置为1set_bar_code_param_specific (BarCodeHandle, '2/5 Industrial', 'min_code_length', 1)
* Note, that this is not recommended in real world applications due to the possibility
* of more false reads.dev_close_window ()
dev_open_window (0, 0, 120, 300, 'black', WindowHandle)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
dev_set_draw ('margin')
dev_set_line_width (3)
for I := 1 to 4 by 1read_image (Image, 'barcode/25industrial/25industrial0' + I)get_image_size (Image, Width, Height)dev_set_window_extents (0, 0, Width - 1, Height - 1)dev_display (Image)dev_set_color ('green')* Read bar code, the resulting string includes the check character*不检查校验位的情况*设置条码参数,不检查校验位。set_bar_code_param (BarCodeHandle, 'check_char', 'absent')*寻找图像中的条码,并将解码结果存储在变量DecodedDataStrings中。此处使用了'2/5 Industrial'条码类型。find_bar_code (Image, SymbolRegions, BarCodeHandle, '2/5 Industrial', DecodedDataStrings)*在窗口中显示解码结果,颜色为黑色。disp_message (WindowHandle, DecodedDataStrings, 'window', 12, 12, 'black', 'false')*获取解码结果的长度,并将其存储在变量LastChar中。LastChar := strlen(DecodedDataStrings) - 1*在窗口中显示解码结果的最后一个字符,并将其颜色设置为森林绿。disp_message (WindowHandle, sum(gen_tuple_const(LastChar,' ')) + DecodedDataStrings{LastChar}, 'window', 12, 12, 'forest green', 'false')stop ()* Read bar code using the check character to check the result, i.e.,* the check character does not belong to the returned string anymore.* If the check character is not correct, the bar code reading failsdev_set_color ('green')
*检查校验位的情况*设置条码参数,检查校验位set_bar_code_param (BarCodeHandle, 'check_char', 'present')*再次寻找图像中的条码,并将解码结果存储在变量DecodedDataStrings中。此处同样使用了'2/5 Industrial'条码类型。find_bar_code (Image, SymbolRegions, BarCodeHandle, '2/5 Industrial', DecodedDataStrings)*在窗口中显示重新解码得到的结果,颜色为黑色。disp_message (WindowHandle, DecodedDataStrings, 'window', 36, 12, 'black', 'false')dev_set_color ('magenta')if (I < 4)stop ()endif
endfor

Halcon 自带案例2(设置对比度识别条码)

在这里插入图片描述

设置条码的对比度来减少运行的时间增加正确性,通过改变set_bar_code_param (BarCodeHandle, ‘contrast_min’, ContrastMinValue) 的值设置对比度计算时间,并且增加识别的正确率
在这里插入图片描述
ContrastMinValue :=0的的情况下识别到一维码的个数在这里插入图片描述
在这里插入图片描述

* Example program for the usage of the bar code parameter
* 'contrast_min'.
* 
* This parameter can be used to reduce the runtime of find_bar_code
* in the presence of a low contrast bar-like structures in an image.
* Moreover 'contrast_min' can also be used to reduce the number of
* false positives if the expected barcodes have high contrast.
* 
* Create bar code reader model
create_bar_code_model ([], [], BarCodeHandle)
* 
* Initialization
dev_update_off ()
dev_close_window ()
* 
* Read and display example images without any visible bar codes
read_image (Image, 'barcode/25interleaved/25interleaved_zeiss1')
* 
* Set display defaults
dev_open_window_fit_image (Image, 0, 0, 600, 500, WindowHandle)
dev_display (Image)
dev_set_draw ('margin')
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
get_window_extents (WindowHandle, Row, Column, Width, Height)
dev_open_window (0, Width + 5, 400, 300, 'white', WindowHandleText)
set_display_font (WindowHandleText, 14, 'mono', 'true', 'false')
* 
* Display information about the example
Message[0] := 'This example demonstrates the use of the bar code parameter \'contrast_min\'.'
Message[1] := ' '
Message[2] := 'The parameter \'contrast_min\' can be used to reduce the runtime of find_bar_code in the presence of low contrast bar-like structures in an image. Moreover \'contrast_min\' can also be used to reduce the number of false positives in applications where the expected barcodes have a high contrast.'
MessageWrapped := regexp_replace(Message + ' ',['(.{0,35})\\s','replace_all'],'$1\n')
disp_message (WindowHandleText, MessageWrapped, 'window', 12, 12, 'black', 'false')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
dev_set_window (WindowHandle)
dev_clear_window ()
dev_display (Image)* 首先将'contrast_min'参数设置为默认值0,并执行find_bar_code函数,记录运行时间
* Number of repetitions for runtime measurements(运行时测量的次数)
NumRepeat := 100
* 
* First, set the minimum contrast of the bar code candidate regions
* to 0 (default)
ContrastMinValue := 0
*set_bar_code_param (条形码读取器模型的句柄, 要设置的参数名称, 参数的值)
set_bar_code_param (BarCodeHandle, 'contrast_min', ContrastMinValue)
* 
* The bar code reader finds many bar code candidate regions that have
* a low absolute contrast
Times := []
for I := 0 to NumRepeat by 1count_seconds (Start)*查找条形码find_bar_code (Image, SymbolRegions, BarCodeHandle, '2/5 Interleaved', DecodedDataStrings)count_seconds (End)Time := End - StartTimes := [Times,Time]
endfor
RunTimeContrastMinLow := 1000 * median(Times)
* Get candidate regions and display results(选取区域并显示结果)
*访问在条码符号搜索或解码过程中创建的标志性对象。
get_bar_code_object (BarCodeObjects, BarCodeHandle, 'all', 'candidate_regions')
count_obj (BarCodeObjects, Number)
dev_set_color ('red')
dev_set_line_width (5)
dev_display (BarCodeObjects)
dev_set_window (WindowHandleText)
dev_clear_window ()
Message := ['\'contrast_min\' = ' + ContrastMinValue + ':','   Found ' + Number + ' candidate(s) in ' + (RunTimeContrastMinLow$'.4') + ' ms']
disp_message (WindowHandleText, Message, 'window', 12, 12, '', 'false')
* 
* Now, set the bar code reader parameter 'contrast_min' to a value
* greater than 0.0 to consider only candidates having an absolute
* contrast of at least that value.
*'contrast_min'参数设置为大于0的值,并再次执行find_bar_code函数,记录运行时间:
ContrastMinValue := 120
set_bar_code_param (BarCodeHandle, 'contrast_min', ContrastMinValue)
* 
* Search again for bar codes. Now, a significantly smaller number of
* candidates should be found, and the overall runtime should be reduced.
Times := []
for I := 0 to NumRepeat by 1count_seconds (Start)find_bar_code (Image, SymbolRegions, BarCodeHandle, '2/5 Interleaved', DecodedDataStrings)count_seconds (End)Time := End - StartTimes := [Times,Time]
endfor
RunTimeContrastMinHigh := 1000 * median(Times)
* Get candidate regions and display results
dev_set_window (WindowHandle)
get_bar_code_object (BarCodeObjects, BarCodeHandle, 'all', 'candidate_regions')
count_obj (BarCodeObjects, Number)
dev_set_color ('forest green')
dev_set_line_width (3)
dev_display (BarCodeObjects)
smallest_rectangle1 (SymbolRegions, Row1, Column1, Row2, Column2)
get_bar_code_result (BarCodeHandle, 'all', 'decoded_types', DecodedTypes)
disp_message (WindowHandle, DecodedTypes + '\n' + DecodedDataStrings, 'image', Row1, Column2 + 20, 'black', 'true')
dev_set_window (WindowHandleText)
Message := ['\'contrast_min\' = ' + ContrastMinValue + ':','   Found ' + Number + ' candidate(s) in ' + (RunTimeContrastMinHigh$'.4') + ' ms']
disp_message (WindowHandleText, Message, 'window', 62, 12, 'forest green', 'false')
Message := 'Setting \'contrast_min\' to a higher value typically results in a faster execution and in fewer false positives.'
MessageWrapped := regexp_replace(Message + ' ',['(.{0,35})\\s','replace_all'],'$1\n')
disp_message (WindowHandleText, MessageWrapped, 'window', 122, 12, 'black', 'false')

Halcon 自带案例3(存在曲面变形)

在这里插入图片描述

分别设置set_bar_code_param (BarCodeHandle, ‘element_size_variable’, ‘false\true’)
对于条形码不变形的情况下element_size_variable的情况在这里插入图片描述
条形码扭曲变形的情况下在这里插入图片描述

* This example demonstrates how to use the bar code parameter
* 'element_size_variable' if surface deformations are present.
* 
* To illustrate the effect of the parameter, both decoding results
* are shown in contrast. It is shown that, under cylindrical
* surface deformation, setting the bar code parameter to 'true'
* will lead to successful decoding.
* ****************************************************************
* 
* Initialization
dev_update_off ()
dev_close_window ()
* 
* Create two bar code model with different element_size_variable values
create_bar_code_model ([], [], BarCodeHandle)
set_bar_code_param (BarCodeHandle, 'element_size_variable', 'false')
create_bar_code_model ([], [], BarCodeHandleVarSize)
set_bar_code_param (BarCodeHandleVarSize, 'element_size_variable', 'true')
CodeTypes := ['GS1 DataBar Limited','GS1 DataBar Expanded','GS1 DataBar Expanded Stacked']
* 
* Prepare graphics window
read_image (Image, 'barcode/gs1databar_limited/gs1databar_limited_no_deform')
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
* 
* Define bar code region
DecodeRectRow := 315
DecodeRectColumn := 490
DecodeRectPhi := 0
DecodeRectLength1 := 410
DecodeRectLength2 := 200
* 
* Part 1
* Read bar code without distortions
* 
* Decode the bar code with default setting
decode_bar_code_rectangle2 (Image, BarCodeHandle, CodeTypes, DecodeRectRow, DecodeRectColumn, DecodeRectPhi, DecodeRectLength1, DecodeRectLength2, DecodedDataStrings)
get_bar_code_object (SymbolRegions, BarCodeHandle, 'all', 'symbol_regions')
* 
* Allow variable element sizes ('element_size_variable' = 'true')
decode_bar_code_rectangle2 (Image, BarCodeHandleVarSize, CodeTypes, DecodeRectRow, DecodeRectColumn, DecodeRectPhi, DecodeRectLength1, DecodeRectLength2, DecodedDataStringsVarSize)
get_bar_code_object (SymbolRegionsVarSize, BarCodeHandleVarSize, 'all', 'symbol_regions')
* 
* Display results
Message := 'If the bar code is not deformed, it is found with default settings.'
Message[1] := 'Setting \'element_size_variable\' to \'true\' is not necessary.'
display_results (Image, SymbolRegions, SymbolRegionsVarSize, WindowHandle, DecodedDataStrings, DecodedDataStringsVarSize, Message)
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* 
* Part 2
* Read distorted bar code
* 
read_image (Image, 'barcode/gs1databar_limited/gs1databar_limited_cylinder')
* 
* Use default setting
decode_bar_code_rectangle2 (Image, BarCodeHandle, CodeTypes, DecodeRectRow, DecodeRectColumn, DecodeRectPhi, DecodeRectLength1, DecodeRectLength2, DecodedDataStrings)
get_bar_code_object (SymbolRegions, BarCodeHandle, 'all', 'symbol_regions')
* Use 'element_size_variable' = 'true'
decode_bar_code_rectangle2 (Image, BarCodeHandleVarSize, CodeTypes, DecodeRectRow, DecodeRectColumn, DecodeRectPhi, DecodeRectLength1, DecodeRectLength2, DecodedDataStringsVarSize)
get_bar_code_object (SymbolRegionsVarSize, BarCodeHandleVarSize, 'all', 'symbol_regions')
* 
* Display results
Message := 'If the bar code is bended, the code will only be read with'
Message[1] := '\'element_size_variable\' set to \'true\'.'
display_results (Image, SymbolRegions, SymbolRegionsVarSize, WindowHandle, DecodedDataStrings, DecodedDataStringsVarSize, Message)

Halcon 自带案例4(设置条码扫描线)

在这里插入图片描述

设置扫描线set_bar_code_param (BarCodeHandle, ‘majority_voting’, MajorityVotingSetting)
当MajorityVotingSetting为false的情况在这里插入图片描述
当MajorityVotingSetting为true的情况在这里插入图片描述

* This example program shows the effect of the bar code
* parameter 'majority_voting'.
* 
* The bar code reader analyses multiple scanlines and tries
* to decode them. By default, this process is terminated as soon
* as one scanline can be decoded successfully. In some cases,
* the result derived from this scanline may be wrong due to
* local printing errors.
* 
* If the parameter 'majority_voting' is set to 'true', all
* scanlines are analysed. The final reading result is then
* determined by a majority voting of all successful decoding
* results.
* 
* Prepare visualization settings and objects
dev_update_off ()
dev_close_window ()
read_image (Image, 'barcode/ean13/ean13_label')
get_image_size (Image, WidthI, HeightI)
dev_open_window_fit_image (Image, 0, 0, 600, 500, WindowHandle)
dev_display (Image)
dev_set_draw ('margin')
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
get_window_extents (WindowHandle, Row, Column, Width, Height)
WindowZoomFactor := real(WidthI) / Width
dev_open_window (0, Width + 5, 400, 300, 'white', WindowHandleText)
set_display_font (WindowHandleText, 14, 'mono', 'true', 'false')
* 
* Create bar code reader model
create_bar_code_model ([], [], BarCodeHandle)
* 
* PART I, read bar code without majority voting
MajorityVotingSetting := 'false'
set_bar_code_param (BarCodeHandle, 'majority_voting', MajorityVotingSetting)
* 持久性模型设置
set_bar_code_param (BarCodeHandle, 'persistence', 1)
* 定义条码阈值
set_bar_code_param (BarCodeHandle, 'meas_thresh', 0.1)
* 防止'meas_thresh'参数出现错误检测
set_bar_code_param (BarCodeHandle, 'meas_thresh_abs', 30)
* 
* Perform decoding and prepare results
dev_set_window (WindowHandle)
* 直接扫描所提供的区域以寻找条形码
decode_bar_code_rectangle2 (Image, BarCodeHandle, 'EAN-13', 280, 223, 3.14125 / 2.0, 200, 50, DecodedDataStrings)
*得到解码区域
get_bar_code_object (SymbolRegion, BarCodeHandle, 'all', 'symbol_regions')
*得到解码类型
get_bar_code_result (BarCodeHandle, 'all', 'decoded_types', DecodedTypes)
*得到解析条码的字符串结果
get_bar_code_result (BarCodeHandle, 'all', 'decoded_strings', DecodedData)
*访问在条码符号搜索或解码过程中创建的标志性对象
get_bar_code_object (ValidScanlines, BarCodeHandle, 'all', 'scanlines_valid')
get_majority_voting_example_symbols (SymbolRegion, ValidScanlines, DecodedData, BarCodeHandle, DecodedTypes, DecodedData)
* 选中某一个连通域
select_obj (ValidScanlines, FirstScanline, 5)
* 
* Display results and labels
disp_message (WindowHandle, ['Looking for bar codes','\'majority_voting\' = \'' + MajorityVotingSetting + '\''], 'window', 12, 12, 'black', 'true')
ColorsText := ['forest green','red']
dev_set_line_width (2)
smallest_rectangle1 (SymbolRegion, Row1, Column1, Row2, Column2)
dev_set_color ('red')
dev_display (SymbolRegion)
dev_display (FirstScanline)
* 
Message := ['Codetype: ','Data:     '] + [DecodedTypes,DecodedData]
disp_message (WindowHandle, Message, 'image', Row1 - 40 * WindowZoomFactor, Column2 - 100, ColorsText[1], 'true')
* 
* Display description
Message := 'By default \'majority_voting\' is set to \'false\'.'
Message[1] := ' '
Message[2] := 'If the parameter \'majority_voting\' is set to \'false\', the reading result is set to the decoding result of the first scanline that could be decoded.'
Message[3] := ' '
Message[4] := 'Here, an \'EAN-13\' code is detected wrongly inside of another \'EAN-13\' because of a printing error.'
MessageWrapped := regexp_replace(Message + ' ',['(.{0,35})\\s','replace_all'],'$1\n')
disp_message (WindowHandleText, MessageWrapped, 'window', 12, 12, 'black', 'false')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* 
* PART II, enable majority voting
* 
MajorityVotingSetting := 'true'
set_bar_code_param (BarCodeHandle, 'majority_voting', MajorityVotingSetting)
* 
* Perform decoding and prepare results
decode_bar_code_rectangle2 (Image, BarCodeHandle, 'EAN-13', 280, 223, 3.14125 / 2.0, 200, 50, DecodedDataStrings)
get_bar_code_object (SymbolRegion, BarCodeHandle, 'all', 'symbol_regions')
get_bar_code_result (BarCodeHandle, 'all', 'decoded_types', DecodedTypes)
get_bar_code_result (BarCodeHandle, 'all', 'decoded_strings', DecodedData)
get_bar_code_object (ValidScanlines, BarCodeHandle, 'all', 'scanlines_valid')
* 选取 1-4,6,7根线
select_obj (ValidScanlines, MajorityScanlines, [[1:4],6,7])
* 
* Display results and labels
dev_display (Image)
disp_message (WindowHandle, ['Looking for bar codes','\'majority_voting\' = \'' + MajorityVotingSetting + '\''], 'window', 12, 12, 'black', 'true')
dev_set_draw ('margin')
dev_set_color ('green')
dev_set_line_width (2)
smallest_rectangle1 (SymbolRegion, Row1, Column1, Row2, Column2)
dev_display (SymbolRegion)
dev_display (MajorityScanlines)
Message := ['Codetype: ','Data:     '] + [DecodedTypes,DecodedData]
disp_message (WindowHandle, Message, 'image', Row1 - 40 * WindowZoomFactor, Column2 - 100, 'forest green', 'true')
* 
* Display description
dev_set_window (WindowHandleText)
Message := 'If majority voting is enabled, the bar code reader uses a majority voting scheme to determine the reading results. It returns the result that has been decoded from the majority of all scanlines.'
Message[1] := ' '
Message[2] := 'Here, the correct \'EAN-13\' code is found.'
MessageWrapped := regexp_replace(Message + ' ',['(.{0,35})\\s','replace_all'],'$1\n')
dev_clear_window ()
disp_message (WindowHandleText, MessageWrapped, 'window', 12, 12, 'black', 'false')

Halcon 自带案例5(一维码阈值)

在这里插入图片描述

通过改变set_bar_code_param (BarCodeHandle, ‘meas_thresh_abs’, MeasThreshAbsValue) 的阈值来提高识别的准确度

* Example program for the usage of the bar code parameter
* 'meas_thresh_abs'.
* This parameter can be used to reduce the number of false
* positives when finding bar code symbol regions which we expect
* to have a high contrast. To artificially demonstrate this we
* try to find the bar code type PharmaCode that has no check
* character and therefore is recognized very easily within
* noise. As 'noise' we use images that do not contain any bar
* codes at all.
* By using 'meas_thresh_abs' with a value greater than 0.0 we
* force the bar code reader to use only the parts of a scanline
* with an absolute contrast of at least that value and thus
* reduce the number of false positives.
* 
* Create bar code reader model
create_bar_code_model ([], [], BarCodeHandle)
* 
* Initialization
dev_update_var ('off')
dev_update_pc ('off')
dev_update_window ('off')
dev_close_window ()
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
dev_set_draw ('margin')
dev_set_line_width (3)
* 
* Read and display example images without any visible bar codes
ExampleImages := ['zeiss1','patras','fabrik']
for Index := 0 to |ExampleImages| - 1 by 1FileName := ExampleImages[Index]read_image (Image, FileName)rgb1_to_gray (Image, Image)* * Set display defaultsget_image_size (Image, Width, Height)dev_set_window_extents (-1, -1, Width, Height)dev_display (Image)* * First, disable absolute thresholds by setting the value 0.0* for the parameter 'meas_thresh_abs'MeasThreshAbsValue := 0.0set_bar_code_param (BarCodeHandle, 'meas_thresh_abs', MeasThreshAbsValue)* * The bar code reader finds many (wrong) potential bar codes using* scanlines that have a low absolute contrastfind_bar_code (Image, SymbolRegions, BarCodeHandle, 'PharmaCode', DecodedDataStrings)dev_set_color ('red')dev_display (SymbolRegions)disp_message (WindowHandle, ['Found ' + |DecodedDataStrings| + ' potential bar codes','  with parameter meas_thresh_abs = ' + MeasThreshAbsValue], 'window', 12, 12, '', 'true')disp_continue_message (WindowHandle, 'black', 'true')stop ()* * Now, set the bar code reader parameter 'meas_thresh_abs' to a value* greater than 0.0 to use only parts of a scanline having an absolute* contrast of at least that value.MeasThreshAbsValue := 10.0set_bar_code_param (BarCodeHandle, 'meas_thresh_abs', MeasThreshAbsValue)* * Look again for a bar code. Now, significant less scanlines should be found.find_bar_code (Image, SymbolRegions, BarCodeHandle, 'PharmaCode', DecodedDataStrings)dev_set_color ('lime green')dev_display (SymbolRegions)disp_message (WindowHandle, ['Found ' + |DecodedDataStrings| + ' potential bar codes','  with parameter meas_thresh_abs = ' + MeasThreshAbsValue], 'window', 5 * 12, 12, '', 'true')disp_continue_message (WindowHandle, 'black', 'true')stop ()
endfor

阈值为0
在这里插入图片描述阈值为10
在这里插入图片描述

Halcon Codabar编码案例

在这里插入图片描述

find_bar_code (Image, SymbolRegions, BarCodeHandle, ‘Codabar’, DecodedDataStrings)

* Read bar codes of type Codabar
* 
create_bar_code_model ([], [], BarCodeHandle)
* We expect to decode a single bar code per image
set_bar_code_param (BarCodeHandle, 'stop_after_result_num', 1)
dev_close_window ()
dev_open_window (0, 0, 120, 300, 'black', WindowHandle)
dev_set_color ('green')
dev_set_draw ('margin')
dev_set_line_width (3)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
for I := 1 to 6 by 1read_image (Image, 'barcode/codabar/codabar' + (I$'.2'))get_image_size (Image, Width, Height)dev_set_window_extents (0, 0, Width - 1, Height - 1)dev_display (Image)set_bar_code_param (BarCodeHandle, 'check_char', 'present')find_bar_code (Image, SymbolRegions, BarCodeHandle, 'Codabar', DecodedDataStrings)disp_message (WindowHandle, DecodedDataStrings, 'window', 12, 12, 'black', 'false')LastChar := strlen(DecodedDataStrings) - 1disp_message (WindowHandle, DecodedDataStrings{0} + sum(gen_tuple_const(LastChar - 1,' ')) + DecodedDataStrings{LastChar}, 'window', 12, 12, 'forest green', 'false')if (I < 6)stop ()endif
endfor

Halcon Code128编码案例

在这里插入图片描述

find_bar_code (Image, SymbolRegions, BarCodeHandle, ‘Code 128’, DecodedDataStrings) 改变的参数

* Read bar codes of type Code 128
* 
create_bar_code_model ([], [], BarCodeHandle)
dev_close_window ()
dev_open_window (0, 0, 600, 600, 'black', WindowHandle)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_set_draw ('margin')
dev_set_color ('green')
dev_set_line_width (3)
for I := 1 to 3 by 1read_image (Image, 'barcode/code128/code128' + (I$'.2'))dev_resize_window_fit_image (Image, 0, 0, -1, -1)find_bar_code (Image, SymbolRegions, BarCodeHandle, 'Code 128', DecodedDataStrings)get_bar_code_result (BarCodeHandle, 0, 'decoded_reference', Reference)String := ''for J := 0 to strlen(DecodedDataStrings) - 1 by 1if (ord(DecodedDataStrings{J}) < 32)Char := '\\x' + ord(DecodedDataStrings{J})$'02x'elseChar := DecodedDataStrings{J}endifString := String + Charendfordisp_message (WindowHandle, String, 'window', 12, 12, 'black', 'true')if (I < 3)disp_continue_message (WindowHandle, 'black', 'true')stop ()endif
endfor

Halcon 符合编码案例

在这里插入图片描述

set_bar_code_param (BarCodeHandle, ‘composite_code’, ‘CC-A/B’) 设置为复合码

* 
* This program demonstrates the decoding of GS1 DataBar bar
* codes with Composite component
* 
dev_close_window ()
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
* 
ScaleWindow := 1.0
* 
dev_set_draw ('margin')
dev_set_color ('green')
dev_set_line_width (3)
* 
create_bar_code_model ([], [], BarCodeHandle)
set_bar_code_param (BarCodeHandle, 'composite_code', 'CC-A/B')
* 
set_bar_code_param (BarCodeHandle, 'element_size_min', 1.5)
* 
TestParams := []
TestParams := [TestParams,'gs1databar_stacked_composite_01','GS1 DataBar Stacked']
TestParams := [TestParams,'gs1databar_limited_composite_01','GS1 DataBar Limited']
TestParams := [TestParams,'gs1databar_limited_composite_02','GS1 DataBar Limited']
TestParams := [TestParams,'gs1databar_expanded_composite_01','GS1 DataBar Expanded']
* 
* 
for I := 0 to |TestParams| - 2 by 2File := 'barcode/gs1databar_composite/' + TestParams[I]CodeType := TestParams[I + 1]* read_image (Image, File)get_image_size (Image, Width, Height)dev_set_window_extents (0, 0, ScaleWindow * Width, ScaleWindow * Height)disp_message (WindowHandle, 'Barcode Type: ' + CodeType, 'window', 12, 12, 'black', 'true')* find_bar_code (Image, SymbolRegions, BarCodeHandle, CodeType, DecodedDataStrings)* get_bar_code_result (BarCodeHandle, 'all', 'decoded_strings', DecodedStrings)get_bar_code_result (BarCodeHandle, 'all', 'composite_strings', CompositeStrings)disp_message (WindowHandle, DecodedStrings + '\nComposite: ' + CompositeStrings, 'image', 36, 12, 'black', 'true')if (I < |TestParams| - 2)disp_continue_message (WindowHandle, 'black', 'true')stop ()endif
endfor

Halcon 当条码被遮挡的情况下

在这里插入图片描述在这里插入图片描述

* 
* This program demonstrates visualization of bar code scanlines.
* This visualization can be used to inspect the quality of images, where
* occlusions or print defects might prevent the bar code reader from
* successfully decoding the underlying bar code.
* 
* With 'scanlines_all' all scanlines that the bar code reader would
* eventually use to decode a  candidate bar code (here, colored in red).
* With 'scanlines_valid' are visualized all scanlines that can be decoded
* as well (here, colored in green)
* 
dev_update_off ()
dev_close_window ()
dev_open_window (0, 0, 512, 512, 'black', WindowHandle)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_set_draw ('margin')
* 
Files := ['ean1314','ean1313','ean13_defect_05']
Color := ['magenta','red','green','yellow']
* 
create_bar_code_model ([], [], BarCodeHandle)
set_bar_code_param (BarCodeHandle, 'persistence', 1)
* 
for I := 0 to |Files| - 1 by 1Filename := 'barcode/ean13/' + Files[I]* read_image (Image, Filename)get_image_size (Image, Width, Height)dev_set_window_extents (0, 0, Width, Height)dev_display (Image)* find_bar_code (Image, SymbolRegions, BarCodeHandle, 'EAN-13', DecodedDataStrings)* Display only scanlines of the decoded candidate region.if (|DecodedDataStrings|)CandidateIds := [0:|DecodedDataStrings| - 1]elseCandidateIds := 'all'endif* get_bar_code_object (Candidates, BarCodeHandle, CandidateIds, 'candidate_regions')dev_set_line_width (4)dev_set_color (Color[0])dev_display (Candidates)* 获取所有扫描线对象(红色)get_bar_code_object (AllScanlines, BarCodeHandle, CandidateIds, 'scanlines_all')dev_set_color (Color[1])dev_display (AllScanlines)* 获取有效的扫描线对象(绿色)get_bar_code_object (ValidScanlines, BarCodeHandle, CandidateIds, 'scanlines_valid')dev_set_line_width (2)dev_set_color (Color[2])dev_display (ValidScanlines)* 获取边缘的扫描线对象(黄色)get_bar_code_object (MergedScanlines, BarCodeHandle, CandidateIds, 'scanlines_merged_edges')dev_set_color (Color[3])dev_display (MergedScanlines)* disp_message (WindowHandle, 'Decoded data string: ' + DecodedDataStrings, 'window', 12, 12, 'black', 'true')disp_message (WindowHandle, ['Candidate region','Extracted scanlines','Valid scanlines','Edges used for merging'], 'window', 42, 12, Color, ['black','false'])if (I < |Files| - 1)disp_continue_message (WindowHandle, 'black', 'true')stop ()endif
endfor

在这里插入图片描述

主要代码

* 获取候选条形码区域的对象get_bar_code_object (Candidates, BarCodeHandle, CandidateIds, 'candidate_regions')dev_set_line_width (4)dev_set_color (Color[0])dev_display (Candidates)* 获取所有扫描线对象(红色)get_bar_code_object (AllScanlines, BarCodeHandle, CandidateIds, 'scanlines_all')dev_set_color (Color[1])dev_display (AllScanlines)* 获取有效的扫描线对象(绿色)get_bar_code_object (ValidScanlines, BarCodeHandle, CandidateIds, 'scanlines_valid')dev_set_line_width (2)dev_set_color (Color[2])dev_display (ValidScanlines)* 获取边缘的扫描线对象(黄色)get_bar_code_object (MergedScanlines, BarCodeHandle, CandidateIds, 'scanlines_merged_edges')dev_set_color (Color[3])dev_display (MergedScanlines) 

Halcon 检测条码绘制条码矩形 decode_bar_code_rectangle2

* This example demonstrates how to use the operator
* decode_bar_code_rectangle2.
* The bar code region is extracted by simple segmentation
* and the returned rectangle2 parameters are passed to
* the operator decode_bar_code_rectangle2.
* 
* Initialize variables
Directory := 'barcode/ean13/'
TeaBoxRegExp := '.*tea_box.*'
* 
* Initialize settings
dev_update_window ('off')
dev_set_draw ('margin')
* 
* Get list of tea_box*-image files
list_image_files (Directory, 'default', [], ImageFiles)
ImageFilesTea := regexp_select(ImageFiles,[TeaBoxRegExp,'ignore_case'])
read_image (Image, ImageFilesTea[0])
* 
* Prepare graphics window and bar code model
dev_close_window ()
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
dev_set_line_width (2)
create_bar_code_model ([], [], BarCodeHandle)
* 
* Decode tea_box*-images
for ImageIndex := 0 to |ImageFilesTea| - 1 by 1read_image (Image, ImageFilesTea[ImageIndex])dev_display (Image)* * segment and determine the bar code regionthreshold (Image, RegionThresh, 56, 255)connection (RegionThresh, RegThreshConn)select_shape_std (RegThreshConn, RegionBC, 'max_area', 100)* * create input parameters for decode_bar_code_rectangle2smallest_rectangle2 (RegionBC, Row, Column, Phi, Length1, Length2)gen_rectangle2 (Rectangle2BC, Row, Column, Phi, Length1, Length2)dev_set_color ('magenta')dev_display (Rectangle2BC)* * decode_bar_code_rectangle2 directly uses the passed rectangle2 parameters as ROIdecode_bar_code_rectangle2 (Image, BarCodeHandle, 'EAN-13', Row, Column, Phi, Length1, Length2, Decoded)get_bar_code_object (SymbolRegions, BarCodeHandle, 'all', 'symbol_regions')dev_set_color ('lime green')dev_display (SymbolRegions)disp_message (WindowHandle, 'Found bar code: ' + Decoded, 'window', 5, 5, 'black', 'true')if (ImageIndex < |ImageFilesTea| - 1)disp_continue_message (WindowHandle, 'black', 'true')stop ()endif
endfor

在这里插入图片描述

halcon 设置条码的高度和宽度案例

* This example demonstrates how to use the bar code parameters
* 'barcode_height_min' and 'barcode_width_min' to narrow the
* number of detected candidate regions in which possible bar
* codes are searched for.
* 
* This is especially recommended if some factors like the bar code
* type, the number of encoded characters etc. are constant
* throughout the application. Then, the manual adjustment of
* these parameters can lead to an increasing speed and robustness.
* 
* To illustrate the effect of the parameters, the detected
* candidate regions are displayed before and after setting
* each of the parameters. It is shown that the number of
* potential candidate region decreases when the parameters
* are manually adjusted.
* ****************************************************************
* 
* Initialization
dev_update_off ()
dev_close_window ()
read_image (Image, 'barcode/code39/code3906')
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_set_line_width (3)
dev_set_draw ('margin')
dev_set_color ('green')
* 
MinWidth := 280
MinHeight := 60
* 
* Display a description
Message := 'This example demonstrates how to use the bar code'
Message[1] := 'parameters \'barcode_height_min\' and \'barcode_width_min\''
Message[2] := 'to narrow the number of detected candidate regions in'
Message[3] := 'which the bar codes are searched for.'
disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* 
* Create a bar code model
create_bar_code_model ([], [], BarCodeHandle)
* 
* Adjust the minimal widths of the bar code elements
* and find the bar code
find_bar_code (Image, SymbolRegions, BarCodeHandle, 'Code 39', DecodedDataStrings)
* 
* Get all candidate regions
get_bar_code_object (BarCodeObjects, BarCodeHandle, 'all', 'candidate_regions')
* 
* Display all candidate regions
dev_set_colored (12)
dev_display (Image)
dev_display (BarCodeObjects)
Message := 'Candidate regions with default settings'
disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
Message := 'Adjusting the parameters \'barcode_width_min\' and'
Message[1] := '\'barcode_height_min\' to decrease the number of'
Message[2] := 'found candidate regions and thus to increase the'
Message[3] := 'robustness of the application.'
disp_message (WindowHandle, Message, 'window', 40, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* 
* Minimal bar code width
* -----------------------------
* Set the minimal bar code width and search for bar codes again
set_bar_code_param (BarCodeHandle, 'barcode_width_min', MinWidth)
find_bar_code (Image, SymbolRegions, BarCodeHandle, 'Code 39', DecodedDataStrings)
* 
* Get all candidate regions
get_bar_code_object (BarCodeObjects, BarCodeHandle, 'all', 'candidate_regions')
* 
* Display the results
dev_display (Image)
dev_display (BarCodeObjects)
Message := 'Candidate regions with adjusted parameter:'
Message[1] := '  \'barcode_width_min\' = ' + MinWidth
disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* 
* Minimal bar code height
* -------------------------
* Set the minimal bar code height and search for bar codes again
set_bar_code_param (BarCodeHandle, 'barcode_height_min', MinHeight)
find_bar_code (Image, SymbolRegions, BarCodeHandle, 'Code 39', DecodedDataStrings)
* 
* Get all candidate regions
get_bar_code_object (BarCodeObjects, BarCodeHandle, 'all', 'candidate_regions')
* 
* Display the results
dev_display (Image)
dev_display (BarCodeObjects)
Message := 'Candidate region with adjusted parameters:'
Message[1] := '  \'barcode_width_min\'  = ' + MinWidth
Message[2] := '  \'barcode_height_min\' =  ' + MinHeight
disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* 
* Display the symbol region and the decoded string
dev_display (Image)
dev_set_color ('red')
dev_display (BarCodeObjects)
dev_set_color ('lime green')
dev_display (SymbolRegions)
disp_message (WindowHandle, 'Search with adjusted parameters', 'window', 12, 12, 'black', 'true')
disp_message (WindowHandle, 'Decoded string: ' + DecodedDataStrings, 'window', 40, 12, 'black', 'true')
disp_message (WindowHandle, ['Candidate regions','Symbol region'], 'window', 170, 12, ['red','lime green'], 'true')

没有设置set_bar_code_param (BarCodeHandle, ‘barcode_height_min’, MinHeight) 的情况下
在这里插入图片描述> 设置之后对条码的识别
在这里插入图片描述

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/218906.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

数字孪生 5G时代的重要应用场景 - 读书笔记

作者&#xff1a;陈根 第1章&#xff1a;数字孪生概述 数字孪生&#xff1a;对物理世界&#xff0c;构建数字化实体&#xff0c;实现了解、分析和优化集成技术&#xff1a;AI、机器学习、大数据分析构成&#xff1a;传感器、数据、集成、分析、促动器&#xff08;可以人工干预…

【Pytorch】学习记录分享1——Tensor张量初始化与基本操作

1. 基础资料汇总 资料汇总 pytroch中文版本教程 PyTorch入门教程 B站强推&#xff01;2023公认最通俗易懂的【PyTorch】教程&#xff0c;200集付费课程&#xff08;附代码&#xff09;人工智能_机器 视频 1.PyTorch简介 2.PyTorch环境搭建 basic: python numpy pandas pytroch…

《Effective C++》学习笔记

条款01&#xff1a;把 C 看成一个语言联邦 C由几个重要的次语言构成 C语言&#xff1a;区块&#xff0c;语句&#xff0c;预处理器&#xff0c;数组&#xff0c;指针等等。 类&#xff1a;class&#xff0c;封装&#xff0c;继承&#xff0c;多态......&#xff08;动态绑定等…

GitHub入门介绍:从小白到大佬的旅程

&#x1f337;&#x1f341; 博主猫头虎&#xff08;&#x1f405;&#x1f43e;&#xff09;带您 Go to New World✨&#x1f341; &#x1f984; 博客首页——&#x1f405;&#x1f43e;猫头虎的博客&#x1f390; &#x1f433; 《面试题大全专栏》 &#x1f995; 文章图文…

mybatis动态SQL-choose-when-otherwise

1、建库建表 create database mybatis-example; use mybatis-example; create table emp (empNo varchar(40),empName varchar(100),sal int,deptno varchar(10) ); insert into emp values(e001,张三,8000,d001); insert into emp values(e002,李四,9000,d001); insert into…

一、运行时数据区域

根据 《Java 虚拟机规范》的规定&#xff0c;Java 虚拟机所管理的内存将会包括以下截个运行时数据区域&#xff0c;如图所示。 1、程序计数器 程序计数器是一块较小的内存空间&#xff0c;它可以看做是当前线程所执行的字节码的行号指示器。在 Java 虚拟机的概念模型里&#x…

或许你更胜一筹呢

还记得刚出来时&#xff0c;一位前辈对我说过的一句话&#xff0c;“一定不要妄自菲薄”。说实话&#xff0c;一开始我并不知道这个成语的具体含义。后面百度才知道 妄自菲薄&#xff1a;过分地看轻自己 当时还没毕业&#xff0c;无论是从能力还是学识方面&#xff0c;我都不知…

C#学习笔记 - C#基础知识 - C#从入门到放弃

C# 第1节 C# 简单介绍1.1 C# 是什么1.2 C# 强大的编程功能1.3 C# 发展史1.4 C#与Java区别 第2节 C#程序结构2.1 Hello world2.2 C# 结构解析 第3节 C#基本语法3.1 第1节 C# 简单介绍 1.1 C# 是什么 C# 的发音为“C Sharp”&#xff0c;是一门由微软开发并获得了 ECMA&#xf…

【算法通关村】链表反转经典问题解析

&#x1f6a9;本文已收录至算法学习之旅 一.基础反转 我们通常有两种方法反转链表&#xff0c;一种是直接操作链表实现反转操作&#xff0c;一种是建立虚拟头节点辅助实现反转操作。 力扣习题链接&#xff1a;206. 反转链表 (1) 直接操作实现反转 我们需要一个变量pre来保…

Jmeter接口自动化测试 —— Jmeter变量的使用

​在使用jmeter进行接口测试时&#xff0c;我们难免会遇到需要从上下文中获取测试数据的情况&#xff0c;这个时候就需要引入变量了。 定义变量 添加->配置元件->用户自定义的变量 添加->配置元件->CSV 数据文件设置 变量的调用方式&#xff1a;${变量名} 变量的…

Qt6.5类库实例大全:QWidget

哈喽大家好&#xff0c;我是20YC小二&#xff01;欢迎扫码关注公众号&#xff0c;现在可免费领取《C程序员》在线视频教程哦&#xff01; ~下面开始今天的分享内容~ 1. QWidget介绍 QWidget 是 Qt 框架中的一个核心类&#xff0c;用于创建图形用户界面(GUI)应用程序的基本可视…

iic应用篇

一.iic的优点 1. IIC总线物理链路简单&#xff0c;硬件实现方便&#xff0c;扩展性非常好&#xff08;1个主机控制器可以根据需求增加从机数量&#xff0c;同时删减从机数量也不会影响总线通信&#xff09;&#xff1b;IIC总线只需要SDA和SCL两条信号线&#xff0c;相比于PCI/…

融了超24亿一分钱不花,放银行吃利息,这家存储创企厉害了

​引言&#xff1a;AI与大模型风起云涌&#xff0c;催生了这匹存储“黑马” 【全球存储观察 &#xff5c; 科技热点关注】 这家总部设在美国的存储初创公司&#xff0c;真的赶上AI与大模型时代的风口了。Vast Data公司最新再次获得E轮融资1.18亿美元&#xff0c;但是这个存储…

【MySQL】:表的约束(上)

表的约束 一.非空约束二.default约束三.列描述四.zerofill五.主键1.单个主键2.复合主键 真正约束字段的是数据类型&#xff0c;但是数据类型约束很单一&#xff0c;需要有一些额外的约束&#xff0c;更好的保证数据的合法性&#xff0c;从业务逻辑角度保证数据的正确性。比如有…

TrustGeo代码理解(一)main.py

代码链接&#xff1a;https://github.com/ICDM-UESTC/TrustGeo 一、导入各种模块和数据库 # -*- coding: utf-8 -*- import torch.nnfrom lib.utils import * import argparse, os import numpy as np import random from lib.model import * import copy from thop import p…

sillyGirl(傻妞机器人)安装以及对接go-cqhttp(2023年12月)

目录 编写的原因 下载傻妞 注意&#xff01;&#xff01;注意&#xff01;&#xff01;&#xff01;注意&#xff01;&#xff01;&#xff01;&#xff01; 同样的下载go-cqhttp 安装以及配置 go-cqhttp 下载screen 创建go-cqhttp窗口 创建sillyGirl窗口 常见错误 编写…

Python玩转PDF:几招搞定的高效操作方法

更多资料获取 &#x1f4da; 个人网站&#xff1a;ipengtao.com 当在Python中操作PDF时&#xff0c;有几种常见的方法&#xff0c;每种方法都有其独特的优点和用例。在本文中&#xff0c;我们将深入探讨这些方法&#xff0c;并提供丰富的示例代码&#xff0c;以帮助大家更好地…

「Verilog学习笔记」可置位计数器

专栏前言 本专栏的内容主要是记录本人学习Verilog过程中的一些知识点&#xff0c;刷题网站用的是牛客网 timescale 1ns/1nsmodule count_module(input clk,input rst_n,input set,input [3:0] set_num,output reg [3:0]number,output reg zero);reg [3:0] cnt ; always (posed…

3-分布式存储之Ceph

任务背景 虽然使用了分布式的glusterfs存储, 但是对于爆炸式的数据增长仍然感觉力不从心。对于大数据与云计算等技术的成熟, 存储也需要跟上步伐. 所以这次我们选用对象存储. 任务要求 1, 搭建ceph集群 2, 实现对象存储的应用 任务拆解 1, 了解ceph 2, 搭建ceph集群 3, 了…

深度学习 | Pytorch深度学习实践 (Chapter 12 Basic RNN)

十二、Basic RNN —— 实际上就是对线性层的复用 使用RNN最重要的两点&#xff1a; 了解序列数据的维度&#xff1b;循环过程所用的权重共享机制&#xff1b; 一般就是自己写个循环&#xff0c;权重层重复用就行了&#xff1b; 回顾&#xff1a;-----------------------------…