1.下载安装halcon

通过网盘分享的文件:halcon-25.05.0.0-x64-win64
链接: https://pan.baidu.com/s/1XAx-8ZQM-ZHkgHIc-dhCYw

提取码: whek

2.c#环境配置

创建test_halcon_ocr项目

找到halcon的安装路径

我的:
D:\halcon\HALCON-25.05-Progress\bin\x64-win64
D:\halcon\HALCON-25.05-Progress\bin\dotnet35

添加引用("D:\halcon\HALCON-25.05-Progress\bin\dotnet35\halcondotnet.dll")

把halcondotnet.dll拖到工具箱中。

屏幕录制 2025-08-12 103356

添加HWindowcontrol ,button 和 textbox

设置button1 点击事件

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.IO;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;
using HalconDotNet;namespace test_halcon_ocr
{public partial class Form1 : Form{HObject TestImage;string Path_testimg = "C:\\Users\\86957\\Desktop\\img_ocr_0296.png";public Form1(){InitializeComponent();}private void button1_Click(object sender, EventArgs e){//读取文件HOperatorSet.ReadImage(out TestImage, Path_testimg);//获取图片大小HTuple Width, Height;HOperatorSet.GetImageSize(TestImage, out Width, out Height);//设置图片显示范围 hWindowControl1.HalconWindow获得窗体句柄HOperatorSet.SetPart(hWindowControl1.HalconWindow, 0, 0, (Height - 1), (Width - 1)); //0,0, (Height - 1), (Width - 1)代表从0,0坐标到图片的宽高坐标,达到全屏显示//将图片投射到窗体上HOperatorSet.DispObj(TestImage, hWindowControl1.HalconWindow);//将TestImage变量投射到句柄窗口上}private void button2_Click(object sender, EventArgs e){}}
}

运行,

成功报错。

解决:
项目右键属性,找到生成,取消勾选首选32位。

再次运行,

成功。

3. ocr 字符识别

  1. CreateDeepOcr函数:这个函数负责创建深度OCR处理器。使用HOperatorSet.CreateDeepOcr方法,传入参数"mode"和"recognition",生成一个OCR句柄。然后调用set_suitable_device_in_ocr_handle来设置合适的运行设备(如GPU或CPU),并设置识别图像的宽度参数。这个函数的作用是初始化OCR模型,准备后续的识别操作。

  2. set_suitable_device_in_ocr_handle函数:该函数用于配置OCR处理器使用的计算设备。首先查询可用的深度学习设备(GPU和CPU),优先选择GPU。然后设置识别图像的宽度参数,确保在处理时不会因为内存问题出错。这里涉及到异常处理,确保在设备不可用时能够回退到其他设备,并在最后恢复默认的图像宽度设置。

  3. DeepLearningOCR函数:这是实际的OCR识别函数。它接收图像、ROI坐标、窗口句柄和OCR处理器句柄作为参数。处理步骤包括图像预处理(均值滤波和强调处理)、裁剪ROI区域、应用OCR识别,并将结果返回。识别过程中使用Halcon的ApplyDeepOcr方法,并从结果中提取识别的文本。

运行后,

4.完整代码:


 

using System;
using System.IO;
using System.Windows.Forms;
using HalconDotNet;
namespace test_halcon_ocr
{public partial class Form1 : Form{HObject TestImage;HTuple hv_DeepOcrHandle = new HTuple();string Path_testimg = "C:\\Users\\86957\\Desktop\\img_ocr_0296.png";public Form1(){InitializeComponent();}//自动选择最佳计算设备(GPU优先),优化模型运行效率static void set_suitable_device_in_ocr_handle(HTuple hv_DeepOcrHandle){// Local control variables HTuple hv_DLDeviceHandles = new HTuple(), hv_RecognitionImageWidthDefault = new HTuple();HTuple hv_Exception = new HTuple(), hv_Index = new HTuple();// Initialize local and output iconic variables try{//Determine deep learning device to work with (prefer GPU over CPU).hv_DLDeviceHandles.Dispose();HOperatorSet.QueryAvailableDlDevices((new HTuple("runtime")).TupleConcat("runtime"),(new HTuple("gpu")).TupleConcat("cpu"), out hv_DLDeviceHandles);if ((int)(new HTuple((new HTuple(hv_DLDeviceHandles.TupleLength())).TupleEqual(0))) != 0){throw new HalconException("No supported device found to continue this example.");}//Set recognition_image_width larger for the example to work without memory problems.try{hv_RecognitionImageWidthDefault.Dispose();HOperatorSet.GetDeepOcrParam(hv_DeepOcrHandle, "recognition_image_width",out hv_RecognitionImageWidthDefault);HOperatorSet.SetDeepOcrParam(hv_DeepOcrHandle, "recognition_image_width",260);}// catch (Exception) catch (HalconException HDevExpDefaultException1){HDevExpDefaultException1.ToHTuple(out hv_Exception);}for (hv_Index = 0; (int)hv_Index <= (int)((new HTuple(hv_DLDeviceHandles.TupleLength())) - 1); hv_Index = (int)hv_Index + 1){try{using (HDevDisposeHelper dh = new HDevDisposeHelper()){HOperatorSet.SetDeepOcrParam(hv_DeepOcrHandle, "device", hv_DLDeviceHandles.TupleSelect(hv_Index));}break;}// catch (Exception) catch (HalconException HDevExpDefaultException1){HDevExpDefaultException1.ToHTuple(out hv_Exception);if ((int)(new HTuple(hv_Index.TupleEqual((new HTuple(hv_DLDeviceHandles.TupleLength())) - 1))) != 0){throw new HalconException("Could not set any of the supported devices to continue this example.");}}}//Reset recognition_image_width to the default value.try{HOperatorSet.SetDeepOcrParam(hv_DeepOcrHandle, "recognition_image_width",hv_RecognitionImageWidthDefault);}// catch (Exception) catch (HalconException HDevExpDefaultException1){HDevExpDefaultException1.ToHTuple(out hv_Exception);}hv_DLDeviceHandles.Dispose();hv_RecognitionImageWidthDefault.Dispose();hv_Exception.Dispose();hv_Index.Dispose();return;}catch (HalconException HDevExpDefaultException){hv_DLDeviceHandles.Dispose();hv_RecognitionImageWidthDefault.Dispose();hv_Exception.Dispose();hv_Index.Dispose();throw HDevExpDefaultException;}}//执行完整的OCR识别流程,包含预处理、ROI裁剪、模型推理和结果解析public static string DeepLearningOCR(HObject ho_ImageSource, int row1, int col1, int row2, int col2, HWindow Window, HTuple hv_DeepOcrHandle){// Local iconic variables HObject ho_ImageMean, ho_Image;HObject ho_ImageWord, ho_Rectangle;// Local control variables HTuple hv_Width = new HTuple();HTuple hv_Height = new HTuple(), hv_DeepOcrResult = new HTuple();HTuple hv_RecognizedWord = new HTuple();// Initialize local and output iconic variables //HOperatorSet.GenEmptyObj(out ho_ImageSource);HOperatorSet.GenEmptyObj(out ho_ImageMean);HOperatorSet.GenEmptyObj(out ho_Image);HOperatorSet.GenEmptyObj(out ho_ImageWord);HOperatorSet.GenEmptyObj(out ho_Rectangle);try{ho_ImageMean.Dispose();HOperatorSet.MeanImage(ho_ImageSource, out ho_ImageMean, 3, 3);// 图像预处理ho_Image.Dispose();HOperatorSet.Emphasize(ho_ImageMean, out ho_Image, 9, 9, 1);hv_Width.Dispose(); hv_Height.Dispose();HOperatorSet.GetImageSize(ho_ImageSource, out hv_Width, out hv_Height);////Crop the word to be recognized by the recognition component.ho_ImageWord.Dispose();HOperatorSet.CropRectangle1(ho_Image, out ho_ImageWord, row1, col1, row2, col2);// ROI裁剪ho_Rectangle.Dispose();HOperatorSet.GenRectangle1(out ho_Rectangle, row1, col1, row2, col2);////Apply recognition with default recognition_image_width.hv_DeepOcrResult.Dispose();HOperatorSet.ApplyDeepOcr(ho_ImageWord, hv_DeepOcrHandle, "recognition", out hv_DeepOcrResult);  // 执行OCR识别hv_RecognizedWord.Dispose();HOperatorSet.GetDictTuple(hv_DeepOcrResult, "word", out hv_RecognizedWord);ho_Image.DispObj(Window);Window.SetDraw("margin");Window.SetColor("red");Window.SetPart(0, 0, (int)(hv_Height - 1), (int)(hv_Width - 1));ho_Rectangle.DispObj(Window);}catch (HalconException HDevExpDefaultException){ho_ImageSource.Dispose();ho_ImageMean.Dispose();ho_Image.Dispose();ho_ImageWord.Dispose();ho_Rectangle.Dispose();// hv_DeepOcrHandle.Dispose();hv_Width.Dispose();hv_Height.Dispose();hv_DeepOcrResult.Dispose();hv_RecognizedWord.Dispose();throw HDevExpDefaultException;}ho_ImageSource.Dispose();ho_ImageMean.Dispose();ho_Image.Dispose();ho_ImageWord.Dispose();ho_Rectangle.Dispose();//  hv_DeepOcrHandle.Dispose();hv_Width.Dispose();hv_Height.Dispose();hv_DeepOcrResult.Dispose();hv_RecognizedWord.Dispose();return hv_RecognizedWord.S;}//初始化深度学习OCR模型,创建识别器实例并配置基本参数public static HTuple CreateDeepOcr(){HTuple hv_DeepOcrHandle = new HTuple();hv_DeepOcrHandle.Dispose();HOperatorSet.CreateDeepOcr("mode", "recognition", out hv_DeepOcrHandle);set_suitable_device_in_ocr_handle(hv_DeepOcrHandle);HOperatorSet.SetDeepOcrParam(hv_DeepOcrHandle, "recognition_image_width", 260);return hv_DeepOcrHandle;}private void button1_Click(object sender, EventArgs e)//显示图像{//读取文件HOperatorSet.ReadImage(out TestImage, Path_testimg);//获取图片大小HTuple Width, Height;HOperatorSet.GetImageSize(TestImage, out Width, out Height);//设置图片显示范围 hWindowControl1.HalconWindow获得窗体句柄HOperatorSet.SetPart(hWindowControl1.HalconWindow, 0, 0, (Height - 1), (Width - 1)); //0,0, (Height - 1), (Width - 1)代表从0,0坐标到图片的宽高坐标,达到全屏显示//将图片投射到窗体上HOperatorSet.DispObj(TestImage, hWindowControl1.HalconWindow);//将TestImage变量投射到句柄窗口上}private void button2_Click(object sender, EventArgs e)//字符识别{textBox1.AppendText("开始检测:" + "\r\n");try{hv_DeepOcrHandle = CreateDeepOcr();}catch{MessageBox.Show("error");}if (!File.Exists(Path_testimg)){textBox1.AppendText($"文件不存在: {Path_testimg}" + "\r\n");return;}HalconDotNet.HImage hImage = new HalconDotNet.HImage(Path_testimg);HTuple width, height;hImage.GetImageSize(out width, out height);int intWidth = width.TupleInt();int intHeight = height.TupleInt();textBox1.AppendText("选框坐标为:" + "\r\n");textBox1.AppendText(intWidth.ToString());textBox1.AppendText(width.ToString() + "\r\n");string code = DeepLearningOCR(hImage, 0, 0, intHeight, intWidth, hWindowControl1.HalconWindow, hv_DeepOcrHandle);textBox1.AppendText("code:" + code + "\r\n");}}
}

namespace test_halcon_ocr
{partial class Form1{/// <summary>/// 必需的设计器变量。/// </summary>private System.ComponentModel.IContainer components = null;/// <summary>/// 清理所有正在使用的资源。/// </summary>/// <param name="disposing">如果应释放托管资源,为 true;否则为 false。</param>protected override void Dispose(bool disposing){if (disposing && (components != null)){components.Dispose();}base.Dispose(disposing);}#region Windows 窗体设计器生成的代码/// <summary>/// 设计器支持所需的方法 - 不要修改/// 使用代码编辑器修改此方法的内容。/// </summary>private void InitializeComponent(){this.hWindowControl1 = new HalconDotNet.HWindowControl();this.button1 = new System.Windows.Forms.Button();this.button2 = new System.Windows.Forms.Button();this.textBox1 = new System.Windows.Forms.TextBox();this.SuspendLayout();// // hWindowControl1// this.hWindowControl1.BackColor = System.Drawing.Color.Black;this.hWindowControl1.BorderColor = System.Drawing.Color.Black;this.hWindowControl1.ImagePart = new System.Drawing.Rectangle(0, 0, 640, 480);this.hWindowControl1.Location = new System.Drawing.Point(96, 116);this.hWindowControl1.Name = "hWindowControl1";this.hWindowControl1.Size = new System.Drawing.Size(525, 338);this.hWindowControl1.TabIndex = 0;this.hWindowControl1.WindowSize = new System.Drawing.Size(525, 338);// // button1// this.button1.Location = new System.Drawing.Point(174, 567);this.button1.Name = "button1";this.button1.Size = new System.Drawing.Size(288, 117);this.button1.TabIndex = 1;this.button1.Text = "button1";this.button1.UseVisualStyleBackColor = true;this.button1.Click += new System.EventHandler(this.button1_Click);// // button2// this.button2.Location = new System.Drawing.Point(662, 567);this.button2.Name = "button2";this.button2.Size = new System.Drawing.Size(242, 117);this.button2.TabIndex = 2;this.button2.Text = "button2";this.button2.UseVisualStyleBackColor = true;this.button2.Click += new System.EventHandler(this.button2_Click);// // textBox1// this.textBox1.Location = new System.Drawing.Point(830, 189);this.textBox1.Multiline = true;this.textBox1.Name = "textBox1";this.textBox1.Size = new System.Drawing.Size(317, 254);this.textBox1.TabIndex = 3;// // Form1// this.AutoScaleDimensions = new System.Drawing.SizeF(9F, 18F);this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;this.ClientSize = new System.Drawing.Size(1269, 736);this.Controls.Add(this.textBox1);this.Controls.Add(this.button2);this.Controls.Add(this.button1);this.Controls.Add(this.hWindowControl1);this.Name = "Form1";this.Text = "Form1";this.ResumeLayout(false);this.PerformLayout();}#endregionprivate HalconDotNet.HWindowControl hWindowControl1;private System.Windows.Forms.Button button1;private System.Windows.Forms.Button button2;private System.Windows.Forms.TextBox textBox1;}
}

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。
如若转载,请注明出处:http://www.pswp.cn/diannao/95245.shtml
繁体地址,请注明出处:http://hk.pswp.cn/diannao/95245.shtml
英文地址,请注明出处:http://en.pswp.cn/diannao/95245.shtml

如若内容造成侵权/违法违规/事实不符,请联系英文站点网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

丝杆支撑座怎样助力升降设备实现智能化?

丝杆支撑座作为传动系统中的关键支撑部件&#xff0c;凭借其高刚性、抗冲击及精准定位能力&#xff0c;广泛应用于重型机械与升降设备领域&#xff0c;为设备提供稳定可靠的轴向承载与径向支撑&#xff0c;确保高负荷工况下的安全运行。电梯 / 升降平台&#xff1a;液压电梯的辅…

Notta:高效智能的音频转文字工具

本文转载自&#xff1a;Notta&#xff1a;高效智能的音频转文字工具 - Hello123 ** 一、产品简介 Notta 是一款基于 AI 语音识别引擎的语音转文字工具&#xff0c;支持 58 种语言的转录和 42 种语言的翻译。用户可通过实时录音或上传音频 / 视频文件&#xff08;如 MP3、WAV …

Docker私有仓库创建及Docky存储与网络配置(小白的“升级打怪”成长之路)

目录 一、Docker私有仓库创建 1、在一台安装Docker私有仓库的主机上添加docker-compose 命令 2、安装docker-ce服务 3、Docker 镜像加速 4、安装Harbor仓库 5、使用脚本安装仓库 6、网站登陆 7、客户端使用Harbor仓库 二、Docky存储与网络配置 1、存储与网络 挂载主机…

谷歌ADK接入文件操作MCP

文章目录MCP基础概念文件操作服务器文件操作MCP接入谷歌ADK项目创建多轮对话代码MCP基础概念 MCP技术体系中&#xff0c;会将外部工具运行脚本称作服务器&#xff0c;而接入这些外部工具的大模型运行环境称作客户端。 一个客户端可以接入多个不同类型的服务器&#xff0c;但都…

高光谱技术的独特优势

高光谱技术凭借其‌纳米级连续光谱采集能力‌和‌图谱合一的探测模式‌&#xff0c;在多个领域展现出不可替代的独特优势&#xff1a;一、光谱维度&#xff1a;精细物质指纹识别‌纳米级连续光谱解析‌ 通过 ‌5-10nm带宽的数百个连续波段‌&#xff08;最高330个通道&#xff…

基于Vue+Element UI集成高德地图的完整实践指南

本次开发使用deepseek 简直如虎添翼得心应手 生成模拟数据、解决报错那真是嘎嘎地 在 Vue Element UI 项目中引入高德地图 具体实现步骤&#xff1a; 高德开放平台&#xff1a;注册账号 → 进入控制台 → 创建应用 → 获取 Web端(JS API)的Key https://lbs.amap.com/ 这里需要…

Day50--图论--98. 所有可达路径(卡码网),797. 所有可能的路径

Day50–图论–98. 所有可达路径&#xff08;卡码网&#xff09;&#xff0c;797. 所有可能的路径 刷今天的内容之前&#xff0c;要先去《代码随想录》网站&#xff0c;先看完&#xff1a;图论理论基础和深度优先搜索理论基础。做完之后可以看题解。有余力&#xff0c;把广度优先…

Python 异常捕获

一、获取未知错误try:# 相关处理逻辑 异常后面输出print(输入信息……) except Exception as e:print(未知错误,e)二、获取已知错误except 错误单词&#xff08;来源于错误信息的第一个单词&#xff09;多个已知错误使用 except XXXXX:try:# 相关处理逻辑 异常后面输出print…

RIOT、RT-Thread 和 FreeRTOS 是三种主流的实时操作系统

RIOT、RT-Thread 和 FreeRTOS 是三种主流的实时操作系统&#xff08;RTOS&#xff09;&#xff0c;专为嵌入式系统和物联网&#xff08;IoT&#xff09;设备设计。它们在架构、功能、生态和应用场景上有显著差异&#xff0c;以下是详细对比&#xff1a;1. 架构与设计理念特性RI…

【FAQ】Win11创建资源不足绕开微软账号登录

Win11安装资源限制 因为 Windows 11 有两项强制检测 VMware 8 默认没提供&#xff1a; TPM 2.0&#xff08;可信平台模块&#xff09;Secure Boot&#xff08;安全启动&#xff09; 一步到位解决办法&#xff08;官方兼容方式&#xff09; 关闭虚拟机电源编辑虚拟机设置 选项 →…

Docker使用----(安装_Windows版)

一、Docker Docker 镜像就像是一个软件包&#xff0c;里面包括了应用程序的代码、运行所需的库和工具、配置文件等等&#xff0c;所有这些都打包在一起&#xff0c;以确保应用程序在不同的计算机上运行时&#xff0c;都能保持一致性。 可以把 Docker 镜像想象成一个软件安装文件…

91、23种经典设计模式

设计模式是软件设计中反复出现的解决方案的模板&#xff0c;用于解决特定问题并提高代码的可维护性、可扩展性和可复用性。23种经典设计模式可分为创建型、结构型和行为型三大类&#xff0c;以下是具体分类及模式概述&#xff1a; 一、创建型模式&#xff08;5种&#xff09; 关…

Illustrator总监级AI魔法:一键让低清logo变矢量高清,彻底告别手动描摹!

在海外从事设计十几年&#xff0c;我敢说&#xff0c;每个设计师都经历过一种“史诗级”的折磨&#xff1a;客户发来一个像素低得感人、边缘模糊不清的JPG格式Logo&#xff0c;然后要求你把它用在巨幅海报或者高清视频上。这意味着什么&#xff1f;意味着我们要打开Illustrator…

各种 dp 刷题下

6.#8518 杰瑞征途 / 洛谷 P4072 征途 题意 Pine 开始了从 SSS 地到 TTT 地的征途。从 SSS 地到 TTT 地的路可以划分成 nnn 段&#xff0c;相邻两段路的分界点设有休息站。Pine 计划用 mmm 天到达 TTT 地。除第 mmm 天外&#xff0c;每一天晚上 Pine 都必须在休息站过夜。所以…

本地WSL部署接入 whisper + ollama qwen3:14b 总结字幕增加利用 Whisper 分段信息,全新 Prompt功能

1. 实现功能 M4-3: 智能后处理 - 停顿感知增强版 (终极版) 本脚本是 M4-3 的重大升级&#xff0c;引入了“停顿感知”能力&#xff1a; 利用 Whisper 分段信息: 将 Whisper 的 segments 间的自然停顿作为强信号 ([P]) 提供给 LLM。全新 Prompt: 设计了专门的 Prompt&#xff0c…

微算法科技(NASDAQ:MLGO)开发经典增强量子优化算法(CBQOA):开创组合优化新时代

近年来&#xff0c;量子计算在组合优化领域的应用日益受到关注&#xff0c;各类量子优化算法层出不穷。然而&#xff0c;由于现阶段量子硬件的局限性&#xff0c;如何充分利用已有的经典计算能力来增强量子优化算法的表现&#xff0c;成为当前研究的重要方向。基于此&#xff0…

功能、延迟、部署、成本全解析:本地化音视频 SDK 对比 云端方案

引言 在构建实时音视频系统时&#xff0c;技术选型往往决定了项目的天花板。开发者面临的第一个关键抉择&#xff0c;就是是选择完全可控的本地化音视频内核&#xff0c;还是依赖云厂商的实时音视频服务。 以大牛直播SDK&#xff08;SmartMediaKit&#xff09;为代表的本地部…

微调入门:为什么微调

欢迎来到啾啾的博客&#x1f431;。 记录学习点滴。分享工作思考和实用技巧&#xff0c;偶尔也分享一些杂谈&#x1f4ac;。 有很多很多不足的地方&#xff0c;欢迎评论交流&#xff0c;感谢您的阅读和评论&#x1f604;。 目录1 什么时候我们需要微调呢&#xff1f;1.1 微调的…

3、匹配一组字符

在本章里&#xff0c;你将学习如何与字符集合打交道。与可以匹配任意单个字符的.字符&#xff08;参见第2章&#xff09;不同&#xff0c;字符集合能匹配特定的字符和字符区间。3.1 匹配多个字符中的某一个第2章介绍的.​字符&#xff0c;可以匹配任意单个字符。当时在最后一个…

强化学习在量化交易中的禁区:回测表现好实盘亏钱的4个原因

引言 “为什么你的强化学习策略在回测中年化 50%,到了实盘却三个月亏光本金?” 如果你做过量化交易,尤其是尝试用强化学习(Reinforcement Learning, RL),这种场景可能并不陌生: 回测曲线平滑向上,最大回撤可控,胜率稳定 模型参数和架构调到极致,每次迭代都带来更高的…