Winform基于PaddleOCR图片文字提取方法
1.目的掌握Winform下基于Sdcb. PaddleOCR和OpenCvSharp实现图片文字的提取方法。⒉编程软件Visual Studio 2022⒊界面设计如下图设计了UI界面包括使用Button、PictureBox和RichTextBox控件本案例以下图特定图片为案例进行文字提取⒋代码简述⑴命名空间usingOpenCvSharp;usingSdcb.PaddleInference;usingSdcb.PaddleOCR;usingSdcb.PaddleOCR.Models;usingSdcb.PaddleOCR.Models.Local;usingSystem;usingSystem.Collections.Generic;usingSystem.ComponentModel;usingSystem.Data;usingSystem.Drawing;usingSystem.IO;usingSystem.Linq;usingSystem.Text;usingSystem.Threading.Tasks;usingSystem.Windows.Forms;usingSize OpenCvSharp.Size;namespaceWinformGetTest{publicpartialclassForm1:Form{stringselectedPicture;//图片公共变量publicForm1(){InitializeComponent();}…//内容代码}}⑵“选择文字图片”代码privatevoidbutton1_Click(objectsender,EventArgse)//获取图片{OpenFileDialogopenFileDialog newOpenFileDialog();openFileDialog.Filter Image Files(*.BMP;*.JPG;*.GIF;*.PNG)|*.BMP;*.JPG;*.GIF;*.PNG|All files (*.*)|*.*;openFileDialog.FilterIndex 1;openFileDialog.Multiselect false;if(openFileDialog.ShowDialog() DialogResult.OK){selectedPicture openFileDialog.FileName;Imageimage Image.FromFile(selectedPicture);//使用Image类加载图片pictureBox1.SizeMode PictureBoxSizeMode.Zoom;//让PictureBox完全显示图片pictureBox1.Image image;//将图片显示在PictureBox中}else{MessageBox.Show(您本次没有选择任何图片);}}⑶“图片文字提取”代码privatevoidbutton2_Click(objectsender,EventArgse)//图片文字提取{this.richTextBox1.Clear();//清空 richTextBox1 中的内容FullOcrModelmodel LocalFullModels.ChineseV3;//设置使用的 OCR 模型为中文版模型v3 版本using(PaddleOcrAllall newPaddleOcrAll(model,PaddleDevice.Mkldnn())//初始化 PaddleOCR 实例使用 MKL-DNN 加速{AllowRotateDetection true,//允许检测并识别有角度的文字Enable180Classification true,//允许识别旋转角度大于90度的文字}){//读取用户选择的图片文件using(Matsrc2 Cv2.ImRead(selectedPicture))using(Matpreprocessed PreprocessImageForOcr(src2)){PaddleOcrResultresult all.Run(preprocessed);//使用 OCR 对图片进行识别stringrawText result.Text;stringcorrectedText PostProcessOcrResult(rawText);//后处理校正(使用返回值)richTextBox1.Text correctedText;//将识别出的文本内容显示在 richTextBox1 中}}}privateMatPreprocessImageForOcr(Matsrc)//图片处理{try{Matprocessed src.Clone();// 1.转换为灰度图if(processed.Channels() 3){Cv2.CvtColor(processed, processed,ColorConversionCodes.BGR2GRAY);}// 2.使用中值滤波对文字边缘保护更好Cv2.MedianBlur(processed, processed, 3);// 3.自适应二值化比普通阈值化效果更好Cv2.AdaptiveThreshold(processed, processed, 255,AdaptiveThresholdTypes.GaussianC,ThresholdTypes.Binary, 15, 5);// 4.形态学操作去除噪点(先闭运算再开运算)Matkernel Cv2.GetStructuringElement(MorphShapes.Rect,newSize(2, 2));Cv2.MorphologyEx(processed, processed,MorphTypes.Close, kernel);//Cv2.MorphologyEx(processed, processed, MorphTypes.Open, kernel);// 5.对比度增强processed.ConvertTo(processed, -1, 1.1, 20);returnprocessed;}catch(Exceptionex){MessageBox.Show($图像预处理错误:{ex.Message});returnsrc.Clone();}}privatestringPostProcessOcrResult(stringrawText)// OCR结果后处理校正{varcorrections newDictionarystring,string//常见错别字和语法错误校正{{ 兰动化综, 自动化综 },};stringresult rawText;foreach(varcorrectionincorrections){result result.Replace(correction.Key, correction.Value);}returnresult;}在进行图片处理时参数调整对比优化此外对OCR结果后进行提取校正。⒌测试如下图针对上述图片提取文字进行测试分别为不校正和校正后的文字提取效果
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.coloradmin.cn/o/2408546.html
如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈,一经查实,立即删除!