C# 手写识别的实现示例
作者:qw_6918966011
本文主要介绍了C# 手写识别的实现示例,文章详细介绍了如何使用C#语言调用OpenCV库实现手写识别,并通过示例程序展示了整个手写识别过程,感兴趣的可以了解一下
书写识别,网上的大佬们都有输出。
书写识别存在的2个问题:
- 直接拿官网的案例(将 Windows Ink 笔划识别为文本和形状 - Windows apps | Microsoft Learn),会发现输出准确度不高。
- 另外如果书写过快,词组识别也是个问题,毕竟无法准确分割字之间的笔迹。
我结合之前开发经验,整理下书写识别比较完善的方案。
单个字的识别方案
private List<string> Recognize(StrokeCollection strokes)
{
if (strokes == null || strokes.Count == 0)
return null;
// 创建识别器
var recognizers = new Recognizers();
var chineseRecognizer = recognizers.GetDefaultRecognizer(0x0804);
using var recContext = chineseRecognizer.CreateRecognizerContext();
// 根据StrokeCollection构造 Ink 类型的笔迹数据。
using var stream = new MemoryStream();
strokes.Save(stream);
using var inkStorage = new Ink();
inkStorage.Load(stream.ToArray());
using var inkStrokes = inkStorage.Strokes;
//设置笔画数据
using (recContext.Strokes = inkStrokes)
{
//识别笔画数据
var recognitionResult = recContext.Recognize(out var statusResult);
// 如果识别过程中出现问题,则返回null
return statusResult == RecognitionStatus.NoError ?
recognitionResult.GetAlternatesFromSelection().OfType<RecognitionAlternate>().Select(i => i.ToString()).ToList() :
null;
}
}这里单字识别,想要提高识别率,可以将stroke合并成一个:
var points = new StylusPointCollection();
foreach (var stroke in strokes)
{
points.Add(new StylusPointCollection(stroke.StylusPoints));
}
var newStroke = new StrokeCollection
{
new Stroke(points)
};多字的识别方案
public IEnumerable<string> Recognize(StrokeCollection strokes)
{
if (strokes == null || strokes.Count == 0)
return null;
using var analyzer = new InkAnalyzer();
analyzer.AddStrokes(strokes,0x0804);
analyzer.SetStrokesType(strokes, StrokeType.Writing);
var status = analyzer.Analyze();
if (status.Successful)
{
var alternateCollection = analyzer.GetAlternates();
return alternateCollection.OfType<AnalysisAlternate>().Select(x => x.RecognizedString);
}
return null;
}
看下效果图


环境及DLL引用
引用的命名空间是:Windows.Ink和MicroSoft.Ink,需要引用的DLL文件有四个。
- IACore.dll、IALoader.dll、IAWinFX.dll,这三个DLL文件都是Intel集成显卡驱动的重要组成部分,包含了图形处理模块,尤其是IAWinFX为WPF应用提供了支持硬件加速的图形渲染。
- Microsoft.Ink.dll
值得说明一下,Windows.Ink与Microsoft.Ink在平台支持上不同,如果有要适配不同版本的windows,需要去上方代码修改下
- Microsoft.Ink支持Windows XP、Vista 和 Win7 等旧版 Windows,兼容性高。但Win10及以上版本,官方推荐使用Windows.Ink
- Windows.Ink,则仅支持Win8以上版本
引用了上面4个DLL文件后,还有2个环境问题:
- 在App.config文件中,对节点startup添加属性 useLegacyV2RuntimeActivationPolicy="true"
- 修改项目配置为x86
到此这篇关于C# 手写识别的实现示例的文章就介绍到这了,更多相关C# 手写识别内容请搜索脚本之家以前的文章或继续浏览下面的相关文章希望大家以后多多支持脚本之家!
