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

该示例程序演示了如何为一类ECC 200数据矩阵符号训练2d数据模型,以及如何在训练前后获得模型参数值;

此外,还展示了如何将经过训练的数据代码模型写入文件,以及如何读取保存的数据代码模型,然后使用该模型在一系列图像中查找所有ECC200符号。


总代码:

*初始化图像路径和视觉设置

dev_update_off ()
dev_close_window ()
*图片路径定义
ImageFiles := 'datacode/ecc200/ecc200_cpu_0'
*变量定义
ImageNum := 16

*读取图片
read_image (Image, ImageFiles + '07')
*打开自适应图片的窗口
dev_open_window (0, 0, 760, 570, 'black', WindowHandle)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
dev_set_line_width (3)
dev_set_color ('green')
* 
* Display short description
* This program demonstrates the training of a 2d data code model
* and how to write the trained model into a file and also how to
* read the saved model from the file. The model is then used
* to find data codes in a sequence of images.
*显示简短描述


Message := 'This program demonstrates the training of a 2d data code'
Message[1] := 'model, how to write the trained model into a file, and'
Message[2] := 'also how to read the saved model from the file. The'
Message[3] := 'model is then used to find data codes in a sequence of'
Message[4] := 'images.'
Message[5] := 'Furthermore it is shown how to obtain the values of the'
Message[6] := 'model parameters before and after the training.'
*显示信息--该程序演示了二维数据代码模型的训练,以及如何将训练后的模型写入文件,
*以及如何从文件中读取保存的模型。然后使用该模型在一系列图像中查找数据代码。
disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* 
* Step 1: Create a 2d data code model
* -------------------------------------------------------
* Create a 2d data code model of the 2d data code class
* 'Data Matrix ECC 200'. The operator returns a handle to
* the 2d data code model which can be used for all further
* operations on the data code.
*----------
*步骤1:创建二维数据代码模型
*创建2d数据代码类“data Matrix ECC 200”的2d数据代码模型。
*运算符返回2d数据代码模型的句柄,该句柄可用于对数据代码的所有后续操作。
create_data_code_2d_model ('Data Matrix ECC 200', [], [], DataCodeHandle)
* 
* Get model parameters before the training
*在训练前获取模型参数
* -------------------------------------------------------
*为给定的二维数据代码模型获取可在其他二维数据代码操作符中使用的通用参数或对象的名称
query_data_code_2d_params (DataCodeHandle, 'get_model_params', GenParamName)
get_data_code_2d_param (DataCodeHandle, GenParamName, ModelBeforeTraining)
* 
* Step 2: Train the model
* -------------------------------------------------------
* Train the model on a dark and a bright image
* 
* Dark image
*第二步:训练模型
*在黑暗和明亮的图像上训练模型
*暗影
*读取图片
read_image (Image, 'datacode/ecc200/ecc200_cpu_007')
*寻找二维码
find_data_code_2d (Image, SymbolXLDs, DataCodeHandle, 'train', 'all', ResultHandles, DecodedDataStrings)
dev_display (Image)
dev_display (SymbolXLDs)
TitleMessage := 'Train on a dark image'
*显示数据
display_found_data_codes (SymbolXLDs, WindowHandle, DecodedDataStrings, TitleMessage, [], 'forest green', 'black')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* 
* Bright image
*明亮的图片
*读取图片
read_image (Image, 'datacode/ecc200/ecc200_cpu_008')
*寻找二维码
find_data_code_2d (Image, SymbolXLDs, DataCodeHandle, 'train', 'all', ResultHandles, DecodedDataStrings)
dev_display (Image)
dev_display (SymbolXLDs)
TitleMessage := 'Train on a bright image'
*显示数据
display_found_data_codes (SymbolXLDs, WindowHandle, DecodedDataStrings, TitleMessage, [], 'forest green', 'black')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* 
* Get model parameters after the training
*训练后获取模型参数
* -------------------------------------------------------
get_data_code_2d_param (DataCodeHandle, GenParamName, ModelAfterTraining)
* 
* Display the model parameters
*显示模型参数

*清除窗口 
dev_clear_window ()
ModelAdaption := (GenParamName + ':')$'-35' + ModelBeforeTraining$'15' + ' -> ' + ModelAfterTraining
*显示信息--训练前后的模型参数
disp_message (WindowHandle, 'Model parameters before and after the training:', 'window', 12, 12, 'white', 'false')
disp_message (WindowHandle, ModelAdaption, 'window', 60, 12, 'white', 'false')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* 
* Step 3: Write the model into a file
* --------------------------------------------------------
* Save the model in a file so that it can be used later
*第3步:将模型写入文件
*将模型保存在文件中,以便以后使用
write_data_code_2d_model (DataCodeHandle, '2d_data_code_model.dcm')
* 
* Clear the 2d data code model
*清除二维数据代码模型
clear_data_code_2d_model (DataCodeHandle)
* 
* Display message about saved file and next step
*显示有关已保存文件和下一步的消息
dev_clear_window ()
Message := 'The data code model is saved in \'2d_data_code_model.dcm\'.'
Message[1] := ' '
Message[2] := 'In the next step the saved model is read and used to'
Message[3] := 'find data codes in a sequenz of images.'
*显示信息--数据代码模型保存在'2d_data_code_model.dcm\'中*
*在下一步中,将读取保存的模型,并用于在一系列图像中查找数据代码。
disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* 
* Step 4: Read an existing 2d data code model
* ----------------------------------------------------------
* Read the previously saved data code model
*步骤4:读取现有的二维数据代码模型
*读取以前保存的数据代码模型
read_data_code_2d_model ('2d_data_code_model.dcm', DataCodeHandle)
* 
* Step 5: Read the data codes
* -------------------------------------------------------
* Search and read the data codes in each image and
* display the decoded string for each found data code
*5.读取数据代码
*搜索并读取每个图像中的数据代码
*显示每个找到的数据代码的解码字符串

for Index := 7 to 16 by 1
    *读取图片
    read_image (Image, ImageFiles + Index$'.2d')
    dev_display (Image)
    * 
    * Find and decode the data codes and measure the runtime
    *找到并解码数据代码,测量运行时间
    count_seconds (T1)
    *寻找二维码
    find_data_code_2d (Image, SymbolXLDs, DataCodeHandle, [], [], ResultHandles, DecodedDataStrings)
    count_seconds (T2)
    Time := 1000 * (T2 - T1)
    * 
    * Display the results
    *显示结果
    TitleMessage := 'Image ' + (Index - 6) + ' of ' + (ImageNum - 6)
    ResultMessage := 'Data code found in ' + Time$'.1f' + ' ms'
    *显示结果
    display_found_data_codes (SymbolXLDs, WindowHandle, DecodedDataStrings, TitleMessage, ResultMessage, 'forest green', 'black')
    * 
    * Deactivate the following lines to run the program without breaks
    *停用以下几行以不间断地运行程序
    if (Index < ImageNum)
        disp_continue_message (WindowHandle, 'black', 'true')
        stop ()
    endif
endfor
* 
*清除二维数据代码模型
clear_data_code_2d_model (DataCodeHandle)

逐段分析:

*初始化图像路径和视觉设置
dev_update_off ()
dev_close_window ()

*图片路径定义
ImageFiles := 'datacode/ecc200/ecc200_cpu_0'

*变量定义
ImageNum := 16

*读取图片
read_image (Image, ImageFiles + '07')

*打开自适应图片的窗口
dev_open_window (0, 0, 760, 570, 'black', WindowHandle)
set_display_font (WindowHandle, 14, 'mono', 'true', 'false')
dev_set_line_width (3)
dev_set_color ('green')

*显示简短描述
Message := 'This program demonstrates the training of a 2d data code'
Message[1] := 'model, how to write the trained model into a file, and'
Message[2] := 'also how to read the saved model from the file. The'
Message[3] := 'model is then used to find data codes in a sequence of'
Message[4] := 'images.'
Message[5] := 'Furthermore it is shown how to obtain the values of the'
Message[6] := 'model parameters before and after the training.'

*显示信息--该程序演示了二维数据代码模型的训练,以及如何将训练后的模型写入文件,
*以及如何从文件中读取保存的模型。然后使用该模型在一系列图像中查找数据代码。
disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()

基于Halcon学习的二维码识别【十一】ecc200_training.hdev_第1张图片

*步骤1:创建二维数据代码模型
*创建2d数据代码类“data Matrix ECC 200”的2d数据代码模型。
*运算符返回2d数据代码模型的句柄,该句柄可用于对数据代码的所有后续操作。
create_data_code_2d_model ('Data Matrix ECC 200', [], [], DataCodeHandle)

*在训练前获取模型参数
*为给定的二维数据代码模型获取可在其他二维数据代码操作符中使用的通用参数或对象的名称
query_data_code_2d_params (DataCodeHandle, 'get_model_params', GenParamName)
get_data_code_2d_param (DataCodeHandle, GenParamName, ModelBeforeTraining)

*第二步:训练模型
*在黑暗和明亮的图像上训练模型
*暗影
*读取图片
read_image (Image, 'datacode/ecc200/ecc200_cpu_007')

基于Halcon学习的二维码识别【十一】ecc200_training.hdev_第2张图片

*寻找二维码
find_data_code_2d (Image, SymbolXLDs, DataCodeHandle, 'train', 'all', ResultHandles, DecodedDataStrings)
dev_display (Image)
dev_display (SymbolXLDs)
TitleMessage := 'Train on a dark image'

*显示数据
display_found_data_codes (SymbolXLDs, WindowHandle, DecodedDataStrings, TitleMessage, [], 'forest green', 'black')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()

基于Halcon学习的二维码识别【十一】ecc200_training.hdev_第3张图片

*明亮的图片
*读取图片
read_image (Image, 'datacode/ecc200/ecc200_cpu_008')

基于Halcon学习的二维码识别【十一】ecc200_training.hdev_第4张图片

*寻找二维码
find_data_code_2d (Image, SymbolXLDs, DataCodeHandle, 'train', 'all', ResultHandles, DecodedDataStrings)
dev_display (Image)
dev_display (SymbolXLDs)
TitleMessage := 'Train on a bright image'

*显示数据
display_found_data_codes (SymbolXLDs, WindowHandle, DecodedDataStrings, TitleMessage, [], 'forest green', 'black')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()

基于Halcon学习的二维码识别【十一】ecc200_training.hdev_第5张图片

*训练后获取模型参数
get_data_code_2d_param (DataCodeHandle, GenParamName, ModelAfterTraining)

*显示模型参数
*清除窗口 
dev_clear_window ()
ModelAdaption := (GenParamName + ':')$'-35' + ModelBeforeTraining$'15' + ' -> ' + ModelAfterTraining

*显示信息--训练前后的模型参数
disp_message (WindowHandle, 'Model parameters before and after the training:', 'window', 12, 12, 'white', 'false')
disp_message (WindowHandle, ModelAdaption, 'window', 60, 12, 'white', 'false')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()

基于Halcon学习的二维码识别【十一】ecc200_training.hdev_第6张图片

*第3步:将模型写入文件
*将模型保存在文件中,以便以后使用
write_data_code_2d_model (DataCodeHandle, '2d_data_code_model.dcm')

*清除二维数据代码模型
clear_data_code_2d_model (DataCodeHandle)

*显示有关已保存文件和下一步的消息
dev_clear_window ()
Message := 'The data code model is saved in \'2d_data_code_model.dcm\'.'
Message[1] := ' '
Message[2] := 'In the next step the saved model is read and used to'
Message[3] := 'find data codes in a sequenz of images.'

*显示信息--数据代码模型保存在'2d_data_code_model.dcm\'中*
*在下一步中,将读取保存的模型,并用于在一系列图像中查找数据代码。
disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()

基于Halcon学习的二维码识别【十一】ecc200_training.hdev_第7张图片

*步骤4:读取现有的二维数据代码模型
*读取以前保存的数据代码模型
read_data_code_2d_model ('2d_data_code_model.dcm', DataCodeHandle)

*5.读取数据代码
*搜索并读取每个图像中的数据代码
*显示每个找到的数据代码的解码字符串
for Index := 7 to 16 by 1
    *读取图片
    read_image (Image, ImageFiles + Index$'.2d')
    dev_display (Image)

基于Halcon学习的二维码识别【十一】ecc200_training.hdev_第8张图片

*找到并解码数据代码,测量运行时间
    count_seconds (T1)

    *寻找二维码
    find_data_code_2d (Image, SymbolXLDs, DataCodeHandle, [], [], ResultHandles, DecodedDataStrings)
    count_seconds (T2)
    Time := 1000 * (T2 - T1)

    *显示结果
    TitleMessage := 'Image ' + (Index - 6) + ' of ' + (ImageNum - 6)
    ResultMessage := 'Data code found in ' + Time$'.1f' + ' ms'

    *显示结果
    display_found_data_codes (SymbolXLDs, WindowHandle, DecodedDataStrings, TitleMessage, ResultMessage, 'forest green', 'black')

    *停用以下几行以不间断地运行程序
    if (Index < ImageNum)
        disp_continue_message (WindowHandle, 'black', 'true')
        stop ()
    endif
endfor

基于Halcon学习的二维码识别【十一】ecc200_training.hdev_第9张图片

*清除二维数据代码模型
clear_data_code_2d_model (DataCodeHandle)

基于Halcon学习的二维码识别【十一】ecc200_training.hdev_第10张图片

基于Halcon学习的二维码识别【十一】ecc200_training.hdev_第11张图片

基于Halcon学习的二维码识别【十一】ecc200_training.hdev_第12张图片


主要算子分析:

query_data_code_2d_params( : : DataCodeHandle, QueryName : GenParamName)

功能:为给定的二维码模型获取可在其他二维码操作符中使用的通用参数或对象的名称

DataCodeHandle:二维数据代码模型的句柄

QueryName :参数组的名称

GenParamName:包含受支持的泛型参数名称的列表

get_data_code_2d_param( : : DataCodeHandle, GenParamName : GenParamValue)

功能:获取一个或多个描述二维码模型的参数。所需参数的名称在通用参数GenParamName中传递,相应的值在GenParamValue中返回。

DataCodeHandle:二维数据代码模型的句柄

GenParamName:要为二维数据代码模型查询的常规参数的名称

GenParamValue:通用参数的值

你可能感兴趣的:(Halcon学习笔记,#,二维码识别,Halcon,机器视觉,图像处理,算法,二维码识别)