基于Halcon学习的一维码识别【三十二】barcode_param_stop_after_result_num.hdev

此代码示例展示了如何利用条形码参数'stop_after_result_num'来缩短解码时间。当预期条形码数量已知时,设置此参数能有效减少解码时间,特别是在存在大量错误候选条形码的图像中。示例分为两部分,首先针对单一类型的条形码,然后展示在混合条形码类型场景下,如何为不同类型的条形码设置不同的预期数量以优化解码效率。
摘要由CSDN通过智能技术生成

本例显示了条形码参数'stop_after_result_num',如果已知预期条形码的数量,则使用该参数可以缩短解码时间。


总代码:

*关闭窗口
dev_close_window ()
dev_open_window (0, 0, 640, 480, 'white', WindowHandle)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_update_off ()

*设置边缘描绘
dev_set_draw ('margin')
dev_set_line_width (2)
* 
* Display an introduction
*显示介绍
Message := 'This example presents the bar code parameter\n'
Message[1] := '\'stop_after_result_num\'.\n \n'
Message[2] := 'With this parameter the decoding time can be decreased'
Message[3] := 'if the number of expected bar codes is known.\n \n'
Message[4] := 'Please refer to the reference documentation for further \nexplanations.'

*显示信息--本例显示了条形码参数'stop_after_result_num'
*如果已知预期条形码的数量,则使用该参数可以缩短解码时间。
disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'false')
disp_continue_message (WindowHandle, 'black', 'false')
stop ()

* 创建条形码模板
create_bar_code_model ([], [], BarCodeHandle)
* 
* Case 1 shows the improvement for images containing only bar
* codes of type 'GS1 DataBar Stacked' with many false candidates

*案例1显示对仅包含条形图的图像的改进
*“GS1数据库”类型的代码与许多错误的候选者堆叠在一起

StatusHeight := 180
for I := 1 to 2 by 1
    *清除窗口
    dev_clear_window ()
    * 
    * read image, initialize window
    *读取图像,初始化窗口
    read_image (Image, 'barcode/gs1databar_stacked/gs1databar_stacked_0' + I)
    get_image_size (Image, Width, Height)
    
    *设置窗口的大小
    dev_set_window_extents (-1, -1, Width, Height + StatusHeight)
    dev_set_part (0, 0, Height + StatusHeight - 1, Width - 1)
    dev_display (Image)
    * 
    * run detection with the default number for decodings
    * (which is practically infinite)
    *使用解码的默认编号运行检测
    *(实际上是无限的)
    *将解码所有候选条形码
    set_bar_code_param (BarCodeHandle, 'stop_after_result_num', 0)
    * 
    * determine the runtime of find_bar_code
    *确定查找条形码的运行时间
    find_bar_code_timing (Image, SymbolRegions, BarCodeHandle, 'GS1 DataBar Stacked', DecodedDataStrings, Time1)
    * 
    * run the detection and stop after the first successfully decoded
    * bar code
    *运行检测并在第一次成功解码条形码后停止
    set_bar_code_param (BarCodeHandle, 'stop_after_result_num', 1)
    * 
    * determine the runtime of find_bar_code
    *确定查找条形码的运行时间

    find_bar_code_timing (Image, SymbolRegions, BarCodeHandle, 'GS1 DataBar Stacked', DecodedDataStrings, Time2)
    * 
    * show candidates (red) and decoded symbols (green)
    *显示候选符号(红色)和解码符号(绿色)
    get_bar_code_object (Candidates, BarCodeHandle, 'all', 'candidate_regions')
    dev_set_color ('red')
    dev_display (Candidates)
    dev_set_color ('green')
    *显示解码区域
    dev_display (SymbolRegions)
    * 
    
    *显示的时间
    Message := 'Elapsed time \n'
    Message[1] := '    all candidates(default) :   ' + Time1$'7.2f' + ' ms\n'
    Message[2] := '    decode only one symbol  :   ' + Time2$'7.2f' + ' ms\n'
    disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
    
    *如果是第一张图片
    if (I == 1)
        Message := 'If the number of expected bar codes is known in\n'
        Message[1] := 'advance (here: 1) this can be set as parameter\n'
        Message[2] := '\'stop_after_result_num\' in set_bar_code_param\n'
        Message[3] := 'or set_bar_code_param_specific.\n\n'
        Message[4] := 'Typically, the decoding time decreases in images\n'
        Message[5] := 'with many false candidates.'
        
        *显示信息--如果已知预期条形码的数量
        *提前(此处:1)这可以设置为参数
        *通常,图像中的解码时间会减少有很多虚假候选区域
        disp_message (WindowHandle, Message, 'window', Height + 12, 12, 'black', 'false')
    endif
    disp_continue_message (WindowHandle, 'black', 'false')
    stop ()
endfor
* 
* Case 2 shows the usage of stop_after_result_num in the
* operator set_bar_code_param_specific for mixed bar code types.

*案例2显示了在
*特定于混合条形码类型的运算符集\条形码\参数。

* For each image the expected bar code types and
* their numbers are given
*对于每张图像,给出了预期的条形码类型及其编号
CodeTypes := ['Code 39','EAN-13']
NumCode39 := [2,1,3,2]
NumEAN13 := [1,1,1,1]
* 
* Display an introduction
*显示介绍
dev_set_window_extents (-1, -1, 640, 480)
dev_clear_window ()
Message := 'The number of expected bar codes can be set differently'
Message[1] := 'for each type (here: ' + CodeTypes[0] + ' and ' + CodeTypes[1] + ') with the\n'
Message[2] := 'operator set_bar_code_param_specific.\n \n'
Message[3] := 'Please note that in some cases there is no speed-up'
Message[4] := 'even though stop_after_result_num has been set, e.g.\n'
Message[5] := 'in cases where the number of candidates equals the\n'
Message[6] := 'number of expected bar codes.'
*显示信息
*对于每种类型(此处:“+CodeTypes[0]+”和“+CodeTypes[1]+”)的预期条形码数量可以
*通过特定的运算符set_bar_code_param进行不同的设置。请注意,在某些情况下,
*即使在设置了stop_after_result_num,也不会加快速度,例如,候选数量等于预期条形码数量的情况
disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'false')
disp_continue_message (WindowHandle, 'black', 'false')
stop ()
* 
*循环读取图片
for I := 1 to 4 by 1
    *清除窗口
    dev_clear_window ()
    * 
    * read image, initialize window
    *读取图像,初始化窗口
    read_image (Image, 'barcode/mixed/barcode_mixed_0' + I)
    get_image_size (Image, Width, Height)
    
    *设置窗口大小
    dev_set_window_extents (-1, -1, Width / 2, Height / 2)
    dev_set_part (0, 0, Height - 1, Width - 1)
    dev_display (Image)
    * 
    * run the detection with the default number of decodings (which
    * is practically infinite) for all supported bar code types
    *使用默认的解码数运行检测(其中
    *实际上是无限的)适用于所有受支持的条形码类型
    set_bar_code_param (BarCodeHandle, 'stop_after_result_num', 0)
    * 
    * determine the runtime of find_bar_code
    *确定查找条形码的运行时间
    find_bar_code_timing (Image, SymbolRegions, BarCodeHandle, CodeTypes, DecodedDataStrings, Time1)
    * 
    * run the detection and stop after a given number of
    * successfully decoded bar codes of the given type
    *在给定数量的给定类型条形码成功解码后,运行检测并停止
    set_bar_code_param_specific (BarCodeHandle, CodeTypes[0], 'stop_after_result_num', NumCode39[I - 1])
    set_bar_code_param_specific (BarCodeHandle, CodeTypes[1], 'stop_after_result_num', NumEAN13[I - 1])
    * 
    * determine the runtime of find_bar_code
    *确定查找条形码的运行时间
    find_bar_code_timing (Image, SymbolRegions, BarCodeHandle, CodeTypes, DecodedDataStrings, Time2)
    * 
    * show candidates (red) and decoded symbols (green)
    *显示候选符号(红色)和解码符号(绿色)
    get_bar_code_object (Candidates, BarCodeHandle, 'all', 'candidate_regions')
    dev_set_color ('red')
    dev_display (Candidates)
    dev_set_color ('green')
    dev_display (SymbolRegions)
    *
    *显示信息--经过的时间
    Message := 'Elapsed time \n'
    Message[1] := '    all candidates(default) :   ' + Time1$'7.2f' + ' ms\n'
    Message[2] := '    decode only given number:   ' + Time2$'7.2f' + ' ms\n'
    disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
    disp_continue_message (WindowHandle, 'black', 'true')
    stop ()
endfor
* 
*释放条形码阅读器分配的内存
clear_bar_code_model (BarCodeHandle)
dev_close_window ()

逐段分析:

*关闭窗口
dev_close_window ()
dev_open_window (0, 0, 640, 480, 'white', WindowHandle)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_update_off ()

*设置边缘描绘
dev_set_draw ('margin')
dev_set_line_width (2)

*显示介绍
Message := 'This example presents the bar code parameter\n'
Message[1] := '\'stop_after_result_num\'.\n \n'
Message[2] := 'With this parameter the decoding time can be decreased'
Message[3] := 'if the number of expected bar codes is known.\n \n'
Message[4] := 'Please refer to the reference documentation for further \nexplanations.'

*显示信息--本例显示了条形码参数'stop_after_result_num'
*如果已知预期条形码的数量,则使用该参数可以缩短解码时间。
disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'false')
disp_continue_message (WindowHandle, 'black', 'false')
stop ()

* 创建条形码模板
create_bar_code_model ([], [], BarCodeHandle)

*案例1显示对仅包含条形图的图像的改进
*“GS1数据库”类型的代码与许多错误的候选者堆叠在一起

*开始循环读取两张图片
StatusHeight := 180
for I := 1 to 2 by 1
    *清除窗口
    dev_clear_window ()

    *读取图像,初始化窗口
    read_image (Image, 'barcode/gs1databar_stacked/gs1databar_stacked_0' + I)
    get_image_size (Image, Width, Height)

    *设置窗口的大小
    dev_set_window_extents (-1, -1, Width, Height + StatusHeight)
    dev_set_part (0, 0, Height + StatusHeight - 1, Width - 1)
    dev_display (Image)

    *使用解码的默认编号运行检测(实际上是无限的)
    *将解码所有候选条形码
    set_bar_code_param (BarCodeHandle, 'stop_after_result_num', 0)
    
    *确定查找条形码的运行时间
    find_bar_code_timing (Image, SymbolRegions, BarCodeHandle, 'GS1 DataBar Stacked', DecodedDataStrings, Time1)

    *确定查找条形码的运行时间
    find_bar_code_timing (Image, SymbolRegions, BarCodeHandle, 'GS1 DataBar Stacked', DecodedDataStrings, Time1)

    *运行检测并在第一次成功解码条形码后停止
    set_bar_code_param (BarCodeHandle, 'stop_after_result_num', 1)

    *确定查找条形码的运行时间
    find_bar_code_timing (Image, SymbolRegions, BarCodeHandle, 'GS1 DataBar Stacked', DecodedDataStrings, Time2)

    *显示候选符号(红色)和解码符号(绿色)
    get_bar_code_object (Candidates, BarCodeHandle, 'all', 'candidate_regions')
    dev_set_color ('red')
    dev_display (Candidates)
    dev_set_color ('green')

    *显示解码区域
    dev_display (SymbolRegions)

    *显示的时间
    Message := 'Elapsed time \n'
    Message[1] := '    all candidates(default) :   ' + Time1$'7.2f' + ' ms\n'
    Message[2] := '    decode only one symbol  :   ' + Time2$'7.2f' + ' ms\n'
    disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')

    *如果是第一张图片
    if (I == 1)
        Message := 'If the number of expected bar codes is known in\n'
        Message[1] := 'advance (here: 1) this can be set as parameter\n'
        Message[2] := '\'stop_after_result_num\' in set_bar_code_param\n'
        Message[3] := 'or set_bar_code_param_specific.\n\n'
        Message[4] := 'Typically, the decoding time decreases in images\n'
        Message[5] := 'with many false candidates.'
        
        *显示信息--如果已知预期条形码的数量
        *提前(此处:1)这可以设置为参数
        *通常,图像中的解码时间会减少有很多虚假候选区域
        disp_message (WindowHandle, Message, 'window', Height + 12, 12, 'black', 'false')
    endif
    disp_continue_message (WindowHandle, 'black', 'false')
    stop ()
endfor

*对于每张图像,给出了预期的条形码类型及其编号
CodeTypes := ['Code 39','EAN-13']
NumCode39 := [2,1,3,2]
NumEAN13 := [1,1,1,1]

*显示介绍
dev_set_window_extents (-1, -1, 640, 480)
dev_clear_window ()
Message := 'The number of expected bar codes can be set differently'
Message[1] := 'for each type (here: ' + CodeTypes[0] + ' and ' + CodeTypes[1] + ') with the\n'
Message[2] := 'operator set_bar_code_param_specific.\n \n'
Message[3] := 'Please note that in some cases there is no speed-up'
Message[4] := 'even though stop_after_result_num has been set, e.g.\n'
Message[5] := 'in cases where the number of candidates equals the\n'
Message[6] := 'number of expected bar codes.'

*显示信息
*对于每种类型(此处:“+CodeTypes[0]+”和“+CodeTypes[1]+”)的预期条形码数量可以
*通过特定的运算符set_bar_code_param进行不同的设置。请注意,在某些情况下,
*即使在设置了stop_after_result_num,也不会加快速度,例如,候选数量等于预期条形码数量的情况
disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'false')
disp_continue_message (WindowHandle, 'black', 'false')
stop ()

*循环读取图片
for I := 1 to 4 by 1
    *清除窗口
    dev_clear_window ()
   
    *读取图像,初始化窗口
    read_image (Image, 'barcode/mixed/barcode_mixed_0' + I)
    get_image_size (Image, Width, Height)

    *设置窗口大小
    dev_set_window_extents (-1, -1, Width / 2, Height / 2)
    dev_set_part (0, 0, Height - 1, Width - 1)
    dev_display (Image)

   *使用默认的解码数运行检测(其中
    *实际上是无限的)适用于所有受支持的条形码类型
    set_bar_code_param (BarCodeHandle, 'stop_after_result_num', 0)

    *确定查找条形码的运行时间
    find_bar_code_timing (Image, SymbolRegions, BarCodeHandle, CodeTypes, DecodedDataStrings, Time1)

    *在给定数量的给定类型条形码成功解码后,运行检测并停止
    set_bar_code_param_specific (BarCodeHandle, CodeTypes[0], 'stop_after_result_num', NumCode39[I - 1])
    set_bar_code_param_specific (BarCodeHandle, CodeTypes[1], 'stop_after_result_num', NumEAN13[I - 1])

    *确定查找条形码的运行时间
    find_bar_code_timing (Image, SymbolRegions, BarCodeHandle, CodeTypes, DecodedDataStrings, Time2)

    *显示候选符号(红色)和解码符号(绿色)
    get_bar_code_object (Candidates, BarCodeHandle, 'all', 'candidate_regions')
    dev_set_color ('red')
    dev_display (Candidates)
    dev_set_color ('green')
    dev_display (SymbolRegions)

    *显示信息--经过的时间
    Message := 'Elapsed time \n'
    Message[1] := '    all candidates(default) :   ' + Time1$'7.2f' + ' ms\n'
    Message[2] := '    decode only given number:   ' + Time2$'7.2f' + ' ms\n'
    disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
    disp_continue_message (WindowHandle, 'black', 'true')
    stop ()

*释放条形码阅读器分配的内存
clear_bar_code_model (BarCodeHandle)
dev_close_window ()

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值