本文主要是介绍HALCON例程:2d_data_codes_data_access.hdev,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
一、程序解读
* 程序描述:
这个程序展示了如何在搜索数据代码时访问中间结果。这使我们可以获得有关如何增强搜索过程的运行时间的提示,或者为什么没有找到或解码符号。
该程序首先创建一个2d数据代码模型,并查询可用参数的名称。在下一步中,在每个图像中搜索和解码数据代码符号。然后查询有关搜索过程和符号(候选)的进一步信息和一些更具体的信息,以获得关于运行时如何增强搜索过程的提示,或者为什么没有找到或解码符号。左侧显示可用参数列表,右侧显示参数值。
* ***************************************************************
总结:
1.正常二维码图像:获得搜索的基础参数;分割为前景及背景模块,获得有关符号参数结果;获得解码字符串及其ASCII码值。
2.找到但未解码符号,查询状态,decoding_error错误解码,显示模块的二进制符号数据。
3.未搜索到二维码,查询状态,aborted Search 搜索失败,显示前3个无效候选并查询其信息;如果超过3个候选项,则显示所有候选项。
4.显示设置:陈列所有参数,高亮显示获得的参数、图像信息、及参数信息。
搜索的基础参数:
'min_search_level'
'max_search_level'
'pass_num'
'result_num' 结果数
'candidate_num' 候选数
'undecoded_num' 解码符号数
'aborted_num' 中止搜索数
5.程序的大量代码为显示设置。
二、代码详解
* Initialize image path.初始化图片路径。dev_update_off ()
dev_close_window ()
ImagePath := 'datacode/ecc200/'
ImageFiles := ImagePath +
['ecc200_distorted_001','ecc200_distorted_002','ecc200_to_preprocess_002','ecc200_to_preprocess_004','ecc200_to_preprocess_003']* 打开参数列表窗口。dev_open_window (0, 0, 550, 600, 'black', ParamWindow)
set_display_font (ParamWindow, 14, 'mono', 'true', 'false')* 简短描述
Message := 'This program shows how to access intermediate'
Message[1] := 'results while searching for ECC 200 data codes.'
Message[2] := ' '
Message[3] := 'This enables to obtain hints how the search process'
Message[4] := 'can be enhanced with respect to runtime or why a'
Message[5] := 'symbol is not found or decoded.'
disp_message (ParamWindow, Message, 'window', 12, 12, 'black', 'true')
disp_continue_message (ParamWindow, 'black', 'true')
stop ()Step 1: Create a 2d data code model
* -------------------------------------------------------
*创建2d数据代码模型,并将模型设置为中间结果的持久存储。create_data_code_2d_model ('Data Matrix ECC 200', 'default_parameters','enhanced_recognition', DataCodeHandle)
set_data_code_2d_param (DataCodeHandle, 'persistence', 1)
*
* Query the parameter names
*获取可用于查询搜索的字母数字结果和图标对象的参数列表
query_data_code_2d_params (DataCodeHandle, 'get_result_params', GenParamName)
query_data_code_2d_params (DataCodeHandle, 'get_result_objects', GenObjectNames)
*
*设置参数名称的颜色
*gen_tuple_const:生成特定长度的元组并初始化元素ColorAlph := gen_tuple_const(|GenParamName|,'#808080')
ColorIcon := gen_tuple_const(|GenObjectNames|,'#808080')* Open new windows
* ------------------------------
* Open window for iconic objects. 打开图标对象窗口
WWidth := 600
WHeight := 300
WRatio := real(WWidth) / WHeight
dev_open_window (0, 560, WWidth, WHeight, 'white', ImageWindow)
set_display_font (ImageWindow, 14, 'mono', 'true', 'false')
dev_set_line_width (3)
dev_set_color ('green')
*
* Open window for alphanumeric results. 打开窗口以查看字母数字结果
dev_open_window (360, 560, 600, 240, 'white', ResultWindow)
set_display_font (ResultWindow, 14, 'mono', 'true', 'false')
* Step 2: Read the data codes 读取二维码
*搜索每个图像中的数据代码,并尝试解码符号。然后查询字母数字结果和图标对象的搜索和解码过程。for Index := 0 to |ImageFiles| - 1 by 1dev_set_window (ImageWindow)dev_clear_window ()read_image (Image, ImageFiles[Index])* 在候选搜索或解码过程中,一些图像被修改以显示不同的问题。if (Index == 2)zoom_image_size (Image, Image, 230, 230, 'constant')endifif (Index == 4)gray_erosion_rect (Image, Image, 3, 3)gauss_filter (Image, Image, 3)endif*determine_part (Image, WRatio, WWidth, WHeight, Row1, Col1, Row2, Col2)dev_set_part (Row1, Col1, Row2, Col2)dev_display (Image)disp_message (ImageWindow, 'Example ' + (Index + 1) + ' of ' + |ImageFiles|, 'window', 12, 12, 'black', 'true')* Set parameters* 为特定图像设置模块的最小像素,以减少候选图像的数量。if (Index == 3)set_data_code_2d_param (DataCodeHandle, 'module_size_min', 24)endifif (Index == 4)set_data_code_2d_param (DataCodeHandle, 'module_size_min', 10)endif*将Index == 2 的‘small_modules_robustness’设置为'high',因为模块太小了(~ 2像素)。if (Index == 2)set_data_code_2d_param (DataCodeHandle, 'small_modules_robustness', 'high')elseset_data_code_2d_param (DataCodeHandle, 'small_modules_robustness', 'low')endif
* Find and decode the data codes. 寻找并解码find_data_code_2d (Image, SymbolXLDs, DataCodeHandle, [], [], ResultHandles, DecodedDataStrings)* Alphanumeric results: various results about search*字母数字结果:关于搜索的各种结果*获得一些关于搜索过程的字母数字结果。GenResultNames :=['min_search_level','max_search_level','pass_num','result_num','candidate_num','undecoded_num','aborted_num']GenResultValues := []get_data_code_2d_results (DataCodeHandle, 'general', GenResultNames,GenResultValues)GenResult := (GenResultNames + ': ')$'-20' + GenResultValues$'2'** 展示结果disp_message (ResultWindow, 'General alphanumeric results about the search:', 'window', 12, 12, 'black', 'false')disp_message (ResultWindow, GenResult, 'window', 40, 12, 'black', 'false')* 更新参数名称的高亮显示* 清除窗口dev_set_window (ParamWindow)dev_clear_window ()disp_message (ParamWindow, 'Highlighted parameters are shown on the right', 'window', 12, 12, 'black', 'true')** 图标对象参数:disp_message (ParamWindow, 'Parameters for iconic objects:', 'window', 50, 12,'black', 'true')disp_message (ParamWindow, GenObjectNames, 'window', 80, 12, ColorIcon, 'false')** 字母数字结果参数:ColorAlph[0,1,2,3,4,5,6] := 'white'disp_message (ParamWindow, 'Parameters for alphanumeric results:', 'window', 190, 12, 'black', 'true')disp_message (ParamWindow, GenParamName[0:18], 'window', 220, 12, ColorAlph, 'false')ColorAlph[0,1,2,3,4,5,6] := '#808080'disp_message (ParamWindow, GenParamName[19:37], 'window', 220, 260, ColorAlph, 'false')disp_continue_message (ResultWindow, 'black', 'true')stop ()
dev_set_window (ResultWindow)dev_clear_window ()*初始化候选数、解码符号数和中止搜索数的变量。CandidateNum := GenResultValues[4]UndecodedNum := GenResultValues[5]AbortedNum := GenResultValues[6]*查询所有候选的图标对象*对于所有2d数据编码候选,查询并显示搜索图像和处理图像。if (CandidateNum > 0)dev_set_window (ImageWindow)dev_clear_window ()
* Search image 搜索图像
* Search image搜索图像* 获取找到候选项的搜索图像get_data_code_2d_objects (SearchImage, DataCodeHandle, 0, 'search_image')determine_part (SearchImage, WRatio, WWidth, WHeight, SearchRow1, SearchCol1, SearchRow2, SearchCol2)dev_set_part (SearchRow1, SearchCol1, SearchRow2, SearchCol2)dev_display (SearchImage)Message := 'Example ' + (Index + 1) + ' of ' + |ImageFiles| + ' Search image'disp_message (ImageWindow, Message, 'window', 12, 12, 'black', 'true')*更新参数名称的高亮显示dev_set_window (ParamWindow)dev_clear_window ()disp_message (ParamWindow, 'Highlighted parameters are shown on the right', 'window', 12, 12, 'black', 'true')* Parameters for iconic objects:ColorIcon[3] := 'white'disp_message (ParamWindow, 'Parameters for iconic objects:', 'window', 50, 12, 'black', 'true')disp_message (ParamWindow, GenObjectNames, 'window', 80, 12, ColorIcon, 'false')ColorIcon[3] := '#808080'* Parameters for alphanumeric results:disp_message (ParamWindow, 'Parameters for alphanumeric results:', 'window', 190, 12, 'black', 'true')disp_message (ParamWindow, GenParamName[0:18], 'window', 220, 12, ColorAlph, 'false')disp_message (ParamWindow, GenParamName[19:37], 'window', 220, 260, ColorAlph, 'false')disp_continue_message (ResultWindow, 'black', 'true')stop ()
* Process image 处理图像
* Process image* 获得调查候选项的过程图像get_data_code_2d_objects (ProcessImage, DataCodeHandle, 0, 'process_image')determine_part (ProcessImage, WRatio, WWidth, WHeight, ProcessRow1, ProcessCol1, ProcessRow2, ProcessCol2)dev_set_part (ProcessRow1, ProcessCol1, ProcessRow2, ProcessCol2)dev_display (ProcessImage)Message := 'Example ' + (Index + 1) + ' of ' + |ImageFiles| + ' Process image'disp_message (ImageWindow, Message, 'window', 12, 12, 'black', 'true')* 更新参数* Parameters for iconic objects:ColorIcon[4] := 'white'disp_message (ParamWindow, GenObjectNames, 'window', 80, 12, ColorIcon, 'false')ColorIcon[4] := '#808080'stop ()endif
* Successfully decoded symbols 成功解码的符号
* 获取有关已解码符号的当前参数值的信息,以便提高运行时间。if (|ResultHandles| > 0*查询每个已解码数据字符串的信息for J := 0 to |ResultHandles| - 1 by 1* 显示图像和XLD轮廓dev_clear_window ()dev_set_part (Row1, Col1, Row2, Col2)dev_display (Image)dev_display (SymbolXLDs)disp_message (ImageWindow, 'Example ' + (Index + 1) + ' of ' + |ImageFiles|, 'window', 12, 12, 'black', 'true')*(1) Query alphanumeric results: various results* 获取并显示符号和搜索的一些参数值。ResultVariousNames :=['polarity','module_gap','pass','status','mirrored','module_height','module_width','slant','contrast','decoded_string']ResultVariousValues := []get_data_code_2d_results (DataCodeHandle, ResultHandles[J], ResultVariousNames, ResultVariousValues)* 确定过大的解码数据字符串的长度。get_string_extents (ImageWindow, ResultVariousValues[9], Ascent, Descent, TWidth, THeight)if (TWidth >= WWidth)ResultVariousValues[9] := ResultVariousValues[9]{0:20} + ' ...'endifVariousResults := (ResultVariousNames + ':')$'-18' + ResultVariousValues* 在ResultWindow窗口展示结果disp_message (ResultWindow, 'Alphanumeric results about the symbol:', 'window', 12, 12, 'black', 'false')disp_message (ResultWindow, VariousResults, 'window', 30, 12, 'black','false')*(2) Query iconic objects: Regions of the modules * 获取并显示模块,检查模块分类的质量。dev_set_window (ImageWindow)* 前景模块:get_data_code_2d_objects (Foreground, DataCodeHandle, ResultHandles[J], 'module_1_rois')dev_set_line_width (1)dev_set_draw ('margin')dev_set_color ('blue')dev_display (Foreground)*背景模块 :get_data_code_2d_objects (Background, DataCodeHandle, ResultHandles[J], 'module_0_rois')dev_set_color ('yellow')dev_display (Background)disp_message (ImageWindow, 'Foreground and Background', 'window', 40, 12, 'black', 'true')dev_set_line_width (3)dev_set_color ('green')*更新高亮参数dev_set_window (ParamWindow)dev_clear_window ()disp_message (ParamWindow, 'Highlighted parameters are shown on the right', 'window', 12, 12, 'black', 'true')* Parameters of iconic objects:ColorIcon[1,2] := ['blue','yellow']disp_message (ParamWindow, 'Parameters for iconic objects:', 'window', 50, 12, 'black', 'true')disp_message (ParamWindow, GenObjectNames, 'window', 80, 12, ColorIcon, 'false')* Parameters of alphanumeric results:ColorAlph[8,11,16,17,18] := 'white'disp_message (ParamWindow, 'Parameters for alphanumeric results:', 'window', 190, 12, 'black', 'true')disp_message (ParamWindow, GenParamName[0:18], 'window', 220, 12, ColorAlph, 'false')ColorAlph[8,11,16,17,18] := '#808080'ColorAlph[2,3,4,5,6] := 'white'disp_message (ParamWindow, GenParamName[19:37], 'window', 220, 260, ColorAlph, 'false')ColorAlph[2,3,4,5,6] := '#808080'stop ()
*(3) Query alphanumeric result: ASCII Code ('decoded_data')* 查询字母数字结果:ASCII码('decoded_data')*获取并显示解码字符串的ASCII码。get_data_code_2d_results (DataCodeHandle, ResultHandles[J], 'decoded_data', ResultASCIICode)format_text (3, ResultASCIICode, 10, FormattedString)* 在ResultWindow中显示码dev_set_window (ResultWindow)dev_clear_window ()disp_message (ResultWindow, 'ASCII Code of the symbol:', 'window', 12, 12, 'black', 'false')disp_message (ResultWindow, FormattedString, 'window', 40, 12, 'black', 'false')*更新高亮参数dev_set_window (ParamWindow)dev_clear_window ()disp_message (ParamWindow, 'Highlighted parameters are shown on the right', 'window', 12, 12, 'black', 'true')* Parameters of iconic objects:disp_message (ParamWindow, 'Parameters for iconic objects:', 'window', 50, 12, 'black', 'true')disp_message (ParamWindow, GenObjectNames, 'window', 80, 12, ColorIcon, 'false')ColorIcon[1,2] := '#808080'* Parameters of alphanumeric results:disp_message (ParamWindow, 'Parameters for alphanumeric results:', 'window', 190, 12, 'black', 'true')disp_message (ParamWindow, GenParamName[0:18], 'window', 220, 12, ColorAlph, 'false')ColorAlph[12] := 'white'disp_message (ParamWindow, GenParamName[19:37], 'window', 220, 260, ColorAlph, 'false')ColorAlph[12] := '#808080'disp_continue_message (ResultWindow, 'black', 'true')stop ()endforelse
Undecoded symbols 未解码符号
* 获取有关已找到但无法解码的符号的当前参数值的信息。if (UndecodedNum > 0)* 获取所有未解码符号的句柄。get_data_code_2d_results (DataCodeHandle, 'all_undecoded', 'handle', HandlesUndecoded)* 探究符号不能被解码的原因。for J := 0 to |HandlesUndecoded| - 1 by 1dev_set_window (ImageWindow)dev_clear_window ()dev_set_part (Row1, Col1, Row2, Col2)dev_display (Image)Message := 'Example ' + (Index + 1) + ' of ' + |ImageFiles| + ' Undecoded symbol ' + (J + 1) + ' of ' + |HandlesUndecoded|disp_message (ImageWindow, Message, 'window', 12, 12, 'black', 'true')*(1)Query and display the status of the search.*查询和显示搜索的状态。get_data_code_2d_results (DataCodeHandle, HandlesUndecoded[J], 'status', StatusValue)disp_message (ResultWindow, 'Alphanumeric results about the symbol:', 'window', 12, 12, 'black', 'false')disp_message (ResultWindow, 'Status: ' + StatusValue, 'window', 40, 12, 'black', 'false')*(2) Query iconic objects: XLD contour and regions of modules*查询图标对象:模块的XLD轮廓和区域* --------------------------------------------------------* 获取未解码符号的XLD轮廓和模块,以获取候选符号未解码的原因。* XLD contour of the candidate.get_data_code_2d_objects (DataCodeObject, DataCodeHandle, HandlesUndecoded[J], 'candidate_xld')dev_set_color ('red')dev_display (DataCodeObject)* Foreground modules:get_data_code_2d_objects (Foreground, DataCodeHandle, HandlesUndecoded[J], 'module_1_rois')dev_set_line_width (1)dev_set_draw ('margin')dev_set_color ('blue')dev_display (Foreground)* Background modules:get_data_code_2d_objects (Background, DataCodeHandle, HandlesUndecoded[J], 'module_0_rois')dev_set_color ('yellow')dev_display (Background)disp_message (ImageWindow, 'Foreground and Background', 'window', 40, 12, 'black', 'true')dev_set_line_width (3)dev_set_color ('green')* 更新高亮参数* Parameters for iconic objects:ColorIcon[0,1,2] := ['red','blue','yellow']disp_message (ParamWindow, GenObjectNames, 'window', 80, 12, ColorIcon, 'false')ColorIcon[0,1,2] := '#808080'* Parameters for alphanumeric results:ColorAlph[17] := 'white'disp_message (ParamWindow, GenParamName[0:18], 'window', 220, 12, ColorAlph, 'false')ColorAlph[17] := '#808080'disp_message (ParamWindow, GenParamName[19:37], 'window', 220, 260, ColorAlph, 'false')stop ()
*(3)Query alphanumeric results: Binary symbol data*查询字母数字结果:二进制符号数据*获取各个模块的二进制信息,以评估模块分类。get_data_code_2d_results (DataCodeHandle, HandlesUndecoded[J], 'bin_module_data', ResultBinModules)format_text (3, ResultBinModules, 10, FormattedBinModules)*显示二进制结果dev_set_window (ResultWindow)dev_clear_window ()disp_message (ResultWindow, 'Binary symbol data of the modules:', 'window', 12, 12, 'black', 'false')disp_message (ResultWindow, FormattedBinModules, 'window', 30, 12, 'black', 'false')*更新高亮参数* Parameters for alphanumeric results:disp_message (ParamWindow, GenParamName[0:18], 'window', 220, 12, ColorAlph, 'false')ColorAlph[9] := 'white'disp_message (ParamWindow, GenParamName[19:37], 'window', 220, 260, ColorAlph, 'false')ColorAlph[9] := '#808080'disp_continue_message (ResultWindow, 'black', 'true')stop ()* Clear the ResultWindow.dev_set_window (ResultWindow)dev_clear_window ()endforelse
* No canditates found没有找到候选对象
*如果没有找到2d数据编码符号的候选:if (CandidateNum == 0)dev_set_window (ParamWindow)dev_clear_window ()disp_message (ParamWindow, 'No candidate found', 'window', 12, 12, 'black', 'true')disp_continue_message (ResultWindow, 'black', 'true')stop ()endif
* Aborted Search 搜索失败
* 获取有关搜索的当前参数值的信息,以调查为什么搜索被中止。if (AbortedNum > 0)dev_set_window (ImageWindow)dev_clear_window ()dev_set_part (Row1, Col1, Row2, Col2)dev_display (Image)Message := 'Example ' + (Index + 1) + ' of ' + |ImageFiles|Message[1] := 'Search aborted: Invalid candidates'disp_message (ImageWindow, Message, 'window', 12, 12, 'black', 'true')* 获得所有无效候选的句柄get_data_code_2d_results (DataCodeHandle, 'all_aborted', 'handle', HandlesAborted)* 在超过3个候选的情况下显示“前3个无效候选是...”if (|HandlesAborted| > 3)disp_message (ImageWindow, 'The first 3 invalid candidates are ...', 'window', 60, 12, 'black', 'true')endif*更新高亮参数dev_set_window (ParamWindow)dev_clear_window ()disp_message (ParamWindow, 'Highlighted parameters are shown on the right', 'window', 12, 12, 'black', 'true')* Iconic parameter names:disp_message (ParamWindow, 'Parameters for iconic objects:', 'window', 50, 12, 'black', 'true')disp_message (ParamWindow, GenObjectNames, 'window', 80, 12, ColorIcon, 'false')* Alphanumeric parameter names: disp_message (ParamWindow, 'Parameters for alphanumeric results:', 'window', 190, 12, 'black', 'true')ColorAlph[6] := 'white'disp_message (ParamWindow, GenParamName[0:18], 'window', 220, 12, ColorAlph, 'false')ColorAlph[6] := '#808080'
disp_message (ParamWindow, GenParamName[19:37], 'window', 220, 260, ColorAlph, 'false')stop ()
* Query information for each invalid candidate. *查询每个无效候选的信息。for J := 0 to |HandlesAborted| - 1 by 1* Stepwise only for the first candidates.if (J < 3)dev_set_window (ImageWindow)dev_clear_window ()dev_set_part (Row1, Col1, Row2, Col2)dev_display (Image)Message := 'Example ' + (Index + 1) + ' of ' + |ImageFiles| + ' Invalid candidate: ' + (J + 1) + ' of ' + |HandlesAborted|disp_message (ImageWindow, Message, 'window', 12, 12, 'black', 'true')* 获取并显示候选对象的XLD轮廓线。get_data_code_2d_objects (DataCodeObject, DataCodeHandle, HandlesAborted[J], 'candidate_xld')dev_set_color ('yellow')dev_display (DataCodeObject)dev_set_color ('green')*获取并显示搜索的状态。get_data_code_2d_results (DataCodeHandle, HandlesAborted[J], 'status', StatusValue)Substrings := regexp_replace(StatusValue,'(: )',':\n')disp_message (ResultWindow, 'Alphanumeric results about the symbol:', 'window', 12, 12, 'black', 'false')disp_message (ResultWindow, 'status:\n\'' + Substrings + '\'', 'window', 40, 12, 'black', 'false')* 更新高亮参数* Alphanumeric parameter names:ColorIcon[0] := 'yellow'ColorAlph[17] := 'white'disp_message (ParamWindow, GenObjectNames, 'window', 80, 12, ColorIcon, 'false')disp_message (ParamWindow, GenParamName[0:18], 'window', 220, 12, ColorAlph, 'false')ColorIcon[0] := '#808080'ColorAlph[17] := '#808080'disp_continue_message (ResultWindow, 'black', 'true')stop ()* Clear the ResultWindow.dev_set_window (ResultWindow)dev_clear_window ()endifendfor
* In case of more than 3 candidates display all of them.
* 如果超过3个候选项,则显示所有候选项。if (|HandlesAborted| > 3)* Display all invalid candidates.显示所有无效候选项。dev_set_window (ImageWindow)dev_clear_window ()dev_set_colored (12)dev_display (Image)get_data_code_2d_objects (DataCodeObjects, DataCodeHandle, 'all_aborted','candidate_xld')dev_display (DataCodeObjects)disp_message (ImageWindow, 'Example ' + (Index + 1) + ' of ' + |ImageFiles|, 'window', 12, 12, 'black', 'true')disp_message (ImageWindow, 'All ' + |HandlesAborted| + ' invalid candidates', 'window', 40, 12, 'black', 'true')*更新最后一个参数的高亮显示。disp_message (ParamWindow, GenParamName[0:18], 'window', 220, 12, ColorAlph, 'false')if (Index < 4)disp_continue_message (ResultWindow, 'black', 'true')stop ()endifendif
这篇关于HALCON例程:2d_data_codes_data_access.hdev的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!