Halcon一维码识别

news2024/12/23 22:07:11

文章目录

  • 参数连接
  • 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 1
    read_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 fails
    dev_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 1
    count_seconds (Start)
    *查找条形码
    find_bar_code (Image, SymbolRegions, BarCodeHandle, '2/5 Interleaved', DecodedDataStrings)
    count_seconds (End)
    Time := End - Start
    Times := [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 1
    count_seconds (Start)
    find_bar_code (Image, SymbolRegions, BarCodeHandle, '2/5 Interleaved', DecodedDataStrings)
    count_seconds (End)
    Time := End - Start
    Times := [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 1
    FileName := ExampleImages[Index]
    read_image (Image, FileName)
    rgb1_to_gray (Image, Image)
    * 
    * Set display defaults
    get_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.0
    set_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 contrast
    find_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.0
    set_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 1
    read_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) - 1
    disp_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 1
    read_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 1
        if (ord(DecodedDataStrings{J}) < 32)
            Char := '\\x' + ord(DecodedDataStrings{J})$'02x'
        else
            Char := DecodedDataStrings{J}
        endif
        String := String + Char
    endfor
    disp_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 2
    File := '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 1
    Filename := '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]
    else
        CandidateIds := '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 1
    read_image (Image, ImageFilesTea[ImageIndex])
    dev_display (Image)
    * 
    * segment and determine the bar code region
    threshold (Image, RegionThresh, 56, 255)
    connection (RegionThresh, RegThreshConn)
    select_shape_std (RegThreshConn, RegionBC, 'max_area', 100)
    * 
    * create input parameters for decode_bar_code_rectangle2
    smallest_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 ROI
    decode_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.coloradmin.cn/o/1306552.html

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

相关文章

HarmonyOS第一课ArkTS开发语言(TypeScript快速入门)

编程语言介绍 ArkTS是HarmonyOS优选的主力应用开发语言。它在TypeScript&#xff08;简称TS&#xff09;的基础上&#xff0c;匹配ArkUI框架&#xff0c;扩展了声明式UI、状态管理等相应的能力&#xff0c;让开发者以更简洁、更自然的方式开发跨端应用。要了解什么是ArkTS&…

解决设备维修管理问题,易点易动来帮忙!

设备维修管理常常存在一些问题&#xff0c;给企业带来不便和困扰&#xff1a; 1&#xff09;维修信息不及时准确&#xff0c;导致维修延误或错过重要维护时机&#xff1b; 2&#xff09;纸质记录容易丢失或难以管理&#xff0c;使得维修历史不完整&#xff1b; 3&#xff09…

MISC之LSB

LSB隐写 简介 LSB隐写&#xff08;Least Significant Bit Steganography&#xff09;是一种隐写术&#xff0c;它通过将秘密信息嵌入到图像、音频或视频等多媒体文件中的最低有效位中来隐藏信息。在数字图像中&#xff0c;每个像素由红、绿、蓝三个通道的颜色值组成。每个颜色…

c语言插入排序算法(详解)

插入排序是一种简单直观的排序算法&#xff0c;其主要思想是将一个待排序的元素插入到已经排好序的部分的合适位置。 插入排序的原理如下&#xff1a; 将序列分为两部分&#xff1a;已排序部分和未排序部分。初始时&#xff0c;已排序部分只包含第一个元素&#xff0c;未排序…

Unity 射线检测(Raycast)检测图层(LayerMask)的设置

目录 主要内容 拓展&#xff1a; 主要内容 Raycast函数有很多重载(函数的重载根据函数的参数来决定) 这里只涉及这个重载,其余重载可以很方便得在Visual Studio中看源码获取&#xff1b; public static bool Raycast(Vector3 origin, Vector3 direction, out RaycastHit hit…

成绩统计(oj题)

一道考验细节的题 最后是&#xff1f;&#xff1a;运算符用错了 代码如下&#xff1a; #include<stdio.h> #include<string.h> typedef struct Grade{int num;int inv; }Grade; Grade tmp[10]; int n, m, g, interval[10] {0};int main(void) {scanf("%d%d…

智能优化算法应用:基于鸟群算法3D无线传感器网络(WSN)覆盖优化 - 附代码

智能优化算法应用&#xff1a;基于鸟群算法3D无线传感器网络(WSN)覆盖优化 - 附代码 文章目录 智能优化算法应用&#xff1a;基于鸟群算法3D无线传感器网络(WSN)覆盖优化 - 附代码1.无线传感网络节点模型2.覆盖数学模型及分析3.鸟群算法4.实验参数设定5.算法结果6.参考文献7.MA…

Vue3-15-事件处理的基本使用详解

什么是事件处理 事件处理 &#xff1a; 就是对页面上的事件进行捕获并进行逻辑上的处理。 例如 &#xff1a; 点击了一个按钮&#xff0c;捕获点击事件&#xff0c;并进行响应的逻辑处理。vue3中的事件处理的语法 主要使用到的是 v-on 指令&#xff0c; 这个指令的语法糖&…

【教程】如何将重要文件进行混淆和加密

怎么保护苹果手机移动应用程序ipa中文件安全&#xff1f; ios应用程序存储一些图片&#xff0c;资源&#xff0c;配置信息&#xff0c;甚至敏感数据如用户信息、证书、私钥等。这些数据怎么保护呢&#xff1f;可以使用iOS提供的Keychain来保护敏感数据&#xff0c;也可以使用加…

机器学习---TF-IDF算法

1、TF-IDF TF-IDF(Term Frequency-Inverse Document Frequency, 词频-逆文本频率)。TF指词频&#xff0c;IDF指的是逆文本频率。TF-IDF是一种用于信息检索与数据挖掘的常用加权技术&#xff0c;可以评估一个词在一个文件集或者一个语料库中对某个文件的重要程度。一个词语在一篇…

创邻科技上榜中国信通院《高质量数字化转型产品及服务全景图》

近年来&#xff0c;数字化转型浪潮浩浩荡荡&#xff0c;已成为企业高质量发展的必由之路。 但是企业的数字化转型之路并不简单。一方面&#xff0c;企业对数字化转型仍面临着“战略缺位”“能力难建”“价值难现”等问题&#xff1b;另一方面&#xff0c;市场上众多的数字化转…

Day58力扣打卡

打卡记录 下一个更大元素 IV&#xff08;单调栈 x2&#xff09; 链接 class Solution:def secondGreaterElement(self, nums: List[int]) -> List[int]:ans [-1] * len(nums)s []t []for i, x in enumerate(nums):while t and nums[t[-1]] < x:ans[t.pop()] x # t…

班主任,再也不愁怎么给学生发成绩了

作为班主任&#xff0c;我们时常面临着如何有效地将学生的成绩信息传达给家长的问题。传统的纸质成绩单邮寄方式不仅效率低下&#xff0c;而且容易丢失&#xff0c;难以保证信息的及时性和准确性。现在&#xff0c;有了微信「群发成绩」小程序&#xff0c;班主任们终于可以摆脱…

ISP去噪(2)_np 噪声模型

#灵感# ISP 中的去噪&#xff0c;都需要依赖一个噪声模型。很多平台上使用采集的raw进行calibration&#xff0c;可以输出这个模型&#xff0c;通常称为 noise profile。 目录 名词解释&#xff1a; 标定方法&#xff1a; 校准出的noise profile: noise profile 作用域&am…

异常当做业务逻辑处理严重影响性能

一:背景 在项目应该或多或少的见过有人把异常当做业务逻辑处理的情况(┬_┬),比如说判断一个数字是否为整数,就想当然的用try catch包起来,再进行 int.Parse,如果抛异常就说明不是整数,简单粗暴,也不需要写正则或者其他逻辑,再比如一个字符串强制转化为Enum,直接用Enu…

Linux中的堡垒机搭建以及使用

JumpServer搭建 安装应用包 curl -sSL https://resource.fit2cloud.com/jumpserver/jumpserver/releases/latest/download/quick_start.sh | bash 一路回车即可安装完毕&#xff08;可根据需求更改&#xff09; JumpServer的 配置文件路径 /opt/jumpserver/config/config.tx…

python request 不走代理proxy

遇到问题如&#xff1a; requests.exceptions.ProxyError: HTTPSConnectionPool(host‘quake.360.net’, port443): Max retries exceeded with url: /api/v3/search/quake_service (Caused by ProxyError(‘Cannot connect to proxy.’, OSError 解决如下&#xff1a; 一般…

分页存储管理

页框和页面 将内存空间分为一个个大小相等的分区 (比如:每个分区4KB)&#xff0c;每个分区就是一个“页框”(页框页内存块物理块物理页面)。每个页框有一个编号&#xff0c;即“页框号”(页框号页帧号内存块号物理块号物理页号)&#xff0c;页框号从0开始。 为了将各个进程的数…

基于Java SSM框架实现家用电器销售系统项目【项目源码+论文说明】计算机毕业设计

基于java的SSM框架实现家用电器销售系统演示 摘要 家用电器销售网站采用B/S模式&#xff0c;促进了家用电器销售的安全、质量、快捷的发展。传统的管理模式还处于手工处理阶段&#xff0c;管理效率极低&#xff0c;随着用户的不断增多&#xff0c;传统基于手工管理模式已经无法…

后端返回base64文件前端如何下载

1.后端返回base64格式文件 2.前端代码 <style lang"less" scoped> import "./style/common.less";.table-div-a {color: #409EFF;text-decoration: underline;cursor: pointer; } </style><template><div class"template-con…