其他分享
首页 > 其他分享> > 基于Halcon学习的一维码识别【四】barcode.param_contrast.hdev

基于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)


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

标签:hdev,code,bar,barcode,dev,window,set,param,contrast
来源: https://blog.csdn.net/BoomBiuBiu/article/details/123056645