C#?手寫識別的實現示例
更新時間:2023年08月24日 15:13:49 作者:qw_6918966011
本文主要介紹了C#?手寫識別的實現示例,文章詳細介紹了如何使用C#語言調用OpenCV庫實現手寫識別,并通過示例程序展示了整個手寫識別過程,感興趣的可以了解一下
書寫識別,網上的大佬們都有輸出。
書寫識別存在的2個問題:
- 直接拿官網的案例(將 Windows Ink 筆劃識別為文本和形狀 - Windows apps | Microsoft Learn),會發(fā)現輸出準確度不高。
- 另外如果書寫過快,詞組識別也是個問題,畢竟無法準確分割字之間的筆跡。
我結合之前開發(fā)經驗,整理下書寫識別比較完善的方案。
單個字的識別方案
private List<string> Recognize(StrokeCollection strokes)
{
if (strokes == null || strokes.Count == 0)
return null;
// 創(chuàng)建識別器
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;
}
看下效果圖


環(huán)境及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個環(huán)境問題:
- 在App.config文件中,對節(jié)點startup添加屬性 useLegacyV2RuntimeActivationPolicy="true"
- 修改項目配置為x86
到此這篇關于C# 手寫識別的實現示例的文章就介紹到這了,更多相關C# 手寫識別內容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關文章希望大家以后多多支持腳本之家!

