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

此示例程序演示如何读取符号类型为“Micro QR Code”的二维数据代码。在第一步中,创建一个数据代码模型。在下一步中,将读取数据代码,并在图形窗口中显示结果。

请注意,有些符号无法使用标准默认参数找到。原因可能是:
-对比度太低-一些符号是明暗打印的,标准型号没有覆盖
-符号太大(>48x48模块)-模块打印为未连接的小点


总代码:

*初始化图像路径和视觉设置
dev_update_off ()
dev_close_window ()
*图片路径定义
ImageFiles := 'datacode/micro_qr/micro_qr_board_'
*变量定义
ImageNum := 6

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

*打开自适应图片的窗口
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_set_line_width (3)
dev_set_color ('green')
* 
* Display short description
*显示简短描述
Message := 'This simple program demonstrates how to'
Message[1] := 'read Micro QR data codes in a sequence'
Message[2] := 'of images with the standard default'
Message[3] := 'parameter setting.'

*显示信息--这个简单的程序演示了如何使用标准默认参数设置读取一系列图像中的微QR数据码
disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()
* 
* Step 1: Create data code model
* ----------------------------------------------------
* Create a 2d data code model of the 2d data code class
* 'QR Code'. 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数据代码模型的句柄,
*该句柄可用于对数据代码的所有后续操作。
create_data_code_2d_model ('Micro QR Code', [], [], DataCodeHandle)
* 
* Step 2: Read the data codes
* ----------------------------------------------------
* Search and read the data codes in each image and
* display the decoded string for each found data code
*2.读取数据代码
*搜索并读取每个图像中的数据代码
*显示每个找到的数据代码的解码字符串
for Index := 1 to ImageNum by 1
    read_image (Image, ImageFiles + Index$'.2d')
    find_data_code_2d (Image, SymbolXLDs, DataCodeHandle, [], [], ResultHandles, DecodedDataStrings)
    * 
    * Display the results
    *显示结果
    dev_display (Image)
    dev_display (SymbolXLDs)
    disp_message (WindowHandle, 'Image ' + Index + ' of ' + ImageNum, 'window', 12, 12, 'black', 'true')
    disp_message (WindowHandle, DecodedDataStrings, 'window', 40, 12, 'black', 'true')
    * 
    * If no data code could be found
    *如果找不到数据代码
    if (|DecodedDataStrings| == 0)
        disp_message (WindowHandle, 'No data code found.\nPlease adjust the parameters.', 'window', 40, 12, 'red', 'true')
    endif
    * 
    * Deactivate the following lines to run the program without breaks
    *停用以下几行以不间断地运行程序
    if (Index < ImageNum)
        disp_continue_message (WindowHandle, 'black', 'true')
        stop ()
    endif
endfor
* 
* Clear the 2d data code model
*释放二维码阅读器分配的内存
clear_data_code_2d_model (DataCodeHandle)

逐段分析:

*初始化图像路径和视觉设置
dev_update_off ()
dev_close_window ()
*图片路径定义
ImageFiles := 'datacode/micro_qr/micro_qr_board_'
*变量定义
ImageNum := 6

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

*打开自适应图片的窗口
dev_open_window_fit_image (Image, 0, 0, -1, -1, WindowHandle)
set_display_font (WindowHandle, 16, 'mono', 'true', 'false')
dev_set_line_width (3)
dev_set_color ('green')

*显示简短描述
Message := 'This simple program demonstrates how to'
Message[1] := 'read Micro QR data codes in a sequence'
Message[2] := 'of images with the standard default'
Message[3] := 'parameter setting.'

*显示信息--这个简单的程序演示了如何使用标准默认参数设置读取一系列图像中的微QR数据码
disp_message (WindowHandle, Message, 'window', 12, 12, 'black', 'true')
disp_continue_message (WindowHandle, 'black', 'true')
stop ()

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

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

*寻找二维码  
find_data_code_2d (Image, SymbolXLDs, DataCodeHandle, [], [], ResultHandles, DecodedDataStrings)

    *显示结果
    dev_display (Image)
    dev_display (SymbolXLDs)
    
    *显示信息
    disp_message (WindowHandle, 'Image ' + Index + ' of ' + ImageNum, 'window', 12, 12, 'black', 'true')

    *显示读取出来的结果
    disp_message (WindowHandle, DecodedDataStrings, 'window', 40, 12, 'black', 'true')

    *如果找不到数据代码
    if (|DecodedDataStrings| == 0)
        disp_message (WindowHandle, 'No data code found.\nPlease adjust the parameters.', 'window', 40, 12, 'red', 'true')
    endif
    * 
    * 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)

 

  • 1
    点赞
  • 5
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
在C#中使用Halcon实现二维码识别可以通过以下步骤进行: 1. 首先,确保你已经安装了Halcon的开发环境,并且在C#项目中引入了Halcon的相关库。 2. 创建一个Halcon的图像对象,将待识别的图像加载到该对象中。 3. 使用Halcon提供的二维码识别函数对图像进行处理并得到识别结果。例如,可以使用`find_bar_code`函数来查找二维码,并使用`decode_bar_code_2d`函数对二维码进行解码。 4. 获取识别结果并进行后续处理。你可以将识别到的二维码信息显示在界面上或者进行其他操作,如数据存储、网络传输等。 以下是一个简单的示例代码,演示了如何在C#中使用Halcon实现二维码识别: ```csharp using HalconDotNet; class Program { static void Main(string[] args) { // 加载Halcon引擎 HOperatorSet.SetSystem("border_shape_models", "true"); // 创建Halcon图像对象 HObject image = new HObject(); // 从文件中加载待识别的图像 HOperatorSet.ReadImage(out image, "path_to_your_image"); // 定义变量用于存储识别结果 HTuple decodedDataStrings = new HTuple(); // 进行二维码识别 HOperatorSet.FindBarCode(image, out image, new HTuple("QR_CODE"), out decodedDataStrings); // 打印识别结果 Console.WriteLine("QR Code: " + decodedDataStrings.ToString()); // 释放资源 image.Dispose(); // 关闭Halcon引擎 HOperatorSet.CloseAll(); } } ``` 请注意,以上代码只是一个简单的示例,实际应用中可能需要根据具体情况进行适当的参数调整和异常处理。另外,你需要替换代码中的图像路径为你自己的图像路径。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值