基于Halcon学习的一维码识别【四】barcode.param_contrast.hdev

使用条形码参数'contrast_min'的示例程序;
在图像中存在低对比度条形结构的情况下,该参数可用于减少find_bar_code的运行时间;
此外,如果预期的条形码具有高对比度,contrast_min'也可用于减少误报的数量。


总代码:

*创建一个条形码阅读器的模型。
create_bar_code_model ([], [], BarCodeHandle)
*'element_size_min'条码最小尺寸(宽度和间距)指 黑条或者白条的最小尺寸像素是1.5像素
set_bar_code_param (BarCodeHandle, 'element_size_min', 1.5)
* 
* 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)
* 
* Number of repetitions for runtime measurements
*运行时测量的重复次数
NumRepeat := 100
* 
* First, set the minimum contrast of the bar code candidate regions
* to 0 (default)
*首先,将条形码候选区域的最小对比度设置为0(默认值)


ContrastMinValue := 0
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, 'auto', 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.


*对比度增加 能把一些干扰项去除,能够最快最准的找到一些条码
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, 'auto', 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')
* 
* Close the bar code reader
clear_bar_code_model (BarCodeHandle)

逐段分析:

1、当对比度为0的时候

*创建一个条形码阅读器的模型。
create_bar_code_model ([], [], BarCodeHandle)

*'element_size_min'条码最小尺寸(宽度和间距)指 黑条或者白条的最小尺寸像素是1.5像素
set_bar_code_param (BarCodeHandle, 'element_size_min', 1.5)

dev_update_off ()
dev_close_window ()

*读取图片
read_image (Image, 'barcode/25interleaved/25interleaved_zeiss1')

*设置一些参数
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')

*显示提示信息
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)

*运行时测量的重复次数
NumRepeat := 100

*首先,将条形码候选区域的最小对比度设置为0(默认值)
ContrastMinValue := 0

*设置条形码模型的选定参数
set_bar_code_param (BarCodeHandle, 'contrast_min', ContrastMinValue)

*条形码阅读器会找到许多绝对对比度较低的条形码候选区域
Times := []
for I := 0 to NumRepeat by 1
    count_seconds (Start)
    find_bar_code (Image, SymbolRegions, BarCodeHandle, 'auto', DecodedDataStrings)
    count_seconds (End)
    Time := End - Start
    Times := [Times,Time]
endfor
*定义中间值
RunTimeContrastMinLow := 1000 * median(Times)

*获取候选区域并显示结果
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')


2、当对比度为120的时候

*对比度增加 能把一些干扰项去除,能够最快最准的找到一些条码
ContrastMinValue := 120

*设置条形码模型的选定参数
set_bar_code_param (BarCodeHandle, 'contrast_min', ContrastMinValue)

*再次搜索条形码。现在,数量要少得多的应该找到候选者,并减少总体运行时间。
Times := []
for I := 0 to NumRepeat by 1
    count_seconds (Start)
    *检测并读取图像中的条形码符号
    find_bar_code (Image, SymbolRegions, BarCodeHandle, 'auto', DecodedDataStrings)
    count_seconds (End)
    Time := End - Start
    Times := [Times,Time]
endfor
*定义中间时间
RunTimeContrastMinHigh := 1000 * median(Times)

*设置活动窗口
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')

*清除模型句柄
clear_bar_code_model (BarCodeHandle)


 'contrast_min':

条形码元素的前景和背景之间的最小对比度。将此参数设置为大于5的值将有助于操作员查找条形码,以优化候选区域搜索。find_bar_code将拒绝对比度值低于“contrast_min”的所有候选区域。因此,设置较高的“对比度最小”值将提高find_bar_代码的运行时性能。但是,请注意,所有对比度值低于“对比度_min”的条形码都不会被读取。计算出的对比度值是一个近似值,以加快执行时间。尝试为“对比度最小值”设置一个较低的阈值,以查找可能被拒绝且对比度值接近指定“对比度最小值”的条形码。

典型值:[0,40,90,120,]

默认值:0

由此例程可以看出对比度增加 能把一些干扰项去除,能够最快最准的找到一些条形码

  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值